# Error running a processing script on many segments

**URL:** https://discourse.slicer.org/t/error-running-a-processing-script-on-many-segments/22010
**Category:** Development
**Created:** [February 17, 2022, 6:12am UTC](https://discourse.slicer.org/t/error-running-a-processing-script-on-many-segments/22010 "2022-02-17T06:12:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)
#### Post date: [February 17, 2022, 6:12am UTC](https://discourse.slicer.org/t/error-running-a-processing-script-on-many-segments/22010/1 "2022-02-17T06:12:00Z")

</div>

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.

```python
> # 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.

```python
> 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.

---

<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: [February 17, 2022, 5:28pm UTC](https://discourse.slicer.org/t/error-running-a-processing-script-on-many-segments/22010/2 "2022-02-17T17:28:41Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)
#### Post date: [February 18, 2022, 6:07am UTC](https://discourse.slicer.org/t/error-running-a-processing-script-on-many-segments/22010/3 "2022-02-18T06:07:41Z")

</div>

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!

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

```

to

```auto
> 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.

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

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/c/6c0a20880858e019bdd6ccf3de80b66a9683d5db.jpeg)

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

```auto
> segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

```

Can you please give me advice on this situation?

---

<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: [February 18, 2022, 4:40pm UTC](https://discourse.slicer.org/t/error-running-a-processing-script-on-many-segments/22010/4 "2022-02-18T16:40:56Z")

</div>

To avoid seeing the warning above, set the master volume as either the foreground or background volume as shown [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#iterate-over-current-visible-slice-views-and-set-foreground-and-background-images).

---

<div class="post-metadata">

### Author: ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)
#### Post date: [February 21, 2022, 2:19am UTC](https://discourse.slicer.org/t/error-running-a-processing-script-on-many-segments/22010/5 "2022-02-21T02:19:59Z")

</div>

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)
