# Show/hide slice views in 3D view using Python

**URL:** <https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074>\
**Category:** Development\
**Created:** [June 5, 2018, 8:23am UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074 "2018-06-05T08:23:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ollih10](https://avatars.discourse-cdn.com/v4/letter/o/f1d935/32.png) [@ollih10](https://discourse.slicer.org/u/ollih10)\
**Post date:** [June 5, 2018, 8:23am UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/1 "2018-06-05T08:23:03Z")

</div>

Hi developers,

I have a problem switching on the slice views on or off in the 3D view from Python permanently. The code I am using for i.e. switch on the red slice view is:

current\_slice\_node = slicer.mrmlScene.GetNodeByID(‘vtkMRMLModelDisplayNode1’)  
current\_slice\_node.VisibilityOn()

This code works well until one changes the position of the slice by i.e. scrolling in the slice view. I guess since the “eye” in the slice view is still “closed” the update will set the visibility again to 0 and the slice disappears. How can I prevent this from happening? Is there a way to directly manipulate the slice view by i.e. usig the modules logic to switch on slice rendering in 3D?

Slicer version is 4.8.1

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 5, 2018, 12:28pm UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/2 "2018-06-05T12:28:09Z")

</div>

Right - the slice models are controlled at a higher level and so the visibility and transform are controlled by the SliceNode, which is what is tied to the eye icon in the slice controller.

```auto
red = getNode('vtkMRMLSliceNodeRed')
red.SetSliceVisible(1)

```

HTH,  
Steve

---

<div class="post-metadata">

**Author:** ![ollih10](https://avatars.discourse-cdn.com/v4/letter/o/f1d935/32.png) [@ollih10](https://discourse.slicer.org/u/ollih10)\
**Post date:** [June 6, 2018, 8:40am UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/3 "2018-06-06T08:40:32Z")

</div>

Thanks Steve. That was exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![park](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/park/32/5717_2.png) [@park](https://discourse.slicer.org/u/park)\
**Post date:** [February 9, 2022, 2:00am UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/4 "2022-02-09T02:00:04Z")

</div>

Hi Steve

Is there any way to popup the 2D slice only one 3D view (e.g., view1) when using the multiple 3D views ?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 9, 2022, 3:46pm UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/5 "2022-02-09T15:46:38Z")

</div>

Yes, you can set the ViewNodeIDs for the display node associated the with slice model. Similar to what’s done in this snippet:

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#change-markup-point-list-display-properties](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#change-markup-point-list-display-properties)

---

<div class="post-metadata">

**Author:** ![park](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/park/32/5717_2.png) [@park](https://discourse.slicer.org/u/park)\
**Post date:** [February 10, 2022, 10:46am UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/6 "2022-02-10T10:46:56Z")

</div>

Thank you for your reply

I tried this code

```auto
red = getNode('vtkMRMLSliceNodeRed')
red. pointListDisplayNode.SetViewNodeIDs(["vtkMRMLViewNode2"]) 
red.SetSliceVisible(1)

```

But this one is not work

`red. pointListDisplayNode.SetViewNodeIDs(["vtkMRMLViewNode2"])`

- One more , I would like to remove 2D slices on View2 like this figure

 ![sdsd](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/7/d7662e0774a0a59ba18021d13493e4ceb0aec5e9.png)

Thank you

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 10, 2022, 12:59pm UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/7 "2022-02-10T12:59:35Z")

</div>

I don’t have an example that does exactly what you ask, maybe someone has time to write one for you. If not, you need to study the code to see what the `SetViewNodeIDs` on a display node and figure out how to set that parameter on the model display nodes for the slices. It’s not trivial but it can be done.

---

<div class="post-metadata">

**Author:** ![park](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/park/32/5717_2.png) [@park](https://discourse.slicer.org/u/park)\
**Post date:** [February 10, 2022, 4:43pm UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/8 "2022-02-10T16:43:10Z")

</div>

I solve this

It should use ‘vtkMRMLModelDisplayNode’ rather than ‘vtkMRMLSliceNode’

```auto
current_slice_node = slicer.mrmlScene.GetNodeByID('vtkMRMLModelDisplayNode1')
current_slice_node.SetViewNodeIDs([slicer.mrmlScene.GetFirstNodeByName("View1").GetID()])
current_slice_node.VisibilityOn()

```

Thank you

---

<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:** [February 10, 2022, 5:07pm UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/9 "2022-02-10T17:07:56Z")

</div>

As long as you know the ID of the model for the slice display, I think your approach will work. However, here is how you would do it through the slice node:

```auto
greenSliceNode = slicer.mrmlScene.GetNodeByID('vtkMRMLSliceNodeGreen')
greenSliceNode.SetSliceVisible(True) # this will show the slice in all 3D views by default
greenSliceNode.AddThreeDViewID('vtkMRMLViewNode2') # This will show the slice in only View2
greenSliceNode.AddThreeDViewID('vtkMRMLViewNode1') # This will make the slice visible in View1 as well
greenSliceNode.RemoveThreeDViewID('vtkMRMLViewNode2') # This will make the slice no longer visible in View2

```

The logic here is that the slice is visible only in any 3D view which has been explicitly added to the list of 3D view IDs for that slice node, with the exception that, if no view IDs have been added, then the default behavior is for the slice to be visible in all 3D views. If you don’t want the slice visible in 3D at all, then you use `SetSliceVisible(False)`. This is very similar to the way `SetViewNodeIDs()` works on regular display nodes: if no view nodes are set, then the default is visible everywhere, but if any have been set explicitly, then the node is only visible in views on that list.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 10, 2022, 5:09pm UTC](https://discourse.slicer.org/t/show-hide-slice-views-in-3d-view-using-python/3074/10 "2022-02-10T17:09:15Z")

</div>

Glad you were able to figure this out 👍 @park. And thanks @mikebind for the extra details.
