Error running a processing script on many segments

Dear community,
I have problem writing for loop.
When I try the script, it works just fine but it would not work as soon as I try to loop.
There must be something wrong with my script, but I can’t figure it out.
Here is part of my script with 3 segments rather than all 20 I usually use.

> # Create segmentation Node
> segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
> segmentationNode.CreateDefaultDisplayNodes()
>  
> #make new segment with custom color
> s=getNode('vtkMRMLSegmentationNode1')
> se=s.GetSegmentation()
>  
> Seg01 = slicer.vtkSegment()
> Seg01 .SetName("Seg_01")
> Seg01 .SetColor([1.0,0.0,0.0])
> se.AddSegment(Seg01 ,"Seg_01 ")
>  
> Seg02= slicer.vtkSegment()
> Seg02.SetName("Seg_02")
> Seg02.SetColor([1.0,0.7,0.2])
> se.AddSegment(Seg02,"Seg_02")
>  
> Seg03= slicer.vtkSegment()
> Seg03.SetName("Seg_03")
> Seg03.SetColor([1.0,1.0,0.0])
> se.AddSegment(Seg03,"Seg_03")
>  
>  
> segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
> segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
> segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
> segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
>  
> segmentEditorWidget.setSegmentationNode(s)
>  
> # Set overwrite mode: 0/1/2 -> overwrite all/visible/none
> segmentEditorNode.SetOverwriteMode(2)
>  
> # Get the segment IDs
> Seg01 = se.GetSegmentIdBySegmentName('Seg_01')
> Seg02 = se.GetSegmentIdBySegmentName('Seg_02')
> Seg03 = se.GetSegmentIdBySegmentName('Seg_03')
>  
># This is the part that does not work properly
> segIDs=['Seg01','Seg02','Seg03']
> 
>  for segID in segIDs:
>> segmentEditorNode.SetSelectedSegmentID(segID)
>> segmentEditorWidget.setActiveEffectByName("Threshold")
>> effect = segmentEditorWidget.activeEffect()
>> effect.setParameter("MinimumThreshold","80")
>> effect.self().onApply()

In my actual script with all 20 segments, the python interpreter returns the below messages.

> r,g,b = segmentationNode.GetSegmentation().GetSegment(segmentID).GetColor()
> AttributeError: ‘NoneType’Object has no attribute ‘GetColor’

I will be grateful to hear any pieces of advice.
Thank you always.

Your error message means that segmentationNode.GetSegmentation().GetSegment(segmentID) at that point is returning None. This is likely because there is no segment with the given segmentID in the segmentation. Check that the segment ID’s are correct (and are not None themselves).

1 Like

Thank you for your reply!
After your advice, I looked back in to the segment names and was able to make the for loop to work by fixing below!

> segIDs=[‘Seg01’,‘Seg02’,‘Seg03’]

to

> segIDs=[Seg01,Seg02,Seg03]

I still have one more question though.
This works with other modules such as Logical Operators but when I tried thresholding, it keep gives a error message saying that the Master volume is not set as either the forground or background.

> segmentEditorNode.SetSelectedSegmentID(Seg01)
> segmentEditorWidget.setActiveEffectByName("Threshold")
> effect = segmentEditorWidget.activeEffect()
> effect.setParameter("MinimumThreshold","80")
> effect.self().onApply()

I thought this may happened because I did not set the master volume so I added below but it did not worked as well.

> segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

Can you please give me advice on this situation?

1 Like

To avoid seeing the warning above, set the master volume as either the foreground or background volume as shown here.

2 Likes

Thank you for your help!
It works fine now.
I followed the link you showed and set my master volume as forground and background using below

slicer.util.setSliceViewerLayers(background=masterVolumeNode, foreground=masterVolumeNode)