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.