# How to programmatically create the Master Volume for a segmentation node with multiple segments

**URL:** https://discourse.slicer.org/t/how-to-programmatically-create-the-master-volume-for-a-segmentation-node-with-multiple-segments/22098
**Category:** Support
**Tags:** segmentation, python
**Created:** [February 22, 2022, 1:04am UTC](https://discourse.slicer.org/t/how-to-programmatically-create-the-master-volume-for-a-segmentation-node-with-multiple-segments/22098 "2022-02-22T01:04:52Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![PhilipDavis](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/philipdavis/32/14142_2.png) [@PhilipDavis](https://discourse.slicer.org/u/PhilipDavis)
#### Post date: [February 22, 2022, 1:04am UTC](https://discourse.slicer.org/t/how-to-programmatically-create-the-master-volume-for-a-segmentation-node-with-multiple-segments/22098/1 "2022-02-22T01:04:52Z")

</div>

Hi there,  
I’m new to 3D Slicer and now I’m developing a Python scripting module.

Currently, I want to implement a procedure in python code of my module for calculating the volume value of the **overlapping/Intersection** part of two segments.  
As shown in the figure below, at the beginning I have two segmentation nodes, I set their color to red and green respectively:

 ![Figure1](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/f/2fe2dc1e75c31bf339f1060ef9815df592370788.png)  
 ![Figure2](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/8/e89561f83b673965e4a07e8f83c9ec367ddd6fa2.png)

And according to this post:[Calculating shared volume between two models](https://discourse.slicer.org/t/calculating-shared-volume-between-two-models/10144/8), here are the steps I followed to get the Intersection of two segments:

1. Go to the ‘Segmentation’ module and use ‘Export/Import Models and Labelmaps’ to export the two segments to the ‘Models’ module to create two corresponding models:  
 ![Figure3](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/2/a2af7c857fad252dce42f97c5ca820950c757498.png)  
 ![Figure4](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/2/d2116929c5e7df785b8c0048b4c2a4ee48cdef8b.png)

2. Go to the ‘segmentation’ module and create a new segmentation node, I named it ‘ **NEW Segmentation** ’, and import both of models get from the previous step into this new segmentation node using the ‘Export/Import Models and Labelmaps’:  
 ![Figure5](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/c/4ce18231599cdb27c8e7f08c99efad048ce96903.png)

3. Go to the ‘ **Segmentation Editor** ’ module, switch the Segmentation node to ‘ **NEW Segmentation** ’, and now I notice that all effects are greyed out:  
 ![Figure6](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/3/4320ffd723b625b344ee3ef79370dfa3824069fd.png)

According to the example given by @mikebind in this post: [How to programmatically use logical operator add function](https://discourse.slicer.org/t/how-to-programmatically-use-logical-operator-add-function-from-segment-editor/16581/2), I found that in order to use the logical operator effect, which in my case is the ‘INTERSECT’ opreation, a master volume must be selected first, although I know I can create this Master Volume by clicking on the small “Specify geometry button” and selecting the ‘NEW Segmentation’ in the drop down box(the lowest option), but how to create the Master Volume for the ‘NEW Segmentation’ programmatically using python?

Thank you very much in advance!

---

<div class="post-metadata">

### Author: ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)
#### Post date: [February 22, 2022, 8:47am UTC](https://discourse.slicer.org/t/how-to-programmatically-create-the-master-volume-for-a-segmentation-node-with-multiple-segments/22098/2 "2022-02-22T08:47:23Z")

</div>

```auto
seg = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")

```

adds an empty segmentation node to your scene.

---

<div class="post-metadata">

### Author: ![PhilipDavis](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/philipdavis/32/14142_2.png) [@PhilipDavis](https://discourse.slicer.org/u/PhilipDavis)
#### Post date: [February 22, 2022, 5:47pm UTC](https://discourse.slicer.org/t/how-to-programmatically-create-the-master-volume-for-a-segmentation-node-with-multiple-segments/22098/4 "2022-02-22T17:47:33Z")

</div>

Hello Rudolf,  
Thank you for your reply!

I’ve tried the code you provided. However, If I’m correct, this is actually the same as clicking the ‘Create new segmentation’ in the ‘Segmentations’ module, and this is how the ‘NEW segmentation’ I mentioned above with two segments was created:

> 1. Go to the ‘segmentations’ module and create a new segmentation node, I named it ‘ **NEW Segmentation** ’, and import both of models get from the previous step into this new segmentation node using the ‘Export/Import Models and Labelmaps’

So, how to create the **Master Volume** for the ‘NEW Segmentation’ programmatically using python? Because after I create this Master Volume for the ‘NEW Segmentation’, I can execute code similar to the following to use logical operator effects and the ‘INTERSECT’ operation:

> segmentationNode = getNode(‘NEW Segmentation’)  
> **masterVolume = segmentationNode.GetNodeReference(slicer.vtkMRMLSegmentationNode().GetReferenceImageGeometryReferenceRole())**  
> segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()  
> segmentEditorWidget.setMRMLScene(slicer.mrmlScene)  
> segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentEditorNode”)  
> segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)  
> segmentEditorWidget.setSegmentationNode(segmentationNode)  
> segmentEditorWidget.setMasterVolumeNode(masterVolume)  
> tgt = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(‘RED’)  
> src = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(‘GREEN’)  
> segmentEditorNode.SetSelectedSegmentID(tgt)  
> segmentEditorWidget.setActiveEffectByName(“Logical operators”)  
> effect = segmentEditorWidget.activeEffect()  
> effect.setParameter(“Operation”,“INTERSECT”)  
> effect.setParameter(“ModifierSegmentID”,src)  
> effect.self().onApply()

---

<div class="post-metadata">

### Author: ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)
#### Post date: [February 22, 2022, 6:11pm UTC](https://discourse.slicer.org/t/how-to-programmatically-create-the-master-volume-for-a-segmentation-node-with-multiple-segments/22098/5 "2022-02-22T18:11:28Z")

</div>

You can either get the volume node from an MRML node like

```auto
volumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")

```

or create a new one like

```auto
volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")

```

Check out the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html) for more examples.

---

<div class="post-metadata">

### Author: ![PhilipDavis](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/philipdavis/32/14142_2.png) [@PhilipDavis](https://discourse.slicer.org/u/PhilipDavis)
#### Post date: [February 22, 2022, 7:21pm UTC](https://discourse.slicer.org/t/how-to-programmatically-create-the-master-volume-for-a-segmentation-node-with-multiple-segments/22098/6 "2022-02-22T19:21:13Z")

</div>

Thank you so much, it’s working now 👍

---

<div class="post-metadata">

### Author: ![Aboma\_Negasa\_Guracho](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aboma_negasa_guracho/32/12236_2.png) [@Aboma\_Negasa\_Guracho](https://discourse.slicer.org/u/Aboma_Negasa_Guracho)
#### Post date: [February 25, 2022, 10:19am UTC](https://discourse.slicer.org/t/how-to-programmatically-create-the-master-volume-for-a-segmentation-node-with-multiple-segments/22098/7 "2022-02-25T10:19:46Z")

</div>

Thank you so much, it’s working now!
