Chapter 20. User Scripts

Table of Contents

20.1. Example Scripts

A user script is either a single .py Python file or an MDL .script file (with a .py and .mlab file, like any local MacroModule).

When a user script is started, MeVisLab creates a local macro module from the script and calls the run() function, which needs to be declared in the Python script. The run function takes a single argument, which is the context (macro module) that surrounds the currently active MeVisLab network.

Using this argument, the script can work on the active network, get the selected modules, etc.

For instance, the following script just prints the selected modules to the console:

def run(macro): 
  print macro.network().selectedModules()
  

A user script can even show MDL windows from the run() method, which are declared in the .script file of the user script. The user script is destroyed after the last window it creates is closed.

User scripts are added to the Scripting menu by defining Action and SubMenu entries in a UserIDEMenus section in a user script's definition file. The user script's definition file can have any name, but it must have the extension def and it needs to be placed below the Modules directory of any package, because MeVisLab scans only the Modules directories recursively for the *.def files. It is preferred to place the user scripts into a folder named UserScripts below the Modules directory of your package.

Please have a look at the user script definition file MeVisLab/IDE/Modules/IDE/UserScripts.def and the example user scripts in MeVisLab/IDE/Modules/IDE/UserScripts/.

20.1. Example Scripts

  • Replace Inventor Group

    Replaces a single selected Inventor group node (e.g. SoSeparator, SoGroup) by specifiable Inventor group. All connections to the original Inventor group node are restored to the new Inventor group node.

  • Show Random Example Network

    Opens and shows a random example network. This is mainly for testing purposes.