PySide: Using ScriptJobs

ScriptJobs are something I use from time-to-time and I thought I would show a couple examples of how I use them with PySide interfaces.

First up, a single UI window that creates a ScriptJob and then deletes it upon closing.  The ScriptJob in this case sets the text of a QLabel to the name of the selected object. Here is the basic window that extends QDialog.

Now lets add the ScriptJob. In the create method of MainWindow we add the following lines.

When a ScriptJob is first created it returns the job number, which we use later on to delete the job when the window is closed. We’ll add self.onSelectionChange in a second. For now all you need to know is it’s the class method that changes the window’s QLabel text.

Next we extend QDialog’s closeEvent method by adding a line that kills our ScriptJob before closing the window.

And finally, to the MainWindow class we add the method onSelectionChange. This grabs the currently selected objects. If there is only one then it changes the window’s QLabel text to the name of the object.

Put all of that together and we have this.

That’s it! We now have a PySide QDialog that creates a ScriptJob when it’s opened and then kills the job before it closes.

Sometimes it can be useful to list all the active ScriptJobs running in Maya. To do this run the following in the Script Editor.

In the next post I’ll show how I handle ScriptJobs that are created/killed by a dynamically loaded QWidget within a QLayout.