Skip to content

Commit c84c564

Browse files
authored
macro module
1 parent 438ff5b commit c84c564

File tree

4 files changed

+113
-12
lines changed

4 files changed

+113
-12
lines changed
Lines changed: 113 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Example 2: Macro modules and Module Interaction"
3-
date: 2022-06-15T08:58:44+02:00
3+
date: 2025-05-19
44
draft: false
55
weight: 370
66
status: "OK"
@@ -15,16 +15,117 @@ menu:
1515

1616
# Example 2: Macro modules {#TutorialChapter6}
1717

18-
## Macro modules and Module Interactions via User Interface and Python Scripting
18+
## Macro Module
19+
A macro module can be used to develop your own functionality in MeVisLab. You have two main options for how to use it:
1920

20-
MeVisLab provides different types of modules, which can be distinguished by their color. The brown modules are called Macro modules. Macro modules condense a whole network into one module. You can open the internal network by pressing the middle mouse button {{< mousebutton "middle" >}} or via right mouse click {{< mousebutton "right" >}} and select {{< menuitem "Help" "Show Internal Network" >}}. Macro modules provide the possibility to create customized user interfaces and Python interactions.
21+
* **With Internal Networks**: Use a macro module to reuse a network of modules. For example, if you build a network that applies a specific image filter and you want to use this setup in multiple projects, you can wrap the entire network into a single macro module. This way, you don’t need to manually reconnect all the individual modules each time — you just use your macro module. You can see the internal network at the left side of the image below. You can see an example in [Basic Mechanics of MeVisLab (Example: Building a Contour Filter)](/tutorials/basicmechanisms#TutorialMacroModules).
2122

22-
In [Chapter I - Basic Mechanics](./tutorials/basicmechanisms) we built a
23-
contour filter and condensed all the modules into one local Macro module.
24-
Until now, the local Macro module containing the contour filter can only
25-
be used in the current network. In the following chapters, we like to make
26-
the Macro module commonly available throughout projects and equip this
27-
Macro module with panels and help pages. Commonly available macro
28-
modules are called global macros and can be found in MeVisLab {{< menuitem "Module Search" >}}. Global macros and projects are stored
29-
in packages. A package structure makes it easy to exchange projects and
30-
different functionalities between people.
23+
* **Without Internal Networks**: Use a macro module to add your own Python code. If MeVisLab is missing a specific functionality, you can write your own Python script and include it in a macro module.
24+
This allows you to extend the software with custom behavior that integrates smoothly into your project. You can see the Python code at the right side of the image.
25+
26+
27+
You can combine both approaches: have a macro module containing a module network, and additionally Python code to add your own logic or functionality:
28+
29+
![Internal Processing and Python Interaction](/images/tutorials/basicmechanics/with.png "Internal Processing and Python Interaction")
30+
31+
### Benefits:
32+
33+
* **Encapsulation:**
34+
It takes an existing network of modules or Python code. To the user, interacting with the macro module, it appears as a single entity with its own defined inputs, outputs, and parameters. You don't need to know or interact with the internal functionality unless you specifically open the macro module for editing.
35+
* **Reusability:**
36+
Once you've created a macro module, you can easily add it to any other MeVisLab network, just like any built-in module. This promotes modularity and avoids the need to rebuild common processing pipelines repeatedly. For example, if you have a standard preprocessing pipeline for medical images, you can encapsulate it into a macro module and reuse it across different projects.
37+
* **Simplified Interface:**
38+
Often, a macro module will expose a simplified and more focused interface compared to the combined interfaces of all the modules within it. You can choose which inputs, outputs, and parameters of the internal network are exposed at the macro module's level. This makes it easier to use for specific tasks without being overwhelmed by too many options.
39+
40+
### Key characteristics:
41+
* **Implementation:**
42+
Macro modules are primarily defined using the MeVisLab Definition Language (MDL) and often incorporate Python scripting for more dynamic behavior. You don't need to write C++ code to create a macro module.
43+
* **User Interface:**
44+
You can define your own User Interface for macro modules using MDL.
45+
* **Scope:**
46+
Macro modules can be either global (available in all your MeVisLab projects) or local (specific to the current network you are working on).
47+
48+
### Types of Macro modules:
49+
#### Local Macro module:
50+
A Local Macro in MeVisLab is a macro module that exists only within the context of the current network document - i.e. it’s defined *locally* rather than being installed into the global module database. It does not require a package, it lives only inside the current network file (.mlab) you’re working on.
51+
52+
* A local macro is visible and editable only in the current network.
53+
* A local macro is not listed in the Modules panel and module search.
54+
* A local macro can only be reused elsewhere by copying it into another folder with your network file.
55+
56+
#### Global Macro module:
57+
A global macro module is stored in a central location within your MeVisLab installation's module directories (or a custom path) called Package. Once created, it appears in the module browser and can be used in any MeVisLab network you open. See [Package creation](/tutorials/basicmechanisms/macromodules/package/) for details about Packages.
58+
59+
* A global macro can be used in any MeVisLab network.
60+
* A global macro is listed in the Modules panel and module search.
61+
62+
{{<alert class="info" caption="Info">}}
63+
Packages are the way MeVisLab organizes different development projects. You can organize your own modules, test cases or C++ modules in a package.
64+
{{</alert>}}
65+
66+
### Inputs, Outputs, and Fields:
67+
You can add inputs and outputs to your macro module to connect it with the other modules:
68+
* **Inputs:** These are connection points through which the module receives data from other modules. You define inputs in the *\*.script* file using the *Input* keyword. You can also define the type for each input.
69+
* **Outputs:** These are connection points through which the module sends processed data to other modules. You define outputs in the *\*.script* file using the *Output* keyword. You can also define the type for each output.
70+
71+
You can add fields to your macro module. Fields allow you to change parameters for your module or to see the values of results. Fields can also be added to the panel of the macro module so that the user can change them.
72+
* **Fields:** These are parameters that control the module's behavior, typically visible in the module's panel or in the Module Inspector. You define fields in the *\*.script* file using the *Field* keyword, specifying the data type, default value, and other properties.
73+
The below figure shows the input, output and fields:
74+
75+
![Inputs, Outputs, and Fields](/images/tutorials/basicmechanics/fields.png "Inputs, Outputs, and Fields")
76+
77+
You have two options when adding fields to your macro module:
78+
79+
1. **Define your own fields:**
80+
You can create new fields by specifying their name, type, and default value in the *\*.script* file. This allows you to provide custom parameters for your macro module, tailored to your specific needs.
81+
82+
2. **Reuse fields from the internal network:**
83+
Instead of defining a new field, you can expose an existing field from one of the modules inside your macro module's internal network. To do this, you reference the *internalName* of the internal field you want to reuse. This makes the internal field accessible at the macro module level, allowing users to interact with it directly without duplicating parameters.
84+
85+
### Files Associated with a Macro Module:
86+
Macro modules typically contain the following files:
87+
* **Definition file (*\*.def*):** The module definition file contains the definition and information about the module like name, author, package, etc. **Definition files are only available for global macro modules**.
88+
89+
* **Script file (*\*.script*):** The script file defines inputs, outputs, fields and the user interface of the macro module. In case you want to add Python code, it includes the reference to the Python script file. The *\*.script* file allows you to define Python functions to be called on field changes and user interactions.
90+
91+
* **Module initialization**: You can add the *initCommand* to the *Commands* section and the given Python function is called whenever the module is added to the workspace or reloaded.
92+
* **Window creation**: You can add the *initCommand* to the *Window* section and the given Python function is called whenever the panel of the module is opened.
93+
* **User interaction**: You can add commands to any user interface element like *Buttons* to call Python functions on user interactions with this element. The image below shows you the user interface and the internal interface:
94+
![user interface and the internal interface](/images/tutorials/basicmechanics/mycountourFilter.png "user interface and the internal interface")
95+
96+
* **Field changes**: You can also react on any changes of fields in your module and create Field Listeners. See below for details.
97+
* **Python file (*\*.py*):** *(Optional)* The Python file contains the Python code that is used by the module. You can add Python functions to fields or UI elements to react on user interactions in the *\*.script* file.
98+
* **MeVisLab network file (*\*.mlab*):** *(Optional)* Stores the internal network of modules that make up the macro module. This file essentially defines the macro module's internal structure and connections.
99+
* **Macro help file (*\*.help*):** *(Optional)* Provides help documentation for the macro module. This file is used to display information to users about the module’s functionality, usage, and any specific instructions.
100+
### Field Listeners:
101+
Field listeners are mechanisms to execute Python code automatically when the value of a field changes. This allows you to create dynamic responses to user interactions in the module's parameter panel.
102+
103+
You create field listeners within the *Commands* sections of the *\*.script* file. You get a reference to the field object and then use a method to add a callback function that will be executed when the field's value is modified.
104+
105+
For an example see [Example 2.5.2: Module interactions via Python scripting](/tutorials/basicmechanisms/macromodules/scriptingexample2/).
106+
107+
## Summary
108+
* Macro modules allow you to add your own functionality to MeVisLab. You can add inputs and outputs and connect existing modules to your new macro module.
109+
110+
* Benefits:
111+
* Organized structure.
112+
* Easy to reuse across projects.
113+
* Simplified user interface.
114+
115+
* Types:
116+
* Local: Only available in the current network, not shown in the module browser.
117+
* Global: Available in all projects, must be part of a package.
118+
119+
* Files
120+
* (*\*.script*): Defines inputs, outputs, fields, and user interface.
121+
* (*\*.py*): Contains Python code.
122+
* (*\*.def*): Definition file (only for global macros).
123+
* (*\*.mlab*): Stores the internal network of modules within the macro module.
124+
* (*\*.mhelp*): Provides documentation or user assistance for the module.
125+
126+
* Components
127+
* Inputs/Outputs: For connecting the module with other modules.
128+
* Fields: Parameters for user control
129+
* Field Listeners: Run Python code when a field value changes
130+
131+
* Packages: are the way MeVisLab organizes different development projects. Required for global macros.
Loading
Loading
Loading

0 commit comments

Comments
 (0)