//Script designed by Spencer Lindsay and written by Robert Suh - Rockstar Games San Diego //From the perspective camera (persp), select a facet and then execute the script. //The persp camera will move, align and fit with the selected face. global proc faceCam() { sl_alignCam2FaceFromSelected(); changeSelectMode -component; setComponentPickMask "Facet" true; } global proc int sl_getTotalFacesSelected() { string $fIndex[] = `filterExpand -sm 34`; // Returns the Polygon Face return `size $fIndex`; } global proc string sl_ripObjectNameFromFace(string $objName) // pCube1.f[3] { string $regularExpr = "\\.f\\[[0-9]+\\]"; string $s1 = `substitute $regularExpr $objName ""`; return $s1; } global proc string sl_ripFaceIndexFromFace(string $objName) // pCube1.f[3] { string $expression = "\\[[0-9]*\\]"; string $result = `match $expression $objName`; string $expression = "[0-9]+"; string $resultx = `match $expression $result`; return $resultx; } global proc sl_alignCam2FaceFromSelected() { int $total = sl_getTotalFacesSelected(); print ("Total Selected = ("+$total+")\n"); if ($total == 1) { string $fIndex[] = `filterExpand -sm 34`; // Returns the Polygon Face string $objName = sl_ripObjectNameFromFace($fIndex[0]); string $faceIndex = sl_ripFaceIndexFromFace($fIndex[0]); sl_alignCam2Face( "persp", $objName , $faceIndex); } else { print "ERROR : You can only select ONE face\n"; } } global proc sl_alignCam2Face( string $camName, string $objectName, int $faceIndex) { float $normal[] = sl_generateNormalOfFace($objectName, $faceIndex); float $centroid[] = sl_getFaceCentroid($objectName, $faceIndex); float $camPos[] = $centroid; float $distance = 10; $camPos[0] = $camPos[0] + ($normal[0]*$distance); $camPos[1] = $camPos[1] + ($normal[1]*$distance); $camPos[2] = $camPos[2] + ($normal[2]*$distance); select $camName; setAttr ($camName+".translateX") $camPos[0]; setAttr ($camName+".translateY") $camPos[1]; setAttr ($camName+".translateZ") $camPos[2]; viewLookAt -pos $centroid[0] $centroid[1] $centroid[2] $camName; string $name = ($objectName+".f["+$faceIndex+"]"); select -r $name ; viewFit $camName; } global proc float[] sl_generateNormalOfFace( string $objectName, int $faceIndex) { // float[] crossProduct( float $v1[], float $v2[], int $normalizeInputs, int $normalizeResult ) // // float $normal[] = crossProduct ($vec1, $vec2, 1 ,1); // return $normal; int $vertices[] = sl_getFaceVertices($objectName,$faceIndex); int $vertIndex; float $pos1[]; float $pos2[]; float $pos3[]; int $count=0; for ($vertIndex in $vertices) { float $vertPos[] = sl_getVertexPosition($objectName,$vertIndex); if ($count == 0) { $pos1[0] = $vertPos[0]; $pos1[1] = $vertPos[1]; $pos1[2] = $vertPos[2]; } if ($count == 1) { $pos2[0] = $vertPos[0]; $pos2[1] = $vertPos[1]; $pos2[2] = $vertPos[2]; } if ($count == 2) { $pos3[0] = $vertPos[0]; $pos3[1] = $vertPos[1]; $pos3[2] = $vertPos[2]; } $count++; } float $ab[]; float $ac[]; $ab[0] = $pos1[0] - $pos2[0]; $ab[1] = $pos1[1] - $pos2[1]; $ab[2] = $pos1[2] - $pos2[2]; $ac[0] = $pos3[0] - $pos2[0]; $ac[1] = $pos3[1] - $pos2[1]; $ac[2] = $pos3[2] - $pos2[2]; float $normal[] = crossProduct ($ac, $ab, 1,1); return $normal; } global proc float[] sl_getFaceCentroid( string $objectName, int $faceIndex) { int $vertices[] = sl_getFaceVertices($objectName,$faceIndex); int $vertIndex; float $average[]; for ($vertIndex in $vertices) { float $vertPos[] = sl_getVertexPosition($objectName,$vertIndex); $average[0] = $average[0] + $vertPos[0]; $average[1] = $average[1] + $vertPos[1]; $average[2] = $average[2] + $vertPos[2]; } int $totalVerts = `size $vertices`; print ("Total Verts = "+$totalVerts+")\n"); $average[0] = $average[0] / $totalVerts; $average[1] = $average[1] / $totalVerts; $average[2] = $average[2] / $totalVerts; return $average; // reutrn the center of the FACE } global proc float[] sl_getVertexPosition(string $objectName, int $vertIndex) { string $name = ($objectName+".vtx["+$vertIndex+"]"); float $pos[] = `pointPosition -w $name`; return $pos; } global proc int sl_isNumber(string $xx) { if (`gmatch $xx "*[0-9]*"`) return 1; return 0; } global proc int[] sl_getFaceVertices( string $objectName, int $faceIndex) { string $name = ($objectName+".f["+$faceIndex+"]"); string $normalString[] = `polyInfo -fv $name`; string $vIndices[]; int $total = `tokenize $normalString[0] " " $vIndices`; print $vIndices; int $final[]; int $x; for ($x =2; $x < $total; $x++) // starts { float $value = (float) $vIndices[$x]; if (sl_isNumber($vIndices[$x])) { $final[ `size $final`] = $vIndices[$x]; } } return $final; } global proc float[] sl_getFaceNormal( string $objectName, int $faceIndex) { string $name = ($objectName+".f["+$faceIndex+"]"); string $normalString[] = `polyInfo -fn $name`; string $vIndices[]; int $total = `tokenize $normalString[0] " " $vIndices`; print $vIndices; float $final[]; int $x; for ($x =2; $x < $total; $x++) // starts { if (sl_isNumber($vIndices[$x])) { $final[ `size $final`] = $vIndices[$x]; } } return $final; }