Project Scripts

Shared Scripts

Shared Scripts effectively allow you to create your own Python libraries that are stored on the gateway. Once created, these scripts may be called from any resource on the Gateway. Shared Scripts use normal Python syntax, and require you to define each function in the library. 

Consider the following function that would calculate OEE:

def caclulateOEE(lineNum):
	paths = ["[default]Metrics/Line_%f/Quality" % lineNum,
			"[default]Metrics/Line_%f/Availability" % lineNum,
			"[default]Metrics/Line_%f/Performance" % lineNum]
	values = system.tag.readAll(paths)
	#Get your Q, A, and P
	quality = values[0].value
	availability = values[1].value
	performance = values[2].value
	#calculate OEE
	oee = quality * availability * performance
	#Write back to our OEE tag. 
	system.tag.write("[default]Metrics/Line_%f/OEE" % lineNum, oee)

This could be defined as a Shared Script:

This could then be called from a script by calling shared.LibraryName.FuncitonName():


Advanced Scripting

Client vs. Gateway Scripts

Your projects can execute scripts in response to a variety of events and actions that occur within the project's lifecycle. There are two major scopes for scripting: Gateway scripts and Client scripts. 

Client Scripts

Client scripts execute in the running Client, which means that they may never execute (if no clients are running), or they may execute many times (if multiple clients are running). Client scripts will also execute in the Designer, but only in Preview Mode.

Because Clients are full-fledged applications, the scripts run on the computer running the Client, not on the Gateway's host server computer. This means that they don't have access to the Gateway's file system, and so on. It also means that if no clients are launched, the scripts will not be executing.

Note that these global project event scripts are not to be confused with the component event handler scripts.

Gateway Scripts

Gateway scripts execute on the Ignition Gateway service. This means that they will always execute as long as Ignition is running, even if there are no clients are open. If there are multiple clients open, these scripts will still only run once. For example: if you have a script that writes to the gateway when a tag changes, it belongs in the Gateway scope so you don't get duplicate records when multiple clients are open.


Script Modules

Ignition's Shared and Project Script Modules are global libraries of scripts that can be called from anywhere within their scope. Shared Script Modules are available to the whole Gateway, and Project Script Modules are available anywhere in that project. These scripts are organized as named modules that all live under the shared or project module. To open the Script Module Editor find the shared or project Script Library and double-click it in the Project Browser.

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

If you're unsure of when to put scripts in a script module vs. writing 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 right click the Script Library [project] package and click the New Script option. Each module is a python script that may define many functions. You may also organize modules in subpackages if you'd like. Lets suppose you added a script module named myfuncs, whose body was:

def callMe(message):

Now, anywhere in your project you can call this function:

project.myfuncs.callMe('Hello World')

To import System or not to import System

Frequently in Ignition, your scripts get system (the built-in library package in Ignition) and shared or project imported for you automatically.

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.