# Exporting Segment as OBJ through Python

**URL:** https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802
**Category:** Support
**Created:** [March 23, 2020, 7:20pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802 "2020-03-23T19:20:42Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)
#### Post date: [March 23, 2020, 7:20pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/1 "2020-03-23T19:20:42Z")

</div>

Heya there! I would love to get some advice on how to export a segment I created to a file. I found this code from another topic and was wondering how and where I can the info. needed to do this for a .obj.

# Write to STL file

```
surfaceMesh = segmentationNode.GetClosedSurfaceRepresentation(addedSegmentID)
writer = vtk.vtkSTLWriter()
writer.SetInputData(surfaceMesh)
writer.SetFileName("Z:/something.stl")
writer.Update()

```

Thank you in advance \<3

---

<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 23, 2020, 8:19pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/2 "2020-03-23T20:19:48Z")

</div>

Use vtkOBJWriter instead of vtkSTLWriter to write OBJ file.

---

<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 23, 2020, 8:20pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/3 "2020-03-23T20:20:23Z")

</div>

See also this topic:

> [@How to export model node to VRML 2 and STL (ascii and binary)](https://discourse.slicer.org/t/how-to-export-model-node-to-vrml-2-and-stl-ascii-and-binary/10804):
>
> Hello, I need to be able to export a model node as VRML 2, ascii STL, and binary STL files for a module I am developing. I know essentially nothing about vtk so any help or resources you could provide would be lovely. Thank you!

---

<div class="post-metadata">

### Author: ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)
#### Post date: [March 23, 2020, 8:33pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/4 "2020-03-23T20:33:39Z")

</div>

Thank you for the quick reply! It’s working now~

---

<div class="post-metadata">

### Author: ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)
#### Post date: [March 27, 2020, 4:38pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/5 "2020-03-27T16:38:15Z")

</div>

Good day, I have a quick question!

```
outputFolder = "Z:/Dicom2Text/DicomPrefabs"
ExportSegmentsClosedSurfaceRepresentationToFiles(outputFolder, segmentatioNode, [segmentTypeID], "OBJ", true, 1.0, false)

```

Given your advice on the topic you provided me I have been trying to use “ExportSegmentsClosedSurfaceRepresentationToFiles”  
What am I missing from the code above to make it call the method?

Thank you \<3

---

<div class="post-metadata">

### Author: ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)
#### Post date: [March 27, 2020, 6:19pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/6 "2020-03-27T18:19:54Z")

</div>

I fixed it, but now I am getting a different issue

```
outputFolder = "Z:/Dicom2Text/DicomPrefabs"
slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles(outputFolder, segmentationNode, [segmentTypeID], "OBJ", True, 1.0, False)

```

The error I am getting is:  
TypeError: ExportSegmentsClosedSurfaceRepresentationToFiles argument 3: method requires a VTK object

---

<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 27, 2020, 6:39pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/7 "2020-03-27T18:39:35Z")

</div>

To get help on how to use a method, use `help` method in the Python interactor:

```nohighlight
>>> help(slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles)
Help on method_descriptor:

ExportSegmentsClosedSurfaceRepresentationToFiles(...)
    V.ExportSegmentsClosedSurfaceRepresentationToFiles(string,
        vtkMRMLSegmentationNode, vtkStringArray, string, bool, float,
        bool) -> bool
    C++: static bool ExportSegmentsClosedSurfaceRepresentationToFiles(
        std::string destinationFolder,
        vtkMRMLSegmentationNode *segmentationNode,
        vtkStringArray *segmentIds=nullptr,
        std::string fileFormat="STL", bool lps=true,
        double sizeScale=1.0, bool merge=false)
    
    Export closed surface representation of multiple segments to
    files. Typically used for writing 3D printable model files.

```

As you can see, a `vtkStringArray` object is expected, while you provided a Python list of strings. If you want to export all segments, you can use `None`, if you want to export specific segments, create a `vtk.vtkStringArray` object and add the segment IDs that you would like to export.

---

<div class="post-metadata">

### Author: ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)
#### Post date: [March 27, 2020, 7:01pm UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/8 "2020-03-27T19:01:09Z")

</div>

Thank you so much!!! I got it to work and all the information you have provided me has given a much better understand about the magic going on behind the scenes!!!

---

<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 8, 2021, 3:40am UTC](https://discourse.slicer.org/t/exporting-segment-as-obj-through-python/10802/9 "2021-10-08T03:40:25Z")

</div>

A post was split to a new topic: [Save segmentation as PLY file](https://discourse.slicer.org/t/save-segmentation-as-ply-file/20063)
