Top  | Previous | Next

Script Modules

A project's Script Modules are a global library of scripts that can be called from anywhere within the scope of a project. These scripts are organized as named modules that all live under the app module. To open the Script Module Editor double click on the Configuration > Script Modules node in the Project Browser or navigate to the Project > Script Modules menu.

Rule of Thumb: Never Copy-and-Paste a Script

If you're unsure of when to put scripts in a script module vs embedding the script directly in an event handler, follow this simple rule. If you ever find yourself copying a script from one event handler to another, stop and refactor the script into a global script module! Then simply call your new module from the event handler. This rule will help prevent code duplication across your project, a major maintenance liability.

How to use Script Modules

To add a script module, simply select the app package and press the New Module button. Each module is a python script that may define many functions. You may also organize modules in sub-packages if you'd like. Lets suppose you added a script module named myfuncs, whose body was:

def callMe(message):

   import system

   system.gui.messageBox(message)

 

Now, anywhere in your project you can call:

app.myfuncs.callMe('Hello World')

Whats up with that "import system" call?

Frequently in Ignition, your scripts get system (the built-in library package in Ignition) and app (your project's global script modules) imported for you automatically. Whenever you define a new scope (which you've done with def), we can no longer do this for you, and you'll need to import them manually.

 

See also:

About Python

Scope and Import