Scripting

What is Scripting in Ignition?

Users that are new to Ignition focus mainly on the Component Binding system, and for good reason. It's simple, flexible, and generally easy to understand without much of a computer background. In addition to that, Ignition has a complete scripting system built into every place you can think of. Using it is not a requirement, but it can add a significant degree of flexibility and customization to your projects. It allows you to create exactly what you need, giving you total control where pre-canned options fall short.

The majority of your scripting will be done in Event Handlers inside of components. This system makes it very easy to get started scripting with little to no experience. With the four script builders, basic scripting like Navigation and setting Tag values takes just a few clicks!

What is Python?

Most of the time when we talk about "scripting" we're talking about Python scripting. Python is a general purpose programming language that was developed in the early 90's and has gained significant popularity in the 2000's. We like it because it is extremely readable, elegant, powerful, and easy to learn. As an added bonus, it gracefully interacts with Java, giving programmers an extremely powerful tool when paired with Ignition, which is written in Java.

Since Python is such a popular and well-regarded language, there are many high-quality tutorials available on the web. The official Python tutorial, written by the inventor of Python himself, Guido van Rossum, is very good. https://docs.python.org/2/tutorial/index.html. In Ignition, you will be mixing the core Python language with references to other components and a variety of our built-in system functions.

Sample Code
text = event.source.parent.getComponent('Text Field').text
if text == "":
print "You must enter a name"

Python or Jython?

You'll often hear Python referred to as "Jython" by advanced users of Ignition. Python is the language, Jython is the implementation of the language that we use. Most users of Python use the implementation called "CPython" - they just don't realize it. See http://en.wikipedia.org/wiki/Python_(programming_language)#Implementations.

One of the powerful things about using Jython is that your script has access to the entire Java standard library. In the Client, this will be Java 5 or above. When running under the Gateway, this will be Java 6 and above. For more information, see Accessing Java.

Many scripting users are blown away by their script's speed. We can't take credit for this - the Jython engine hot-compiles (compiles the code when it is run) your Jython code to Java bytecode, which means it runs natively in the JVM, which in turn can hot-compile it to machine code. It's fast.

Which version of Python is Used?

Ignition uses Jython 2.5. Jython is the Python programming language implemented over the Java Virtual Machine. When looking at outside documentation, such as on www.python.org, verify that you are looking at the correct version of the documentation.

Jython 2.5 allows us to use the standard functions and tools in Python 2.7, so if you want to look up something in the Python docs, make sure to use version 2.7 (https://docs.python.org/2/).

Why not VBA?

Many HMI/SCADA packages use VBA, or Visual Basic for Applications. As such, many engineers switching to our software inquire about it. There are a variety of reasons we don't use VBA:

  • It is not compatible with Java, the language that Ignition is written in. This also means that it is not cross-platform.

  • It is a dying language (Microsoft has phased it out as of July, 2007).

  • It is full of security holes.

  • It is an ugly language!

Is it easy to use?

Luckily, Python is a simple language to get started with, and using it in an event-driven system takes away a lot of the extra code that normally makes programming time consuming. For those that are already familiar with scripting (and those of you that are learning), we also have a huge list of functions inside Ignition to do some of the common tasks in a single line of code. These System Functions are available while typing. Just start with "system." and press ctrl-space to see a list of available functions.

images/download/attachments/6047078/Scripting_Overview_ctrl_space.png

Where is Scripting Used?

Python is used in many places in Ignition. Each location has it's own events that trigger your scripts to run, and add functionality to your projects in different ways. The most apparent place is in event handlers on components and other objects in Ignition Clients. Project and Gateway event scripts are another major place where Python is used.

Component Event Handlers

Every component has "events" associated with them. Things like when the mouse is clicked, a key is pressed, or a value changes. Each type of component has a slightly different set of events based on what you do with that component. For example, all of them have the Property Change event, but only components that can be activated have the focus events.

images/download/attachments/6047078/Scripting_Overview2.png

Project and Gateway Scripts

Scripts can be set to activate on specific events in the Client or in the Gateway. These scripts may run every 5 seconds, on a tag change, or when the Client or Gateway start up or shut down. There is even a messaging system that allows you to create functions that will run on demand when called by another script.

images/download/attachments/6047078/Scripting_Overview_project_script.png

Extension Functions

Extension Functions are chunks of code that allow you con interact with some of the basic parts of components that would otherwise be hidden. They vary for each component and not all components have them because they are so specialized. These functions allow you to do things like configure a cell or create a right-click menu in a table, or change the way the Easy Chart works.

images/download/attachments/6047078/Scripting_Overview_extension.png

Scripting Modules

Create your own reusable blocks of code in the Script Library. You can set up these Script Modules that exist in just one Project, or ones that are shared between all projects on a Gateway. Once completed, these functions can be called from anywhere in Ignition just like our system.* functions.

images/download/attachments/6047078/Scripting_Overview_script_module.png

Custom Methods

You might want to have a table contain all the scripting to edit it's contents, or a button that does a complicated check before continuing. Using Custom Methods allow you to add functions just like the Scripting Modules, but attach them directly to a specific component. This means that if you have a template or component used in several places, you can set up common functions that will stay with the component.

images/download/attachments/6047078/Scripting_Overview_custom_method.png

Tag Scripts

Once Enabled, these scripts are fired whenever a Tag value changes or an alarm event happens. You can use them for additional diagnostics, to set additional tags, or to react to an alarm event.

images/download/attachments/6047078/Scripting_Overview_tag_script.png

Expression Language & SQL Queries vs Scripting

There are three major languages in Ignition, the Expression language, the SQL Queries, and Python Scripting. It is important to understand the differences between the three and to know where each is used. Scripting is used in the event handlers that are available all over Ignition, but Expressions and SQL are in the Property Binding locations shown here.

The Expression Language

The Expression Language is a simple programming language that we invented (very similar to many other existing expression languages), and is different from the Python scripting you will find in Ignition. The expression language is a very simple kind of language where everything is an expression - which is a single piece of code that returns a value. This means that there are no statements, and no variables, just operators, literals, and functions.

The most common expression language that most people are familiar with is the one found in Microsoft Excel. You can have Excel calculated a cell's value dynamically by typing an expression like =SUM(C5:C10). Our expression language has similar functionality but different syntax. It is mainly used to define dynamic values for tags and component properties. In Ignition's Expression Language, you will use component properties and functions like if({Root Container.type}="Type C",True,False).

Expressions and Python Scripting are often confused but they are used in very different spaces. The Expression language is most commonly used in expression bindings, and the Python language is used in Ignition's Events.

images/download/attachments/6047078/Scripting_Overview_Expression.png

SQL Queries

The SQL language is used for selecting information from a database. It can be used in a variety of ways, but most of them will either make modifications to the database or return a set of values (with a few notable exceptions like Stored Procedures). The majority of users will be returning large chunks of data into a table or report in Ignition. This means a complete data set will be returned based on a user selection, a time range, or any combination of factors. SQL and Python Scripting are sometimes confused because they can be used in the same spaces. SQL can be used in both Bindings and Scripting, but is it's own language even when called from inside scripting.

In a Binding, SQL is often separated into multiple lines:

images/download/attachments/6047078/Scripting_Overview_SQL.png

In scripting, SQL queries are strings ("in quotes") and must be run by one of the system.db.* functions.

images/download/attachments/6047078/Scripting_Overview_SQL_code.png

In This Section ...