# Export multiple individual segment .stl files from one segmented nii.gz file using python script

**URL:** https://discourse.slicer.org/t/export-multiple-individual-segment-stl-files-from-one-segmented-nii-gz-file-using-python-script/15687
**Category:** Support
**Created:** [January 27, 2021, 3:36am UTC](https://discourse.slicer.org/t/export-multiple-individual-segment-stl-files-from-one-segmented-nii-gz-file-using-python-script/15687 "2021-01-27T03:36:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mingxiao\_Tu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mingxiao_tu/32/8447_2.png) [@Mingxiao\_Tu](https://discourse.slicer.org/u/Mingxiao_Tu)
#### Post date: [January 27, 2021, 3:36am UTC](https://discourse.slicer.org/t/export-multiple-individual-segment-stl-files-from-one-segmented-nii-gz-file-using-python-script/15687/1 "2021-01-27T03:36:57Z")

</div>

Hi there,

I am currently new to 3dslicer and I have questions about how to perform a specific task using the python script code for 3d slicer.

The segmented file (nii.gz) can be exported into individual ‘.stl’ files based on different segments using the 3Dslicer software. However I am about to integrate this function into my own python code. Could anyone please give me some tips on where I should look at or even start? Thank you very much.

 ![new](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/0/e03890a85d5381c3a9c8c34755d7992519c29891.png)

---

<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: [January 27, 2021, 3:39am UTC](https://discourse.slicer.org/t/export-multiple-individual-segment-stl-files-from-one-segmented-nii-gz-file-using-python-script/15687/2 "2021-01-27T03:39:46Z")

</div>

See examples how to do this in the script repository: [Documentation/Nightly/ScriptRepository - Slicer Wiki](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Segmentations)

---

<div class="post-metadata">

### Author: ![Mingxiao\_Tu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mingxiao_tu/32/8447_2.png) [@Mingxiao\_Tu](https://discourse.slicer.org/u/Mingxiao_Tu)
#### Post date: [January 27, 2021, 3:46am UTC](https://discourse.slicer.org/t/export-multiple-individual-segment-stl-files-from-one-segmented-nii-gz-file-using-python-script/15687/3 "2021-01-27T03:46:55Z")

</div>

Hi, thank you very much for the link. It is indeed helpful. However I also noticed that in the 3DSlicer we could export segments into ‘.stl’, ‘.obj’ or ‘nrrd’ files, could the python script allow the user to choose which output format?

---

<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: [January 27, 2021, 4:06am UTC](https://discourse.slicer.org/t/export-multiple-individual-segment-stl-files-from-one-segmented-nii-gz-file-using-python-script/15687/4 "2021-01-27T04:06:58Z")

</div>

Yes, you can save segmentations to all these formats. You can find more examples in this forum, for example:

> [@Exporting Segment as OBJ through Python](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/7):
>
> To get help on how to use a method, use help method in the Python interactor: \>\>\> help(slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles) Help on method\_descriptor: ExportSegmentsClosedSurfaceRepresentationToFiles(...) V.ExportSegmentsClosedSurfaceRepresentationToFiles(string, vtkMRMLSegmentationNode, vtkStringArray, string, bool, float, bool) -\> bool C++: static bool ExportSegmentsClosedSurfaceRepresentationToFiles( std::…
