// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // Creation Date: October 30, 2016 // // Procedure Name: // straightenUVs // // Description: // UV workflow function used for straightening a UV selection so that // the edge loops are aligned to the U or V axis. // // Input Arguments // axis - String - Straightening axis. "U", "V", "UV" // tolerance - Float - How much an edge angle has to deviate // from the U or V axis before it gets // straightened. // // Return Value: // None. // // Notes: // This script is part of the implementation of Nightshade UV Editor into // Autodesk Maya. // // =========================================================================== global proc texStraightenUVs(string $axis, float $tolerance) { // Validate and save UV selection texCheckSelection("any"); string $originalSel[] = `ls -sl`; select `polyListComponentConversion -tuv`; string $selection[] = `ls -selection`; // Get UV shells string $shellList[] = texGetShells(); int $shellsRemaining = size($shellList); // Shell counters int $shellsTotal = $shellsRemaining; // Begin progressBar global string $gMainProgressBar; progressBar -e -bp -max $shellsTotal $gMainProgressBar; // Perform straighten for every individual shell for ($shell in $shellList) { // Break if cancelled by user if (`progressBar -q -isCancelled $gMainProgressBar`) { warning((uiRes("m_texStraightenUVs.kUvEditorStraightenUVsInteruptMsg"))); break; } // Step progressBar progressBar -e -s 1 $gMainProgressBar; string $edgeList[], $uvEdgeListU[], $uvEdgeListV[], $shellUVList[], $vertexList[]; // Get the UVs which belong to $selection AND the current UV shell $shellUVList = eval("select " + $shell); $shellUVList = `ls -selection $selection`; // Convert selection to edges $vertexList = `polyListComponentConversion -toVertex $shellUVList`; $edgeList = `polyListComponentConversion -toEdge -internal $vertexList`; $edgeList = `ls -flatten $edgeList`; // Divide edges into two straighten groups (horizontal and vertical) for ($edge in $edgeList) { // Convert edge to UVs and flatten string $uvEdgePairs[] = `polyListComponentConversion -toUV $edge `; $uvEdgePairs = `ls -flatten $uvEdgePairs`; // Check uv count in uvEdge. 1 = single coord 2 = uvEdge 4 = shared edge if (size($uvEdgePairs) != 2){ string $pairs[] = `polyEvaluate -uvEdgePairs $edge`; $uvEdgePairs = stringToStringArray($pairs[0], " "); } // Insert the uvEdgePair(s) into either the $uvEdgeListU/V by checking its arctan angle for ($i = 0; $i < size($uvEdgePairs); $i += 2) { string $uvs[]; $uvs[0] = $uvEdgePairs[$i]; $uvs[1] = $uvEdgePairs[$i+1]; // Check if the UVs of the edge belong to $selection AND the current UV shell if (!texShellArrayContains($uvs[0], $shellUVList)) continue; // Get absolute value of the uvEdge angle (to nearest 90 deg) float $angle = texCalculateAngle($uvs[0], $uvs[1]); float $rotationAngle = `abs $angle`; // Determine where to insert the uvEdge (U or V list) string $pair = stringArrayToString($uvs, " "); if (($rotationAngle >= 0) && ($rotationAngle <= $tolerance)){ $uvEdgeListU[size($uvEdgeListU)] = $pair; // U list } else if (($rotationAngle <= 90) && ($rotationAngle >= (90 - $tolerance) )){ $uvEdgeListV[size($uvEdgeListV)] = $pair; // V list } } } // Straighten UV edge loops if (($axis == "UV") || ($axis == "U")){ texStraightenUVEdges("U", $uvEdgeListU); } if (($axis == "UV") || ($axis == "V")){ texStraightenUVEdges("V", $uvEdgeListV); } // Decrease the shells remaining -counter $shellsRemaining -= 1; } // End progressBar progressBar -e -ep $gMainProgressBar; // Select the original selection select $originalSel; }