# Get volume being shown in qMRMLThreeDView or vtkMRMLViewNode from Python

**URL:** <https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655>\
**Category:** Support\
**Created:** [March 23, 2022, 3:13pm UTC](https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655 "2022-03-23T15:13:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcosrdac](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/marcosrdac/32/14611_2.png) [@marcosrdac](https://discourse.slicer.org/u/marcosrdac)\
**Post date:** [March 23, 2022, 3:13pm UTC](https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655/1 "2022-03-23T15:13:23Z")

</div>

Supose I see a volume in a 3D view. Once I have that view instance in hands, how can I know which volume is being shown there? (instance = qMRMLThreeDView or vtkMRMLViewNode)

Thank you very much!

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [March 23, 2022, 4:29pm UTC](https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655/2 "2022-03-23T16:29:11Z")

</div>

I think you could use a function to send a ray in the camera direction and see with what volume it collides first.  
I think it could be possible but I don’t know how

---

<div class="post-metadata">

**Author:** ![marcosrdac](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/marcosrdac/32/14611_2.png) [@marcosrdac](https://discourse.slicer.org/u/marcosrdac)\
**Post date:** [March 23, 2022, 4:42pm UTC](https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655/3 "2022-03-23T16:42:24Z")

</div>

Thanks, Mauro. I believe your solution would give me some results, though I think it would not meet my needs well. I will try to make myself more clear:

If there is one or more volumes visible in the view, even if my camera is not pointing to them, I would like be able to list their IDs.

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [March 23, 2022, 4:59pm UTC](https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655/4 "2022-03-23T16:59:28Z")

</div>

Ok. Then you just need to traverse all displayNodes of those volumes and

```auto
volumeDisplayNode = volumeNode.GetDisplayNode()
viewNodesIDs = volumeDisplayNode.GetViewNodeIDs()
visibility = False
for i in range(len(viewNodesIDs))
  visibility = volumeDisplayNode.GetVisibility() and volumeDisplayNode.GetViewNodeIDs()[i] == 'myParticularViewID'
  if visibility:
    break

if visibility:
  myListOfVisibleVolumesIDs.append(volumeNode.GetID())

```

to get ‘myParticularViewID’ just use GetID() on the viewNodeThatYouHave.

I think that should work.

---

<div class="post-metadata">

**Author:** ![marcosrdac](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/marcosrdac/32/14611_2.png) [@marcosrdac](https://discourse.slicer.org/u/marcosrdac)\
**Post date:** [March 23, 2022, 5:34pm UTC](https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655/5 "2022-03-23T17:34:42Z")

</div>

I think you walk in a good way. The issue is that `volumeDisplayNode.GetViewNodeIDs()` gives me an empty tuple (`()`) (though in Slicer I definitely see the Red slice view showing my node). I saw in the documentation that it means “every view”. So I would advise people to list all 3D and slice views if they want to have control over the volume views of interest.

---

<div class="post-metadata">

**Author:** ![marcosrdac](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/marcosrdac/32/14611_2.png) [@marcosrdac](https://discourse.slicer.org/u/marcosrdac)\
**Post date:** [March 24, 2022, 2:46pm UTC](https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655/6 "2022-03-24T14:46:48Z")

</div>

Also, thank you very much, @mau_igna_06 !

---

<div class="post-metadata">

**Author:** ![shai-ikko](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shai-ikko/32/15765_2.png) [@shai-ikko](https://discourse.slicer.org/u/shai-ikko)\
**Post date:** [February 13, 2025, 2:20pm UTC](https://discourse.slicer.org/t/get-volume-being-shown-in-qmrmlthreedview-or-vtkmrmlviewnode-from-python/22655/7 "2025-02-13T14:20:32Z")

</div>

(almost three years later)

I was looking for this, and the answer given was insufficient for me (because `GetViewNodeIDs()` returns an empty tuple both for volumes displayed in all views, and for volumes displayed in no view).

I found a way to get from a `sliceWidget` to the displayed view – there is actually, potentially, more than one, because slice widgets can blend two volumes (background and foreground) and a label-map.

Anyway,

```python
node = sliceWidget.sliceLogic().GetBackgroundLayer().GetVolumeNode()

```
