Write several unstructured grids

Is it possible to write several unstructured grids in one file without use MultiblockDataSet?

a single unstructured grid may have any number of connected components, however it will still be a single vtkDataSet.
We may try to use the vtkPXMLUnstructuredGridWriter to write pieces and then abuse the pipeline by requesting pieces independantly, but I definitely don’t think this would be a clean solution.

What is your usecase exactly ?

Now I think to use MultiblockDataSet. On the other hand I’m asking how could I write a vtkMultiPieceDataSet

You may try to use a vtkXMLPartitionedDataSetWriter.
Be careful these class are still under development (https://discourse.paraview.org/t/deprecating-multiblock-datasets/4950)

On the other hand, I’m asking what is the meaning of pieces in unstructured grid:

How can I manage it?

most of the VTK XML Writers (vtkXML…Writer) also have a multi-piece version (vtkXMLP…Writer). For example, the vtkXMLPolyDataWriter can write .vtp files, and the vtkXMLPPolyDataWriter can write .pvtp which are basically composed of:

  • one .pvtp xml files which contain location of vtp files composing the data set
  • (usually) several vtp files, each containing a sub-part of the main PolyData. These parts are named pieces.

Of course, these pieces can be independent from each others, hence we can use this mechanism to write / read several poly data at once, but the result is still composed of several files as the main use of this mechanism is in a distributed environment.
I can give you example of how to use such reader if you want

Yes, this example can be very usefull for me.

Thank you very much

Here is a small example that open each piece and retrieve the number of cell inside, then write the data set again. It is intended to work with pvtu files.

#include <iostream>

#include <vtkUnstructuredGrid.h>
#include <vtkXMLPUnstructuredGridReader.h>
#include <vtkXMLPUnstructuredGridWriter.h>

int main(int argc, char *argv[]) {

  vtkNew<vtkXMLPUnstructuredGridReader> preader;

  // retrieve the number of pieces without reading the whole data

  int nb_pieces = preader->GetNumberOfPieces();

  std::cout << "nb pieces : " << nb_pieces << std::endl;

  for (int cur_piece = 0; cur_piece < nb_pieces; cur_piece++) {
    preader->UpdatePiece(cur_piece, nb_pieces, 0);
    auto *loadedMesh = preader->GetOutput();
    std::cout << cur_piece << " nb cells : " << loadedMesh->GetNumberOfCells() << std::endl;

  vtkNew<vtkXMLPUnstructuredGridWriter> w;

  return 0;


Thank you very much for this example, but I still don’t understand how I can create several pieces in an unstructured grid.