// 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; }