# Unable to export segment with ExportSegmentsClosedSurfaceRepresentationToFiles

**URL:** https://discourse.slicer.org/t/unable-to-export-segment-with-exportsegmentsclosedsurfacerepresentationtofiles/23267
**Category:** Support
**Tags:** python, stl
**Created:** [May 3, 2022, 6:31pm UTC](https://discourse.slicer.org/t/unable-to-export-segment-with-exportsegmentsclosedsurfacerepresentationtofiles/23267 "2022-05-03T18:31:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Xabierenko](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/xabierenko/32/6710_2.png) [@Xabierenko](https://discourse.slicer.org/u/Xabierenko)
#### Post date: [May 3, 2022, 6:31pm UTC](https://discourse.slicer.org/t/unable-to-export-segment-with-exportsegmentsclosedsurfacerepresentationtofiles/23267/1 "2022-05-03T18:31:31Z")

</div>

Hi,

I have been using the code snippet below to export large amounts of segmentations to .stl format.

```auto
segmentation = slicer.util.loadSegmentation(seg)
vol = slicer.util.loadVolume(mag)
    
volumeNode = slicer.util.getNode('*magnitude*') # Get the volume node  
segNode = slicer.util.getNode('*Segment*')
    slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles(out, segNode, None, "STL", False, 0.001, False)
    
slicer.mrmlScene.RemoveNode(volumeNode)
slicer.mrmlScene.RemoveNode(segmentation)

```

I don’t know why when I’m trying the same code to export a new dataset I’m getting the following error.

`TypeError: ExportSegmentsClosedSurfaceRepresentationToFiles argument 2: method requires a vtkMRMLSegmentationNode, a vtkMRMLSegmentationStorageNode was provided.`

Kind regards,  
Xabier

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [May 3, 2022, 6:33pm UTC](https://discourse.slicer.org/t/unable-to-export-segment-with-exportsegmentsclosedsurfacerepresentationtofiles/23267/2 "2022-05-03T18:33:15Z")

</div>

> [@Xabierenko](#):
>
> `segNode = slicer.util.getNode('*Segment*')`

It seems that this returns the wrong node (the segmentation storage node instead of the segmentation node itself). I suggest using a more robust way to get the node.

---

<div class="post-metadata">

### Author: ![Xabierenko](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/xabierenko/32/6710_2.png) [@Xabierenko](https://discourse.slicer.org/u/Xabierenko)
#### Post date: [May 3, 2022, 6:38pm UTC](https://discourse.slicer.org/t/unable-to-export-segment-with-exportsegmentsclosedsurfacerepresentationtofiles/23267/3 "2022-05-03T18:38:41Z")

</div>

Is there a way of getting the first segment following the order displayed in the GUI?

---

<div class="post-metadata">

### Author: ![wawa](https://avatars.discourse-cdn.com/v4/letter/w/f05b48/32.png) [@wawa](https://discourse.slicer.org/u/wawa)
#### Post date: [May 10, 2022, 3:52am UTC](https://discourse.slicer.org/t/unable-to-export-segment-with-exportsegmentsclosedsurfacerepresentationtofiles/23267/4 "2022-05-10T03:52:47Z")

</div>

Here is my working code:

```auto
nii_pathname = ...   
seg = slicer.util.loadSegmentation(nii_pathname)
segmentationNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentationNode")
slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles(out, segmentationNode, None, "STL")
slicer.mrmlScene.RemoveNode(segmentationNode)
slicer.mrmlScene.RemoveNode(seg)

```

I don’t know why using the function “GetFirstNodeByClass” works, but it does meet the need.

---

<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: [May 10, 2022, 4:44am UTC](https://discourse.slicer.org/t/unable-to-export-segment-with-exportsegmentsclosedsurfacerepresentationtofiles/23267/5 "2022-05-10T04:44:10Z")

</div>

> [@wawa](#):
>
> I don’t know why using the function “GetFirstNodeByClass” works, but it does meet the need.

`slicer.util.getNode('*Segment*')` returns the first node in the scene that contains `Segment` in its name or ID (including all the hidden nodes). There can be many nodes like that in the scene: the segmentation node, the segmentation display node, and segmentation storage node. `getNode` helper function (especially when it is used with wildcards as input) is only recommended for quick testing and troubleshooting.

If you specify the kind of node you are looking for (`vtkMRMLSegmentationNode`) and you only have one node of that type in the scene then the result is always as you expect.
