Access an object between modules/extensions in python

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.


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.

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

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

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.

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?

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.

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:


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.

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
slicer.modules.BlenderMonitorWidget.sock = instance pointed
it worked!