Understanding how to build a vtkUnstructuredGrid

I’m new to working with unstructured grids. I am reading some files with the meshio python package, which gives me a mesh object. I can extract numpy arrays from this mesh object, and pass them to a C++ library that I made, in which I want to be able to rebuild a vtkUnstructuredGrid from the data.

From exploring the mesh object obtained from meshio, I see this:

  • A cell_data dictionary associates names to numpy arrays (1-dimensional arrays of floats), which I understand to be the values mapping to cells;
  • A points numpy array of floats, with 2 dimensions (3xN), which I understand corresponds to the list of 3d points composing the mesh;
  • A cells object from which I can get a type attribute showing “hexahedron” and a “data” attribute corresponding to a 6-dimensional integer numpy array, which I guess is the list of hexahedron cells.

Given raw, C pointers to these various numpy array, how do I build the corresponding VTK structure(s) to then pass to a C++ Paraview pipeline?


If I am not mistaken, meshio already seems to have code to do this. If you look at the reader they’ve included here, the RequesData method is pretty much doing what you’re asking i.e. taking a meshio mesh and converting that to vtkUnstructuredGrid via the numpy API.

Thanks, that’s helpful. From what I understand, I should call vtkUnstructuredGrid::SetPoints with a vtkPoints object created from the points numpy array. I should then call vtkUnstructuredGrid::SetCells with a vtkCellArray object built from two offsets and connectivity arrays that I should be able to compute relatively easily from the cells numpy array.

However I couldn’t find the equivalent of the C++ equivalent of this line to set the cell data. How can I do that?

(Note: to detail a bit more what I’m doing: I have a python application that uses meshio to load unstructure meshes, and it must send the mesh data to a server (via a C++ library) where the data is processed using VTK’s C++ API)

That’s just vtkFieldData::AddArray, the name it assigned on the array itself using vtkAbstractArray::SetName

Ah great, thank!