Exporting a segment as .stl model

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

This topic should help:

1 Like

Thanks. Also a very good code example: