Adverti horiz upsell
The scriptJob and the scriptNode
The scriptJob and the scriptNode
Mehdi Louala 3,991 views  Rating:
(2 ratings)
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 ;) !


  • lukenlow

    lukenlow said about 1 month ago:

    I sincerely admire your work, the work that you have done amazes me, you are a great fellow, thank you 192.168.l.l
  • 3847

    3847 said 3 months ago:

    It is not working I have copy and paste script is not working properly color doesnot changes at all
  • Arvind

    Arvind said 3 months ago:

    If you want play online spider solitaire game then it is the best option to get your time pass and it is more effective game that can you play in very cpnstant moind it will be more excited to play this game.
  • said 3 months ago:

    Not all the characteristics of the successful entrepreneur are innate;you should also consider your environment and what influences you have received during your life.Matt Choi "Certus Trading" is one of successful trader and he is well known in business world.

  • beyazpiyon

    beyazpiyon said 5 months ago:

    Awesome tutorial, thanks a lot!
  • daisy walker

    daisy walker said 5 months ago:

    I was so amazed the first time I learned about the script node. Now that I look at it, I feel like it is a necessity, for sure, but back before I had no idea that they, callbacks and script jobs existed, I often thought that it would be amazing to have a way of running code at different points of interaction with rigs. So I finally learned in this tutorial, If you need any help so we are here at Top Essay Writing Service UK for your help all level university students along with high quality.


  • Ricahrd robert

    Ricahrd robert said 7 months ago:

    NodeJs runs Javascript on server-side, the stuff like handling requests and making calls to the database which you'd rather be doing with the personal home page, Python, Ruby or Java. there may be no doubt in Ruby or each different language being used on Node. UK Essay Help

  • invi

    invi said over 1 year ago:

    helped a lot :)

Please sign up or sign in to post a comment.