# Include a manual Segment Editor effect in a module

**URL:** <https://discourse.slicer.org/t/include-a-manual-segment-editor-effect-in-a-module/17464>\
**Category:** Development\
**Tags:** segmentation, module\
**Created:** [May 5, 2021, 11:23am UTC](https://discourse.slicer.org/t/include-a-manual-segment-editor-effect-in-a-module/17464 "2021-05-05T11:23:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guglielmo\_Baccani](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/guglielmo_baccani/32/9204_2.png) [@Guglielmo\_Baccani](https://discourse.slicer.org/u/Guglielmo_Baccani)\
**Post date:** [May 5, 2021, 11:23am UTC](https://discourse.slicer.org/t/include-a-manual-segment-editor-effect-in-a-module/17464/1 "2021-05-05T11:23:02Z")

</div>

I’m building a semi-automatic segmentation module by combining some effects from the Segment Editor. Simplifying the problem, in my module I would like to use the following effects in sequence: Threshold, Scissors (which requires manual user interaction) and Islands. During the Scissors effect, after setting some parameters, I would like to use the shortcut of the space bar of the Segment Editor to switch between the Scissors effect and None so that I can more easily rotate the segmentation in the 3d view.

This is the simplified version of the process function of the Logic class (called from the onApplyButton function):

```auto
  def process(self, inputVolume, outputSegmentationNode):

    segmentation = outputSegmentationNode.GetSegmentation()
    outputSegmentationNode.CreateDefaultDisplayNodes() # only needed for display
    outputSegmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(inputVolume)

    # Create segment editor to get access to effects
    segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
    segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
    segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
    segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
    segmentEditorWidget.setSegmentationNode(outputSegmentationNode)
    segmentEditorWidget.setMasterVolumeNode(inputVolume)

    # Create RoiSegment
    RoiSegmentID = segmentation.AddEmptySegment("ROI")
    segmentEditorNode.SetSelectedSegmentID(RoiSegmentID)

    # Threshold
    segmentEditorWidget.setActiveEffectByName("Threshold")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("MaximumThreshold","-250")
    effect.setParameter("MinimumThreshold","-950")
    effect.self().onApply()

    # Scissor
    slicer.util.selectModule('SegmentEditor')
    segmentEditorWidget2 = slicer.modules.SegmentEditorWidget.editor
    segmentEditorWidget2.setMRMLSegmentEditorNode(segmentEditorNode)
    segmentEditorWidget2.setActiveEffectByName('Scissors')
    effect = segmentEditorWidget2.activeEffect()
    effect.setParameter("Operation","EraseOutside")

    outputSegmentationNode.CreateClosedSurfaceRepresentation()

    try:
      input("Select the ROI from the 3d view, then press Enter to continue...")
    except EOFError:
      pass

    outputSegmentationNode.RemoveClosedSurfaceRepresentation()

    # Keep largest island
    segmentEditorWidget.setActiveEffectByName("Islands")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("Operation","KEEP_LARGEST_ISLAND")
    effect.self().onApply()

    # Delete temporary segment editor
    segmentEditorWidget = None
    segmentEditorWidget2 = None
    slicer.mrmlScene.RemoveNode(segmentEditorNode)
```

Once the module is finished running, after switching to the Segment Editor module, the spacebar shortcut no longer works and this leads me to think that I have made some mistake in the code. I guess I got confused with the two segmentEditorWidgets (of which I don’t understand the difference).  
What is the best way to do the above?

---

<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:** [May 21, 2021, 6:33pm UTC](https://discourse.slicer.org/t/include-a-manual-segment-editor-effect-in-a-module/17464/2 "2021-05-21T18:33:00Z")

</div>

You need to call `self.editor.installKeyboardShortcuts()` / `self.editor.uninstallKeyboardShortcuts()` to enable/disable keyboard shortcuts.
