- /*
- MEL Class 3
-
- Using Procedures to Create
and Control Shaders.
- */
-
-
-
-
- // Part 1. (makeColor.mel)
-
-
-
-
- // In GUI, make a sphere and a Blinn shader.
Connect the two and color it red.
- sphere -p 0 0 0 -ax 0 1 0 -ssw 0 -esw 360
-r 1 -d 3 -ut 0 -tol 0.01 -s 8 -nsp 4 -ch 1;objectMoveCommand;
- shadingNode -asShader
blinn;
- // Result: blinn1 //
- sets -renderable true -noSurfaceShader true -empty -name blinn1SG;
- // Result: blinn1SG //
- connectAttr -f blinn1.outColor
blinn1SG.surfaceShader;
- // Result: Connected blinn1.outColor to blinn1SG.surfaceShader
//
- showEditor blinn1;
- setAttr "blinn1.color" -type double3
1 0.5 0.5 ;
- setAttr "blinn1.color" -type double3
1 0 0.5 ;
- setAttr "blinn1.color"
-type double3 1 0 0 ;
- select -r nurbsSphere1 ;
- sets -e -forceElement
blinn1SG;
- // Result: blinn1SG //
-
-
-
-
-
-
- // Extract the essence of the script created
from the GUI.
- // Insert variables to give it flexibility.
-
-
- string $shNode = `shadingNode
-asShader blinn`;
- string $sgname = `sets
-renderable true -empty`;
- connectAttr -f ($shNode + ".outColor")
($sgname + ".surfaceShader");
- select -r nurbsSphere1;
- sets -forceElement $sgname;
- setAttr ($shNode+".color") -type double3 1 0 0.5 ;
-
-
-
-
-
-
- // Use our previous code to create a procedure:
a black box with a
- // controlled inputs.
-
-
- global proc makeColor(float $r, float $g, float $b, string
$selected){
- string $shNode = `shadingNode -asShader blinn`;
- string $sgname = `sets -renderable true -empty`;
- connectAttr -f ($shNode + ".outColor")
($sgname + ".surfaceShader");
- select -r $selected;
- sets -forceElement $sgname;
- setAttr($shNode+".color") -type
double3 $r $g $b;
- }
-
-
- // Example:
- makeColor( 0, 1, 0, "nurbsSphere1"
);
-
-
-
-
-
-
-
-
-
-
-
-
- // Part 2: (makeColorA.mel)
-
-
- // Previous code is inefficient. It generated
a new shader for each
- // new geometry object. Make it so we can
reuse the same shader for
- // many objects. The unlimited list of objects
gets input in an array.
-
-
-
-
- global proc makeColorA(float $r, float
$g, float $b, string
$selected[]){
- int $i, $n;
- $n = size($selected);
- string $shNode
= `shadingNode -asShader blinn`;
- string $sgname = `sets -renderable true -empty`;
- connectAttr -f ($shNode + ".outColor")
($sgname + ".surfaceShader");
- setAttr ($shNode+".color") -type
double3 $r $g $b;
- for ($i = 0; $i < $n;
$i++) {
- select
-r $selected[$i];
- sets
-forceElement $sgname;
- }
- }
-
-
-
-
- string $ar[] = { "nurbsSphere1",
"nurbsSphere2", "nurbsSphere3" };
- makeColorA( 0, 1, 1, $ar
);
-
-
-
-
-
-
-
-
-
-
-
-
- // Part 3: (makeColorB.mel)
-
-
- // Give the code more flexibility. Redesign
it.
- // Get a "shader group" handle
when we make it so that
- // we can use that handle for assigning geometry
to shaders.
- // Notice first use of a "returned value".
- // Procedures have output as well as inputs.
-
-
- global proc string makeColorShader(float
$r, float $g, float $b){
- string $shNode = `shadingNode -asShader blinn`;
- string $sgname = `sets -renderable true -empty`;
- connectAttr -f ($shNode + ".outColor")
($sgname + ".surfaceShader");
- setAttr ($shNode+".color") -type
double3 $r $g $b;
- return $sgname;
- }
-
-
-
-
-
-
- global proc assignColorShader( string $sgname,
string $selected ) {
- select -r $selected;
- sets -forceElement $sgname;
- }
-
-
-
-
-
-
- global proc assignColorShaders( string $sgname,
string $selected[] ) {
- int $i, $n;
- $n = size($selected);
- for ($i = 0; $i < $n; $i++)
- assignColorShader( $sgname, $selected[$i]
);
- }
-
-
-
-
- string $sel[] = { "nurbsSphere1",
"nurbsSphere2", "nurbsSphere3" };
- string $sel2[] = { "nurbsSphere2",
"nurbsSphere3" };
- string $sg1, $sg2;
-
-
- $sg1 = makeColorShader(
1, 0, 0 );
- assignColorShader( $sg1,
$sel[0] );
- $sg2 = makeColorShader(
0, 1, 0 );
- assignColorShaders( $sg2,
$sel2 );
-
-
-
-
-
-
- // Part 4: (makeColorC.mel)
-
-
- // An even more flexible design.
- // When we create the shader, we now have
three pieces of information
- // to use: two of them make good handles.
- // Notice that we have to use arrays to return
multiple pieces of information.
- // Use a more sophisticated "for"
loop to generate better code.
- // With the addition of the variable "$type",
we have the ability to specify
- // what type of shader we want: "blinn",
"lambert" or "phong".
-
-
- global proc string makeColorShader(float
$r, float $g, float $b, string
$type, string $shout[]){
- string $shNode = `shadingNode -asShader $type`;
- string $sgname = `sets -renderable true -empty`;
- connectAttr -f ($shNode + ".outColor")
($sgname + ".surfaceShader");
- setAttr ($shNode+".color") -type
double3 $r $g $b;
- $shout[0] = $sgname;
- $shout[1] = $shNode;
- $shout[2] = $type;
- return $sgname;
- }
-
-
-
-
-
-
- global proc assignColorShader( string
$sgname, string $selected ) {
- select -r $selected;
- sets -forceElement $sgname;
- }
-
-
-
-
-
-
- global proc assignColorShaders( string
$sgname, string $selected[] ) {
- string $sel;
- for ($sel in $selected)
- assignColorShader( $sgname,
$sel );
- }
-
-
-
-
- string $sel[] = { "nurbsSphere1",
"nurbsSphere2", "nurbsSphere3" };
- string $sel2[] = { "nurbsSphere1",
"nurbsSphere3" };
- string $sout1[3], $sout2[3];
-
-
- makeColorShader( 0, 0,
1, "lambert", $sout1 );
- assignColorShader( $sout1[0],
$sel[1] );
- makeColorShader( 1, 1,
0, "phong", $sout2);
- assignColorShaders( $sout2[0],
$sel2 );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-