// =========================================================================== // 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 9, 2017 // // Description: // This is a helper script to perform the filterCurve -f keyReducer // command using the various options that have been set. // // Input Arguments: // int action 0 - just execute the command // 1 - show the option box dialog // 2 - return the drag command // // Return Value: // None. // proc setOptionVars (int $forceFactorySettings) { keySetOptionBoxCommon( { "keyReducer", "unknown", "setOptionVars", $forceFactorySettings } ); // Override default WhichRange to 4 (Selection) // time range: 1: all, 2: playbackRange, 3: start/end, 4: selection if( $forceFactorySettings || !`optionVar -exists keyReducerWhichRange` ) { optionVar -intValue keyReducerWhichRange 4; } // precision // if ($forceFactorySettings || !`optionVar -exists keyReducerPrecisionMode`) { optionVar -intValue keyReducerPrecisionMode 2; } if ($forceFactorySettings || !`optionVar -exists keyReducerPrecisionAbsolute`) { optionVar -floatValue keyReducerPrecisionAbsolute 1.0; } if ($forceFactorySettings || !`optionVar -exists keyReducerPrecisionPercent`) { optionVar -floatValue keyReducerPrecisionPercent 1.0; } // keySync // if ($forceFactorySettings || !`optionVar -exists keyReducerKeySync`) { optionVar -intValue keyReducerKeySync 0; } // preserveKeyTangent // keySetOptionPreserveTangents( {"keyReducer", "setOptionVars", $forceFactorySettings} ); } global proc keyReducerSetup (string $parent, string $selectionConnection, int $forceFactorySettings) { // Retrieve the option settings // setOptionVars ($forceFactorySettings); setParent $parent; keySetOptionBoxCommon( { "keyReducer", $selectionConnection, "setup", 0 } ); // precision // int $precisionMode = `optionVar -query keyReducerPrecisionMode`; radioButtonGrp -edit -select $precisionMode keyReducerPrecisionMode; float $precisionAbsolute = `optionVar -query keyReducerPrecisionAbsolute`; floatFieldGrp -edit -value1 $precisionAbsolute keyReducerPrecisionAbsolute; float $precisionPercent = `optionVar -query keyReducerPrecisionPercent`; floatSliderGrp -edit -value $precisionPercent keyReducerPrecisionPercent; keyReducerPrecisionModeChanged(); // keySync // int $keySync = `optionVar -query keyReducerKeySync`; checkBoxGrp -edit -value1 $keySync keyReducerKeySync; // preserveKeyTangent // keySetOptionPreserveTangents( {"keyReducer", "setup"} ); keyReducerWidgetsEnable ($selectionConnection); } global proc keyReducerCallback( string $parent, int $doIt, string $selectionConnection, int $performAction ) { setParent $parent; keySetOptionBoxCommon( { "keyReducer", $selectionConnection, "callback" } ); // precision // optionVar -intValue keyReducerPrecisionMode `radioButtonGrp -query -sl keyReducerPrecisionMode`; optionVar -floatValue keyReducerPrecisionAbsolute `floatFieldGrp -query -value1 keyReducerPrecisionAbsolute`; optionVar -floatValue keyReducerPrecisionPercent `floatSliderGrp -query -value keyReducerPrecisionPercent`; // keySync // optionVar -intValue keyReducerKeySync `checkBoxGrp -query -value1 keyReducerKeySync`; // preserveKeyTangent // keySetOptionPreserveTangents( {"keyReducer", "callback"} ); if( $doIt ) { if( keyReducerPreviewToolActive() ) { CompleteCurrentTool(); } else { performKeyReducer( $performAction, $selectionConnection); string $tmpCmd = "performKeyReducer "; $tmpCmd += "\"" + $performAction + "\", "; $tmpCmd += "\"" + $selectionConnection + "\""; addToRecentCommandQueue $tmpCmd "KeyReducer"; } } } // Interactive preview support global proc int keyReducerPreviewToolActive() // // Description: // Returns 1 if the preview tool is active, 0 otherwise. // { string $tool = filterKeyReducerTool(); return (`currentCtx` == $tool); } global proc keyReducerPreviewInit( int $state ) // // Description: // Initialize/uninitialize the preview tool depending // on $state. This initialization is responsible for // installing change callbacks on the widgets to invoke // the ($cmd + "PreviewUpdate") callback and update UI // enable states. // { keySetOptionPreviewInit( "keyReducer", $state ); string $cb = ""; if( $state ) { $cb = "keyReducerPreviewUpdate"; } // Check for existence of UI widgets before updating in case this is invoked // during optionBox UI deletion. if( `floatFieldGrp -ex keyReducerPrecisionAbsolute` ) { floatFieldGrp -e -dragCommand $cb -changeCommand $cb keyReducerPrecisionAbsolute; } if( `floatSliderGrp -ex keyReducerPrecisionPercent` ) { floatSliderGrp -e -dragCommand $cb -changeCommand $cb keyReducerPrecisionPercent; } if( `checkBoxGrp -ex keyReducerKeySync` ) { checkBoxGrp -e -cc $cb keyReducerKeySync; } keySetOptionPreserveTangents( {"keyReducer", "previewInit", $cb} ); string $applyBtn = getOptionBoxApplyBtn(); int $applyEnable = !$state; button -e -enable $applyEnable $applyBtn; } global proc keyReducerPreviewEnable() // // Description: // This callback is invoked when the preview option widget // is checked on. Initialize the preview tool and set it // as the active tool context. // { keyReducerPreviewInit(1); keyReducerPreviewUpdate(); // Enable filterButterworthCtx string $tool = filterKeyReducerTool(); if( `currentCtx` != $tool ) { setToolTo $tool; } } global proc keyReducerPreviewUpdate() // // Description: // This callback is invoked when preview relevant widget // values change. This update call is responsible for // updating the tool context with the tool parameters. // { string $tool = filterKeyReducerTool(); // Update the optionVars global string $gOptionBox; keyReducerCallback( $gOptionBox, false, "", 0 ); // Update filterButterworthCtx from optionVars int $whichRange = `optionVar -q keyReducerWhichRange`; string $range = `optionVar -q keyReducerRange`; float $startTime = `playbackOptions -q -min`; float $endTime = `playbackOptions -q -max`; int $selectedKeys = 0; int $mode = `optionVar -q keyReducerPrecisionMode`; float $preAbs = `optionVar -q keyReducerPrecisionAbsolute`; float $prePer = `optionVar -q keyReducerPrecisionPercent`; float $pre = $prePer; if( $mode == 1 ) { $pre = $preAbs; } int $sync = `optionVar -q keyReducerKeySync`; if( $whichRange == 4 ) { $selectedKeys = 1; } else if( $whichRange > 2 ) { string $buff[]; tokenize $range ":" $buff; $startTime = $buff[0]; $endTime = $buff[1]; } int $preserveTangents[] = keySetOptionPreserveTangents( {"keyReducer", "queryOptionVars"} ); int $preserveFixed = $preserveTangents[0]; int $preserveLinear = $preserveTangents[1]; int $preserveFlat = $preserveTangents[2]; int $preserveSmooth = $preserveTangents[3]; int $preserveStep = $preserveTangents[4]; int $preserveClamped = $preserveTangents[5]; int $preservePlateau = $preserveTangents[6]; int $preserveStepNext = $preserveTangents[7]; int $preserveAuto = $preserveTangents[8]; string $cmd = "filterKeyReducerCtx -e"; $cmd += " -startTime "; $cmd += $startTime; $cmd += " -endTime "; $cmd += $endTime; $cmd += " -selectedKeys "; $cmd += $selectedKeys; $cmd += " -precisionMode "; $cmd += ($mode-1); // keyReducerPrecisionMode is 1-based index. $cmd += " -precision "; $cmd += $pre; $cmd += " -keySync "; $cmd += $sync; if( $preserveFixed ) { $cmd += " -preserveKeyTangent \"fixed\""; } if( $preserveLinear ) { $cmd += " -preserveKeyTangent \"linear\""; } if( $preserveFlat ) { $cmd += " -preserveKeyTangent \"flat\""; } if( $preserveSmooth ) { $cmd += " -preserveKeyTangent \"smooth\""; } if( $preserveStep ) { $cmd += " -preserveKeyTangent \"step\""; } if( $preserveClamped ) { $cmd += " -preserveKeyTangent \"clamped\""; } if( $preservePlateau ) { $cmd += " -preserveKeyTangent \"plateau\""; } if( $preserveStepNext ) { $cmd += " -preserveKeyTangent \"stepnext\""; } if( $preserveAuto ) { $cmd += " -preserveKeyTangent \"auto\""; } $cmd += (" " + $tool); eval($cmd); } global proc keyReducerPreviewDisable() // // Description: // This callback is invoked when the preview option widget // is checked off. Uninitialize the preview tool and cancel // the tool context. // { string $tool = filterKeyReducerTool(); if( `currentCtx` == $tool ) { escapeCurrentTool(); } keyReducerPreviewInit(0); } // Widgets global proc keyReducerPrecisionModeChanged() { int $mode = `radioButtonGrp -q -sl keyReducerPrecisionMode`; // Update the optionVar optionVar -intValue keyReducerPrecisionMode $mode; // Update the visible preview widget int $isAbs = 0; if( $mode == 1 ) { $isAbs = 1; } floatFieldGrp -e -vis $isAbs -en $isAbs keyReducerPrecisionAbsolute; floatSliderGrp -e -vis (!$isAbs) -en (!$isAbs) keyReducerPrecisionPercent; if( keyReducerPreviewToolActive() ) { keyReducerPreviewUpdate(); } } global proc keyReducerWidgetsEnable(string $selectionConnection) { keySetOptionBoxCommon( { "keyReducer", $selectionConnection, "enable" } ); int $previewActive = keyReducerPreviewToolActive(); string $applyBtn = getOptionBoxApplyBtn(); int $applyEnable = !$previewActive; button -e -enable $applyEnable $applyBtn; } proc string keyReducerWidgets( string $tabLayout, string $selectionConnection ) { setParent $tabLayout; string $tabForm = `columnLayout -adjustableColumn true`; keySetOptionBoxCommon( { "keyReducer", $selectionConnection, "widgets", 1, 0, 1, 1, 1, 1 } ); radioButtonGrp -label (uiRes("m_performKeyReducer.kPrecisionMode")) -label1 (uiRes("m_performKeyReducer.kAbsolute")) -label2 (uiRes("m_performKeyReducer.kPercent")) -nrb 2 -sl 2 -cc "keyReducerPrecisionModeChanged" -ann (uiRes("m_performKeyReducer.kPrecisionModeAnnotation")) keyReducerPrecisionMode; floatFieldGrp -label (uiRes("m_performKeyReducer.kPrecision")) -nf 1 -precision 3 -value1 1.0 -step 0.1 -vis 0 -en 0 -ann (uiRes("m_performKeyReducer.kPrecisionAbsoluteAnnotation")) keyReducerPrecisionAbsolute; floatSliderGrp -label (uiRes("m_performKeyReducer.kPrecision")) -f 1 -pre 3 -v 1.0 -min 0.0 -max 100.0 -fieldMinValue 0.0 -fieldMaxValue 1000.0 -fieldStep 0.1 -step 1.0 -vis 1 -en 1 -ann (uiRes("m_performKeyReducer.kPrecisionPercentageAnnotation")) keyReducerPrecisionPercent; separator; checkBoxGrp -label (uiRes("m_performKeyReducer.kKeySync")) -value1 0 -ann (uiRes("m_performKeyReducer.kSyncKeys")) keyReducerKeySync; keySetOptionPreserveTangents( {"keyReducer", "widgets"} ); int $previewActive = keyReducerPreviewToolActive(); keyReducerPreviewInit($previewActive); return $tabForm; } global proc keyReducerDoSelectionChanged(string $selectionConnection) // // Description: // Since timeRange is irrelevant when there are // picked keyframes, collapse the timeRange // when there are. (This is hooked in to a SelectionChanged // trigger.) // { keySetOptionBoxCommon( { "keyReducer", $selectionConnection, "selectionChanged" } ); keyReducerWidgetsEnable ($selectionConnection); } proc keyReducerOptions( string $selectionConnection, int $performAction ) { // Customisation options // // Name of the command for this option box (think of it as the base class) string $commandName = "keyReducer"; // Title for the option box window string $optionBoxTitle = (uiRes("m_performKeyReducer.kKeyReducerCurveOptions")); // Title for the apply button string $applyTitle = (uiRes("m_performKeyReducer.kApplyAndClose")); // Build the option box "methods" // string $callback = ($commandName + "Callback"); string $setup = ($commandName + "Setup"); // Build the window, with a tab layout // string $widgetList[] = `getStandardWindow $optionBoxTitle 0 "noOptions"`; setUITemplate -pushTemplate DefaultTemplate; // Make the form invisible while we create the widgets in the window // formLayout -e -vis false $widgetList[1]; // Attach each tab // keyReducerWidgets $widgetList[2] $selectionConnection; // Attach the standard buttons // string $buttonList[] = `addStandardButtons $commandName $applyTitle $widgetList[1] $widgetList[2] "noOptions"`; // attach commands to the standard buttons // // Save // button -e -c ($callback + " " + $widgetList[0] + " false \"\" " + $performAction + "; hideOptionBox()") $buttonList[3]; // Close // button -edit -label (uiRes("m_performKeyReducer.kCancel")) -command ("keyReducerPreviewDisable; hideOptionBox") $buttonList[2]; // Reset // button -edit -command ($setup + " " + $widgetList[0] + " " + $selectionConnection + " true") $buttonList[1]; // Do It // button -edit -command ($callback + " " + $widgetList[0] + " true \"" + $selectionConnection + "\"" + $performAction) $buttonList[0]; // Preview close callback keySetOptionPreviewOnClose( "keyReducerPreviewDisable" ); // Make the form layout visible so we can see what we built, and // reset the template // formLayout -e -vis true $widgetList[1]; setUITemplate -popTemplate; // Customize the 'Help' menu item text. // This is called from 3 different places so look at the selection // connection to try to figure out which one. // if (match ("graphEditor", $selectionConnection) != "") { setOptionBoxHelpTag( "GraphKeyReducerCurve" ); } else if (match ("dopeSheet", $selectionConnection) != "") { setOptionBoxHelpTag( "DopeKeyReducerCurve" ); } else { setOptionBoxHelpTag( "" ); } // Call the setup "method" to fill in the current settings // eval (($setup + " " + $widgetList[0] + " " + $selectionConnection + " false")); showOptionBox(); showWindow $widgetList[0]; keyReducerDoSelectionChanged($selectionConnection); scriptJob -protected -event "SelectionChanged" ("keyReducerDoSelectionChanged " + $selectionConnection) -parent $widgetList[2]; } proc string assembleCmd( string $selectionConnection, string $options ) { int $precisionMode = `optionVar -q keyReducerPrecisionMode`; float $precisionAbsolute = `optionVar -q keyReducerPrecisionAbsolute`; float $precisionPercent = `optionVar -q keyReducerPrecisionPercent`; float $precision = $precisionPercent; if( $precisionMode == 1 ) { $precision = $precisionAbsolute; } int $preserveTangents[] = keySetOptionPreserveTangents( {"keyReducer", "queryOptionVars"} ); int $preserveFixed = $preserveTangents[0]; int $preserveLinear = $preserveTangents[1]; int $preserveFlat = $preserveTangents[2]; int $preserveSmooth = $preserveTangents[3]; int $preserveStep = $preserveTangents[4]; int $preserveClamped = $preserveTangents[5]; int $preservePlateau = $preserveTangents[6]; int $preserveStepNext = $preserveTangents[7]; int $preserveAuto = $preserveTangents[8]; string $cmd = "doKeyReducerArgList 1 { " + "\"" + `optionVar -q keyReducerWhichRange` + "\"" + ",\"" + `optionVar -q keyReducerRange` + "\"" + ",\"" + $precisionMode + "\"" + ",\"" + $precision + "\"" + ",\"" + `optionVar -q keyReducerKeySync` + "\"" + ",\"" + $preserveFixed + "\"" + ",\"" + $preserveLinear + "\"" + ",\"" + $preserveFlat + "\"" + ",\"" + $preserveSmooth + "\"" + ",\"" + $preserveStep + "\"" + ",\"" + $preserveClamped + "\"" + ",\"" + $preservePlateau + "\"" + ",\"" + $preserveStepNext + "\"" + ",\"" + $preserveAuto + "\"" + ",\"" + $selectionConnection + "\"" + ",\"" + $options + "\"" + " };"; return $cmd; } // The action variable means // 0 - do the command // 1 - show the option box // 2 - return the drag command // 3 - do the command (bufferCurve version) // 4 - show the option box (bufferCurve version) // 5 - return the drag command (bufferCurve version) global proc string performKeyReducer( int $action, string $selectionConnection ) { string $cmd = ""; switch( $action ) { case 0: setOptionVars( false ); $cmd = assembleCmd( $selectionConnection, "noOptions" ); eval( $cmd ); break; case 1: keyReducerOptions( $selectionConnection, 0 ); break; case 2: setOptionVars( false ); $cmd = assembleCmd( $selectionConnection, "noOptions" ); break; case 3: setOptionVars( false ); $cmd = assembleCmd( $selectionConnection, "bufferCurve" ); eval( $cmd ); break; case 4: keyReducerOptions( $selectionConnection, 3 ); break; case 5: setOptionVars( false ); $cmd = assembleCmd( $selectionConnection, "bufferCurve" ); break; } return ($cmd); }