Prevent a segment from changing when editing other segments

Hi Andras,
I am trying to do the following
import segment to segmentationNode
create a new segment
copy the import segment to new segment
grow the newsegment with margin

When I do all this I do not retain the old imported segment. I could not find mistake. I am pasting code here. Could you please help me in this. Thank you

outputVolume.SetName("brain")
    segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
    segmentationNode.CreateDefaultDisplayNodes() # only needed for display
    #segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(inputVolume)
    
    # Create temporary segment editor to get access to effects
    segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
    segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
    segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
    segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
    segmentEditorWidget.setSegmentationNode(segmentationNode)
    #segmentEditorWidget.setMasterVolumeNode(inputVolume)
    
    #import volume to labelmap     
    slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(outputVolume, segmentationNode) 
    segmentationNode.CreateClosedSurfaceRepresentation()
    segmentEditorWidget.setCurrentSegmentID(segmentationNode.GetSegmentation().GetNthSegmentID(0))
    
    segmentation = segmentationNode.GetSegmentation()
    segmentation.AddEmptySegment("Skull")
    
    #copy brain into this new segment
    segmentEditorWidget.setActiveEffectByName("Logical operators")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("Operation", "UNION")
    brain = segmentation.GetSegmentIdBySegmentName("brain")
    skull = segmentation.GetSegmentIdBySegmentName("skull")
    effect.setParameter("SelectedSegmentID", skull) 
    effect.setParameter("ModifierSegmentID", brain) 
    #segmentEditorWidget.setCurrentSegmentID(skull)
    effect.self().onApply()
    
    #grow the skull
    
    segmentEditorWidget.setActiveEffectByName("Margin")
    effect = segmentEditorWidget.activeEffect()
    segmentEditorWidget.setSegmentationNode(segmentationNode)
    #segmentEditorWidget.setCurrentSegmentID(skull)
    segmentEditorWidget.setActiveEffectByName("Margin")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("MarginSizeMm","4")
    effect.setParameter("SelectedSegmentID", skull) 

    effect.self().onApply()

You need to enable segments to overlap. Otherwise segments overwrite each other when being edited.

Hi Andras,
Sorry I do not know how to do that.

I searched in examples Script repository — 3D Slicer documentation

I found this
defaultSegmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
defaultSegmentEditorNode.SetOverwriteMode(slicer.vtkMRMLSegmentEditorNode.OverwriteNone)
slicer.mrmlScene.AddDefaultNode(defaultSegmentEditorNode)

is this what you are taking about.

but I put this in code still doesnt work. The segment disappears

Any help.

Thanks

You need to modify the segment editor node that is associated with your segment editor widget.

1 Like

yes got this

thank you so much