# Hide a volume programmatically

**URL:** <https://discourse.slicer.org/t/hide-a-volume-programmatically/26654>\
**Category:** Support\
**Tags:** volume, subject-hierarchy\
**Created:** [December 8, 2022, 8:14pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654 "2022-12-08T20:14:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhodesdante](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@rhodesdante](https://discourse.slicer.org/u/rhodesdante)\
**Post date:** [December 8, 2022, 8:14pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/1 "2022-12-08T20:14:03Z")

</div>

I am writing a custom module in Python and I would like to:

1. Replace the active volume in the slice viewer (similar to clicking on the “eye” icon of a different volume) with the next volume in the subject hierarchy
2. Shut the eye icon on the subject hierarchy of the volume I just turned off

I was able to do the first bit with the following chunk of code, but I can’t seem to shut the eye in the subject hierarchy–both eyes stay on even though only one volume is present in the slice viewer. Is it possible to shut that eye icon programmatically? Thank you very much!

```
# self.shNode is a subjectHierarchy node
def viewNextVolume(self, reverse = False):
  # First, get all valid volume vtkIDs
  sceneID = self.shNode.GetSceneItemID()
  allChildren = []
  self.shNode.GetItemChildren(sceneID, allChildren)
  validChildren = [child for child in allChildren if self.shNode.GetItemDataNode(child).IsTypeOf("vtkMRMLScalarVolumeNode")]

  # Second, get the vtkID of the current volume
  layoutManager = slicer.app.layoutManager()
  redVolumeID=layoutManager.sliceWidget("Red").sliceLogic().GetSliceCompositeNode().GetBackgroundVolumeID()
  currentvtkID = self.shNode.GetItemByDataNode(slicer.mrmlScene.GetNodeByID(redVolumeID))

  # third, advance the slice viewer
  nextDex = (validChildren.index(currentvtkID) + 1* (-1)**reverse) % len(validChildren) 
  newNode = self.shNode.GetItemDataNode(validChildren[nextDex])
  for sliceViewName in layoutManager.sliceViewNames():
    compositeNode = layoutManager.sliceWidget(sliceViewName).sliceLogic().GetSliceCompositeNode()
    compositeNode.SetBackgroundVolumeID(newNode.GetID())
    layoutManager.sliceWidget(sliceViewName).fitSliceToBackground()

  # HOW TO SHUT OFF EYE OF PREVIOUS VOLUME?

```

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [December 8, 2022, 10:23pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/2 "2022-12-08T22:23:50Z")

</div>

For the MRHead example I can set the visibility in the subject hierarchy programmatically using:

```auto
volNode = getNode("MRHead")
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
volItem = shNode.GetItemByDataNode(volNode)
pluginHandler = slicer.qSlicerSubjectHierarchyPluginHandler().instance()
volPlugin = pluginHandler.getOwnerPluginForSubjectHierarchyItem(volItem)
volPlugin.setDisplayVisibility(volItem, 0)

```

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [December 8, 2022, 10:26pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/3 "2022-12-08T22:26:11Z")

</div>

I’m not sure why

```auto
volNode.GetDisplayNode().SetVisibility(0)
volNode.SetDisplayVisibility(0)

```

don’t work to hide the slice view of the volume, maybe someone else can weigh in? I also noticed if the volume is in a folder, hiding the folder does not hide the slice view of the volume.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [December 8, 2022, 11:07pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/4 "2022-12-08T23:07:49Z")

</div>

> [@smrolfe](#):
>
> I also noticed if the volume is in a folder, hiding the folder does not hide the slice view of the volume.

See the following and the insights liked from there:  
(ultimately it is due to turning the visibility on would only work for 2 volumes, since Slicer can’t show more than 2 volumes in a given slice view)

> <https://github.com/Slicer/Slicer/issues/6413>
>
> As reported on https://discourse.slicer.org/t/folder-visibility-confusing/23753
> …
> 
> The eye icon is both loved and hated by our users, but I believe there is a confusing behavior for the folder eye icon. When I close the folder’s eye and have both 2D and 3D renderings, only the 3D hides:
> 
> !\[image\](https://user-images.githubusercontent.com/4790289/172384582-08113351-73e5-48a4-9e64-a5b77eabbc80.png)
> 
> !\[image\](https://user-images.githubusercontent.com/4790289/172384604-57108b69-ab8d-4f87-af99-69d81c7be87b.png)
> 
> 
> To confuse even more the users, the 2D slice is kept on the 3D view. I’ve personally spent 10 minutes fiddling with the volume rendering sliders only to discover that the folder eye was closed. I was seeing the 2D slice at the 3D view and at the slicewidgets but the 3D rendering was not showing, so I thought the visibility was fine.

> <https://github.com/Slicer/Slicer/issues/4961#issuecomment-638710730>
>
> The visibility toggle button (ie. the eyeball button) in the subject hierarchy c…an be used to show or hide the children of the hierarchy node. However, visibility of children volume nodes is not affected in the 2D slice views.
> 
> \## Steps to reproduce
> 
> See discourse post. 
> https://discourse.slicer.org/t/hiding-patient-in-hierarchy/11850
> 
> \## Expected behavior
> 
> Clicking the visibility button of a parent should allow user to control all children nodes in a uniform manner.
> 
> \## Environment
> \- Slicer version: 4.11.0-2020-03-26 r28898 / f9eb7e9
> \- Operating system: Linux

---

<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:** [December 9, 2022, 6:00am UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/5 "2022-12-09T06:00:00Z")

</div>

You can show-hide volumes in slice views by using [slicer.util.setSliceViewerLayers()](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.setSliceViewerLayers).

You can hide volume rendering in 3D views like this (if you don’t want to use the subject hierarchy plugin based solution above):

```python
vrDisplayNode = slicer.modules.volumerendering.logic().GetFirstVolumeRenderingDisplayNode(volumeNode)
# hide volume rendering:
vrDisplayNode.SetVisibility(False)
# prevent showing volume rendering if eye icon is clicked in Subject Hierarchy:
vrDisplayNode.SetShowMode(slicer.vtkMRMLDisplayNode.ShowIgnore)

```

---

<div class="post-metadata">

**Author:** ![rhodesdante](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@rhodesdante](https://discourse.slicer.org/u/rhodesdante)\
**Post date:** [December 12, 2022, 5:51pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/6 "2022-12-12T17:51:35Z")

</div>

slicer.util.setSliceViewerLayers() link worked very nicely for this purpose. Thanks!

---

<div class="post-metadata">

**Author:** ![rohithck](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohithck/32/77403_2.png) [@rohithck](https://discourse.slicer.org/u/rohithck)\
**Post date:** [July 23, 2024, 12:08pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/7 "2024-07-23T12:08:39Z")

</div>

For a newly loaded volume, In the mentioned method

```auto
vrDisplayNode = slicer.modules.volumerendering.logic().GetFirstVolumeRenderingDisplayNode(volumeNode)
vrDisplayNode.SetVisibility(False)

```

it is only show or hide volume if I show volume in Volume Rendering plugin at first time. Otherwise getting error:  
`AttributeError: 'NoneType' object has no attribute 'SetVisibility'`

---

<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:** [July 23, 2024, 12:53pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/8 "2024-07-23T12:53:48Z")

</div>

If the volume rendering display node (the data object that stores volume rendering settings) has not been created yet then you can create it by calling `CreateDefaultVolumeRenderingNodes`. There are several examples in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html).

---

<div class="post-metadata">

**Author:** ![rohithck](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohithck/32/77403_2.png) [@rohithck](https://discourse.slicer.org/u/rohithck)\
**Post date:** [July 29, 2024, 1:04pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/9 "2024-07-29T13:04:08Z")

</div>

when I use `CreateDefaultVolumeRenderingNodes`, the volume property set to US-Fetal (Image 1). But if I show volume in Volume Rendering plugin at first time after loading the .mha file, it will automatically create a volume property with actual color of the image (Image 2). How can i solve this?

```auto
for file in mha_files:
    volume = slicer.util.loadVolume(file)
    vrDisplayNode = slicer.modules.volumerendering.logic().CreateDefaultVolumeRenderingNodes(volume)
    vrDisplayNode.SetVisibility(True)

```

Image 1

 ![Image 1](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/5/d5390a01bd87bce587e49290098f1f5f5928082d.png)

Image 2

 ![Image 2](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/2/727104f82801067346d9932cbbd988153b2c535a.png)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [August 2, 2024, 1:03pm UTC](https://discourse.slicer.org/t/hide-a-volume-programmatically/26654/10 "2024-08-02T13:03:57Z")

</div>

You can get the existing display node like this:

```auto
volRenLogic = slicer.modules.volumerendering.logic()
displayNode = volRenLogic.GetFirstVolumeRenderingDisplayNode(volume)

```
