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:
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?
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')
red.SetSliceVisible(1)
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.
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.