Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

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 including every project, and Project Script Modules are available anywhere in the project that they were created in. These scripts are organized as named modules that all live under the shared or project Script Library. To open the Script Module Editor, find the shared or project Script Library and double-click it in the Project Browser.

On this page ...


Project Scripts


Shared Scripts

Rule of Thumb: Never Copy-and-Paste

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 which can be a major maintenance liability. By consolidating scripts into one location makes them easier to update.

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.

def callMe(message):

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

project.myFuncs.callMe('Hello World')

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:

project.myFuncs.addNumbers(14, 87)

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

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.

Project Vs. Shared Scripts

******************Maria, I don't like the way this sounds. Hopefully you get what I am trying to convey, and can organize it much better.**********************

There are only a few differences between Project and Shared Script Modules. The first is in the way they are each called, with shared scripts being called with "shared.*" while project scripts are called with "project.*" The other difference is where each can be used. The Project Script Modules are limited to the Project Scope and only within the project that 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 elsewhere in your Gateway. The Shared Script Modules are not limited by scope and can be used anywhere within any project or the Gateway. However, this means the scripts can be used by projects that possibly shouldn't have access to them. They are also stored within the Gateway, and are not a part of a normal project export, so moving a project to a new Gateway will require that the shared script also be transferred.

A Simple Example

In our projects, we are using a fairly simple script that calculates OEE, and writes it to a tag:

def caclulateOEE(lineNum):
	#Build the Tagpaths with the dynamic line numbers.
	paths = ["[default]Metrics/Line_%f/Quality" % lineNum,
			"[default]Metrics/Line_%f/Availability" % lineNum,
			"[default]Metrics/Line_%f/Performance" % lineNum]
	#Read the values from the Tags.		
	values = system.tag.readAll(paths)
	#Get your Quality, Availability, and Performance.
	quality = values[0].getValue()
	availability = values[1].getValue()
	performance = values[2].getValue()
	#calculate OEE
	oee = quality * availability * performance
	#Write back to our OEE tag. 
	system.tag.write("[default]Metrics/Line_%f/OEE" % lineNum, oee)

Instead of copying this script between projects and throughout all of the windows that need it, we can instead create a Shared Script Module and put our script within. Here we have created a shared script and named it "metrics", and then copied our script into the script area:

This could then be called from a script in any project by calling shared.metrics.calculateOEE():

  • No labels