# Access an object between modules/extensions in python

**URL:** <https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351>\
**Category:** Development\
**Tags:** extensions-manager\
**Created:** [July 3, 2020, 4:54am UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351 "2020-07-03T04:54:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)\
**Post date:** [July 3, 2020, 4:54am UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351/1 "2020-07-03T04:54:59Z")

</div>

Hey all,  
If I have module\_A that I have previously used and have some generic object within it, how can I access that object in module\_A from module\_B?  
I know I can use another module/extension’s logic but I need to access the instance of the object to check its state between 2 extensions.

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 3, 2020, 5:05am UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351/2 "2020-07-03T05:05:36Z")

</div>

Except very rare cases, modules should share data with each other via MRML nodes stored in the scene. MRML nodes often wrap various generic data objects.

---

<div class="post-metadata">

**Author:** ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)\
**Post date:** [July 3, 2020, 5:47am UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351/3 "2020-07-03T05:47:23Z")

</div>

Any examples you can point me to that create custom MRML nodes in python?

---

<div class="post-metadata">

**Author:** ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)\
**Post date:** [July 3, 2020, 2:19pm UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351/4 "2020-07-03T14:19:54Z")

</div>

Or at least how to store an instance of an object so it can be shared between two modules.

---

<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 3, 2020, 2:25pm UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351/5 "2020-07-03T14:25:38Z")

</div>

You can only create new node types in C++. In Python but you can customize any existing nodes (add custom attributes, parameters, node references). If there is no specific node that you want the associate these additional information with then you can use a [vtkMRMLScriptedModuleNode](http://apidocs.slicer.org/master/classvtkMRMLScriptedModuleNode.html).

What kind of object do you want to share between modules? Is it Python-wrapped C++ object or a pure Python object? What does it store?

---

<div class="post-metadata">

**Author:** ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)\
**Post date:** [July 3, 2020, 3:02pm UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351/6 "2020-07-03T15:02:29Z")

</div>

The extension i am working on creates a TCP/IP socket connection with a server. this sock variable stores the instance which I use throughout the extension to handle events of incoming data. I would like to be able to access that instance from another extension.

> <https://github.com/dentsoft-foundation/linkSlicerBlender/blob/32141199088743155233998dc8deea31d4667b5f/slicer_module/BlenderMonitor.py#L438>

---

<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 3, 2020, 3:18pm UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351/7 "2020-07-03T15:18:22Z")

</div>

If there is only a single instance of this object, you don’t need to observe it, and you don’t need to serialize its state to the scene, you can simply make it a member of the module logic. You can access any a module’s logic class from any other module. From one Python scripted module you can access another Python scripted module’s logic like this:

```
slicer.modules.someothermodule.widgetRepresentation().self().logic

```

Instead of exposing the socket object directly, you may consider exposing methods that operate on it.

This Blender integration could enable some interesting applications. What Blender features do you plan to use in Slicer (or Slicer features to use in Blender)?

Note that you can use Blender features in Slicer by importing Blender’s Python library into Slicer.

For robust Boolean operations on meshes, you can try [vtkbool library](https://github.com/zippy84/vtkbool).

---

<div class="post-metadata">

**Author:** ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)\
**Post date:** [July 4, 2020, 2:59am UTC](https://discourse.slicer.org/t/access-an-object-between-modules-extensions-in-python/12351/8 "2020-07-04T02:59:59Z")

</div>

thanks!  
I was able to access the object instance  
I didn’t know slicer extension instances, once launched, are saved and can be accessed via slicer.modules… i thought i can only access logic and routines not instanced objects  
so  
slicer.modules.BlenderMonitorWidget.sock = instance pointed  
it worked!
