Interacting with Segment Editor Widget through Python

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

API of SegmentEditor:
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

Let us know if you have more questions.

3 Likes

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?

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.

Also note there are convenience functions for creating segments directly from polydata or labelmap:
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(...) and
segmentationNode.AddSegmentFromBinaryLabelmapRepresentation(..) in segmentation node; or importing from existing model or labelmap nodes using segmetations module logic

1 Like

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)

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

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

effect.setParameter("Operation","INTERSECT")
1 Like

A post was split to a new topic: Set segmentation master volume from Python

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

self.scriptedEffect.selectEffect(“Paint”)

1 Like