# Pre-selecting Foreground and Background Volumes

**URL:** <https://discourse.slicer.org/t/pre-selecting-foreground-and-background-volumes/2503>\
**Category:** Development\
**Created:** [April 3, 2018, 3:15pm UTC](https://discourse.slicer.org/t/pre-selecting-foreground-and-background-volumes/2503 "2018-04-03T15:15:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asheu](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@asheu](https://discourse.slicer.org/u/asheu)\
**Post date:** [April 3, 2018, 3:15pm UTC](https://discourse.slicer.org/t/pre-selecting-foreground-and-background-volumes/2503/1 "2018-04-03T15:15:52Z")

</div>

I want to pre-select the background and foreground volumes manually. Like seen below, right now there are no volumes selected for the background and foreground layer.

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/4/a4b3309a7c348e33c719f84c3ba1716bb8f8ae7b.png)

I have tried the following methods but even after updating the view, the combo box value itself is not changed.

lm = slicer.app.layoutManager()  
sliceWidget = lm.sliceWidget(‘Red’)  
ForeCombo = findChild(sliceWidget, ‘ForegroundComboBox’)  
ForeCombo.setCurrentNodeID(‘whole\_mouse\_80micron\_ct’)

I have also tried directly modifying the slice widget like so:

red\_logic = slicer.app.layoutManager().sliceWidget(“Red”).sliceLogic()  
red\_cn = red\_logic.GetSliceCompositeNode()  
red\_logic.GetSliceCompositeNode().SetBackgroundVolumeID(slicer.util.getNode(‘dwi’).GetID())

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [April 3, 2018, 3:30pm UTC](https://discourse.slicer.org/t/pre-selecting-foreground-and-background-volumes/2503/2 "2018-04-03T15:30:27Z")

</div>

This works for me:

```auto
import SampleData
h = SampleData.SampleDataLogic().downloadMRHead()
c = SampleData.SampleDataLogic().downloadCTACardio()
red_logic = slicer.app.layoutManager().sliceWidget('Red').sliceLogic()
red_cn = red_logic.GetSliceCompositeNode()
red_cn.SetBackgroundVolumeID(h.GetID())

```

And afterwards you should have this:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/6/a662831bd997a5168d1e1a2f08488b012b01e89d.jpeg)

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [April 3, 2018, 3:32pm UTC](https://discourse.slicer.org/t/pre-selecting-foreground-and-background-volumes/2503/3 "2018-04-03T15:32:30Z")

</div>

As Steve mentioned. More examples at

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 3, 2018, 10:36pm UTC](https://discourse.slicer.org/t/pre-selecting-foreground-and-background-volumes/2503/4 "2018-04-03T22:36:30Z")

</div>

I think the easiest way to set which volumes to show in slice views is by using slicer.util.setSliceViewerLayers:

[http://slicer.readthedocs.io/en/latest/developer\_guide/slicer.html#slicer.util.setSliceViewerLayers](http://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.setSliceViewerLayers)
