Using variables from module1 in module2

I have two modules, module1 and module2. I am asking the user to select a folder using qFileDialog in this part of module 1

class module1Logic(ScriptedLoadableModuleLogic):
def run(self):
exampath = qt.QFileDialog.getExistingDirectory(0,(“Select folder for DCE-MRI exam”))

Then in this part of module2

class module2Widget(ScriptedLoadableModuleWidget):
def setup(self):

I want to be able to reuse that same exampath that they chose in module1 without making the user select the exampath from QFileDialog again.

What would be the syntax for doing this?

Rohan Nadkarni

Modules should only interact through nodes in the MRML scene, i.e., you select parameter node of one module as input of the other module. If you don’t want to require users to select nodes then you can use singleton parameter node (then you can look it up based on singleton tag).

Note that it is the best to minimize usage of file paths in the scene, as you don’t want your processing code littered with file reader/writer code. Instead, load data into nodes once and then only work with data nodes and node references. If you need to import data from proprietary file formats then you can implement a custom file reader in Python.

Thanks, I ended up doing this in module1:

exampath_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScriptedModuleNode","path node")

And this in module2:

exampath_node = slicer.util.getNode("path node")

self.exampath = exampath_node.GetParameter("exampath")

Thanks for sharing your solution. Referring to nodes by name is very fragile, because you can have many nodes in the scene by the same name. Instead, if you want to find a node in the scene by a string identifier then set that unique string in its singleton tag and retrieve it using slicer.mrmlScene.GetSingletonNode(). You can use something like “YourModuleName.SomeOtherString” as singleton tag to ensure that it is unique (each module names is unique).

1 Like