How can I set masking settings on a segment editor effect in python?

I am trying to write an extension which automates use of some segment editor effects. Right now, I am trying to use the “Smoothing” effect on several segments, but I want to change the masking settings programmatically for different segments. How can I control, for example, the “Editable intensity range” or which segments are allowed to be overwritten? I’ve gotten far enough that I understand how to set the smoothing method and kernel size, but the masking settings appear to be handled differently. Thanks!

I think I may have figured this out.
I believe the masking settings are accessed through the SegmentationNode (I’ll call mine ‘segNode’ below) for the segmentation being modified. Specifically, to control the checkmark for “Editable intensity range”, use segNode.MasterVolumeIntensityMaskOn() to check or segNode.MasterVolumeIntensityMaskOff() to uncheck. To set the range, use segNode.SetMasterVolumeIntensityMaskRange(minVal, maxVal).

To control the “Overwrite other segments” part, use segNode.SetOverwriteMode(N) where N is 0, 1, or 2. 0 means overwrite all, 1 means overwrite visible, and 2 means overwrite none.

The most complex one is to control the “Editable Area”. It is segNode.SetMaskMode(N) where N is 0, 1, 2, 3, 4, or 5. 0 means paint allowed everywhere, 1 means paint allowed only inside all segments, 2 means paint allowed only inside visible segments, 3 means paint only allowed outside all segments, 4 means paint only allowed outside all visible segments, and 5 means paint only allowed inside one segment. To specify which segment paint is allowed in one must specify segNode.SetMaskSegmentID(segID), where segID is the segment ID for the editable segment.

If someone reads this and I’ve gotten something wrong or there are nuances I’m missing, please let me know, I’m quite a novice at this.

3 Likes

Looks like you are on the right track. For reference, you can use the enums defined here:

https://github.com/Slicer/Slicer/blob/4c0b0b15276aedc0e12d77e2227536426979d773/Modules/Loadable/Segmentations/MRML/vtkMRMLSegmentEditorNode.h#L46-L85

Then for clarity in the pythoncode you can use:

slicer.vtkMRMLSegmentEditorNode.PaintAllowedInsideSingleSegment
1 Like

Segmentation node (vtkMRMLSegmentationNode) stores segmentation. Editing options are stored in segment editor node (vtkMRMLSegmentEditorNode). For the built-in Segment Editor module, you can get segment editor node like this:

segmentEditorNode = slicer.modules.segmenteditor.widgetRepresentation().self().editor.mrmlSegmentEditorNode()

See several examples of how to use segment editor effects from Python in the script repository.

1 Like

Thanks for the clarification about which node type is the right one. I have looked at the examples in the script repository, which are very helpful, but I didn’t see any examples controlling the masking settings.

Thank you very much for the clarification. I found the enum definition, but am unfamiliar with C++ and interacting with it from python, so I couldn’t figure out how to access the enum values in the call to SetOverwriteMode. I agree, this will be much clearer than just using an integer.

The enums are available like this: slicer.vtkMRMLSegmentEditorNode.PaintAllowedEverywhere, slicer.vtkMRMLSegmentEditorNode.OverwriteAllSegments, etc.