//============================================================================ // km_randomInstanceRotation is teh main procedure that add the attributes // and expressions. //============================================================================ // USAGE: // 0. Source this code // 1. First select the particle object // 2. Then select the instancer node (NOT the instanced geometry) // 3. Run km_randomInstanceRotation //============================================================================ global proc km_randomInstanceRotation() { //get selected particles string $particleObject[] = `ls -sl`; string $particleShape[] = `getShapes $particleObject[0]`; //get instancer string $instancer = $particleObject[1]; //add attributes needed for expression addAttr -ln rotMax -at double $particleShape; setAttr -e -keyable true ($particleShape[0]+ ".rotMax"); addAttr -ln axisPP -dt doubleArray $particleShape; setAttr -e -keyable true ($particleShape[0]+ ".axisPP"); addAttr -ln rotPP -dt vectorArray $particleShape; setAttr -e -keyable true ($particleShape[0]+ ".rotPP"); addAttr -ln rotMaxRandPP -dt doubleArray $particleShape; setAttr -e -keyable true ($particleShape[0]+ ".rotMaxRandPP"); //================================================== // CREATION expression string $expressionC; $expressionC = ($particleShape[0] + ".axisPP = floor(rand(3)); \n"); $expressionC += ($particleShape[0]+ ".rotPP = <>;\n"); $expressionC += ($particleShape[0]+ ".rotMaxRandPP = rand( 0 - " + $particleShape[0]+ ".rotMax," + $particleShape[0]+ ".rotMax)/1000;\n"); dynExpression -s $expressionC -c $particleShape[0]; //================================================== // RUNTIME expression string $expressionR; $expressionR = ("if (" + $particleShape[0]+ ".axisPP == 0)\n"); $expressionR += ($particleShape[0] + ".rotPP = " +$particleShape[0]+ ".rotPP + <<" + $particleShape[0] + ".rotMaxRandPP,0,0>>;\n"); $expressionR += ("else if (" + $particleShape[0] + ".axisPP == 1)\n"); $expressionR += ($particleShape[0] + ".rotPP = " + $particleShape[0] + ".rotPP + << 0, " + $particleShape[0] + ".rotMaxRandPP, 0 >>;\n"); $expressionR += ("else\n"); $expressionR += ($particleShape[0] + ".rotPP = " + $particleShape[0]+ ".rotPP + << 0, 0, " + $particleShape[0] + ".rotMaxRandPP >>;" ); dynExpression -s $expressionR -rbd $particleShape[0]; // set correct rotation for instancer particleInstancer -e -name $instancer -rotation rotPP $particleShape[0]; } //=================================================== //getShapes gets the shaped of a selected transform // (from Bryans site) //=================================================== proc string[] getShapes( string $xform ) { string $shapes[]; $shapes[0] = $xform; if ( "transform" == `nodeType $xform` ) // If given node is not a transform, assume it is a shape // and pass it through { $shapes = `listRelatives -fullPath -shapes $xform`; } return $shapes; }