# How to programmatically use logical operator add function from segment editor?

**URL:** <https://discourse.slicer.org/t/how-to-programmatically-use-logical-operator-add-function-from-segment-editor/16581>\
**Category:** Development\
**Tags:** python\
**Created:** [March 16, 2021, 5:00pm UTC](https://discourse.slicer.org/t/how-to-programmatically-use-logical-operator-add-function-from-segment-editor/16581 "2021-03-16T17:00:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [March 16, 2021, 5:00pm UTC](https://discourse.slicer.org/t/how-to-programmatically-use-logical-operator-add-function-from-segment-editor/16581/1 "2021-03-16T17:00:22Z")

</div>

Operating system:windows 10  
Slicer version:4.11

I want to programmatically use the add operator in the logical operators segment editor effect. Is there any way to do this? I want to use the existing segments in the segmentation nodes and add them with a button click. How do I access this with code?

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [March 17, 2021, 7:21am UTC](https://discourse.slicer.org/t/how-to-programmatically-use-logical-operator-add-function-from-segment-editor/16581/2 "2021-03-17T07:21:38Z")

</div>

Here are a couple python functions I have which I use to copy or subtract segments, adding would be a simple variation:

```
def setup_segment_editor(segmentationNode=None, masterVolumeNode=None):
  '''Runs standard setup of segment editor widget and segment editor node'''
  if segmentationNode is None:
    # Create segmentation node
    segmentationNode = slicer.vtkMRMLSegmentationNode()
    slicer.mrmlScene.AddNode(segmentationNode)
    segmentationNode.CreateDefaultDisplayNodes()
  segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
  segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
  slicer.mrmlScene.AddNode(segmentEditorNode)
  segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
  segmentEditorWidget.setSegmentationNode(segmentationNode)
  segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
  if masterVolumeNode:
    segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
  return segmentEditorWidget, segmentEditorNode, segmentationNode

def copy_segment(newSegmentName, segmentationNode, segmentIDToCopy):
  """ Copy an existing segment into a new segment without overwriting """
  import SegmentEditorEffects
  segmentEditorWidget, segmentEditorNode, segmentationNode = setup_segment_editor(segmentationNode)
  newSegmentID = segmentationNode.GetSegmentation().AddEmptySegment(newSegmentName)
  segmentEditorNode.SetSelectedSegmentID(newSegmentID)
  segmentEditorWidget.setActiveEffectByName('Logical operators')
  effect = segmentEditorWidget.activeEffect()
  effect.setParameter("Operation", SegmentEditorEffects.LOGICAL_COPY)
  effect.setParameter("ModifierSegmentID", segmentIDToCopy)
  # Masking Settings
  effect.setParameter("BypassMasking",1) # probably not necessary since I adjust masking settings next
  segmentEditorNode.SetMaskMode(segmentEditorNode.PaintAllowedEverywhere)
  segmentEditorNode.MasterVolumeIntensityMaskOff()
  segmentEditorNode.SetOverwriteMode(segmentEditorNode.OverwriteNone)
  # Do the copy
  effect.self().onApply()
  slicer.mrmlScene.RemoveNode(segmentEditorNode)
  return newSegmentID

def subtract_segment(segmentationNode, segmentID, to_subtract_segmentID):
  '''Perform logical subtraction of to_subtract_segmentID from segmentID'''
  import SegmentEditorEffects
  segmentEditorWidget, segmentEditorNode, segmentationNode = setup_segment_editor(segmentationNode)
  segmentEditorNode.SetSelectedSegmentID(segmentID)
  segmentEditorWidget.setActiveEffectByName('Logical operators')
  effect = segmentEditorWidget.activeEffect()
  effect.setParameter("Operation", SegmentEditorEffects.LOGICAL_SUBTRACT)
  effect.setParameter("BypassMasking",1)
  effect.setParameter("ModifierSegmentID",to_subtract_segmentID)
  effect.self().onApply()
  slicer.mrmlScene.RemoveNode(segmentEditorNode)
```

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [August 31, 2021, 7:40am UTC](https://discourse.slicer.org/t/how-to-programmatically-use-logical-operator-add-function-from-segment-editor/16581/3 "2021-08-31T07:40:39Z")

</div>

I used ADD in the operation name but it says  
Processing started

Unknown operation: Add

I used ADD but it still dont recognise

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [August 31, 2021, 3:59pm UTC](https://discourse.slicer.org/t/how-to-programmatically-use-logical-operator-add-function-from-segment-editor/16581/4 "2021-08-31T15:59:59Z")

</div>

The operation for adding is “SegmentEditorEffects.LOGICAL\_UNION” rather than LOGICAL\_ADD.

---

<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:** [August 31, 2021, 7:31pm UTC](https://discourse.slicer.org/t/how-to-programmatically-use-logical-operator-add-function-from-segment-editor/16581/5 "2021-08-31T19:31:11Z")

</div>

See description of all parameters in the [Developer manual](https://slicer.readthedocs.io/en/latest/developer_guide/modules/segmenteditor.html#effect-parameters).
