You can use this to display any Scalar Volume Node in Slicer
def setSlicerViews(backgroundID, foregroundID):
mainWindow = slicer.util.mainWindow()
if mainWindow is not None:
layoutManager = slicer.app.layoutManager()
if layoutManager is not None:
makeSlicerLinkedCompositeNodes()
slicer.util.setSliceViewerLayers(background=backgroundID, foreground=foregroundID, foregroundOpacity=0.5)
layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpView)
slicer.util.resetSliceViews()
def makeSlicerLinkedCompositeNodes():
# Set linked slice views in all existing slice composite nodes and in the default node
sliceCompositeNodes = slicer.util.getNodesByClass('vtkMRMLSliceCompositeNode')
defaultSliceCompositeNode = slicer.mrmlScene.GetDefaultNodeByClass('vtkMRMLSliceCompositeNode')
if not defaultSliceCompositeNode:
defaultSliceCompositeNode = slicer.mrmlScene.CreateNodeByClass('vtkMRMLSliceCompositeNode')
slicer.mrmlScene.AddDefaultNode(defaultSliceCompositeNode)
for sliceCompositeNode in sliceCompositeNodes:
sliceCompositeNode.SetLinkedControl(True)
defaultSliceCompositeNode.SetLinkedControl(True)
Maybe the issue is that a volume rendering display node is created very early and therefore a regular volume display node is not created (since you already have display node). Creating the default scalar volume display node before creating any additional special display nodes should fix the problem.
If you allocate memory in Python then you cannot transfer its ownership to a VTK class. If performance is extremely important and you work with enormous data sets then you may try to manually manage memory, but in general I would recommend to use slicer.util.updateVolumeFromArray to safely copy values from numpy array to vtkImageData in a volume node.
Slice offset slider range is determined from the extent of the displayed background volume. If you have a single-slice volume then you will not be able to move away from it using the slider. You can use Shift + Mouse Move in other views or set a larger volume as background volume.
How could I set extent in my volume node or make large background volume?
I already set extent in “vtkimage.SetExtent()” but it seems like not working.
After calling SetExent() you need to call AllocateScalars (see example here). Of course reallocation clears current image content, so if you want to keep the image content then create a new image and resample the old image into that (an implementation of this is available in “Crop volume” module).