When I open the segment editor module, I’d like the source volume and geometry to automatically be set to a specific volume which I have loaded into the display view. Is it possible to automate this in my code?
mw = slicer.util.mainWindow()
modulePanel = findChild(mw, "ModulePanel")
def onModuleEntered(moduleName):
if moduleName != "SegmentEditor":
return
print(f"Entered {moduleName}")
# Get reference to the volume loaded a background
sliceNode = slicer.util.getNode("vtkMRMLSliceNodeRed")
sliceLogic = slicer.app.applicationLogic().GetSliceLogic(sliceNode)
sourceVolume = sliceLogic.GetBackgroundLayer().GetVolumeNode()
# Get reference to the vtkMRMLSegmentEditorNode used in the segment editor module
# This node is storing parameters associated with the segment editor widget (qMRMLSegmentEditorWidget)
segmentEditorNode = slicer.modules.SegmentEditorWidget.parameterSetNode
segmentEditorNode.SetAndObserveSourceVolumeNode(sourceVolume)
modulePanel.moduleAdded.connect(onModuleEntered)
Adding this to the Slicer Application startup file would ensure it always happen.