// =========================================================================== // 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: November 26, 2016 // // Procedure Name: // texSortUVsByValue // // Description: // UV workflow function used for sorting UV shells by their coordinate // value (U or V). Used by linearAlignUVs(). // // Input Arguments // uvList - String[] - A list of UV components // axis - String - Value to sort by. Either "U" or "V" // // Return Value: // String[] - The UV list sorted in ascending order. Each // element in the list starts with a coordinates // prefix and is separated by % (see notes below). // // Notes: // This script is part of the implementation of Nightshade UV Editor into // Autodesk Maya. This script is sourced by other scripts. Do not run it // directly. Example of returned list: // 0.250000:0.850000%pCube1.map[6] // // =========================================================================== global proc string[] texSortUVsByValue(string $uvList[], string $axis) { // MEL has no dictionaries so we will use the coordinates as a prefix to // the shell string in order to perform sorting. float $value[]; string $prefixList[]; // Add coordinate value as prefixes to the UV list if ($axis == "U"){ for ($i = 0; $i < `size($uvList)`; $i++){ $value = `polyEditUV -q $uvList[$i]`; $value[0] = texRoundOff($value[0], 6); $value[1] = texRoundOff($value[1], 6); string $valueUString = (string)$value[0]; string $valueVString = (string)$value[1]; // Convert int value to float with 6 trailing zeroes... if (`gmatch $valueUString "*.*"` == false){ $valueUString += ".000000"; } else { // ...or add more trailing zeroes if necessary string $decimals = `match "\\.(.*)" $valueUString`; int $trailingZeroes = 7 - `size($decimals)`; for ($j = 0; $j < $trailingZeroes; $j++){ $valueUString += "0"; } } if (`gmatch $valueVString "*.*"` == false){ $valueVString += ".000000"; } else { string $decimals = `match "\\.(.*)" $valueVString`; int $trailingZeroes = 7 - `size($decimals)`; for ($j = 0; $j < $trailingZeroes; $j++){ $valueVString += "0"; } } // Append prefix string $prefixList[$i] = ($valueUString + ":" + $valueVString + "%" + $uvList[$i]); } } else { // "V" for ($i = 0; $i < `size($uvList)`; $i++){ $value = `polyEditUV -q $uvList[$i]`; $value[0] = texRoundOff($value[0], 6); $value[1] = texRoundOff($value[1], 6); string $valueUString = (string)$value[0]; string $valueVString = (string)$value[1]; // Convert int value to float with 6 trailing zeroes... if (`gmatch $valueUString "*.*"` == false){ $valueUString += ".000000"; } else { // ...or add more trailing zeroes if necessary string $decimals = `match "\\.(.*)" $valueUString`; int $trailingZeroes = 7 - `size($decimals)`; for ($j = 0; $j < $trailingZeroes; $j++){ $valueUString += "0"; } } if (`gmatch $valueVString "*.*"` == false){ $valueVString += ".000000"; } else { string $decimals = `match "\\.(.*)" $valueVString`; int $trailingZeroes = 7 - `size($decimals)`; for ($j = 0; $j < $trailingZeroes; $j++){ $valueVString += "0"; } } // Append prefix string $prefixList[$i] = ($valueVString + ":" + $valueUString + "%" + $uvList[$i]); } } // Sort the list. string $sortedList[] = `sort($prefixList)`; if ($axis == "V"){ // U and V values are flipped in prefix, so flip them back for ($i = 0; $i < `size($sortedList)`; $i++){ string $uvElements[] = stringToStringArray($sortedList[$i], "%"); string $uvCoords[] = stringToStringArray($uvElements[0], ":"); string $temp = $uvCoords[0]; $uvCoords[0] = $uvCoords[1]; $uvCoords[1] = $temp; $uvElements[0] = stringArrayToString($uvCoords, ":"); $sortedList[$i] = $uvElements[0] + "%" + $uvElements[1]; } } return $sortedList; }