# Interacting with Segment Editor Widget through Python

**URL:** <https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [June 14, 2017, 8:57pm UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505 "2017-06-14T20:57:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kohle](https://avatars.discourse-cdn.com/v4/letter/k/f04885/32.png) [@kohle](https://discourse.slicer.org/u/kohle)\
**Post date:** [June 14, 2017, 8:57pm UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/1 "2017-06-14T20:57:08Z")

</div>

Hi I’m new to the slicer python api and was wondering if someone could point me in the right direction.  
I would like to use a the python api to create serveral segements through the “Logic operators” function of the segment editor widget. I would like to create these segements from existing segments.

I managed to load an example extentsion for slicer in which I select the master volume and segmentation through python.

My main question is how I can find out about the different classes and functions that I want to use.  
For example:  
How do I change the “overwrite other segment” parameter ?  
I found the setActiveEffectByName funtction were can I find its parameters ?  
How do figure out what the SegmentID of each segment is?

Thanks for your help

---

<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:** [June 15, 2017, 2:02am UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/2 "2017-06-15T02:02:15Z")

</div>

API of SegmentEditor:  
[http://apidocs.slicer.org/master/classvtkMRMLSegmentEditorNode.html](http://apidocs.slicer.org/master/classvtkMRMLSegmentEditorNode.html)

Examples for using segmentations and segment editor from Python scripts: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Segmentations](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Segmentations)

Let us know if you have more questions.

---

<div class="post-metadata">

**Author:** ![kohle](https://avatars.discourse-cdn.com/v4/letter/k/f04885/32.png) [@kohle](https://discourse.slicer.org/u/kohle)\
**Post date:** [June 15, 2017, 10:00am UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/3 "2017-06-15T10:00:32Z")

</div>

Thanks for pointing me in the right direction.

I am not quite sure how to set the parameters of the effects.

```
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation", LOGICAL_INTERSECT)

```

Does not seem to be the right code.  
Also I don’t know how to change the name of new segments after creating them.

```
inSegmentation.GetSegmentation().AddEmptySegment(segmentName = "test")

```

Does not work for me.  
Neither does changing the name after creating it with

```
inSegmentation.GetSegmentation().GetNthSegment(1).SetName("test")

```

Can someone please point out my mistakes?

---

<div class="post-metadata">

**Author:** ![kohle](https://avatars.discourse-cdn.com/v4/letter/k/f04885/32.png) [@kohle](https://discourse.slicer.org/u/kohle)\
**Post date:** [June 15, 2017, 11:14am UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/4 "2017-06-15T11:14:31Z")

</div>

I manged to find a the problem with

```
inSegmentation.GetSegmentation().AddEmptySegment(segmentName = "test")

```

it is meant to be

```
 inSegmentation.GetSegmentation().AddEmptySegment("test")

```

this changes name and ID of the segment.

---

<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:** [June 15, 2017, 4:59pm UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/5 "2017-06-15T16:59:51Z")

</div>

Also note there are convenience functions for creating segments directly from polydata or labelmap:  
`segmentationNode.AddSegmentFromClosedSurfaceRepresentation(...)` and  
`segmentationNode.AddSegmentFromBinaryLabelmapRepresentation(..)` in [segmentation node](http://apidocs.slicer.org/master/classvtkMRMLSegmentationNode.html); or importing from existing model or labelmap nodes using [segmetations module logic](http://apidocs.slicer.org/master/classvtkSlicerSegmentationsModuleLogic.html)

---

<div class="post-metadata">

**Author:** ![kohle](https://avatars.discourse-cdn.com/v4/letter/k/f04885/32.png) [@kohle](https://discourse.slicer.org/u/kohle)\
**Post date:** [June 16, 2017, 11:29am UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/6 "2017-06-16T11:29:30Z")

</div>

Thanks for your help again.

I would like to create new segmentations with the threshold tool and save them. I am not sure if using the widget is the best way to do it. How do you think I can achive this easiest?  
Where can I find the different parameters each editor effect accepts ? as this does not seem to work:

```
effect.setParameter("Operation", LOGICAL_INTERSECT)
```

---

<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:** [June 16, 2017, 5:02pm UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/7 "2017-06-16T17:02:49Z")

</div>

> [@kohle](#):
>
> as this does not seem to work

Do you get an error message? Please copy-paste it here.

---

<div class="post-metadata">

**Author:** ![kohle](https://avatars.discourse-cdn.com/v4/letter/k/f04885/32.png) [@kohle](https://discourse.slicer.org/u/kohle)\
**Post date:** [June 17, 2017, 9:00pm UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/8 "2017-06-17T21:00:45Z")

</div>

Thanks again for your great support. I managed to set the parameters with

```
effect.setParameter("Operation","INTERSECT")
```

---

<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 9, 2017, 9:20pm UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/9 "2017-08-09T21:20:25Z")

</div>

A post was split to a new topic: [Set segmentation master volume from Python](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851)

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [August 25, 2017, 12:42am UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/10 "2017-08-25T00:42:25Z")

</div>

How do I get the segmenteditor widget from Python when I am in the SegmentEditorEffect class? I want to use the setActiveEffectByName method

---

<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 25, 2017, 2:17am UTC](https://discourse.slicer.org/t/interacting-with-segment-editor-widget-through-python/505/11 "2017-08-25T02:17:42Z")

</div>

self.scriptedEffect.selectEffect(“Paint”)
