# How to get the current ROINode programmingly according to the volume in the volume rendering module?

**URL:** https://discourse.slicer.org/t/how-to-get-the-current-roinode-programmingly-according-to-the-volume-in-the-volume-rendering-module/21540
**Category:** Development
**Tags:** python
**Created:** [January 20, 2022, 6:45am UTC](https://discourse.slicer.org/t/how-to-get-the-current-roinode-programmingly-according-to-the-volume-in-the-volume-rendering-module/21540 "2022-01-20T06:45:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)
#### Post date: [January 20, 2022, 6:45am UTC](https://discourse.slicer.org/t/how-to-get-the-current-roinode-programmingly-according-to-the-volume-in-the-volume-rendering-module/21540/1 "2022-01-20T06:45:38Z")

</div>

Hi,all.  
There are three volumes in the volume rendering module and there are three corresponding AnnotationROIs,as follows：

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/1/5111bca141c61259ded1e536014c629c6656d88c.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/8/786ddcd95604f2b0f5461736bb78505eb5ef4ecb.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/7/77c2a7aeaeaf3bee5a446527eef91bc352113c40.png)  
yeah,I want to get the current ROINode,for example right now,I choose the first volume so I get the vtkMRMLAnnotationROINode1 and when I change to select the second volume the ROINode should be vtkMRMLAnnotationROINode2.  
Usually, I hard code to get the ROINode but it is not convenient:

```auto
# get the first ROINode
AnnotationROINodeID='vtkMRMLAnnotationROINode1'
roi = slicer.mrmlScene.GetNodeByID(AnnotationROINodeID)

# get the second ROINode
AnnotationROINodeID='vtkMRMLAnnotationROINode2'
roi = slicer.mrmlScene.GetNodeByID(AnnotationROINodeID)

```

So,is there a way to get the current roiNode according to the selected volume?  
Thank you in advance for your possible advice!

---

<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: [January 21, 2022, 7:52pm UTC](https://discourse.slicer.org/t/how-to-get-the-current-roinode-programmingly-according-to-the-volume-in-the-volume-rendering-module/21540/2 "2022-01-21T19:52:31Z")

</div>

This should work for that:

```auto
myVolumeName = 'MRHead' # replace with the displayed name of your volume
volNode = getNode(volumeName) # get the mrml scalar volume node
vrLogic = slicer.modules.volumerendering.logic() # get the volume rendering module logic
vrDispNode = logic.GetFirstVolumeRenderingDisplayNode(volNode) # get the volume rendering display node 
roiNode = vrDispNode.GetROINode() # get the associated ROI node

```

If you have an ROI node and want to know which volume is rendered using that node, you can do this

```auto
vrLogic = slicer.modules.volumerendering.logic()
vrDispNode = logic.GetFirstVolumeRenderingDisplayNodeByROINode(roiNode)
vrDispNode.GetVolumeNode()

```

See the documentation here: [Slicer: vtkSlicerVolumeRenderingLogic Class Reference](https://apidocs.slicer.org/v4.11/classvtkSlicerVolumeRenderingLogic.html)

---

<div class="post-metadata">

### Author: ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)
#### Post date: [January 27, 2022, 2:14am UTC](https://discourse.slicer.org/t/how-to-get-the-current-roinode-programmingly-according-to-the-volume-in-the-volume-rendering-module/21540/3 "2022-01-27T02:14:02Z")

</div>

Thanks Mike,  
This is not working and also I have seen the documentation ,still can’t figure it out.  
Maybe I am not making myself clear,my end goal is to take screenshots as png files as you helped me before.But when I load a scene.mrml file into Slicer ,there are three volumes.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/3/6361b1240e43a56935fd5099adf23087fc76c045.png)  
I have currently designed a GUI like this:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/d/ddcb4fd2cb48d7f98f73dcc1abbe43df14d3ca85.png)  
I can take screenshots given the necessary parameters ,but **only the first volume** ,which means if I select the second volume in volume rendering module and click the eye icon to see it in display area,I still take screenshots in the first volume.So what I what is as follows:  
select the second volume and make it displayed:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/a/caddb7a798e3b1e68cadd6db5ed3acc22a3b2a94.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/9/09c9f691cbaa77a56c4b750a87548582c5af67a4.jpeg)  
Is there someway to get the ROINode at the moment I selected the volume or clicked the eye icon to see it?  
In other words,at the moment I selected the third volume and clicked the eye icon,the ROINode should change itself.  
Thank you for your continued attention on this topic!

---

<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: [January 27, 2022, 5:30pm UTC](https://discourse.slicer.org/t/how-to-get-the-current-roinode-programmingly-according-to-the-volume-in-the-volume-rendering-module/21540/4 "2022-01-27T17:30:23Z")

</div>

I notice there was a typo in the included code from two posts ago. I had changed a variable called `logic` to `vrLogic` to try to be a little clearer, but I didn’t change it everywhere in the pasted code. Hopefully that’s all that was leading to your errors.

```auto
vrLogic = slicer.modules.volumerendering.logic()

vol1node = getNode('vol1')
vol2node = getNode('vol2')

# Get or create the volume rendering display node for vol1
vrDispNode1 = vrLogic.GetFirstVolumeRenderingDisplayNode(vol1node)
if vrDispNode1 is None:
  # no existing display node, create a new one
  vrDispNode1 = vrLogic.CreateDefaultVolumeRenderingNodes(vol1Node)

# Get or create the volume rendering display node for vol2
vrDispNode2 = vrLogic.GetFirstVolumeRenderingDisplayNode(vol2node)
if vrDispNode2 is None:
  vrDispNode2 = vrLogic.CreateDefaultVolumeRenderingNodes(vol2node)

```

Then

```auto
# To show vol2 instead of vol1
vrDispNode1.SetVisibility(0) # hide vol1 rendering
vrDispNode2.SetVisibility(1) # show vol2 rendering
```

---

<div class="post-metadata">

### Author: ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)
#### Post date: [January 28, 2022, 1:17am UTC](https://discourse.slicer.org/t/how-to-get-the-current-roinode-programmingly-according-to-the-volume-in-the-volume-rendering-module/21540/5 "2022-01-28T01:17:46Z")

</div>

Thanks Mike, it is not about the type error.It seems like if I want the ROINode I have to get the volumeNode by ID or name.  
Thank you for your code and help anyway!

---

<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: [January 28, 2022, 4:27pm UTC](https://discourse.slicer.org/t/how-to-get-the-current-roinode-programmingly-according-to-the-volume-in-the-volume-rendering-module/21540/6 "2022-01-28T16:27:49Z")

</div>

I think I’m not totally clear on what the problem you are running into is. I thought you were saying that the problem you were having was that the first volume rendering was being captured twice instead of switching to the second volume, so I was showing how to hide one volume rendering and show another.

Separately, once you have a volume rendering display node, you can get the ROI node from that via the `GetROINode()` method.
