# Sequential Segment Editor Effects not applied in script

**URL:** <https://discourse.slicer.org/t/sequential-segment-editor-effects-not-applied-in-script/15968>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [February 12, 2021, 1:41pm UTC](https://discourse.slicer.org/t/sequential-segment-editor-effects-not-applied-in-script/15968 "2021-02-12T13:41:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pete](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Pete](https://discourse.slicer.org/u/Pete)\
**Post date:** [February 12, 2021, 1:41pm UTC](https://discourse.slicer.org/t/sequential-segment-editor-effects-not-applied-in-script/15968/1 "2021-02-12T13:41:51Z")

</div>

Hi,

I have written a script to apply thresholds to a segment and then to apply median smoothing. Both these actions work exactly as expected separately, but when chained together in the script only the first (thresholding) is applied. Anyone able to help with this?

```
# Create new segments in Segmentation
segmentationNode = getNode('Segmentation')

SingleSegment = segmentationNode.GetSegmentation().AddEmptySegment('Single')
MultiSegment = segmentationNode.GetSegmentation().AddEmptySegment('Multi')

# OPTIONAL -- Set masterVolumeNode to first volume node
masterVolumeNode = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode')

segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)

# OPTIONAL -- 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(masterVolumeNode)

# OPTIONAL -- Create segments by thresholding
### This will show available effects and parameters: print(slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentEditorNode"))
segmentsFromHounsfieldUnits = [
    ["Mask", -200, -50] ]
for segmentName, thresholdMin, thresholdMax in segmentsFromHounsfieldUnits:
    # Create segment
    addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment(segmentName)
    segmentEditorNode.SetSelectedSegmentID(addedSegmentID)
    # Fill by thresholding
    segmentEditorWidget.setActiveEffectByName("Threshold")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("MinimumThreshold",str(thresholdMin))
    effect.setParameter("MaximumThreshold",str(thresholdMax))
    effect.self().onApply()

# OPTIONAL -- Apply smoothing to threshold mask
smoothSegments = [
    ["Mask"] ]
maskSmoothID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName("Mask")
    
for smoothSegment in smoothSegments:
    segmentEditorNode.SetSelectedSegmentID(maskSmoothID)
    segmentEditorWidget.setActiveEffectByName("Smoothing")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("KernelSizeMm",3)
    effect.self().onApply()
    
# OPTIONAL -- Delete temporary segment editor
segmentEditorWidget = None
slicer.mrmlScene.RemoveNode(segmentEditorNode)
```

---

<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:** [February 12, 2021, 2:52pm UTC](https://discourse.slicer.org/t/sequential-segment-editor-effects-not-applied-in-script/15968/2 "2021-02-12T14:52:51Z")

</div>

The script works well for me.

The only suspicious thing is in your second `for` loop. You either don’t need this loop (and just use `maskSmoothID`) or you need to change the `for` loop to use `smoothSegment`.
