# Export volume (or segment?) to stl file with python

**URL:** <https://discourse.slicer.org/t/export-volume-or-segment-to-stl-file-with-python/25111>\
**Category:** Development\
**Tags:** python\
**Created:** [September 5, 2022, 10:51pm UTC](https://discourse.slicer.org/t/export-volume-or-segment-to-stl-file-with-python/25111 "2022-09-05T22:51:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Flanigan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mike_flanigan/32/14586_2.png) [@Mike\_Flanigan](https://discourse.slicer.org/u/Mike_Flanigan)\
**Post date:** [September 5, 2022, 10:51pm UTC](https://discourse.slicer.org/t/export-volume-or-segment-to-stl-file-with-python/25111/1 "2022-09-05T22:51:45Z")

</div>

Hi there,  
Currently I’ve been working on automating a work flow in python that goes like the following:

1. Load CT scans (as .tiffs)
2. Create a segmentation and threshold to identify air in the scans
3. Automate a mouse click in a known location and use the “Remove selected island” feature
4. Apply the “Keep largest island” feature
5. Calculate and report statistics

I’d like to add a 6th step that saves the final resulting volume as an STL file so users could easily open up a visual artifact at a later date to make sure that the script worked as intended, but I’m struggling to get the Segmentation Export Widget (or the ExportNode) syntax to work in python.

If anyone could provide an example of calling a/that function to export a volume/segment as an STL I’d really appreciate it!

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [September 6, 2022, 2:58pm UTC](https://discourse.slicer.org/t/export-volume-or-segment-to-stl-file-with-python/25111/2 "2022-09-06T14:58:06Z")

</div>

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:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [September 6, 2022, 3:13pm UTC](https://discourse.slicer.org/t/export-volume-or-segment-to-stl-file-with-python/25111/3 "2022-09-06T15:13:26Z")

</div>

You can use the vtkSlicerSegmentationsModuleLogic::ExportSegmentsClosedSurfaceRepresentationToFiles function to export the segmentation to stl:  
[https://apidocs.slicer.org/master/classvtkSlicerSegmentationsModuleLogic.html#a4b5328985b2d98cbbc93a5b2ae7fde6b](https://apidocs.slicer.org/master/classvtkSlicerSegmentationsModuleLogic.html#a4b5328985b2d98cbbc93a5b2ae7fde6b)

---

<div class="post-metadata">

**Author:** ![Mike\_Flanigan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mike_flanigan/32/14586_2.png) [@Mike\_Flanigan](https://discourse.slicer.org/u/Mike_Flanigan)\
**Post date:** [September 21, 2022, 2:18am UTC](https://discourse.slicer.org/t/export-volume-or-segment-to-stl-file-with-python/25111/4 "2022-09-21T02:18:05Z")

</div>

This post ended up providing enough of a solution framework for me to figure it out.

> [@Tips for getting started loading and batch segmenting files with Slicer in python?](https://discourse.slicer.org/t/tips-for-getting-started-loading-and-batch-segmenting-files-with-slicer-in-python/24850/4):
>
> Check this script example for running island tool and exporitng the segmentation and writing to the disk:
