Python called qSlicerSubjectHierarchyParseLocalDataPlugin function

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:

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?

1 Like

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.

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.

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!

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.

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.

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.

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.

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

1 Like

Right, thanks for mentioning it here, I forgot :slight_smile: