# Model hiearchy visibility returns an exception

**URL:** <https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344>\
**Category:** Development\
**Tags:** python\
**Created:** [June 26, 2021, 2:45am UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344 "2021-06-26T02:45:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [June 26, 2021, 2:45am UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/1 "2021-06-26T02:45:30Z")

</div>

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:

```auto
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
sceneItemID = shNode.GetSceneItemID()
sceneitem_node = shNode.GetItemDataNode(sceneItemID)
for modelnode in slicer.util.getNodesByClass("vtkMRMLModelNode"):
    try:
        print("===========")
        print("model name: ", modelnode.GetName())
        print("model visibility: ", bool(modelnode.GetDisplayVisibility()))
        print("hiearchy visiblity: ", sceneitem_node.GetHierarchyVisibility(modelnode))
    except Exception as ex:
        print(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!

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [June 26, 2021, 5:22am UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/2 "2021-06-26T05:22:26Z")

</div>

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()`

---

<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:** [June 28, 2021, 2:27pm UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/3 "2021-06-28T14:27:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [June 29, 2021, 10:49pm UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/4 "2021-06-29T22:49:31Z")

</div>

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)

---

<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:** [June 30, 2021, 1:22am UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/5 "2021-06-30T01:22:09Z")

</div>

`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](https://apidocs.slicer.org/master/classvtkMRMLFolderDisplayNode.html#a947a6549ec5be1e3f705bc9b76145a81) you can see that it is a static method, therefore the correct way to call it is this:

```python
slicer.vtkMRMLFolderDisplayNode.GetHierarchyVisibility(modelNode)

```

See how to use doxygen-generated documentation for Python in the [developer manual](https://slicer.readthedocs.io/en/latest/developer_guide/api.html#doxygen-style-documentation).

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [July 8, 2021, 11:46pm UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/6 "2021-07-08T23:46:36Z")

</div>

> [@lassoan](#):
>
> `slicer.vtkMRMLFolderDisplayNode.GetHierarchyVisibility(modelNode)`

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

---

<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 9, 2021, 12:11am UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/7 "2021-07-09T00:11:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [July 9, 2021, 2:51am UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/8 "2021-07-09T02:51:41Z")

</div>

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)

---

<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 9, 2021, 4:01am UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/9 "2021-07-09T04:01:56Z")

</div>

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.

> [@Amine](#):
>
> it would be nice if more modules were available to browse in python as opposed to c++ (in github)

There is a topic in github that makes it easy to find all the Slicer extensions: [3d-slicer-extension · GitHub Topics · GitHub](https://github.com/topics/3d-slicer-extension)

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?

> [@Amine](#):
>
> 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

All the basics (how to get subject hierarchy node and how to use it) is demonstrated in several examples in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#subject-hierarchy).

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](http://apidocs.slicer.org/master/classvtkMRMLSubjectHierarchyNode.html)?

> [@Amine](#):
>
> 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

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

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [July 10, 2021, 3:28am UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/10 "2021-07-10T03:28:45Z")

</div>

> [@lassoan](#):
>
> Were you aware that this topic exist?

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)

> [@lassoan](#):
>
> Was it just too difficult to find the relevant repositories?

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!

> [@lassoan](#):
>
> How do you usually use github search? Do you type complete class names there? How do you find the relevant class names?

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

> [@lassoan](#):
>
> All the basics (how to get subject hierarchy node and how to use it) is demonstrated in several examples in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#subject-hierarchy).
> 
> Were the examples in the script repository insufficient? What additional examples would have helped?

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.

> [@lassoan](#):
>
> Was there any subject hierarchy related feature that you did not find in the [subject hierarchy node documentation](http://apidocs.slicer.org/master/classvtkMRMLSubjectHierarchyNode.html)?

In a previous forum post (where I asked about a similar issue) I was linked to [this function](https://github.com/Slicer/Slicer/blob/master/Libs/MRML/Core/vtkMRMLFolderDisplayNode.h#L90) which eventually led to using it as `sceneitem_node.GetHierarchyVisibility(modelnode)` and while I could find it under [vtkMRMLFolderDisplayNode](http://apidocs.slicer.org/master/classvtkMRMLFolderDisplayNode.html) 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.

> [@lassoan](#):
>
> The same resources are available for Slicer (API reference, tutorials, forum, script repository, module template, and all the existing modules that serve as examples).

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.

---

<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:** [July 10, 2021, 5:03pm UTC](https://discourse.slicer.org/t/model-hiearchy-visibility-returns-an-exception/18344/11 "2021-07-10T17:03:46Z")

</div>

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

> <https://github.com/Slicer/Slicer/pull/5737>
>
> As suggested in this thread: https://discourse.slicer.org/t/model-hiearchy-visib…ility-returns-an-exception/18344/10
