# Convert mesh to volume

**URL:** <https://discourse.slicer.org/t/convert-mesh-to-volume/21686>\
**Category:** Support\
**Created:** [January 29, 2022, 12:54am UTC](https://discourse.slicer.org/t/convert-mesh-to-volume/21686 "2022-01-29T00:54:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Artur\_V](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/artur_v/32/14037_2.png) [@Artur\_V](https://discourse.slicer.org/u/Artur_V)\
**Post date:** [January 29, 2022, 12:54am UTC](https://discourse.slicer.org/t/convert-mesh-to-volume/21686/1 "2022-01-29T00:54:39Z")

</div>

Thank you for this information, but i ran into some problems with the code and the model is not displayed.  
I’m trying to convert a .VTK file from an atlas (unstructured grid) into a mesh or volume to create a .nrrd or nifti file for a medical project.  
Here is a link to the VTK - [M1\_cf\_face.1k.vtk - Google Drive](https://drive.google.com/file/d/107T-ZeqlZIl22GMbxN_bKGPGcKWfI_4M/view?usp=sharing)

These are the errors im running into:

```auto
'[success, modelNode] = slicer.util.loadModel(sampleModelFile, returnNode=True)'
Error: loadNodeFromFile `returnNode` argument is deprecated. Loaded node is now returned directly if `returnNode` is not specified.

'modelNode.GetBounds(bounds)'
Error: Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'GetBounds'

'transformer.SetInputConnection(modelNode.GetMeshConnection())'
Error: Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'GetMeshConnection'

'modelNode.GetDisplayNode().BackfaceCullingOff()'
Error: Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'GetDisplayNode'

'display()'
Error: Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'display' is not defined

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 29, 2022, 2:50am UTC](https://discourse.slicer.org/t/convert-mesh-to-volume/21686/2 "2022-01-29T02:50:26Z")

</div>

The file that you linked above is not an unstructured grid but a polydata containing 1000 line cells.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/e/fef30819e532af6bd36cc3a4b66907182b403cb9.png)

Since lines are one-dimensional objects (have zero volume), before you can convert this to a labelmap, you need to give it some thickness. You can use vtkTubeFilter or vtkImplicitModeller+vtkContourFilter or Delaunay triangulation for this.

For example:

```python
modelNode = getNode('M1*')
outputModelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
outputModelNode.CreateDefaultDisplayNodes()

decimate = vtk.vtkDecimatePolylineFilter()
decimate.SetInputData(modelNode.GetPolyData())
decimate.SetTargetReduction(0.9)

delaunay = vtk.vtkDelaunay3D()
delaunay.SetInputConnection(decimate.GetOutputPort())
delaunay.SetAlpha(5.0)
delaunay.BoundingTriangulationOn()
delaunay.AlphaTetsOn()
delaunay.AlphaLinesOff()
delaunay.AlphaTrisOff()

geometry = vtk.vtkDataSetSurfaceFilter()
geometry.SetInputConnection(delaunay.GetOutputPort())

normals = vtk.vtkPolyDataNormals()
normals.SetInputConnection(geometry.GetOutputPort())
outputModelNode.SetPolyDataConnection(normals.GetOutputPort())

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/6/b6ceabc50d7f4ed5cbe6973743793f09a4af54aa.png)

You can then convert this model to a segmentation node and get a binary labelmap representation.

---

<div class="post-metadata">

**Author:** ![Artur\_V](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/artur_v/32/14037_2.png) [@Artur\_V](https://discourse.slicer.org/u/Artur_V)\
**Post date:** [October 11, 2022, 2:32am UTC](https://discourse.slicer.org/t/convert-mesh-to-volume/21686/3 "2022-10-11T02:32:55Z")

</div>

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![tuncerms](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@tuncerms](https://discourse.slicer.org/u/tuncerms)\
**Post date:** [August 9, 2023, 10:24am UTC](https://discourse.slicer.org/t/convert-mesh-to-volume/21686/4 "2023-08-09T10:24:29Z")

</div>

Hi!

First of all, thanks a lot for the helpful comments regarding this issue. I found this topic recently as I actually have exactly the same issue with the same vtk files.

I tried out your code, but I get this error “NameError: name ‘normals’ is not defined” referring to the last command “outputModelNode.SetPolyDataConnection(normals.GetOutputPort())”. The Model is not displayed like in the second image you showed.

Since it worked for you, it has to be an issue with my setup. Is there a solution for this?

Thanks a lot in advance!

Best,  
Mehmet

---

<div class="post-metadata">

**Author:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [August 9, 2023, 2:39pm UTC](https://discourse.slicer.org/t/convert-mesh-to-volume/21686/5 "2023-08-09T14:39:04Z")

</div>

You can change ‘normals’ to ‘geometry’.

```auto
outputModelNode.SetPolyDataConnection(geometry.GetOutputPort())

```

Or, you can add a normal filter, which would create a smoother looking surface:

```auto
modelNode = getNode('M1*')
outputModelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
outputModelNode.CreateDefaultDisplayNodes()

decimate = vtk.vtkDecimatePolylineFilter()
decimate.SetInputData(modelNode.GetPolyData())
decimate.SetTargetReduction(0.9)

delaunay = vtk.vtkDelaunay3D()
delaunay.SetInputConnection(decimate.GetOutputPort())
delaunay.SetAlpha(5.0)
delaunay.BoundingTriangulationOn()
delaunay.AlphaTetsOn()
delaunay.AlphaLinesOff()
delaunay.AlphaTrisOff()

geometry = vtk.vtkDataSetSurfaceFilter()
geometry.SetInputConnection(delaunay.GetOutputPort())

normals = vtk.vtkPolyDataNormals()
normals.SetInputConnection(geometry.GetOutputPort())
outputModelNode.SetPolyDataConnection(normals.GetOutputPort())

```

---

<div class="post-metadata">

**Author:** ![tuncerms](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@tuncerms](https://discourse.slicer.org/u/tuncerms)\
**Post date:** [August 9, 2023, 3:16pm UTC](https://discourse.slicer.org/t/convert-mesh-to-volume/21686/6 "2023-08-09T15:16:50Z")

</div>

works! Thanks a lot!
