# How to use Segment Editor Margin Effect from Python script

**URL:** <https://discourse.slicer.org/t/how-to-use-segment-editor-margin-effect-from-python-script/26512>\
**Category:** Support\
**Created:** [November 30, 2022, 3:50pm UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-margin-effect-from-python-script/26512 "2022-11-30T15:50:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caetox](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/caetox/32/17513_2.png) [@Caetox](https://discourse.slicer.org/u/Caetox)\
**Post date:** [November 30, 2022, 3:50pm UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-margin-effect-from-python-script/26512/1 "2022-11-30T15:50:10Z")

</div>

I am trying to grow segments with the margin effect. I have a segmentation with 5 segments, all segments are supposed to grow by 1mm (with overlap).  
This is how I use it in my script:

```auto
        segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
        segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
        segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
        slicer.mrmlScene.AddNode(segmentEditorNode)
        segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)

        segmentEditorWidget.setSegmentationNode(segmentationNode)
        segmentEditorWidget.setMasterVolumeNode(volumeNode)
        segmentEditorWidget.setActiveEffectByName("Margin")
        effect = segmentEditorWidget.activeEffect()
        effect.setParameter("MarginSizeMm", 1.0)
        effect.setParameter("ApplyToAllVisibleSegments", 1)
        segmentEditorNode.SetOverwriteMode(2)
        segmentEditorNode.SetMaskMode(0)
        effect.self().onApply()

```

When i run this, the effect is applied only to the first segment in my segmentation, and it is applied 5 times to this segment. Instead I want the effect to be applied once to each segment. It works perfectly if I do this manually in the Segment Editor.  
How can I solve this?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/e/1ef5487e433e4ec8a423475a7b4cd8eac9ea767d.jpeg)

---

<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:** [December 1, 2022, 2:59am UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-margin-effect-from-python-script/26512/2 "2022-12-01T02:59:54Z")

</div>

Good catch, the issue was due to the effect iterated through effects in the Segment Editor module’s widget and not in your custom widget. I’ve [fixed](https://github.com/Slicer/Slicer/commit/b803ad48595c71a6e8768f920dab9f86c18be893) this now, so the Slicer Preview Release that you download tomorrow or later should work well.
