Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- proc vector getFaceCenter( string $pFaceName ) {
- vector $vertexPositions[] = `xform -q -ws -t $pFaceName`;
- vector $sum = << 0, 0, 0 >>;
- vector $v;
- for( $v in $vertexPositions ) {
- $sum = $sum + $v;
- }
- vector $average;
- int $numVertices = size( $vertexPositions );
- if( catch( $average = $sum / $numVertices ) ) {
- print( "Attempt to divide by 0: " + getLastError() );
- $average = << 0, 0, 0 >>;
- }
- return $average;
- }
- proc vector getFaceNormal( string $pFaceName ) {
- string $polyInfoResult[] = `polyInfo -fn $pFaceName`;
- string $stringToParse = $polyInfoResult[0];
- string $items[];
- int $numTokens = `tokenize $stringToParse " " $items`;
- float $x = ($items[2]);
- float $y = ($items[3]);
- float $z = ($items[4]);
- vector $normal = << $x, $y, $z >>;
- string $parentShape[] = `listRelatives -parent $pFaceName`;
- string $parentTransform[] = `listRelatives -parent $parentShape[0]`;
- float $transformMatrix[] = `xform -q -m -ws $parentTransform[0]`;
- vector $worldNormal = `pointMatrixMult $normal $transformMatrix`;
- vector $unitWorldNormal = unit( $worldNormal );
- return $unitWorldNormal;
- }
- proc moveAlign( string $pObjectName, vector $pNormal, vector $pPosition ) {
- vector $tangent1 = unit( cross( $pNormal, << 0, 1, 0 >> ) );
- if( mag( $tangent1 ) == 0 ) {
- $tangent1 = << 1, 0, 0 >>;
- }
- vector $tangent2 = unit( cross( $pNormal, $tangent1 ) );
- matrix $m[4][4] = <<
- ($tangent2.x), ($tangent2.y), ($tangent2.z), 0.0;
- ($pNormal.x), ($pNormal.y), ($pNormal.z), 0.0;
- ($tangent1.x), ($tangent1.y), ($tangent1.z), 0.0;
- ($pPosition.x), ($pPosition.y), ($pPosition.z), 1.0 >>;
- xform -ws -m
- ($m[0][0]) ($m[0][1]) ($m[0][2]) ($m[0][3])
- ($m[1][0]) ($m[1][1]) ($m[1][2]) ($m[1][3])
- ($m[2][0]) ($m[2][1]) ($m[2][2]) ($m[2][3])
- ($m[3][0]) ($m[3][1]) ($m[3][2]) ($m[3][3]) $pObjectName;
- }
- string $selection[] = `ls -os -fl`;
- string $faceNames[] = `filterExpand -selectionMask 34 -expand true $selection`;
- string $objectToInstance = $selection[0];
- if( `objectType $objectToInstance` == "transform" ) {
- string $face;
- for( $face in $faceNames ) {
- string $newInstance[] = `instance $objectToInstance`;
- vector $position = getFaceCenter( $face );
- vector $normal = getFaceNormal( $face );
- moveAlign( $newInstance[0], $normal, $position );
- }
- } else {
- print "Please ensure the first object you select is a transform.";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement