Accessing modules from Python

Hello,

I’m new to Slicer and would like to understand better how to access scripted modules using python. I’m interested in using Python scripts to automate tasks by setting properties and executing the modules themselves, rather than the underlying filters. There are some good examples on the wiki using the segmentation editor and I’d like to understand how to generalize this to other modules.

I can access the widgets and logic using:
widget = slicer.modules.myModule.widgetRepresentation()
logic = slicer.modules.myModule.logic()

Any tips on a general approach to identifying and setting the properties from the GUI would be helpful.

Thanks,
Sara

Hi Sara -

As a general rule, scripted modules should divide the work between the Widget class (pure GUI) and the Logic class that does all the actual work. Following this design makes it easier to reuse the functionality in the way you are looking for, but it’s a little extra effort and not all modules are as clean as we would like.

Generally the easiest way to learn what’s going on is to have a look at the module code. If you learn how to use the GUI, then you can trace what widget signals are connected slots that implement the behavior and you can tell quickly what is performed in the logic classes.

Also note that the two calls you cited both create new instances of the classes in question. This can be useful if you want to create a copy of the widget to embed in another layout.

If the module has been entered in the current Slicer session, then a widget is already created and is cached for subsequent access. So you can get it with something like (substitute the module name for “LabelStatistics” of the example):

slicer.modules.LabelStatisticsWidget

so you could use the GUI components and invoke callbacks if needed.

Hope that helps,
Steve

Hi Steve,
Thanks for your reply, I appreciate the clarification. My eventual goal is to provide some simplified automation instructions for individuals familiar with the GUIs but with less programming experience.

I’m curious whether there’s been any discussion on providing some documentation or interface to translate module GUI options to Python commands? Thanks again for your input.

Hi Sara,
I also have similar requirements, we need to call the module through Python and make it complete certain work or modify some of the default values in the module. Have you solved your problem? Can you give me some relevant information? Thanks very much.

If there are any specific operations that you cannot figure out how to access from Python then feel free to post it as a new topic on this forum.