This lesson is part of the Scripting in Ignition course. You can browse the rest of the lessons below.


Autoplay Off


Learn how to create your own scripting libraries in this lesson.

Video recorded using: Ignition 8.1


(open in window)

[00:00] In this lesson, we're going to be taking a look at custom built scripting libraries that you can configure as project resources. The end result is going to be something akin to a script template, meaning a chunk of code that you can use repeatedly throughout your project or share with many projects as an inherited resource. So to get us started, I'm going to find my way into the scripting section of the project browser. Expand it. Then, right click on project library. From there, I'll have two options. One is to create a new script, which is basically like making a new custom group of scripting functions or I can select new package, which is effectively just a folder that holds some number of scripts. To keep things simple I'm going to select a new script and then we'll be prompted to name our script. So I'll go ahead and call it message since I'll be using it to show a message in just a second. Now, once we click create script, we'll be given a large empty work area here. And in that work area, we just need to define one or more Python functions.

[01:06] So I'll just put one function in here. And the way that I do that is by saying d-e-f and then the name of my functions so maybe something like, sayHello, and then in parentheses we'll put any arguments we want our function to take so maybe I'll do something like name so that will say hello to a specific user based on their name and then I'll close off the line with a colon character. Then on the next line, I'll tab over and now we're ready to specify what this function actually does. So I'll say system dot and then hit control space to open up our interactive menu and say gui and then message box, which is going to open up a little dialogue box for the user. Now I'll have to tell the function what to say. So I'll do a parenthesis and then say hello in quotes plus name, which will incorporate that name parameter we're passing in. And that's it.

[02:01] I'm ready to test out my project script. Now system.gui.messageBox is a Vision function. So this project script is really only going to work in Vision, but I do want to emphasize that more generally project scripts can be called throughout the project, including in perspective environments, alarm pipelines reports and so on. That said, I'm going to go into a Vision window with a button on it that I've set up already. And then on that window, I'm going to right click on the button, go to scripting, go into the action performed event handler and click on these script editor tab. And from here, all I need to do to invoke that product script code is give my script a name. So message followed by the function name so say hello and then a name parameter. So how about Jane. Now one final note before we move on here if I had put my script inside of a package, I would need to include that before the script name. So something like mypackage.message.sayHello. The other note here is that if you're running Ignition 7 instead of Ignition 8, you would say project.message.sayHello more on that in a minute.

[03:12] So with that, let's go ahead and click okay. Go into preview mode and when we click on the button, it tells us hello Jane, which is great. Now just a final note on how Ignition 7 and Ignition 8 handle project scripts. So in Ignition 7 we have the concept of a shared script, which is a script that can be used across all projects in any gateway scope, like on a tag event script. We phased out shared scripts on Ignition 8 and replaced them with project inheritance meaning that if you want to have shared scripts, you can simply put project scripts in a parent project and inherit from that parent project across your other projects. For more info, check out the project inheritance video. Additionally, if you would like to have project scripts run in a gateway scope, like on a tag, you can find the gateway setting section of the gateway webpage and scroll down until you find the gateway scripting hitter, where you just need to set a project with those gateway scope scripts as the gateway scripting project.

[04:06] So that about wraps up our project script system. It's a convenient way to take a script that you repeatedly used throughout your project and configure it in a single easy to find place.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.