/* Script Name:acPlanar.mel Author: Allen Curtis Date: May 2007 Description: Changes angle between 2 vertices to match angle of 2 previously selected vertices. Aids in making non-planar polygons planar. Details: - Two textfields show xyz angles of two edges based on selected vertices. - Third textfield shows difference in angles on x, y, or z axis. - Rotate edge (two vertices) around desired axis by amount of difference or enter a specific desired angle for rotation. For instance, enter negative value to rotate in opposite direction. - Change pivot for rotation from center to either end.Usually you will want to rotate from one end of edge instead of from center. - Convert selection from vertices to edges or faces. - In Maya 8.5, toggle view of non-planar polygons. */ //Add window. global proc acPlanar(){ if (`window -exists acPlanarWin`) deleteUI acPlanarWin; window -t "acPlanar" acPlanarWin; string $form = `formLayout -numberOfDivisions 100`; //Keep specific window size when reopened. window -edit -widthHeight 514 402 acPlanarWin; string $columnTop = `columnLayout -adjustableColumn false`; text -label "Select 2 Vertices." -align "center"; button -label "Angle" -align "center" -width 125 -command "getAngle"; textField -width 450 AngleTextField; text -label "Select 2 Vertices." -align "center"; button -label "2nd Angle" -align "center" -width 125 -command "getAngle2"; textField -width 450 Angle2TextField; radioButtonGrp -numberOfRadioButtons 3 -columnAlign 1 "left" -columnWidth 1 80 -label "Pivot" -labelArray3 "Left Pivot" "Center Pivot" "Right Pivot" -select 2 radioPivot; //Parent columnLayout to the formLayout. string $columnPlanar = `columnLayout -adjustableColumn false -parent $form`; button -label "View Non-Planar" -align "center" -width 140 -command "viewNPlanar"; //Parent columnLayout to the formLayout. string $columnDiff = `columnLayout -adjustableColumn false -parent $form`; textField -width 250 DifferenceTextField; button -label "Get X Difference" -align "center" -width 170 -command "getDifferenceX"; button -label "Get Y Difference" -align "center" -width 170 -command "getDifferenceY"; button -label "Get Z Difference" -align "center" -width 170 -command "getDifferenceZ"; //Parent another columnLayout to the formLayout. string $columnRotate = `columnLayout -adjustableColumn false -parent $form`; button -label "Rotate X" -align "center" -width 170 -command "matchXAxis"; button -label "Rotate Y" -align "center" -width 170 -command "matchYAxis"; button -label "Rotate Z" -align "center" -width 170 -command "matchZAxis"; //Parent another columnLayout to the formLayout. string $columnSelections = `columnLayout -adjustableColumn false -parent $form`; radioButtonGrp -numberOfRadioButtons 3 -columnAlign 1 "left" -columnWidth 1 80 -label "Selection" -labelArray3 "Faces" "Edges" "Vertices" -select 3 radioConvertSelection; //Parent another columnLayout to the formLayout. string $columnClose = `columnLayout -adjustableColumn false -parent $form`; $close = `button -l "Close" -width 125 -c "deleteUI acPlanarWin"`; //Parent another columnLayout to the formLayout. string $columnBottom = `columnLayout -adjustableColumn false -parent $form`; button -label "Convert Selection" -align "center" -width 170 -command "convertSelectionView"; formLayout -edit -attachForm $columnTop "top" 5 -attachForm $columnTop "left" 20 -attachControl $columnTop "bottom" 10 $columnPlanar -attachNone $columnPlanar "top" -attachForm $columnPlanar "right" 20 -attachControl $columnPlanar "bottom" 10 $columnDiff -attachNone $columnDiff "top" -attachForm $columnDiff "left" 70 -attachControl $columnDiff "bottom" 5 $columnSelections -attachNone $columnRotate "top" -attachForm $columnRotate "right" 70 -attachControl $columnRotate "bottom" 5 $columnSelections -attachNone $columnSelections "top" -attachForm $columnSelections "left" 5 -attachControl $columnSelections "bottom" 5 $columnBottom -attachNone $columnBottom "top" -attachForm $columnBottom "left" 100 -attachForm $columnBottom "bottom" 20 -attachNone $columnClose "top" -attachForm $columnClose "right" 100 -attachForm $columnClose "bottom" 20 $form; showWindow acPlanarWin; } global proc getAngle() { float $radian3; float $radian2; float $radian1; float $degree1; float $degree2; float $degree3; string $xyzAngle; string $angle; float $xSlope; float $ySlope; float $zSlope; float $xDivisor1; float $xDivisor2; float $yDivisor1; float $yDivisor2; float $zDivisor1; float $zDivisor2; string $sel[]=`ls -sl -fl`; if(size($sel)==2) {//outer if else float $tail[]= `pointPosition $sel[0]`; float $head[]=`pointPosition $sel[1]`; //Slope formula: (change in y)/(change in x) etc. $xDivisor1 = $head[1] - $tail[1]; $xDivisor2 = $head[2] - $tail[2]; $yDivisor1 = $head[2] - $tail[2]; $yDivisor2 = $head[0] - $tail[0]; $zDivisor1 = $head[1] - $tail[1]; $zDivisor2 = $head[0] - $tail[0]; //Catch a divide-by-zero error. Changes slope to 0 instead of divisor. if($xDivisor1==0||$xDivisor2==0) {//inner if else $xSlope = 0; }else //inner if else $xSlope = ($xDivisor1)/($xDivisor2); if($yDivisor1==0||$yDivisor2==0) {//inner if else $ySlope = 0; }else //inner if else $ySlope = ($yDivisor1)/($yDivisor2); if($zDivisor1==0||$zDivisor2==0) {//inner if else $zSlope = 0; }else //inner if else $zSlope = ($zDivisor1)/($zDivisor2); //Find the angle of slope using arc tan. $radian3 = `atan $xSlope`; $radian2 = `atan $ySlope`; $radian1 = `atan $zSlope`; $degree1 = `rad_to_deg $radian1`; $degree2 = `rad_to_deg $radian2`; $degree3 = `rad_to_deg $radian3`; $xyzAngle = $degree3 + " " + $degree2 + " " + $degree1; $angle = `textField -edit -text $xyzAngle AngleTextField`; }else{//outer if else confirmDialog -title "Component Error" -message "Select two vertices." -button "OK"; }//outer if else ends } global proc getAngle2() { float $radian3; float $radian2; float $radian1; float $degree1; float $degree2; float $degree3; string $xyzAngle; string $angle; float $xSlope; float $ySlope; float $zSlope; float $xDivisor1; float $xDivisor2; float $yDivisor1; float $yDivisor2; float $zDivisor1; float $zDivisor2; string $sel[]=`ls -sl -fl`; if(size($sel)==2) {//outer if else float $tail[]= `pointPosition $sel[0]`; float $head[]=`pointPosition $sel[1]`; //Slope formula: (change in y)/(change in x) etc. $xDivisor1 = $head[1] - $tail[1]; $xDivisor2 = $head[2] - $tail[2]; $yDivisor1 = $head[2] - $tail[2]; $yDivisor2 = $head[0] - $tail[0]; $zDivisor1 = $head[1] - $tail[1]; $zDivisor2 = $head[0] - $tail[0]; //Catch a divide-by-zero error. Changes slope to 0 instead of divisor. if($xDivisor1==0||$xDivisor2==0) {//inner if else $xSlope = 0; }else //inner if else $xSlope = ($xDivisor1)/($xDivisor2); if($yDivisor1==0||$yDivisor2==0) {//inner if else $ySlope = 0; }else //inner if else $ySlope = ($yDivisor1)/($yDivisor2); if($zDivisor1==0||$zDivisor2==0) {//inner if else $zSlope = 0; }else //inner if else $zSlope = ($zDivisor1)/($zDivisor2); //Find the angle of slope using arc tan. $radian3 = `atan $xSlope`; $radian2 = `atan $ySlope`; $radian1 = `atan $zSlope`; $degree1 = `rad_to_deg $radian1`; $degree2 = `rad_to_deg $radian2`; $degree3 = `rad_to_deg $radian3`; $xyzAngle = $degree3 + " " + $degree2 + " " + $degree1; $angle = `textField -edit -text $xyzAngle Angle2TextField`; }else{//outer if else confirmDialog -title "Component Error" -message "Select two vertices." -button "OK"; }//outer if else ends } global proc getDifferenceX() { string $getText = `textField -q -tx AngleTextField`; string $Angle1[]; //Separate 3 values in textField into an array. //returns integer, amount of tokens int $angle1Token = tokenize($getText, $Angle1); float $angle1Float[3]; //Convert string to float. $angle1Float[0] =$Angle1[0]; $angle1Float[1] =$Angle1[1]; $angle1Float[2] =$Angle1[2]; $getText = `textField -q -tx Angle2TextField`; string $Angle2[]; //Separate 3 values in textField into an array. int $angle2Token = tokenize($getText, $Angle2); float $angle2Float[3]; $angle2Float[0] =$Angle2[0]; $angle2Float[1] =$Angle2[1]; $angle2Float[2] =$Angle2[2]; //Get absolute value. float $difference1 = `abs $angle1Float[0]`; float $difference2 = `abs $angle2Float[0]`; if($difference1>$difference2) { float $planar = $difference1 - $difference2; string $angle = `textField -edit -text $planar DifferenceTextField`; }else if($difference2>$difference1){ float $planar = $difference2 - $difference1; string $angle = `textField -edit -text $planar DifferenceTextField`; }else confirmDialog -title "No Difference" -message "Same angle or 0 value." -button "OK"; } global proc getDifferenceY() { string $getText = `textField -q -tx AngleTextField`; string $Angle1[]; //Separate 3 values in textField into an array. //returns integer, amount of tokens int $angle1Token = tokenize($getText, $Angle1); float $angle1Float[3]; //Convert string to float. $angle1Float[0] =$Angle1[0]; $angle1Float[1] =$Angle1[1]; $angle1Float[2] =$Angle1[2]; $getText = `textField -q -tx Angle2TextField`; string $Angle2[]; //Separate 3 values in textField into an array. int $angle2Token = tokenize($getText, $Angle2); float $angle2Float[3]; $angle2Float[0] =$Angle2[0]; $angle2Float[1] =$Angle2[1]; $angle2Float[2] =$Angle2[2]; float $difference1 = `abs $angle1Float[1]`; float $difference2 = `abs $angle2Float[1]`; if($difference1>$difference2) { float $planar = $difference1 - $difference2; string $angle = `textField -edit -text $planar DifferenceTextField`; }else if($difference2>$difference1){ float $planar = $difference2 - $difference1; string $angle = `textField -edit -text $planar DifferenceTextField`; }else confirmDialog -title "No Difference" -message "Same angle or 0 value." -button "OK"; } global proc getDifferenceZ() { string $getText = `textField -q -tx AngleTextField`; string $Angle1[]; //Separate 3 values in textField into an array. //returns integer, amount of tokens int $angle1Token = tokenize($getText, $Angle1); float $angle1Float[3]; //Convert string to float. $angle1Float[0] =$Angle1[0]; $angle1Float[1] =$Angle1[1]; $angle1Float[2] =$Angle1[2]; $getText = `textField -q -tx Angle2TextField`; string $Angle2[]; //Separate 3 values in textField into an array. int $angle2Token = tokenize($getText, $Angle2); float $angle2Float[3]; $angle2Float[0] =$Angle2[0]; $angle2Float[1] =$Angle2[1]; $angle2Float[2] =$Angle2[2]; float $difference1 = `abs $angle1Float[2]`; float $difference2 = `abs $angle2Float[2]`; if($difference1>$difference2) { float $planar = $difference1 - $difference2; string $angle = `textField -edit -text $planar DifferenceTextField`; }else if($difference2>$difference1){ float $planar = $difference2 - $difference1; string $angle = `textField -edit -text $planar DifferenceTextField`; }else confirmDialog -title "No Difference" -message "Same angle or 0 value." -button "OK"; } global proc matchXAxis() { string $sel[]=`ls -sl -fl`; float $tail[]= `pointPosition $sel[0]`; float $head[]=`pointPosition $sel[1]`; float $pivot[]; int $radioPivotValue = `radioButtonGrp -q -sl radioPivot`; if($radioPivotValue==1){ //Rotation Pivot at 1st vertex. $pivot[0] = $tail[0]; $pivot[1] = $tail[1]; $pivot[2] = $tail[2]; }else if($radioPivotValue==2){ //Rotation Pivot at midpoint. //pivot between two components //based on formula for finding midpoint $pivot[0] = ($tail[0] + $head[0])/2; $pivot[1] = ($tail[1] + $head[1])/2; $pivot[2] = ($tail[2] + $head[2])/2; }else if($radioPivotValue==3){ //Rotation Pivot at 2nd vertex. $pivot[0] = $head[0]; $pivot[1] = $head[1]; $pivot[2] = $head[2]; } //temporary pivot string $centerX = $pivot[0] + "cm"; string $centerY = $pivot[1] + "cm"; string $centerZ = $pivot[2] + "cm"; string $getAngleText = `textField -q -tx DifferenceTextField`; float $matchAngle = $getAngleText; string $pivot_X = `rotate -r -p $centerX $centerY $centerZ -os $matchAngle 0 0`; } global proc matchYAxis() { string $sel[]=`ls -sl -fl`; float $tail[]= `pointPosition $sel[0]`; float $head[]=`pointPosition $sel[1]`; float $pivot[]; int $radioPivotValue = `radioButtonGrp -q -sl radioPivot`; if($radioPivotValue==1){ //Rotation Pivot at 1st vertex. $pivot[0] = $tail[0]; $pivot[1] = $tail[1]; $pivot[2] = $tail[2]; }else if($radioPivotValue==2){ //Rotation Pivot at midpoint. //pivot between two components //based on formula for finding midpoint $pivot[0] = ($tail[0] + $head[0])/2; $pivot[1] = ($tail[1] + $head[1])/2; $pivot[2] = ($tail[2] + $head[2])/2; }else if($radioPivotValue==3){ //Rotation Pivot at 2nd vertex. $pivot[0] = $head[0]; $pivot[1] = $head[1]; $pivot[2] = $head[2]; } //temporary pivot string $centerX = $pivot[0] + "cm"; string $centerY = $pivot[1] + "cm"; string $centerZ = $pivot[2] + "cm"; string $getAngleText = `textField -q -tx DifferenceTextField`; float $matchAngle = $getAngleText; string $pivot_Y = `rotate -r -p $centerX $centerY $centerZ -os 0 $matchAngle 0`; } global proc matchZAxis() { string $sel[]=`ls -sl -fl`; float $tail[]= `pointPosition $sel[0]`; float $head[]=`pointPosition $sel[1]`; float $pivot[]; int $radioPivotValue = `radioButtonGrp -q -sl radioPivot`; if($radioPivotValue==1){ //Rotation Pivot at 1st vertex. $pivot[0] = $tail[0]; $pivot[1] = $tail[1]; $pivot[2] = $tail[2]; }else if($radioPivotValue==2){ //Rotation Pivot at midpoint. //pivot between two components //based on formula for finding midpoint $pivot[0] = ($tail[0] + $head[0])/2; $pivot[1] = ($tail[1] + $head[1])/2; $pivot[2] = ($tail[2] + $head[2])/2; }else if($radioPivotValue==3){ //Rotation Pivot at 2nd vertex. $pivot[0] = $head[0]; $pivot[1] = $head[1]; $pivot[2] = $head[2]; } //temporary pivot string $centerX = $pivot[0] + "cm"; string $centerY = $pivot[1] + "cm"; string $centerZ = $pivot[2] + "cm"; string $getAngleText = `textField -q -tx DifferenceTextField`; float $matchAngle = $getAngleText; string $pivot_Z = `rotate -r -p $centerX $centerY $centerZ -os 0 0 $matchAngle`; } /* Convert Selection commands added for easy access. */ global proc convertSelectionView() { int $radioSelection = `radioButtonGrp -q -sl radioConvertSelection`; if($radioSelection==1){ ConvertSelectionToFaces; PolySelectConvert 1; setComponentPickMask "Facet" true; updateComponentSelectionMasks; updateObjectSelectionMasks; }else if($radioSelection==2){ ConvertSelectionToEdges; PolySelectConvert 2; setComponentPickMask "Line" true; updateComponentSelectionMasks; updateObjectSelectionMasks; }else if($radioSelection==3){ ConvertSelectionToVertices; PolySelectConvert 3; setComponentPickMask "Point" true; updateComponentSelectionMasks; updateObjectSelectionMasks; } } global proc viewNPlanar() { //Comment out one or the other command. //In Maya 8.5, toggle view of non-planar polygons. setPolygonDisplaySettings("nonPlanar"); //In Maya 7 or earlier, open Custom Polygon Display Options //to view non-planar. //polysDisplaySetup 1; }