Converting .stl to nifti without showing GUI

For the registration of multiple binairy STL files of a femur bone, I want to use Elastix. I need the transformation / rotation matrices as well for the further project. To use Elastix properly I have to convert the STL to a niftii file, so it works with ther program and stays on the same place in the coordinate system. I have a couple questions:

  • I’m using the Python code beneath. I’m running it in Visual Studio Code, not in 3D slicer, is that necessary? Maybe that is the reason for the Error. If not, how can I solve the error?
  • Is there another easier way to convert the STL to a niftii?
  • Is there another easier way to registrate two STL’s (while keeping the same coordinate systems)?

Thanks for your help in advance! Kind Regards.

    segmentationNode = util.loadSegmentation(stl_file_name)
    outputLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
    slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, outputLabelmapVolumeNode)
    slicer.util.saveNode(outputLabelmapVolumeNode, output_file_name)


I get the following error:

**---------------------------------------------------------------------------** **ImportError** Traceback (most recent call last) **

<ipython-input-10-bde7d0eba18b>** in <module> 31 output_file_name **=** **"vol_stat_T_R.nii"** 32 **---> 33** segmentationNode **=** util **.** loadSegmentation **(** stl_file_name **)** 34 outputLabelmapVolumeNode **=** slicer **.** mrmlScene **.** AddNewNodeByClass **(** **'vtkMRMLLabelMapVolumeNode'** **)** 35 slicer **.** modules **.** segmentations **.** logic **(** **)** **.** ExportVisibleSegmentsToLabelmapNode **(** segmentationNode **,** outputLabelmapVolumeNode **)** 

**c:\Users\Acer\Desktop\TM-II\Stages-TM\Stage_4\Thijs van Deudekom\Slicer\Base\Python\slicer\util.py** in loadSegmentation **(filename, returnNode)** 796 If returnNode **is** **True** then a status flag **and** loaded node are returned **.** 797 """ **--> 798 ****return****** loadNodeFromFile **(** filename **,** **'SegmentationFile'** **,** **{** **}** **,** returnNode **)** 799 800 **def** loadTransform **(** filename **,** returnNode **=** **False** **)** **:** 

**c:\Users\Acer\Desktop\TM-II\Stages-TM\Stage_4\Thijs van Deudekom\Slicer\Base\Python\slicer\util.py** in loadNodeFromFile **(filename, filetype, properties, returnNode)** 629 **:** raises RuntimeError **:** **in** case of failure 630 """ **--> 631 ****from****** Base **.** Python **import** mrml 632 **from** vtk **import** vtkCollection 633 properties **[** **'fileName'** **]** **=** filename **ImportError** : cannot import name 'app' from 'slicer' (C:\Users\Acer\Miniconda3\lib\site-packages\slicer\__init__.py)

You need to run these scripts in Slicer’s Python environment or Slicer’s Jupyter kernel. You can configure Visual Studio Code to use Slicer’s Python (such as c:\Users\yourusername\AppData\Local\NA-MIC\Slicer...\bin\PythonSlicer.exe) as Python interpreter.

@lassoan Hi, I chose the second option you mentioned when I used Python to convert the.nrrd file to nifti without displaying the GUI:
I used the pythonSlicer.exe in the Slicer installation directory as the virtual environment for my project when I created it. My program prompts the following error after execution:
No module named ‘logic’
Traceback (most recent call last):
File “D:/software/PycharmProjects/For_Xv_of_Tinti/main.py”, line 290, in
execute(slicer)
File “D:/software/PycharmProjects/For_Xv_of_Tinti/main.py”, line 106, in execute
slicer.util.loadSegmentation(codePath+’/L2.nii’)
File “D:\software\Slicer 4.11.20210226\bin\Python\slicer\util.py”, line 801, in loadSegmentation
return loadNodeFromFile(filename, ‘SegmentationFile’, {}, returnNode)
File “D:\software\Slicer 4.11.20210226\bin\Python\slicer\util.py”, line 634, in loadNodeFromFile
from slicer import app
ImportError: cannot import name ‘app’
I have tried many methods but failed to solve it. I hope to get your help. Thank you very much!

If you want to use any Slicer application features then you need to run your script not just in a Slicer virtual Python environment but Slicer application’s Python environment (by starting the Slicer application and passing your Python script to it).