// =========================================================================== // 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. // =========================================================================== global proc performInterpolatorCreateGroup(string $tplsAndTplGroups[]) // // Description: // Create a new pose interpolator group containing all the items in // $tplsAndTplGroups (-1/-2/Interpolator1/Interpolator2/-3...) // { if (size($tplsAndTplGroups) == 0) $tplsAndTplGroups = getPoseEditorTreeviewSelection(3); // 1. Gather child items for the new created group string $groupItems[]; for ($item in $tplsAndTplGroups) { if (startsWith($item, "-")) // pose interpolator group $groupItems[size($groupItems)] = $item; else { // pose interpolator int $id = `getAttr ($item + ".midLayerId")`; if ($id >= 0) $groupItems[size($groupItems)] = $id; } } // 2. Set child items for the new created group int $directoryIndex = unusedPoseInterpolatorDirectoryIndex(); setPoseInterpolatorDirectoryChildren($directoryIndex, $groupItems); // 3. Update parents for the items of the new group int $groupItemParents[]; for ($item in $tplsAndTplGroups) { string $parentIndexAttr; if (startsWith($item, "-")) // pose interpolator group $parentIndexAttr = "poseInterpolatorManager.poseInterpolatorDirectory[" + stringRemovePrefix($item, "-") + "].parentIndex"; else // pose interpolator $parentIndexAttr = $item + ".midLayerParent"; int $parentIndex = `getAttr $parentIndexAttr`; if ($parentIndex < 0) $parentIndex = 0; if (!intArrayContains($parentIndex, $groupItemParents)) $groupItemParents[size($groupItemParents)] = $parentIndex; setAttr $parentIndexAttr $directoryIndex; } if (size($groupItemParents) == 0) $groupItemParents[0] = 0; setAttr ("poseInterpolatorManager.poseInterpolatorDirectory[" + $directoryIndex + "].parentIndex") $groupItemParents[0]; // 3. Insert the new group into the grand parent, and remove the group items from their old parent for ($i = 0; $i < size($groupItemParents); $i++) { int $childIndices[] = `getAttr ("poseInterpolatorManager.poseInterpolatorDirectory[" + $groupItemParents[$i] + "].childIndices")`; string $buffer[] = stringToStringArray(intArrayToString($childIndices, ","), ","); if ($i == 0) { if (size($groupItems) > 0) { int $indexToInsert = stringArrayFind($groupItems[0], 0, $buffer); stringArrayInsertAtIndex($indexToInsert, $buffer, stringAddPrefix($directoryIndex, "-")); } else $buffer[size($buffer)] = stringAddPrefix($directoryIndex, "-"); } $buffer = stringArrayRemove($groupItems, $buffer); setPoseInterpolatorDirectoryChildren($groupItemParents[$i], $buffer); } // 4. Set the new group's name string $directoryName = unusedPoseInterpolatorDirectoryName($groupItemParents[0]); setAttr ("poseInterpolatorManager.poseInterpolatorDirectory[" + $directoryIndex + "].directoryName") -type "string" $directoryName; } global proc int unusedPoseInterpolatorDirectoryIndex() { int $result = 0; int $indexArray[] = `getAttr -multiIndices "poseInterpolatorManager.poseInterpolatorDirectory"`; for ($index in $indexArray) { if ($index > $result) return $result; $result = $index + 1; } return $result; } global proc string unusedPoseInterpolatorDirectoryName(string $parentIndex) { string $usedNames[]; int $childIndices[] = `getAttr ("poseInterpolatorManager.poseInterpolatorDirectory[" + $parentIndex + "].childIndices")`; for ($child in $childIndices) { if ($child < 0) $usedNames[size($usedNames)] = `getAttr ("poseInterpolatorManager.poseInterpolatorDirectory[" + (-$child) + "].directoryName")`; } for ($i = 1; ; $i++) { string $name = "Group " + $i; if (!stringArrayContains($name, $usedNames)) return $name; } } global proc setPoseInterpolatorDirectoryChildren(string $directoryIndex, string $childItems[]) { string $setChildCmd = "setAttr poseInterpolatorManager.poseInterpolatorDirectory[" + $directoryIndex + "].childIndices -type Int32Array " + size($childItems); for ($item in $childItems) { $setChildCmd += " " + $item; } $setChildCmd += ";"; eval($setChildCmd); }