// =========================================================================== // 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 normalizeCurve (string $curveName) { if (!`objExists $curveName`) { return; } if (!isAnimCurve($curveName) || `reference -isNodeReferenced $curveName`) { return; } string $normalizerType = "curveNormalizerLinear"; if (`nodeType $curveName` == "animCurveTA") { if (`getAttr ($curveName + ".rotationInterpolation")` == 3) { // We don't deal with quaternions well, so bail here warning ( (uiRes("m_normalizeCurve.kCurveNormalization")) ); return; } $normalizerType = "curveNormalizerAngle"; } int $keyCount = `getAttr -size ($curveName+".ktv")`; if ($keyCount < 2) { // not enough keys to normalize // return; } float $minTime = 1000000000; float $maxTime = -1000000000; int $key; for ($key = 0; $key < $keyCount; $key++) { float $timeVal = `getAttr ($curveName+".ktv["+$key+"].kt")`; $minTime = min ($minTime, $timeVal); $maxTime = max ($maxTime, $timeVal); } for ($key = 0; $key < $keyCount; $key++) { float $timeVal = `getAttr ($curveName+".ktv["+$key+"].kt")`; $minTime = min ($minTime, $timeVal); $maxTime = max ($maxTime, $timeVal); } float $evalTime; float $maxValue = 0.0; for ($evalTime = $minTime; $evalTime <= $maxTime; $evalTime++) { float $timeVals[] = `keyframe -absolute -time $evalTime -query -eval $curveName`; $maxValue = max(abs($maxValue), abs($timeVals[0])); } if (equivalent ($maxValue, 0.0)) { // don't normalize curves that are 0 everywhere. return; } string $destNodes[] = `listConnections -source false -plugs false ($curveName+".output")`; string $destNode = ""; if (size ($destNodes) > 0) { $destNode = $destNodes[0]; } string $selection[] = `ls -selection`; $destNode = $destNodes[0]; if (($destNode != "") && (`nodeType $destNode` == $normalizerType)) { float $currentNormFactor = `getAttr ($destNode+".scalar")`; setAttr ($destNode+".scalar") ($currentNormFactor*$maxValue); } else { string $curveDests[] = `listConnections -source false -plugs true ($curveName+".output")`; string $curveDest = ""; if (size($curveDests) > 0) { $curveDest = $curveDests[0]; } if (($curveDest != "") && (`connectionInfo -isLocked $curveDest`)) { return; } string $multNode = `createNode -name ($curveName + "_Normalizer") $normalizerType`; setAttr ($multNode+".isHistoricallyInteresting") 0; setAttr ($multNode+".scalar") $maxValue; if ($curveDest != "") { disconnectAttr ($curveName+".output") $curveDest; } connectAttr ($curveName+".output") ($multNode+".animInput"); if ($curveDest != "") { connectAttr ($multNode+".output") $curveDest; } } scaleKey -valueScale (1.0/$maxValue) $curveName; if (`bufferCurve -exists $curveName`) { bufferCurve -swap $curveName; scaleKey -valueScale (1.0/$maxValue) $curveName; bufferCurve -swap $curveName; } select -replace $selection; }