How to use Script Modules
To add a script module, simply right click the Script Library [project] package and click the New Script option. Each module is a python script that can define many functions. You can also keep your scripting modules organized in Packages (i.e., folders) by creating additional nested subpackages. To create a Package, click on the Script Library [project] and click the New Package option.
For example, let's suppose you added the following script module named
myFuncs, whose body is shown below.
Now, anywhere in your project you can call this function.
Because each module can hold many functions, you can add any functions you need to this Script Module.
Just like before, you can call this function using:
Note, that the addNumbers function also has a return value. This will be returned to the script that called the function, allowing you to use it within that script.
To import System or not to import System - Unedited content
Frequently in Ignition, your scripts get
system library (the built-in library package in Ignition) and shared and project libraries imported for you automatically. However,
Subsequent versions of Ignition beyond version 7 no longer need import system every time you create a new scope. However versions prior to version 7 still require an import system every time a system associated function is called inside a function. Ignition versions prior to version 7 still need the import system when system functions are called inside functions. Regardless, typing
import system in any version of Ignition will result in the system library being available for use inside the function.
system import - paolo version 2.0
In Ignition, certain libraries are automatically imported into any scripts you do to make everything easier. For example the System library is automatically imported, giving you access to all of the
system. functions without having to add in
import system at the beginning of your script. The Project and Shared libraries are also imported, giving you access to any of the Script Modules you may have written. However, prior to version 7.7.0, Script Modules did not automatically import the System Library. This means that if using a version older than version 7.7.0, you will need to add
import system at the beginning of your script to be able to use the System Library.
When to Import the System Library - edited content - Maria
In current versions of Ignition (version 7 and later), the System Library (containing the built-in library package), Shared and Project Script Libaries are imported for you automatically when creating a new project. Subsequent versions of Ignition beyond version 7 no longer require you to import the System Libary every time you create a new scope. However, versions prior to version 7 still require you to import the System Library every time a system associated function is called inside a function. Regardless, typing the "import system" command in any version of Ignition, will result in the System Library being available for use inside the function.
Project Scripts vs. Shared Scripts
There are a few differences between Project and Shared Script Modules. The first difference is the way each script type is called. Shared Scripts are called with "shared." preceeding the event handler function, while Project Scripts are called with "project." The other difference is where Shared and Project Script Modules can be used. Project Script Modules are limited to the Project Scope, and can only be used within the project they are located in. This is useful when you are using a script many times within a project, but do not want it to be used by any other projector, or elsewhere in your Gateway.
Shared Script Modules, on the other hand, are not limited by scope and can be used anywhere within any project or Gateway. However, this means the Shared scripts can be used by projects that possibly shouldn't have access to them. Since Shared Script Modules are stored on the Gateway, they are not part of a normal project export. So moving a project to a new Gateway will require that Shared scripts also be included in the transfer.
A Simple Example
In our projects, we use a fairly simple script that calculates OEE, and writes it to a Tag as shown below.
Instead of copying this script between projects and throughout all of the windows that need it, a Shared Script Module was created in the Shared Script Library called "Metrics." The code was copied and pasted into the scripting area in the Shared Script Library.
This could then be called from a script in any project by calling shared.metrics.calculateOEE():