Updating Closed Surface Representation

Heya! I am trying to work with the closed surface representation. Manually I can get the results I want. With code it seems as if some of the steps I want it to perform are lost or never occur.

Steps
1. Create a segmentation, set name/color
2. Using the SegmentationEditor set threshold min/max
3. Closed Surface Representation (1.5x Oversampling, 0 joint smoothing, 0 smoothing factor, 0 decimation)
4. Using the SegmentationEditor set median smoothing 2mm to this segmentation
5. Update the Closed Surface Representation (1.5x Oversampling, 1 joint smoothing, 1 smoothing factor, 0.9 decimation)

Essentially, I want to smooth the representation that isn’t decimated. After smoothing, decimate it. I imagine there must be a way to update the surface representation, but I can’t seem to find the necessary snippet to do this. I would appreciate any and all direction on the approach

# Access segmentation module
slicer.util.selectModule('Segment Editor')
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(seriesVolumeNode)

# Create spine segment
segmentTypeID = "Spine"
newSegment = slicer.vtkSegment()
newSegment.SetName(segmentTypeID)
newSegment.SetColor([0.89, 0.85, 0.78])
segmentationNode.GetSegmentation().AddSegment(newSegment,segmentTypeID)

# Create segment editor widget to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)

# Access segment editor node
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
slicer.mrmlScene.AddNode(segmentEditorNode)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(seriesVolumeNode)

# Segment Editor Effect: Thresholding
segmentEditorWidget.setActiveEffectByName("Threshold")
thresholdEffect = segmentEditorWidget.activeEffect()
thresholdEffect.setParameter("MinimumThreshold","90")
thresholdEffect.self().onApply()
thresholdEffect.setParameter("MaximumThreshold","1600")
thresholdEffect.self().onApply()

# Create Closed Surface Representation
segmentationNode.GetSegmentation().SetConversionParameter("Oversampling factor", "1.5")
segmentationNode.GetSegmentation().SetConversionParameter("Joint smoothing", "0.00")
segmentationNode.GetSegmentation().SetConversionParameter("Smoothing factor", "0.00")
segmentationNode.GetSegmentation().SetConversionParameter("Decimation factor", "0.00")
segmentationNode.CreateClosedSurfaceRepresentation()

# Segment Editor Effect: Smoothing
segmentEditorWidget.setActiveEffectByName("Smoothing")
smoothingEffect = segmentEditorWidget.activeEffect()
# 2mm MEDIAN Smoothing
smoothingEffect.setParameter("SmoothingMethod", "MEDIAN")
smoothingEffect.self().onApply
smoothingEffect.setParameter("KernelSizeMm", 2)
smoothingEffect.self().onApply

# Update Closed Surface Representation
segmentationNode.GetSegmentation().SetConversionParameter("Oversampling factor", "1.5")
segmentationNode.GetSegmentation().SetConversionParameter("Joint smoothing", "1.00")
segmentationNode.GetSegmentation().SetConversionParameter("Smoothing factor", "1.00")
segmentationNode.GetSegmentation().SetConversionParameter("Decimation factor", "0.90")
# Update the smoothed closed surface representation some how

Any advice helps <3 Even a pointer to where to find out what functions could update the surface representation would come a long way!

It looks like your code already does what you need. Whenever you change the binary labelmap representation (smoothing, etc.), the binary labelmap representation is re-converted to closed surface representation (including smoothing and decimation).

For final export, you can do additional combination of smoothing and decimation using VTK filters (in just a few lines of code, using vtkQuadricDecimation, vtkDecimatePro, vtkWindowedSincPolyDataFilter etc.). Decimate CLI module has an additional FastQuadric method that you may try, too.