Scripted Loadable Modules Accessing Eachother's Logic

I have multiple scripted loadable modules (python) where I need modules to access each other’s logic.

In the setup() method of Module 1’s ScriptedLoadableModuleWidget there is the line
self.logic = Module1Logic()

Now in my Module 2, if I want to access Module 1’s logic, do I import the class and instantiate it every time I want to use the logic? Like:

from Module1 import Module1Logic
logic = Module1Logic()

Or do I access the singleton logic object stored in Module1Widget like:

logic = slicer.modules.module1.widgetRepresentation().self().logic

When accessing logic from a C++ module a singleton is accessed every time.

Furthermore, if I have a ScriptedLoadableModule with no widget class, how should I access its logic?

These questions and other essential techniques are explained in PerkLab bootcamp Slicer programming tutorial.

I’ve read through the powerpoint and it says a logic reference is myWidget.logic, but the powerpoint doesn’t specify if this is just how the myWidget class accesses logic, or if that is how other modules should access the logic as well.

This slide says logic may be instantiated many times, does this mean it should be instantiated in other modules?

This is included in the standard scripted module template (created by Extension Wizard):

For loadable and CLI modules the logic is instantiated at application startup. You can access them as shown in the slide you referenced above. For scripted modules, optimal choice depends on the module. If the the scripted module logic is active (e.g., continuously observe the scene and act on scene or node changes automatically) then you need to access the existing module logic (typically via the module object). However, in most cases the module logic is just passive (it is just a collection of functions, does not do any observations) and in these cases you can instantiate another logic class and use that, as it is done for example here:

1 Like