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?

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:

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:

Hello cpinter,

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

Tesla

Hi Csaba,

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

Thanks again anyway.

Tesla

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?

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:

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

compositeNodes = slicer.util.getNodesByClass('vtkMRMLSliceCompositeNode')
1 Like

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.

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?

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.

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.

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?

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

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.).

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~

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.

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)

Thanks for sharing the solution that worked for you.