# How to show/hide volume node from python script

**URL:** <https://discourse.slicer.org/t/how-to-show-hide-volume-node-from-python-script/21078>\
**Category:** Development\
**Tags:** python\
**Created:** [December 15, 2021, 5:10pm UTC](https://discourse.slicer.org/t/how-to-show-hide-volume-node-from-python-script/21078 "2021-12-15T17:10:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ziri](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@ziri](https://discourse.slicer.org/u/ziri)\
**Post date:** [December 15, 2021, 5:10pm UTC](https://discourse.slicer.org/t/how-to-show-hide-volume-node-from-python-script/21078/1 "2021-12-15T17:10:43Z")

</div>

Hi,  
I’m trying to create a node from numpy array as shown below:

> nodeName = “NewVolumeNode”  
> voxelType=vtk.VTK\_UNSIGNED\_CHAR  
> imageOrigin = [0.0, 0.0, 0.0]  
> imageSpacing = [1.0, 1.0, 1.0]  
> imageDirections = [[1,0,0], [0,1,0], [0,0,1]]
> 
> data\_vtk = vtk.util.numpy\_support.numpy\_to\_vtk(num\_array = imgdata.ravel(), deep = True, array\_type = vtk.VTK\_FLOAT)  
> imageData = vtk.vtkImageData()  
> imageData.SetDimensions(imgdata.shape[::-1])  
> imageData.GetPointData().SetScalars(data\_vtk)
> 
> volumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLScalarVolumeNode”, nodeName)  
> volumeNode.SetOrigin(imageOrigin)  
> volumeNode.SetSpacing(imageSpacing)  
> volumeNode.SetIJKToRASDirections(imageDirections)  
> volumeNode.SetAndObserveImageData(imageData)  
> volumeNode.CreateDefaultDisplayNodes()  
> volumeNode.CreateDefaultStorageNode()

However, I didn’t find a way on the wiki or other posts to toggle the volume node show/hidden button under Data/Subject Hierachy/Node. I tried:

> volumeNode.GetDisplayNode().SetVisibility(True)

But it didn’t work, and I have to toggle the show/hidden manually in slicer UI under Data/Subject Hierachy/Node. Is there any python command to do this? Thanks!

---

<div class="post-metadata">

**Author:** ![Juicy](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Juicy](https://discourse.slicer.org/u/Juicy)\
**Post date:** [December 16, 2021, 4:12am UTC](https://discourse.slicer.org/t/how-to-show-hide-volume-node-from-python-script/21078/2 "2021-12-16T04:12:36Z")

</div>

The following code will make the ‘volumeNode’ volume appear in the slice windows:

```auto
slicer.util.setSliceViewerLayers(background = volumeNode)

```

and this code will reset the slice windows to be centred on the new volume:

```auto
slicer.util.resetSliceViews()

```

This should effectively do the same thing as toggling the show hide button on the volume in the data module.

---

<div class="post-metadata">

**Author:** ![ziri](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@ziri](https://discourse.slicer.org/u/ziri)\
**Post date:** [December 16, 2021, 2:47pm UTC](https://discourse.slicer.org/t/how-to-show-hide-volume-node-from-python-script/21078/3 "2021-12-16T14:47:56Z")

</div>

This works great, thanks!

---

<div class="post-metadata">

**Author:** ![ziri](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@ziri](https://discourse.slicer.org/u/ziri)\
**Post date:** [December 16, 2021, 4:07pm UTC](https://discourse.slicer.org/t/how-to-show-hide-volume-node-from-python-script/21078/4 "2021-12-16T16:07:31Z")

</div>

Sorry I have one follow-up question… I found it is not quite the same thing as toggling the show/hide button. For example, I was trying to load a 2D ultrasound image (volume size=[1,480,640]) and apply transformation on the image. After initial loading, I did

> usImgTransformNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLTransformNode”, “TrackedUSImagePose”)  
> usImgNode.SetAndObserveTransformNodeID(usImgTransformNode.GetID())  
> and do `slicer.util.resetSliceViews()` I got:
> 
> ![Image 003](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/4/044a41963209e445e17172cc91688053e8f1c84b.png)  
> Then I fill in the transformation node with actual matrix by `usImgTransformNode.SetMatrixTransformToParent(transformMatrix)` and do `slicer.util.resetSliceViews()`, I got:  
> ![Image 005](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/f/5fb6d5ed3d3160146278bce6b9d50c796f67a493.png)  
> which is not showing the correct reformatted slice aligned with the volume. However, if I first hide the by clicking the hide button in Data widget  
> ![Image 006](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/8/6883abd9a570e511b716e453169f8dad3d9b44da.png)  
> and re-click the show button, I have:  
> ![Image 007](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/1/a1f10503d0f60a83e6057c5ec4ce2b94a9a5d752.png)  
> which is the display I’d like to have. I wonder is there a mechanism behind the show/hide button that I can do with script to enable this kind of behavior? I essentially would like to change the transformation matrice multiple times and show the US image moving in 3D as well as showing the image in one of the slice view.  
> Thanks!

---

<div class="post-metadata">

**Author:** ![Juicy](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Juicy](https://discourse.slicer.org/u/Juicy)\
**Post date:** [December 16, 2021, 9:09pm UTC](https://discourse.slicer.org/t/how-to-show-hide-volume-node-from-python-script/21078/5 "2021-12-16T21:09:07Z")

</div>

I am not really sure what is going on there as my slicer program does not have that same behaviour although I am using an older version (4.11.20200930)

Usually the red, yellow and green slice views just look along the primary axes, e.g. the red slice always looks normal to the Sup/Inf axis. It looks like when you pressed the show hide button that the yellow view has become reformatted to look at the rotated volume. As I said my slicer does not reformat the slice views by toggling the show hide button so I am not sure why yours does this?

Anyway if you want some code which is equivalent to pressing the " Rotate to volume plane" button which reformats the slice views to look at the rotated volume try this:

```auto
layoutManager = slicer.app.layoutManager()
sliceWidget = layoutManager.sliceWidget('Red')
controller = sliceWidget.sliceController()
controller.rotateSliceToBackground()
slicer.util.resetSliceViews()

```

This just rotates the red slice to the volume. Just replace the ‘Red’ with ‘Yellow’ or ‘Green’ if you want to rotate the other slices. If you want to rotate all 3 then you will need to loop through them:

```auto
layoutManager = slicer.app.layoutManager()
for sliceViewName in layoutManager.sliceViewNames():
    sliceWidget = layoutManager.sliceWidget(sliceViewName)
    controller = sliceWidget.sliceController()
    controller.rotateSliceToBackground()

slicer.util.resetSliceViews()

```

---

<div class="post-metadata">

**Author:** ![ziri](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@ziri](https://discourse.slicer.org/u/ziri)\
**Post date:** [December 19, 2021, 9:33pm UTC](https://discourse.slicer.org/t/how-to-show-hide-volume-node-from-python-script/21078/6 "2021-12-19T21:33:26Z")

</div>

Thanks! I am using the latest stable version (4.11.20210226, revision 29738, built 2021-03-01), and I found that currently there is an attribute error when doing

> controller.rotateSliceToBackground()  
> AttributeError: qMRMLSliceControllerWidget has no attribute named ‘rotateSliceToBackground’

I think now it has been updated to `controller.rotateSliceToLowestVolumeAxes()` which works great.
