How to use Segment Editor Margin Effect from Python script

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:

        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?

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 this now, so the Slicer Preview Release that you download tomorrow or later should work well.