Introduction
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 ;
-
Add a custom attribute to our object
-
Create a Python function which will act depending on our custom attribute's value
-
Connect this function using a scriptJob so this will be executed every time our attribute is changed
-
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;
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' % cmds.ls(sl=True)[0] if cmds.getAttr(obj_attr)==0 : # when the attribute is set to 'Blue' cmds.polyColorPerVertex(r=0.0,g=0.0,b=1.0,a=1,cdo=True) elif cmds.getAttr(obj_attr)==1 : # when the attribute is set to 'Red' cmds.polyColorPerVertex(r=1.0,g=0.0,b=0.0,a=1,cdo=True) elif cmds.getAttr(obj_attr)==2 : # when the attribute is set to 'Green' cmds.polyColorPerVertex(r=0.0,g=1.0,b=0.0,a=1,cdo=True)
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
scriptJob
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 !
cmds.scriptJob(attributeChange=['pSphere1.color',colorChange])
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 =)
scriptNode
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'
Multi-line code sample | |
1 2 3 4 5 |
myCode = ''' multi lines code ''' |
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 ;
myCode = ''' import maya.cmds as cmds def colorChange() : obj_attr = '%s.color' % cmds.ls(sl=True)[0] if cmds.getAttr(obj_attr)==0: cmds.polyColorPerVertex(r=0.0,g=0.0,b=1.0,a=1,cdo=True) elif cmds.getAttr(obj_attr)==1: cmds.polyColorPerVertex(r=1.0,g=0.0,b=0.0,a=1,cdo=True) elif cmds.getAttr(obj_attr)==2: cmds.polyColorPerVertex(r=0.0,g=1.0,b=0.0,a=1,cdo=True) cmds.scriptJob(attributeChange=['pSphere1.color',colorChange]) ''' 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 ;) !
Author: mlouala
Submitted: 2017-04-16 11:17:52 UTC
Tags: scripting, #Python, Maya Python, scriptJob, and scriptNode
Software: Maya
Views: 11,632
Related Items
-
Python Batch for Maya 2.0.0 (maya script)
$20.00 (USD) -
Colt Python 375 3D Model
$24.00 (USD) -
Gradient Data Manager script for Maya for Maya 1.1.1 (maya script)
$20.00 (USD) -
Quick set panel for Maya 1.0.0 for Maya (maya script)
$20.00 (USD) -
Fantasy Monster Python 3D Model
$35.00 (USD) -
Speed Facial Rig: Full version for Maya 2.0.2 (maya script)
$99.00 (USD) -
Autorig : bird and flying creature for Maya 1.4.0 (maya script)
$30.00 (USD) -
Anko - Rigged Realistic Woman for Maya 3D Model
$120.00 (USD) -
Arnold ID Aovs Tool 0.0.1 for Maya (maya script)
$20.00 (USD)