Adverti horiz upsell
The scriptJob and the scriptNode
The scriptJob and the scriptNode
Mehdi Louala 51 views  Rating:
(1 rating)
Page 1 of 1

Hi there,
Here is a tutorial to explain you how you can control advanced behavior in Maya when toggling an attribute, the technique can be used for several other purposes, it's basically the way to create inner callbacks in your Maya's scene.
The tutorial has been originally wrote for, feel free to check the other tutorial we released :-) !


In this tutorial we're going to build a simple system based on scriptNode and scriptJob , with these two kind of nodes, we're going to be able to automate more complex things in our Maya scene !

This example will be very easy, we're just going to add an attribute on an object which allow the user to change the vertexColor value of the selected object !

The concept will be something like ;

    1. Add a custom attribute to our object

    2. Create a Python function which will act depending on our custom attribute's value

    3. Connect this function using a scriptJob so this will be executed every time our attribute is changed

    4. Create a scriptNode which will contains all this stuff and execute when our scene is started, so it'll be in memory and ready-to-use =) !

Let's go practice !


First part ; preparing our scene & tools

Setting our custom attribute

First we need to create an attribute, let's say on a sphere, we select our sphere, go in the Channel Box then Edit → Add Attribute and we create a new attribute named 'color', we select the Enum kind, and we add three Elements (by clicking on a new empty line in the Enum Names list, this will add a new item), Red, Green and Blue

Writing our function

Then we're going to write a Python function, names colorchange, we're going to need one 'special' cmds function, polyColorPerVertex which will allow us to replace and/or update the colorSet of our object to change the color globally. For this example we're going to consider that the target object, is the selected one when the function is executed, which is the most common case, but this should be changed for more advanced work, if you use an external command control panel for instance.

Our function should looks like something like this;

color changing function
import maya.cmds as cmds
def colorChange():
    # we are changing the colorVertex info of our object depending of the value
    # of the 'color' attribute on the selected object
    obj_attr = '%s.color' %[0]
    if cmds.getAttr(obj_attr)==0 : # when the attribute is set to 'Blue'
    elif cmds.getAttr(obj_attr)==1 : # when the attribute is set to 'Red'
    elif cmds.getAttr(obj_attr)==2 : # when the attribute is set to 'Green'


Okay that's it ! Our little function here, if you call it, with colorChange() will change the colorVertex info of our object depending of the value of it's 'color' attribute !

Second part ; creating and connecting the scriptJob & scriptNode


The Python Maya syntax to create a callback can be achieved using a scriptJob which will be then connected to an event, namely our fresh colorChange function !



Executing this line of code above, will register a new callback in Maya, then every time you change the value of the attribute 'color', the function 'colorChange' will be called, simple and easy =)


The last step for us will be to write our famous 'colorChange' function and the scriptJob call in a string variable, we'll connect the whole thing to a scriptNode , so it's gonna be run everytime our scene is started !

The Maya Python syntax to do so is ;

cmds.scriptNode(st = 2, bs = myCode , n = 'sn_colorChange', stp = 'python')

The differents attributes we're going to use on the scriptNode are ;

st = 2                       # type of execution (this one is 'when scene starts')
bs = myCode                    # our string variable containing our function
n = 'sn_colorChange'          # name of our node
stp = 'python'                # the type of our string function, 'python' or 'mel'

Note ; Here we have to notice two things, first, if you want to see you're freshly created node, in the Outliner you need to RMB → Show DAG Objects, this will display several nodes in the lists and you're scriptNode is here =).

The second thing is, if you want to write a multi-line variable in Python you need to declare the variable's content using triple-quotations ''', like that ;

Multi-line code sample
myCode = '''

 Now we just going to copy and paste our whole function inside a our myCode variable, and create a scriptJob which contains our variable !

Ending & Assembly

While the creation of our scriptNode, we're just going to replace our multi-line variable to make it read-able by Maya. By replacing the triple-quotations of our string by double !

We get ;

scriptJob and scriptNode assembly
myCode = '''
import maya.cmds as cmds
def colorChange() :
    obj_attr = '%s.color' %[0]
    if cmds.getAttr(obj_attr)==0:
    elif cmds.getAttr(obj_attr)==1:
    elif cmds.getAttr(obj_attr)==2:
cmds.scriptNode( st=2, bs=myCode.replace("'''","''" ), n='sn_colorChange', stp='python')

Easy ! Run that and save your scene, open it again (in order to trigger our scriptNode when the scene starts), and you're sphere will gets it's color changed everytime you modify it's 'color' attribute !
Here is a demo ;

You can also download the demo-scene on 3dbunk, you'll see how it works, this is very easy !
Don't forget to display your DAG objects if you want to find the scriptNode ;) !


Please sign up or sign in to post a comment.