# Prevent a segment from changing when editing other segments

**URL:** <https://discourse.slicer.org/t/prevent-a-segment-from-changing-when-editing-other-segments/19433>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [August 31, 2021, 8:16am UTC](https://discourse.slicer.org/t/prevent-a-segment-from-changing-when-editing-other-segments/19433 "2021-08-31T08:16:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [August 31, 2021, 8:16am UTC](https://discourse.slicer.org/t/prevent-a-segment-from-changing-when-editing-other-segments/19433/1 "2021-08-31T08:16:03Z")

</div>

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

```python
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()

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 31, 2021, 1:38pm UTC](https://discourse.slicer.org/t/prevent-a-segment-from-changing-when-editing-other-segments/19433/2 "2021-08-31T13:38:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 1, 2021, 5:44am UTC](https://discourse.slicer.org/t/prevent-a-segment-from-changing-when-editing-other-segments/19433/3 "2021-09-01T05:44:49Z")

</div>

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

I searched in examples [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#segmentations)

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 2, 2021, 11:41pm UTC](https://discourse.slicer.org/t/prevent-a-segment-from-changing-when-editing-other-segments/19433/4 "2021-09-02T23:41:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 3, 2021, 5:42am UTC](https://discourse.slicer.org/t/prevent-a-segment-from-changing-when-editing-other-segments/19433/5 "2021-09-03T05:42:29Z")

</div>

yes got this

thank you so much
