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;
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
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 ;
The differents attributes we're going to use on the scriptNode are ;
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 ;
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 ;) !
Submitted: 2017-04-16 11:17:52 UTC
Tags: scripting, #Python, Maya Python, scriptJob, and scriptNode
Python Batch for Maya 2.0.0 (maya script)
Colt Python 375 3D Model
Gradient Data Manager script for Maya for Maya 1.1.1 (maya script)
Quick set panel for Maya 1.0.0 for Maya (maya script)
Fantasy Monster Python 3D Model
Speed Facial Rig: Full version for Maya 2.0.2 (maya script)
Autorig : bird and flying creature for Maya 1.4.0 (maya script)
Anko - Rigged Realistic Woman for Maya 3D Model
Arnold ID Aovs Tool 0.0.1 for Maya (maya script)