Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. {
  2.     proc vector getFaceCenter( string $pFaceName ) {
  3.        
  4.         vector $vertexPositions[] = `xform -q -ws -t $pFaceName`;
  5.        
  6.         vector $sum = << 0, 0, 0 >>;
  7.        
  8.         vector $v;
  9.         for( $v in $vertexPositions ) {
  10.            
  11.             $sum = $sum + $v;
  12.            
  13.         }
  14.        
  15.         vector $average;
  16.        
  17.         int $numVertices = size( $vertexPositions );
  18.        
  19.         if( catch( $average = $sum / $numVertices ) ) {
  20.             print( "Attempt to divide by 0: " + getLastError() );
  21.             $average = << 0, 0, 0 >>;
  22.         }
  23.        
  24.         return $average;
  25.        
  26.     }
  27.    
  28.     proc vector getFaceNormal( string $pFaceName ) {
  29.        
  30.         string $polyInfoResult[] = `polyInfo -fn $pFaceName`;
  31.         string $stringToParse = $polyInfoResult[0];
  32.        
  33.         string $items[];
  34.         int $numTokens = `tokenize $stringToParse " " $items`;
  35.        
  36.         float $x = ($items[2]);
  37.         float $y = ($items[3]);
  38.         float $z = ($items[4]);
  39.        
  40.         vector $normal = << $x, $y, $z >>;
  41.        
  42.         string $parentShape[] = `listRelatives -parent $pFaceName`;
  43.         string $parentTransform[] = `listRelatives -parent $parentShape[0]`;
  44.        
  45.         float $transformMatrix[] = `xform -q -m -ws $parentTransform[0]`;
  46.        
  47.         vector $worldNormal = `pointMatrixMult $normal $transformMatrix`;
  48.        
  49.         vector $unitWorldNormal = unit( $worldNormal );
  50.        
  51.         return $unitWorldNormal;
  52.        
  53.     }
  54.    
  55.     proc moveAlign( string $pObjectName, vector $pNormal, vector $pPosition ) {
  56.        
  57.         vector $tangent1 = unit( cross( $pNormal, << 0, 1, 0 >> ) );
  58.         if( mag( $tangent1 ) == 0 ) {
  59.             $tangent1 = << 1, 0, 0 >>;
  60.         }
  61.        
  62.         vector $tangent2 = unit( cross( $pNormal, $tangent1 ) );
  63.        
  64.         matrix $m[4][4] = <<
  65.             ($tangent2.x), ($tangent2.y), ($tangent2.z), 0.0;
  66.             ($pNormal.x),  ($pNormal.y),  ($pNormal.z),  0.0;
  67.             ($tangent1.x), ($tangent1.y), ($tangent1.z), 0.0;
  68.             ($pPosition.x), ($pPosition.y), ($pPosition.z), 1.0 >>;
  69.        
  70.         xform -ws -m
  71.             ($m[0][0]) ($m[0][1]) ($m[0][2]) ($m[0][3])
  72.             ($m[1][0]) ($m[1][1]) ($m[1][2]) ($m[1][3])
  73.             ($m[2][0]) ($m[2][1]) ($m[2][2]) ($m[2][3])
  74.             ($m[3][0]) ($m[3][1]) ($m[3][2]) ($m[3][3]) $pObjectName;
  75.        
  76.     }
  77.    
  78.     string $selection[] = `ls -os -fl`;
  79.    
  80.     string $faceNames[] = `filterExpand -selectionMask 34 -expand true $selection`;
  81.    
  82.     string $objectToInstance = $selection[0];
  83.    
  84.     if( `objectType $objectToInstance` == "transform" ) {
  85.        
  86.         string $face;
  87.         for( $face in $faceNames ) {
  88.            
  89.             string $newInstance[] = `instance $objectToInstance`;
  90.            
  91.             vector $position = getFaceCenter( $face );
  92.            
  93.             vector $normal = getFaceNormal( $face );
  94.            
  95.             moveAlign( $newInstance[0], $normal, $position );
  96.            
  97.         }
  98.        
  99.     } else {
  100.        
  101.         print "Please ensure the first object you select is a transform.";
  102.        
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement