How can I export to a segmentation to OBJ file using Python?

I have several segmentations and would like to export each to an OBJ file. The segmentations were created by segmenting each frame of a Sequence of image volumes. The segmentations are not currently organized into a Sequence, but easily could be. However, I haven’t done so because manually exporting to OBJ (using Segmentations/Export to files…) just exports the single segmentation currently associated with the proxy node (which is sensible, but not what I want). The segmentations are generated in a custom extension I’ve written where I do one careful manual segmentation and then automatically modify and propagate it to segment all other image volumes in the Sequence. There are 10 to 20 volumes in each Sequence I’m interested in processing, so it would save a lot of time if I could just add a button to my extension that would cycle through the generated segmentations and export each to OBJ rather than having to manually export each one. I have successfully understood how to work with the python-based nodes to use segment editor effects, but the “Export to files” code seems to be entirely C++ based, and I don’t see how to interact with it from my extension. I’d appreciate any help anyone can offer. Also, I’m not clear on whether a post like this should be tagged Development (because it involves questions about coding) or Support (because it’s a question about usage). Advice on that would also be welcome. Thanks!

1 Like

Thanks for the nice explanation of your question!
The part of that C++ code that actually does the exporting is this one function call:

Type this in the python console and you’ll get info about how to use it:

help(slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles)

(I think it’s fine to put this post to the development category, as it’s about extension programming)

1 Like

Thanks, this is exactly what I needed.

1 Like