# Understanding how to build a vtkUnstructuredGrid

**URL:** https://discourse.paraview.org/t/understanding-how-to-build-a-vtkunstructuredgrid/7879
**Category:** ParaView Support
**Created:** [August 27, 2021, 2:15pm UTC](https://discourse.paraview.org/t/understanding-how-to-build-a-vtkunstructuredgrid/7879 "2021-08-27T14:15:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mdorier](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/eb9ed0/32.png) [@mdorier](https://discourse.paraview.org/u/mdorier)
#### Post date: [August 27, 2021, 2:15pm UTC](https://discourse.paraview.org/t/understanding-how-to-build-a-vtkunstructuredgrid/7879/1 "2021-08-27T14:15:28Z")

</div>

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?

Thanks!

---

<div class="post-metadata">

### Author: ![utkarsh.ayachit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/utkarsh.ayachit/32/39_2.png) [@utkarsh.ayachit](https://discourse.paraview.org/u/utkarsh.ayachit)
#### Post date: [August 27, 2021, 2:26pm UTC](https://discourse.paraview.org/t/understanding-how-to-build-a-vtkunstructuredgrid/7879/2 "2021-08-27T14:26:56Z")

</div>

If I am not mistaken, `meshio` already seems to have code to do this. If you look at the reader they’ve included [here](https://github.com/nschloe/meshio/blob/main/tools/paraview-meshio-plugin.py#L69), 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.

---

<div class="post-metadata">

### Author: ![mdorier](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/eb9ed0/32.png) [@mdorier](https://discourse.paraview.org/u/mdorier)
#### Post date: [August 27, 2021, 3:17pm UTC](https://discourse.paraview.org/t/understanding-how-to-build-a-vtkunstructuredgrid/7879/3 "2021-08-27T15:17:50Z")

</div>

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](https://github.com/nschloe/meshio/blob/main/tools/paraview-meshio-plugin.py#L106) 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)

---

<div class="post-metadata">

### Author: ![utkarsh.ayachit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/utkarsh.ayachit/32/39_2.png) [@utkarsh.ayachit](https://discourse.paraview.org/u/utkarsh.ayachit)
#### Post date: [August 27, 2021, 3:46pm UTC](https://discourse.paraview.org/t/understanding-how-to-build-a-vtkunstructuredgrid/7879/4 "2021-08-27T15:46:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mdorier](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/eb9ed0/32.png) [@mdorier](https://discourse.paraview.org/u/mdorier)
#### Post date: [August 27, 2021, 4:34pm UTC](https://discourse.paraview.org/t/understanding-how-to-build-a-vtkunstructuredgrid/7879/5 "2021-08-27T16:34:27Z")

</div>

Ah great, thank!
