Convert a surface mesh to a volumetric mesh in 3D Slicer

Hi Andras,
I am writing vtk file from within slicer using meshio. I am getting the following error. Is there any solution?

mesh = n.GetMesh()

points = mesh.GetPoints()
cells = mesh.GetCells()
import meshio
meshio.write_points_cells(“example.vtk”, points, cells)
Traceback (most recent call last):
File “”, line 1, in
File “/home/saima/Slicer-4.11.0-2020-02-25-linux-amd64/lib/Python/lib/python3.6/site-packages/meshio/_helpers.py”, line 81, in write_points_cells
cells = {key: numpy.asarray(value) for key, value in cells.items()}
AttributeError: ‘vtkCommonDataModelPython.vtkCellArray’ object has no attribute ‘items’

thank you

Regards,
Saima Safdar

I don’t recall seeing this error. Please report it to meshio developers. They are very responsive. Attach a sample data set that reproduces the issue.

Hi Andras,
I have a vtkcellArray with two types of cells in it, triangle and tetra. How can I extract cell array within slicer like following.

In meshio if the mesh.cells is called it gives the following. I need to get the cells from within 3d slicer for a mesh and give it to meshio. Any idea? I got the points and converted to numpy array but for cells I dont know how to get the arrays.

{‘triangle’: array([[ 0, 1, 2],
[ 0, 19, 1],
[ 4, 0, 2],
…,
[1989, 1984, 1991],
[1996, 1991, 1984],
[1987, 1993, 1994]], dtype=int32), ‘tetra’: array([[2600, 3531, 2730, 3868],
[2254, 2779, 2382, 2911],
[2242, 3405, 2782, 3546],
…,
[3710, 1693, 1478, 1477],
[ 544, 4188, 584, 3182],
[ 544, 584, 4188, 1388]], dtype=int32)}

You can save the mesh as .vtk and you should be able to load it using meshio. If there is any error then report it to meshio developers (attach the file that cannot be imported or converted).

If you want, then of course you can recreate the data meshio data structure from a VTK object in memory (vtkCellArray class is Python-wrapped, so you can access all methods from Python), but it is expected to be about a magnitude slower than the C++ implementation (that writes to file).

A post was split to a new topic: How to visualize volumetric mesh?