Automate setting source volume and geometry in Segment Editor

Hello,

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?

To display the desired node I used :

lm = slicer.app.layoutManager()
redCompositeNode = lm.sliceWidget('Red').mrmlSliceCompositeNode() 
redCompositeNode.SetBackgroundVolumeID(visulization_image_id) 

Thank you

You should be able to adapt the following:

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.

See https://slicer.readthedocs.io/en/latest/user_guide/settings.html#application-startup-file