# Converting a STL file to nifti

**URL:** https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638
**Category:** Support
**Created:** [November 16, 2020, 4:21pm UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638 "2020-11-16T16:21:07Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ycaspi](https://avatars.discourse-cdn.com/v4/letter/y/a88e4f/32.png) [@ycaspi](https://discourse.slicer.org/u/ycaspi)
#### Post date: [November 16, 2020, 4:21pm UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638/1 "2020-11-16T16:21:08Z")

</div>

Hello Guys,

I am trying to convert an stl file to nifti using Dicer.

I found the following: Conversation: [Converting .stl files to binary label maps in .nii format using Python](https://discourse.slicer.org/t/converting-stl-files-to-binary-label-maps-in-nii-format-using-python/13038/6)

I have tried to follow the instructions of Donnie Cameron.

However, when I give the command:

```auto
seg.SetReferenceImageGeometryParameterFromVolumeNode(referenceVolumeNode)

```

I get the error message:

```auto
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: SetReferenceImageGeometryParameterFromVolumeNode argument 1: method requires a VTK object

```

Similarly: If I follow Andras Lasso script and give the command:

```auto
outputLabelmapVolumeNode = 
slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')

```

I also get the error:

```auto
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: ExportVisibleSegmentsToLabelmapNode argument 1: method requires a VTK object

```

Does anyone know what do I do wrong?

Best,  
Yaron

---

<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: [November 17, 2020, 12:23am UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638/2 "2020-11-17T00:23:57Z")

</div>

See complete example in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Rasterize_a_model_and_save_it_to_a_series_of_image_files).

---

<div class="post-metadata">

### Author: ![ycaspi](https://avatars.discourse-cdn.com/v4/letter/y/a88e4f/32.png) [@ycaspi](https://discourse.slicer.org/u/ycaspi)
#### Post date: [November 17, 2020, 10:26am UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638/3 "2020-11-17T10:26:12Z")

</div>

Dear Andras,

Thanks for your reply. I have tried to follow the script for converting stl to TIFF images but still get error messages. In particular, the command

```auto
inputModel.GetBounds(bounds) 

```

returns the error message:

```auto
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'bool' object has no attribute 'GetBounds'

```

Similarly, the command:  
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(inputModel, seg)  
Returns the error message:

```auto
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: ImportModelToSegmentationNode argument 1: method requires a VTK object

```

To me it looks as if the method to load the stl file is the problematic one (`inputModel = slicer.util.loadModel(inputModelFile)` ).  
Any idea how to solve it?

Thanks,  
Yaron

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [November 17, 2020, 10:58am UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638/4 "2020-11-17T10:58:36Z")

</div>

The variable `inputModel` should contain a vtkMRMLModelNode and not a boolean

---

<div class="post-metadata">

### Author: ![ycaspi](https://avatars.discourse-cdn.com/v4/letter/y/a88e4f/32.png) [@ycaspi](https://discourse.slicer.org/u/ycaspi)
#### Post date: [November 17, 2020, 11:32am UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638/5 "2020-11-17T11:32:17Z")

</div>

Dear Csaba,

The variable `inputModel` was set according to the script as:

```auto
inputModel = slicer.util.loadModel(inputModelFile)

```

Where `inputModelFile` was my path to my stl file.

And that created the boolean object.

Hence, I think that there is a problem in setting the stl as a vtk object in Slicer.  
Any idea of how to do it?

Yaron

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [November 17, 2020, 11:34am UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638/6 "2020-11-17T11:34:41Z")

</div>

At this point this is regular Python programming.

If you see the documentation of the function you use you’ll see why you get a boolean:

```auto
>>> help(slicer.util.loadModel)
Help on function loadModel in module slicer.util:

loadModel(filename, returnNode=False)
    Load node from file.
    
    :param filename: full path of the file to load.
    :param returnNode: Deprecated.
    :return: loaded node (if multiple nodes are loaded then a list of nodes).
      If returnNode is True then a status flag and loaded node are returned.

```

---

<div class="post-metadata">

### Author: ![ycaspi](https://avatars.discourse-cdn.com/v4/letter/y/a88e4f/32.png) [@ycaspi](https://discourse.slicer.org/u/ycaspi)
#### Post date: [November 17, 2020, 4:14pm UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638/7 "2020-11-17T16:14:15Z")

</div>

Thanks.

Using the syntax:

```auto
[Status, inputModel] = slicer.util.loadModel(inputModelFile, returnNode=True)

```

indeed solved the problem.

Yaron

---

<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: [November 17, 2020, 4:19pm UTC](https://discourse.slicer.org/t/converting-a-stl-file-to-nifti/14638/8 "2020-11-17T16:19:49Z")

</div>

The API is changed in Slicer-4.11. By default we don’t use Boolean flag to indicate error anymore, but raise an exception instead. This allows simpler syntax of `inputModel = slicer.util.loadModel(inputModelFile)`.

I would recommend to update to latest Slicer stable or preview release.
