Show/hide slice views in 3D view using Python

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’)

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

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.

red = getNode('vtkMRMLSliceNodeRed')


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

Hi Steve

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

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

Thank you for your reply

I tried this code

red = getNode('vtkMRMLSliceNodeRed')
red. pointListDisplayNode.SetViewNodeIDs(["vtkMRMLViewNode2"]) 

But this one is not work

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

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

Thank you

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.

I solve this

It should use ‘vtkMRMLModelDisplayNode’ rather than ‘vtkMRMLSliceNode’

current_slice_node = slicer.mrmlScene.GetNodeByID('vtkMRMLModelDisplayNode1')

Thank you

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:

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.


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

1 Like