# Scissors in console vs module

**URL:** <https://discourse.slicer.org/t/scissors-in-console-vs-module/28367>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [March 14, 2023, 3:16am UTC](https://discourse.slicer.org/t/scissors-in-console-vs-module/28367 "2023-03-14T03:16:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [March 14, 2023, 3:16am UTC](https://discourse.slicer.org/t/scissors-in-console-vs-module/28367/1 "2023-03-14T03:16:18Z")

</div>

Hi,  
I am writing a module where I need to use threshold and scissors from the Segment Editor.

If I run the following code in the console it works as expected and I can draw the ROI but when I copy the code to my module, the thresholding is done and the cursor changes to the scissor icon but I cannot create the ROI. Any suggestions?

```auto
masterVolumeNode = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode')

# Create segmentation
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)

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

addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("t_stomach")
segmentEditorNode.SetSelectedSegmentID(addedSegmentID)

segmentEditorWidget.setActiveEffectByName("Threshold")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold","0")
effect.self().onApply()

segmentEditorWidget.setActiveEffectByName("Scissors")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation","EraseOutside")
effect.setParameter("Shape","FreeForm")

```

---

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [March 14, 2023, 3:45am UTC](https://discourse.slicer.org/t/scissors-in-console-vs-module/28367/2 "2023-03-14T03:45:03Z")

</div>

If I put my code in a function on the console I have the same behavior so I need to pause for user input inside the function. What is the best way to do that?

---

<div class="post-metadata">

**Author:** ![Honeee](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/honeee/32/66218_2.png) [@Honeee](https://discourse.slicer.org/u/Honeee)\
**Post date:** [July 31, 2023, 9:58am UTC](https://discourse.slicer.org/t/scissors-in-console-vs-module/28367/3 "2023-07-31T09:58:28Z")

</div>

Hello Mark, I have the same issue.  
I’m using application startup script from Slicer. From my pyhton script, I managed to perform a few segmentation operations such as Tresholding and Smoothing, but then I’d like to create a rectangle shape from scissors and then Erase Outside.

I keep having the same mistake :  
AttributeError: qSlicerSegmentEditorScissorsEffect has no attribute named ‘onApply’

Did you manage to go further ?

---

<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:** [July 31, 2023, 5:18pm UTC](https://discourse.slicer.org/t/scissors-in-console-vs-module/28367/4 "2023-07-31T17:18:35Z")

</div>

Scissors effect is an ineractive effect. How would you use it without user input?

---

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [July 31, 2023, 10:42pm UTC](https://discourse.slicer.org/t/scissors-in-console-vs-module/28367/5 "2023-07-31T22:42:01Z")

</div>

I did make my code work by adding a pause after setting the Draw effect

```auto
        segmentEditorWidget.setActiveEffectByName("Draw")
        try:
            input("continue:")
        except:
            pass

```

which pauses further processing until the user presses the enter key in the python console.

I didn’t like this solution so now the user adds a markup ROI and then a binary mask is created from the markup points. Our images are very low resolution so using markups allows the user to adjust the ROI until they are satisfied with the result.

---

<div class="post-metadata">

**Author:** ![Honeee](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/honeee/32/66218_2.png) [@Honeee](https://discourse.slicer.org/u/Honeee)\
**Post date:** [August 1, 2023, 9:14am UTC](https://discourse.slicer.org/t/scissors-in-console-vs-module/28367/6 "2023-08-01T09:14:56Z")

</div>

Ok I understand and that’s what I thought.  
However, I was wondering if it could be possible with a python scrip because I read topic “Scissors segment editor effects from a script” and thought it could be possible to draw a shape (a rectangle for instance) inside a segment and then erase outside the shape.  
Tell me if i’m wrong or if I missunderstood.

---

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [August 2, 2023, 12:12am UTC](https://discourse.slicer.org/t/scissors-in-console-vs-module/28367/7 "2023-08-02T00:12:00Z")

</div>

Here is a complete example. As previously mentioned, I could not work out how to avoid using keyboard input in the python console and using markups is better in my application.

```auto
def makeMaskedVol(volumeNode):
    if volumeNode is None:
        return
    maskedVolume = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", "maskedvol")
    segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode", "testSegment")
    segmentationNode.CreateDefaultDisplayNodes() # only needed for display
    segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(volumeNode)

    # Create temporary segment editor to get access to effects
    segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
    segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
    segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
    segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
    segmentEditorWidget.setSegmentationNode(segmentationNode)
    segmentEditorWidget.setSourceVolumeNode(volumeNode)

    addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment()
    segmentEditorNode.SetSelectedSegmentID(addedSegmentID)

    segmentEditorWidget.setActiveEffectByName("Threshold")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("MinimumThreshold","0")
    effect.self().onApply()
    segmentationNode.GetDisplayNode().SetOpacity(0.4)

    segmentEditorWidget.setActiveEffectByName("Scissors")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("Operation","EraseOutside")
    effect.setParameter("Shape","FreeForm")
    try:
        input("continue:")
    except:
        pass
    segmentEditorWidget.setActiveEffectByName("Mask volume")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("BinaryMaskFillValueInside","1")
    effect.setParameter("BinaryMaskFillValueOutside","0")
    effect.setParameter("FillValue","0")
    effect.setParameter("InputVisibility","True")
    effect.setParameter("Operation","FILL_OUTSIDE")
    effect.self().outputVolumeSelector.setCurrentNode(maskedVolume)
    effect.self().onApply()
    return segmentationNode

```
