# Exporting a segment as .stl model

**URL:** https://discourse.slicer.org/t/exporting-a-segment-as-stl-model/30044
**Category:** Development
**Tags:** segmentation, 3d-model
**Created:** [June 14, 2023, 9:30pm UTC](https://discourse.slicer.org/t/exporting-a-segment-as-stl-model/30044 "2023-06-14T21:30:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![S\_Arbabi](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@S\_Arbabi](https://discourse.slicer.org/u/S_Arbabi)
#### Post date: [June 14, 2023, 9:30pm UTC](https://discourse.slicer.org/t/exporting-a-segment-as-stl-model/30044/1 "2023-06-14T21:30:44Z")

</div>

I would like to export a segment of a segmentation node to a .stl 3d model file.  
I’m wondering how can I do it in python? I could create the volume like this, but then in saving it didn’t work the way I expected.

segmentationNode = getNode(“segmentationNode”)  
#I’m not sure if I can also get a specific segment (and not the whole segmentation node) only for exporting as model?  
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()  
exportFolderItemId = shNode.CreateFolderItem(shNode.GetSceneItemID(), “Segments”)  
slicer.modules.segmentations.logic().ExportAllSegmentsToModels(segmentationNode, exportFolderItemId)

but then how can I save the model? I tried:  
slicer.util.saveNode(slicer.util.getNode(“Segments”), r"./seg.stl")  
#with error: “slicer.util.MRMLNodeNotFoundException: could not find nodes in the scene by name or id ‘Segments’”

thanks in advance

---

<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: [June 16, 2023, 2:16pm UTC](https://discourse.slicer.org/t/exporting-a-segment-as-stl-model/30044/2 "2023-06-16T14:16:09Z")

</div>

This topic should help:

> [@Export volume (or segment?) to stl file with python](https://discourse.slicer.org/t/export-volume-or-segment-to-stl-file-with-python/25111/2):
>
> Hi, segmentation nodes cannot be saved as STL directly. You need to first export the segmentation to model node: [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#export-model-nodes-from-segmentation-node) Then, model nodes can be saved as STL files.

---

<div class="post-metadata">

### Author: ![S\_Arbabi](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@S\_Arbabi](https://discourse.slicer.org/u/S_Arbabi)
#### Post date: [June 16, 2023, 8:28pm UTC](https://discourse.slicer.org/t/exporting-a-segment-as-stl-model/30044/3 "2023-06-16T20:28:25Z")

</div>

Thanks. Also a very good code example:

> **[Python vtkStringArray Examples, vtk.vtkStringArray Python Examples - HotExamples](https://python.hotexamples.com/examples/vtk/-/vtkStringArray/python-vtkstringarray-function-examples.html)**
>
> Python vtkStringArray - 60 examples found. These are the top rated real world Python examples of vtk.vtkStringArray extracted from open source projects. You can rate examples to help us improve the quality of examples.
