Converting a STL file to nifti

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

I have tried to follow the instructions of Donnie Cameron.

However, when I give the command:

seg.SetReferenceImageGeometryParameterFromVolumeNode(referenceVolumeNode)

I get the error message:

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:

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

I also get the error:

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

See complete example in the script repository.

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

inputModel.GetBounds(bounds) 

returns the error message:

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:

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

The variable inputModel should contain a vtkMRMLModelNode and not a boolean

Dear Csaba,

The variable inputModel was set according to the script as:

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

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:

>>> 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.

Thanks.

Using the syntax:

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

indeed solved the problem.

Yaron

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.