Model hiearchy visibility returns an exception

HI, I made a script to find out which models are visible both individually and in the hiearchy ( hidden hierarchy is independent of individual model visibility so both have to be True for the model to be effectively visible on the 3d viewer).

here is the script:

shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
sceneItemID = shNode.GetSceneItemID()
sceneitem_node = shNode.GetItemDataNode(sceneItemID)
for modelnode in slicer.util.getNodesByClass("vtkMRMLModelNode"):
        print("model name: ", modelnode.GetName())
        print("model visibility: ", bool(modelnode.GetDisplayVisibility()))
        print("hiearchy visiblity: ", sceneitem_node.GetHierarchyVisibility(modelnode))
    except Exception as ex:

My problem is that sceneitem_node.GetHierarchyVisibility(modelnode) returns an exception 'NoneType' object has no attribute 'GetHierarchyVisibility' , the workaround is simply to drag and drop one of the models on the parent folder, the script does work fine then (for all models).

Reproducing this issue is consistent, i simply made segments and exported them to models, then entered that script, both on slicer 4.11 2019 and the last 2021 nightly.

PS: as an alternative, any other consistent way to find out if the model is visible on 3d (covering both individual visibility and hiearchy) would be welcome, thank you!

Update: I have been able to bypass that function by iterating through parent folders until one is not visible using shNode.GetItemParent() and shNode.GetItemDisplayVisibility(), this does exactly what i expected from GetHierarchyVisibility()

For context, sceneitem_node is None, as there is no data node for the scene. It is always a good idea to check the output of each line of your Python script because then it will be obvious what each variable contains.

i see thanks, but why would it return None with an existing scene full of models but will return the correct data when a model is drag-dropped on a folder (hierarchy change I guess) (this issue also happens after a scene is saved and re-opened)

sceneitem_node = shNode.GetItemDataNode(sceneItemID) is expected to set sceneitem_node to None, because the scene item is not associated with any data node.

In vtkMRMLFolderDisplayNode documentation you can see that it is a static method, therefore the correct way to call it is this:


See how to use doxygen-generated documentation for Python in the developer manual.

1 Like

This works great, thank you.

I’m a bit used to reading slicer documentation but sometimes finding the correct syntax to use can be tricky

1 Like

Happy to hear that it works. Let us know if you have any suggestions for improving the documentation.

1 Like

There is a section in slicer’s documentation that has a lot of code examples for basic operations, that was the most helpful reference in the beginning.

I later started to use slicer’s github to search for implementation examples of classes or parameters if they didn’t work as intended, it would be nice if more modules were available to browse in python as opposed to c++ (in github)

Full slicer modules in python contain a lot of building blocks for various operations that were not featured in slicer’s documentation examples and were by far the most helpful ressource for module building (for example the first three lines in my code example are very hard if not impossible to find out from the generated documentation, i would look into the code of a module that uses folder hiearchy to find out how it’s done)

The doxygen generated documentation only provides the names of methods for each class or what type of argument is required for methods but finding out the sequence of code lines to actually obtain those arguments would require browsing into some related module’s code (especially when the arguments are “multi layered” to obtain like sceneItemID in my example)

Thank you for taking the time to give feedback. It helps us a lot, because most documentation is written by experienced Slicer developers and for them it is not obvious what information a new developer may miss. It would be great if you could provide a bit more details by answering the questions below.

There is a topic in github that makes it easy to find all the Slicer extensions: 3d-slicer-extension · GitHub Topics · GitHub

Were you aware that this topic exist? Was it just too difficult to find the relevant repositories?

I find Github’s search quite poor (compared to global Google search or full-text search in a cloned repository on my computer). How do you usually use github search? Do you type complete class names there? How do you find the relevant class names?

All the basics (how to get subject hierarchy node and how to use it) is demonstrated in several examples in the script repository.

Were the examples in the script repository insufficient? What additional examples would have helped?

Was there any subject hierarchy related feature that you did not find in the subject hierarchy node documentation?

This is indeed something to learn in all application frameworks, at all levels. The same applies at lower levels - for example for Qt. Although every Qt class is documented in detail, you need to look at Qt examples and search on forums to be able to accomplish anything. The same resources are available for Slicer (API reference, tutorials, forum, script repository, module template, and all the existing modules that serve as examples).

No I did not, it has indeed many python written modules that can be very helpful!, I used to look for those either in GitHub if the developer linked to that, in slicer’s git hub or in python’s install folder (with no luck sometimes)

Yes it was often difficult, unless I found a link to the developer’s git. the slicer extension topic you sent is very rich and should definitely get more exposure!

It takes some trial and error, i sometimes search using the module’s name ( the modules self test provides some insight on how to access the methods of the core modules), and looking for class names across slicer’s GitHub is useful when i don’t know what module they were used in, as for obtaining class/method names i get a node in slicer’s console and use tab to browse through its methods, looking for what could be of use ( note on this: it’s tedious to look into those methods in the tiny scroll list that appears and oddly enough it doesn’t always correspond to the doxygen generated documentation!)

I wrote that code quite some time ago and i probably used the examples in the script repository you linked (my bad!) but there are still quite some code examples that are not present in the script repository ( and rightfully so, i guess it cannot contain everything needed for more complex needs)

In this case it would be this line that is not present in the script repo but i understand it’s a rather uncommon neeed slicer.vtkMRMLFolderDisplayNode.GetHierarchyVisibility(modelNode)
i used it in combination with the model’s visibility to know if it is effectively visible.

In a previous forum post (where I asked about a similar issue) I was linked to this function which eventually led to using it as sceneitem_node.GetHierarchyVisibility(modelnode) and while I could find it under vtkMRMLFolderDisplayNode It’s not easy to figure out how to write the correct syntax (that you wrote in your reply).

As for for subject hierarchy node, the type of variables to use and how to get them is clear from the examples in the script repository, and additional methods are well listed in the doxygen documentation so basic usage is well covered.

Yes those are indeed a necessary reference to make anything in slicer, in fact, as a surgeon with no formal programming background, I was able to develop a fully working slicer module (segmentation related features mainly) only using that, qt documentation and help from this forum/stackoverflow. (some of those functions are quite innovative, I’ll describe what they do/link you the module if you’d like).

The GitHub topic with slicer extensions would be of enormous help to anyone and I think it should be linked very clearly on top of the script repository, as a more in depth and complete example stack completing the purpose of the script repo.

1 Like

Very helpful discussion. Here’s a PR to add some links.