# How Can I use python to get the status of a node's show/hides status in the "data" or "DICOM" module in the Subject hierarchy area?

**URL:** <https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786>\
**Category:** Development\
**Tags:** dicom, python, subject-hierarchy\
**Created:** [July 30, 2020, 5:41am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786 "2020-07-30T05:41:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [July 30, 2020, 5:41am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/1 "2020-07-30T05:41:44Z")

</div>

Operating system: Windows 10  
Slicer version: 4.10.2  
Expected behavior: Using python to get the status of a node’s show/hides status in the “data” or “DICOM” module in the Subject hierarchy area?  
Actual behavior:

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

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 30, 2020, 8:32am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/2 "2020-07-30T08:32:48Z")

</div>

Volumes work quite differently in terms of visibility than any other type, so simple `GetDisplayVisibility()` won’t work.

However, if you take a look at how the subject hierachy itself decides if it’s visible, that should help:

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Volumes/SubjectHierarchyPlugins/qSlicerSubjectHierarchyVolumesPlugin.cxx#L353-L356>

---

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [July 30, 2020, 8:38am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/3 "2020-07-30T08:38:12Z")

</div>

Hello cpinter,

Thank you very much for your help. I will try it later~

Tesla

---

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [July 30, 2020, 11:59pm UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/4 "2020-07-30T23:59:11Z")

</div>

Hi Csaba,

I can’t found a python api of this function. Because I am using python to develop.

Thanks again anyway.

Tesla

---

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [July 31, 2020, 12:16am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/5 "2020-07-31T00:16:35Z")

</div>

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get\_current\_mouse\_coordinates\_in\_a\_slice\_view](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_current_mouse_coordinates_in_a_slice_view)

In here I found an example may help, But I can’t make it work, is there anyone can help me to check if this block of example works or not?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/9/c9faa4717b7e04b2263a80a7021d52dd2568661f.png)

---

<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:** [July 31, 2020, 2:53am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/6 "2020-07-31T02:53:19Z")

</div>

If you are interested if a volume is visible in any of the slice views then you can iterate through the `vtkMRMLSliceCompositeNode` nodes in the scene and check if the volume matches the foreground or background volume, as you can see it in the code snippet that @cpinter referred to:

> <https://github.com/Slicer/Slicer/blob/100b7432cc6d31a1eb7f89a723e61f0d73eedd22/Modules/Loadable/Volumes/SubjectHierarchyPlugins/qSlicerSubjectHierarchyVolumesPlugin.cxx#L507-L526>

In Python, you can use this convenience method to get all the slice composite nodes:

```
compositeNodes = slicer.util.getNodesByClass('vtkMRMLSliceCompositeNode')
```

---

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [July 31, 2020, 3:19am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/7 "2020-07-31T03:19:53Z")

</div>

Hello lassoan,

Thank you very much, This method works well. According to your method, I Can get the current volumeNode by Script, like the picture shows below.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/3/7300df7cb7ead950e44de864bd4e7041997e7b32.png)

Further more, I want to observe the ModifiedEvent (When the status changed from hide to show,or change to another volume). I failed to make it successful. Could you help me check is there any error in this function below?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/e/0e7a1f9d784dff2afeb3147dbba1cf95c991c66e.png)

---

<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:** [July 31, 2020, 3:54am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/8 "2020-07-31T03:54:08Z")

</div>

You need to add observers to all slice composite nodes if you want to get notified when a different volume gets visualized in any of the slice viewers.

Why do you need to continuously monitor when the user changed the displayed volume? It is usually not necessary.

---

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [July 31, 2020, 6:32am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/9 "2020-07-31T06:32:22Z")

</div>

Hello lassoan,

Thank you for your quickly reply. I can’t really understand how to implement what you said. Could you give me an example code?

I would like to process the current show volume and display the processed volume in “Slice4” and “Yellow” View Simultaneously, please see the picture below. So I need to know the current show volume.

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

---

<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:** [July 31, 2020, 1:58pm UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/10 "2020-07-31T13:58:07Z")

</div>

Do you have questions about how to set which volume is visible in which slice? Or you are unsure about how to detect when user selects a different volume for display in a slice view?

What is your overall goal? What is the workflow you are trying to implement?

---

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [August 1, 2020, 1:48am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/11 "2020-08-01T01:48:57Z")

</div>

Hello lassoan,

1、Do you have questions about how to set which volume is visible in which slice?  
I don’t have this question. I know How to do this actually.

2、Or you are unsure about how to detect when user selects a different volume for display in a slice view?  
Yes, I would like to observe the change event of volume in “Subject hierarchy”.

3、What is your overall goal? What is the workflow you are trying to implement?  
I would like to observe the modify-event of the current volume, and process the volume, and put the processed volume in “Slice4” and “Yellow” view.

Thank you very much

---

<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:** [August 1, 2020, 4:23am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/12 "2020-08-01T04:23:35Z")

</div>

Instead of trying to figure out what volume the user looks at, I would recommend to add an input and output volume node selector (as it is done in all other modules). If you want to a simplified user interface, then I would recommend to allow the user to select input volume only in your module (not using slicer view controllers, not using Data module, etc.).

---

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [August 1, 2020, 12:45pm UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/13 "2020-08-01T12:45:51Z")

</div>

Hello lassoan,

I would like to make the User Interface looks friendly and concisely. So I don’t want to use input volume simply.

Thanks~

---

<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:** [August 1, 2020, 1:42pm UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/14 "2020-08-01T13:42:06Z")

</div>

My suggestion for a simplified GUI would be to not let the user switch between modules or access slice view controllers, but your module would select a volume to display (or if you have multiple volumes loaded, the user would select the current volume in your module).

If you described your workflow in more detail then we can give more specific advice.

---

<div class="post-metadata">

**Author:** ![Tesla2687](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tesla2687](https://discourse.slicer.org/u/Tesla2687)\
**Post date:** [August 4, 2020, 3:10am UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/15 "2020-08-04T03:10:03Z")

</div>

Thank you very much for your help. @lassoan, @cpinter. I have found a method to implement my idea.  
please check the code below.

```
#Observe the change of the current volume
lastVolumeNodeID=''
def toDoSomethingInCurrentVolume(caller=None,event=None):
    global lastVolumeNodeID
    layoutManager = slicer.app.layoutManager()
    redCompsiteNode=layoutManager.sliceWidget("Red").sliceLogic().GetSliceCompositeNode()
    redVolumeID = redCompsiteNode.GetBackgroundVolumeID()
    if lastVolumeNodeID==redVolumeID:
        return 
    elif :
        redBackgroundVolumeNode=slicer.mrmlScene.GetNodeByID(redVolumeID)
        process(redBackgroundVolumeNode)
    
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
shNode.AddObserver(shNode.SubjectHierarchyItemModifiedEvent,toDoSomethingInCurrentVolume)
```

---

<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:** [August 4, 2020, 12:18pm UTC](https://discourse.slicer.org/t/how-can-i-use-python-to-get-the-status-of-a-nodes-show-hides-status-in-the-data-or-dicom-module-in-the-subject-hierarchy-area/12786/16 "2020-08-04T12:18:21Z")

</div>

Thanks for sharing the solution that worked for you.
