# Automate setting source volume and geometry in Segment Editor 

**URL:** <https://discourse.slicer.org/t/automate-setting-source-volume-and-geometry-in-segment-editor/31324>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [August 23, 2023, 10:06pm UTC](https://discourse.slicer.org/t/automate-setting-source-volume-and-geometry-in-segment-editor/31324 "2023-08-23T22:06:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![warrj](https://avatars.discourse-cdn.com/v4/letter/w/e9bcb4/32.png) [@warrj](https://discourse.slicer.org/u/warrj)\
**Post date:** [August 23, 2023, 10:06pm UTC](https://discourse.slicer.org/t/automate-setting-source-volume-and-geometry-in-segment-editor/31324/1 "2023-08-23T22:06:48Z")

</div>

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 :

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

```

Thank you

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [August 24, 2023, 4:37am UTC](https://discourse.slicer.org/t/automate-setting-source-volume-and-geometry-in-segment-editor/31324/2 "2023-08-24T04:37:55Z")

</div>

You should be able to adapt the following:

```python
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](https://slicer.readthedocs.io/en/latest/user_guide/settings.html#application-startup-file)
