# Set segmentation master volume from Python

**URL:** <https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851>\
**Category:** Support\
**Created:** [August 9, 2017, 9:04pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851 "2017-08-09T21:04:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_McCurdy](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@Colin\_McCurdy](https://discourse.slicer.org/u/Colin_McCurdy)\
**Post date:** [August 9, 2017, 9:04pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851/1 "2017-08-09T21:04:28Z")

</div>

I have a question related to this topic (sorry if I should have posted a new topic!)

I am trying to auto-create segmentations from multiple master volumes using python, but when trying to do the second segmentation it has trouble setting the active segmentation with this code:

`segmentEditorWidget.setSegmentationNode(segmentationNode) segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)`

Interestingly, calling `setSegmentationNode(segmentationNode)` twice sets the node properly, but the master volume node setting is for some reason disabled, as shown:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/2/c21e2d4b8f7515b1d5d5cc132af4c0a4865c24d8.png)

I’m not sure if I can simply enable that menu (or how to do that) or if I’m missing some sort of ‘reset’ I need to do to access this menu.

Thanks,

Colin

---

<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:22pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851/2 "2017-08-09T21:22:10Z")

</div>

After setting segmentation node, you should be able to set the master node by simply calling setMasterVolumeNode. Are you using the latest nightly version of Slicer?

---

<div class="post-metadata">

**Author:** ![Colin\_McCurdy](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@Colin\_McCurdy](https://discourse.slicer.org/u/Colin_McCurdy)\
**Post date:** [August 10, 2017, 3:15pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851/3 "2017-08-10T15:15:15Z")

</div>

Nope, I’ll try it out.  
I agree and for the first iteration of my code that method works perfectly. I’ll update after I try in the newest nightly…

**Update** : still not working. I’m wondering if I’m not setting up the widget properly as I do a few things within the loop.

I set this up outside of the loop:  
`segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()`

then within  
`for volNum, vol in enumerate(vols):`

`# ... code for setup of segmentation...`

`segmentEditorWidget.setMRMLScene(slicer.mrmlScene)`  
`segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()`  
`slicer.mrmlScene.AddNode(segmentEditorNode)`  
`segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)`  
`segmentEditorWidget.setSegmentationNode(segmentationNode)`  
`segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)`

`# ... after seed growth and at end of loop...`  
`slicer.mrmlScene.RemoveNode(segmentEditorNode)`

Then the error results when I attempt to grow from seeds during the second iteration of the loop.

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [August 10, 2017, 4:20pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851/4 "2017-08-10T16:20:54Z")

</div>

@che85 did this in another module - can you point @Colin_McCurdy to the relevant part in QuantitativeReporting Christian?

---

<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 10, 2017, 4:51pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851/5 "2017-08-10T16:51:48Z")

</div>

Also, have a look at the simple [example script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script). Does that work correctly?

---

<div class="post-metadata">

**Author:** ![Colin\_McCurdy](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@Colin\_McCurdy](https://discourse.slicer.org/u/Colin_McCurdy)\
**Post date:** [August 10, 2017, 5:20pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851/6 "2017-08-10T17:20:00Z")

</div>

My code is based off of the SegmentGrowCutSimple code.

It’s moreso using the segmentEditorWidget within a loop that seems to be the issue.

---

<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 10, 2017, 5:35pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851/7 "2017-08-10T17:35:09Z")

</div>

If you don’t let events to be processed (because you don’t return from your loop) then it might be an issue. Try if adding `slicer.app.processEvents()` in your loop fixes the problems.

Is your module on GitHub? If you send the repository URL then I can have a look.

---

<div class="post-metadata">

**Author:** ![Colin\_McCurdy](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@Colin\_McCurdy](https://discourse.slicer.org/u/Colin_McCurdy)\
**Post date:** [August 10, 2017, 5:54pm UTC](https://discourse.slicer.org/t/set-segmentation-master-volume-from-python/851/8 "2017-08-10T17:54:02Z")

</div>

not quite a module, just a script for now.  
But you can view it here:  
[dtiAnalysis/dtiAnalysisNrrd.py at master · colinmccurdy/dtiAnalysis · GitHub](https://github.com/colinmccurdy/dtiAnalysis/blob/master/dtiAnalysisNrrd.py) , let me know if you see any glaring issues !

also, I tried processEvents in a few locations and it did not make a difference.

Another note: even when I run a single iteration I get the following errors:

> void \_\_cdecl qMRMLSegmentEditorWidget::onSegmentSelectionChanged(const class QItemSelection &,const class QItemSelection &) : Invalid segment editor parameter set node  
> void \_\_cdecl qMRMLSegmentEditorWidget::onSegmentationNodeChanged(class vtkMRMLNode \*) : Invalid segment editor parameter set node  
> void \_\_cdecl qMRMLSegmentEditorWidget::onSegmentSelectionChanged(const class QItemSelection &,const class QItemSelection &) : Invalid segment editor parameter set node  
> void \_\_cdecl qMRMLSegmentEditorWidget::onSegmentationNodeChanged(class vtkMRMLNode \*) : Invalid segment editor parameter set node  
> void \_\_cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode \*) : Invalid segment editor parameter set node  
> void \_\_cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode \*) : Invalid segment editor parameter set node  
> void \_\_cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode \*) : Invalid segment editor parameter set node  
> void \_\_cdecl qMRMLSegmentEditorWidget::updateWidgetFromMRML(void) : Invalid segment editor parameter set node  
> vtkSlicerSegmentationsModuleLogic::GetSegmentForSegmentSubjectHierarchyItem: Segmentation does not contain segment with given ID: PhantomVolume  
> vtkSlicerSegmentationsModuleLogic::GetSegmentForSegmentSubjectHierarchyItem: Segmentation does not contain segment with given ID: Background  
> vtkSlicerSegmentationsModuleLogic::GetSegmentForSegmentSubjectHierarchyItem: Segmentation does not contain segment with given ID: Feature

The code seems to still execute though with a single one.
