Export volume (or segment?) to stl file with python

Hi there,
Currently I’ve been working on automating a work flow in python that goes like the following:

  1. Load CT scans (as .tiffs)
  2. Create a segmentation and threshold to identify air in the scans
  3. Automate a mouse click in a known location and use the “Remove selected island” feature
  4. Apply the “Keep largest island” feature
  5. Calculate and report statistics

I’d like to add a 6th step that saves the final resulting volume as an STL file so users could easily open up a visual artifact at a later date to make sure that the script worked as intended, but I’m struggling to get the Segmentation Export Widget (or the ExportNode) syntax to work in python.

If anyone could provide an example of calling a/that function to export a volume/segment as an STL I’d really appreciate it!

Hi, segmentation nodes cannot be saved as STL directly. You need to first export the segmentation to model node: Script repository — 3D Slicer documentation
Then, model nodes can be saved as STL files.

1 Like

You can use the vtkSlicerSegmentationsModuleLogic::ExportSegmentsClosedSurfaceRepresentationToFiles function to export the segmentation to stl:

1 Like

This post ended up providing enough of a solution framework for me to figure it out.