How to select a volume automatically in volume rendering?

Hi, I’m making some script to make my work faster.

I made a simple shortcut button that just goes to volume rendering.

When it changes to volume rendering module, no volume is selected as a default(even when some volumes are loaded in data)

image

I want to make a code that switches to volume rendering and select volumes that I want(like first-loaded volume, second-loaded volume…etc).
Making a button for switching was simple but selecting some volume as a default is not solved…

Any help appreciating. Thanks.

You can probably write a little function that will cycle over the loaded volumes in the scene after a keystroke. In each iteration you can turn off the previous volume and turn on the next one,

Otherwise I am not sure how can rendering will know what volume you want to render automatically.

After you switched to a module, you can use setEditedNode to select what node the module should display.

For example:

myModuleWidget = slicer.modules.mymodule.widgetRepresentation()
myVolume = slicer.util.getNode('myVolume')
myModuleWidget.setEditedNode(myVolume)

Thanks!
I solved my problem with the code below:

volumeNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLScalarVolumeNode')
secondVolumeNode = volumeNodes.GetItemAsObject(1)
vrWidget = slicer.modules.volumerendering.widgetRepresentation()
vrWidget.setMRMLVolumeNode(secondVolumeNode)

With this, second loaded volume is selected in Volume Rendering combobox if I want to load second-loaded volume among several loaded volume.

Please help one more!(Maybe not one…but…)
I made a shortcut to go Segment Editor module and automatically load source volume and add segmentation and segment.
In this situation, I also want to control “specify geometry” button which is next to source volume.
I want to choose ROI that I made previously and set spacing as 0.15mm.
But I never could find the function codes for controlling ‘specify geometry’ :frowning:

Thresholding for sharing the solution that worked for you. I would recommend to use myModuleWidget.setEditedNode(myVolume) method, as that is a higher-level API that works all modules.

Please create a new topic for new questions that are unrelated to this topic.