# Python called qSlicerSubjectHierarchyParseLocalDataPlugin function

**URL:** https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600
**Category:** Development
**Tags:** python, loadable-module
**Created:** [July 9, 2021, 12:48pm UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600 "2021-07-09T12:48:40Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![StephenLeePeng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stephenleepeng/32/11892_2.png) [@StephenLeePeng](https://discourse.slicer.org/u/StephenLeePeng)
#### Post date: [July 9, 2021, 12:48pm UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/1 "2021-07-09T12:48:40Z")

</div>

Operating system: Ubuntu 18.04  
Slicer version: 4.13.0 - self build.  
Expected behavior:  
When I load a directory contains subdirectory in the Slicer. The directory contains some .stl files. And after load, these stl models will be displayed in the Node subject hierarchy window by name, but without any hierarchy format.  
When I right click the scene option: “Create hierarchy from loaded directory structure”, these stl models will be rearranged by directory structure.  
Above description is manually operations. And I want realize this process in Python terminal. So I faced a problem: qSlicerSubjectHierarchyParseLocalDataPlugin is a Loadable module, how I can use Python to called Loadable module qSlicerSubjectHierarchyParseLocalDataPlugin’s slot function: createHierarchyFromLoadedDirectoryStructure.

Actual behavior:

---

<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 9, 2021, 1:07pm UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/2 "2021-07-09T13:07:24Z")

</div>

It is a protected slot, so you cannot currently call it directly from code. We could move this code out to a class and make it public from which you can call it. Unfortunately I don’t see a class that is a natural place for this function (the logic would be it but it is VTK based so we cannot put it there). @lassoan how about `qMRMLSubjectHierarchyTreeView`? `qSlicerSubjectHierarchyPluginLogic` maybe? Or the model?

---

<div class="post-metadata">

### Author: ![StephenLeePeng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/stephenleepeng/32/11892_2.png) [@StephenLeePeng](https://discourse.slicer.org/u/StephenLeePeng)
#### Post date: [July 12, 2021, 6:41am UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/3 "2021-07-12T06:41:18Z")

</div>

Thank you for your reply.  
As my aim is to create an extension, combine some features originally belong to different modules, such as Data, Models, Segment Editor and so on.  
To achieve python called qSlicerSubjectHierarchyParseLocalDataPlugin’s protected slot function: createHierarchyFromLoadedDirectoryStructure aim, I modify the qSlicerSubjectHierarchyParseLocalDataPlugin.h, change the function type, from “protected slots” to “public slots”, then make the slicer project again.  
After make finished, I can called the slot function in the Python Interactor.

```python
plugin = slicer.qSlicerSubjectHierarchyPluginHandler.instance()
p = plugin.pluginByName("ParseLocalData")
p.createHierarchyFromLoadedDirectoryStructure()

```

After call the public slot func, the models which sorted by default name, display in the Node tree view window, will be rearranged by local loaded directory structure.

Of course, this is a tempory solution. And I hope there has been a more suitable solution.

---

<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 12, 2021, 12:08pm UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/4 "2021-07-12T12:08:31Z")

</div>

This is an acceptable workaround until we make this function accessible.

@lassoan can you please see my question above? If we decide where to move this function I can do that. 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: [July 12, 2021, 1:52pm UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/5 "2021-07-12T13:52:59Z")

</div>

I find organizing of loaded data into folders based on file location a very useful feature, but underused - because it is hard to discover it. What do you think about making it the default behavior somehow?

If this feature would become essential infrastructure (e.g., used in all data loading) then the most appropriate place would be to have it in vtkSlicerSubjectHierarchyModuleLogic. Otherwise exposing it as a public method of the plugin sounds like a completely fine solution to me.

---

<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 12, 2021, 3:00pm UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/6 "2021-07-12T15:00:18Z")

</div>

I agree with your choice of class, but porting it to VTK would be a pain. We could make it default if we could detect if the user is loading a folder structure, but I don’t see how to do it conveniently without showing a popup about this every time the user loads more than one file at a time.

---

<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 12, 2021, 7:58pm UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/7 "2021-07-12T19:58:47Z")

</div>

We could always do this when the user loads multiple files. It could be a feature of the “Add data” dialog and we could add a checkbox to enable/disable it. It would take a lot of effort to implement this, and it would just save two clicks for the user; but it could still make sense to implement this at some point, because users otherwise would probably not know that this feature exists.

> [@cpinter](#):
>
> I agree with your choice of class, but porting it to VTK would be a pain.

kwSys has a number of file and directory manipulation functions, so nothing major or risky would need to be developed, but I agree that it could take a few hours.

---

<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 18, 2021, 2:37am UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/8 "2021-07-18T02:37:25Z")

</div>

@cpinter has made `createHierarchyFromLoadedDirectoryStructure` method available in current Slicer Preview Release.

---

<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 18, 2021, 12:58pm UTC](https://discourse.slicer.org/t/python-called-qslicersubjecthierarchyparselocaldataplugin-function/18600/9 "2021-07-18T12:58:55Z")

</div>

Right, thanks for mentioning it here, I forgot 🙂
