# Converting .stl to nifti without showing GUI

**URL:** <https://discourse.slicer.org/t/converting-stl-to-nifti-without-showing-gui/20287>\
**Category:** Development\
**Created:** [October 21, 2021, 2:36pm UTC](https://discourse.slicer.org/t/converting-stl-to-nifti-without-showing-gui/20287 "2021-10-21T14:36:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThijsvD](https://avatars.discourse-cdn.com/v4/letter/t/22d042/32.png) [@ThijsvD](https://discourse.slicer.org/u/ThijsvD)\
**Post date:** [October 21, 2021, 2:36pm UTC](https://discourse.slicer.org/t/converting-stl-to-nifti-without-showing-gui/20287/1 "2021-10-21T14:36:22Z")

</div>

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.

```python
    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)

```

---

<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:** [October 22, 2021, 6:12pm UTC](https://discourse.slicer.org/t/converting-stl-to-nifti-without-showing-gui/20287/2 "2021-10-22T18:12:17Z")

</div>

You need to run these scripts in Slicer’s Python environment or [Slicer’s Jupyter kernel](https://github.com/Slicer/SlicerJupyter). 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.

---

<div class="post-metadata">

**Author:** ![pleuvoir](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pleuvoir](https://discourse.slicer.org/u/pleuvoir)\
**Post date:** [February 28, 2022, 7:23am UTC](https://discourse.slicer.org/t/converting-stl-to-nifti-without-showing-gui/20287/3 "2022-02-28T07:23:44Z")

</div>

@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!

---

<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:** [March 13, 2022, 5:51am UTC](https://discourse.slicer.org/t/converting-stl-to-nifti-without-showing-gui/20287/4 "2022-03-13T05:51:22Z")

</div>

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