# Scripted Loadable Modules Accessing Eachother's Logic

**URL:** https://discourse.slicer.org/t/scripted-loadable-modules-accessing-eachothers-logic/17677
**Category:** Development
**Tags:** python
**Created:** [May 18, 2021, 2:42pm UTC](https://discourse.slicer.org/t/scripted-loadable-modules-accessing-eachothers-logic/17677 "2021-05-18T14:42:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nathanbmnt](https://avatars.discourse-cdn.com/v4/letter/n/a9adbd/32.png) [@nathanbmnt](https://discourse.slicer.org/u/nathanbmnt)
#### Post date: [May 18, 2021, 2:42pm UTC](https://discourse.slicer.org/t/scripted-loadable-modules-accessing-eachothers-logic/17677/1 "2021-05-18T14:42:50Z")

</div>

Hello,  
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()  
> logic.myMethod()

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

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

When accessing logic from a C++ module a singleton is accessed every time.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/d/1d1228b870e677c83ccce4e61635f8d5e198d79f.png)

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

---

<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: [May 19, 2021, 3:53am UTC](https://discourse.slicer.org/t/scripted-loadable-modules-accessing-eachothers-logic/17677/2 "2021-05-19T03:53:12Z")

</div>

These questions and other essential techniques are explained in [PerkLab bootcamp Slicer programming tutorial](https://github.com/PerkLab/PerkLabBootcamp/raw/master/Doc/day3_2_SlicerProgramming.pptx).

---

<div class="post-metadata">

### Author: ![nathanbmnt](https://avatars.discourse-cdn.com/v4/letter/n/a9adbd/32.png) [@nathanbmnt](https://discourse.slicer.org/u/nathanbmnt)
#### Post date: [May 19, 2021, 1:45pm UTC](https://discourse.slicer.org/t/scripted-loadable-modules-accessing-eachothers-logic/17677/3 "2021-05-19T13:45:05Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/9/d9425a5115a8dabff7c71428984ba875ce7c4259.png)  
This slide says logic _may_ be instantiated many times, does this mean it should be instantiated in other modules?  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/7/277bd64529af3b6818614856e3e2e12b2b7515e7.png)

---

<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: [May 19, 2021, 2:55pm UTC](https://discourse.slicer.org/t/scripted-loadable-modules-accessing-eachothers-logic/17677/4 "2021-05-19T14:55:22Z")

</div>

> [@nathanbmnt](#):
>
> the powerpoint doesn’t specify if this is just how the myWidget class accesses logic

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

> <https://github.com/Slicer/Slicer/blob/7b60eed09c2cef6358e2f67935623e8a90b28cb1/Utilities/Templates/Modules/Scripted/TemplateKey.py#L122-L124>

> [@nathanbmnt](#):
>
> if that is how other modules should access the logic as well

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:

> <https://github.com/moselhy/SlicerSequenceRegistration/blob/a6b0409b8e25a4f60eb4a459fea39f53273f1f92/SequenceRegistration/SequenceRegistration.py#L490-L491>
