Assign a specific volume into slice

Hi, I have a short question.

I am browsing multiple MR images at the same time.
After loading multiple images (usually done by drag and drop), I want to assign a specific volume using its IDs (such as ‘vtkMRMLScalarVolumeNode11’) into a particular slice( such as ‘slicer5’ or ‘Red’).

I googled it here and there, but I could not find a good answer, so I am posting it here.

Thank you for your time!

These instructions should help:

https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#iterate-over-current-visible-slice-views-and-set-foreground-and-background-images

If you need something finer grained, see the implementation of setSliceViewerLayers here:

1 Like

Thanks, Steve.
I read the instructions and tried it.
But it is about overlaying images, not assigning a specific image node into a specific location of slices, such as a Red or Green panel.
Maybe the setSliceViewerLayers function could do the job that I want to do, and I am trying to read the suggested github page.

Thank you again!

I’m not sure, but it sounds like you want this:

n =  slicer.util.getNode("YourVolumeNode")
for color in ["Red", "Yellow", "Green"]:
  slicer.app.layoutManager().sliceWidget(color).sliceLogic().GetSliceCompositeNode().SetForegroundVolumeID(n.GetID())

From here.

1 Like

Yes, it works!
Thank you @pieper

Here is what I used.

ID = "vtkMRMLScalarVolumeNode1" # node ID for image volume
Panel = "Red" # node name for panel view (slice)
slicer.app.layoutManager().sliceWidget(Panel).sliceLogic().GetSliceCompositeNode().SetBackgroundVolumeID(ID)

Thank you again!