Wednesday, March 21, 2012

How I write a MEL script - Idea to Scriptlet

The Idea and Scriptlet phase is quick.  I see them as a sketch or thumbnail; something that can be quickly roughed out, changed and erased.

Reminder: The example will be to help lighters make a matte render.



Idea

Now that I'm ready to write the proc, I'll put down a few steps shorthand style.  This gives me a quick and easy way to restructure the flow of the proc and to think about the logic.


/////////
// make random matte colors for selected geo
// grab a switch node
     // connect geo
     // make a gamma node per geo
     // set a random value to gamma node


Scriptlet

Now I write hard-coded lines to test out each step.  This will evolve in the full blown proc, but it's faster to write non-error checked code and to do some of the steps in Maya's UI rather than script everything from the beginning.  Error checking will take up at least 50% of your code, if it doesn't, then you're not error checking enough for anyone to use it other than yourself.  Plus forcing me to do some of the steps by hand gives me the opportunity to try lots of methods, all in an effort to find the most efficient path.  Maya is a deep program, and this is a chance to dig.

/////////
// make random matte colors for selected geo
// grab a switch node
string $ls[] = `ls -sl`;
// connect geo - connect the shapes by-hand for now

// get the connected shapes from the switch node
string $cons[] = `listAttr ($ls[0]+".input[*].inShape")`;
// iterate through the connections

for( $i=0;$i<size($cons);$i++ ) {
    // make a gamma node per geo
    string $gamma = `shadingNode -asUtility gammaCorrect`;
    connectAttr -f ($gamma+".outValue") ($ls[0]+".input["+$i+"].inTriple");

    // set a random value to gamma node
    float $r = abs(noise($i));
    float $g = abs(noise($i+1.0));
    float $b = abs(noise($i+2.0));
    setAttr ($gamma+".value") $r $g $b;
}

No comments:

Post a Comment