// =========================================================================== // 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. // =========================================================================== // // // Description: // Option box dialog for importing atom animation // global proc int animImportSetFile( string $file, string $type ) { textFieldGrp -e -tx $file importFileWidget; animImportAtomFileCallBack(); return 1; } global proc animImportBrowseForFile() { string $cmd = "animImportSetFile "; string $action = (uiRes("m_performImportAnim.kSetDir")); string $startInDir; $startInDir = `textFieldGrp -q -tx importFileWidget`; if ( size( $startInDir ) == 0 ) { $startInDir = `internalVar -uwd`; } else { $startInDir = dirname($startInDir); } if ( `file -q -ex $startInDir` ) { workspace -dir $startInDir; } fileBrowser( $cmd, $action, "atomImport", 0 ); } proc setOptionVars(int $forceFactorySettings) { global string $gAnimImportCurrentProject; if( $forceFactorySettings || !`optionVar -exists animImportDirName`) { optionVar -sv animImportDirName "CurrentProject"; } if( $forceFactorySettings || !`optionVar -exists animImportFileName`) { optionVar -sv animImportFileName ""; } setImportAnimSharedOptionVars($forceFactorySettings); } global proc animImportSetup (string $parent, int $forceFactorySettings) { if (! `exists animImportOptionsEnable`) source "importAnimSharedOptions"; setOptionVars($forceFactorySettings); setParent $parent; if (! `pluginInfo -q -loaded atomImportExport`) loadPlugin atomImportExport; string $filename = `optionVar -q animImportFileName`; string $dirname = animImportGetCurrentDir(); string $fullPath = $dirname + $filename; textFieldGrp -e -tx $fullPath importFileWidget; importAnimUpdateSharedOptions(); separator; animImportOptionsEnable(); } global proc animImportCallback(string $parent, int $doIt) { // update the UI for shared options // importAnimSharedOptionsCallback(); if ($doIt) { performImportAnim 0; } } // // Callback called when the file name change. // // This function check if the animation file exists and extract the lowest and hightest frame from it to // update the start/end text fields (source and destination). The lowest frame from the file is set as // minimum value for the source start frame text field and the highest frame is set as maximum value // for the source end frame text feild. // global proc animImportAtomFileCallBack() { // Check if the file name is empty. If is empty, print an error and bail. string $fullFilename = `textFieldGrp -q -tx importFileWidget`; if (size($fullFilename) == 0) { error((uiRes("m_performImportAnim.kMustSpecifyFilename2"))); } // Check if the user specified file exists and is readable. If not, print an error and bail. int $exists = `filetest -r $fullFilename`; if ($exists == false) { string $fmt = (uiRes("m_performImportAnim.kFileNotFound2")); error(`format -s $fullFilename $fmt`); } // save the file name animImportSetDirOptionVar(); string $filename = basename($fullFilename,""); optionVar -stringValue animImportFileName $filename; atomImportHierarchyOptionEnable(); // // Extract start / end key frame from the atom file // // The minimum and maximum key are not in the header, so parse the file // to extract the keys. // float $startTime = 1; float $endTime = 1; int $fileId=`fopen $fullFilename "r"`; if($fileId == 0) return; int $inKeysSection = 0; int $isFisrtKeyRead = 1; string $nextWord = `fgetword $fileId`; while ( size( $nextWord ) > 0 ) { if($nextWord == "keys") { $inKeysSection = 1; $nextWord = `fgetword $fileId`; // remove the { character } else if($inKeysSection == 1) { $nextWord = `fgetline $fileId` ; $nextWord = strip($nextWord); if($nextWord == "}") { $inKeysSection = 0; } else { string $strArray[] = stringToStringArray($nextWord, " "); if( size($strArray) > 0 ) { float $key = $strArray[0]; if($isFisrtKeyRead == 1) { $startTime = $key; $endTime = $key; $isFisrtKeyRead = 0; } else { if($key < $startTime) { $startTime = $key; } else if($key > $endTime) { $endTime = $key; } } } } } else { $nextWord = `fgetword $fileId`; } } fclose $fileId; // // Update source/destination start/end frames with the lowest and highest frame extracted from the file. // Also set the minimum and maximum value for the source start/end fields. // floatField -e -v $startTime -minValue $startTime srcFrameStart; floatField -e -v $endTime -maxValue $endTime srcFrameEnd; floatField -e -v $startTime dstFrameStart; floatField -e -v $endTime dstFrameEnd; optionVar -fv animImportStartTime $startTime; optionVar -fv animImportEndTime $endTime; optionVar -fv animImportDestinationStartTime $startTime; optionVar -fv animImportDestinationEndTime $endTime; } proc createAnimImportOptions() { // Name of the command for this option box // string $commandName = "atomImport"; // Build the option box "methods" // string $callback = ("animImportCallback "); string $setup = ("animImportSetup"); // STEP 1: Get the option box. // ============================ // // The value returned is the name of the layout to be used as // the parent for the option box UI. // string $layout = getOptionBox(); setParent $layout; // STEP 2: Pass the command name to the option box. // ================================================= // // Any default option box behaviour based on the command name is set // up with this call. For example, updating the 'Help' menu item with // the name of the command. // setOptionBoxCommandName($commandName); // STEP 3: Activate the default UI template. // ========================================== // // Activate the default UI template so that the layout of this // option box is consistent with the layout of the rest of the // application. // setUITemplate -pushTemplate DefaultTemplate; // STEP 4: Create option box contents. // =================================== // // This, of course, will vary from option box to option box. // // Turn on the wait cursor. // waitCursor -state 1; tabLayout -scr true -tv false; string $parent = `columnLayout -adjustableColumn 1`; // Create the widgets for this option box // // Directory name // rowLayout -nc 3 -cw3 400 5 50 -adjustableColumn 1 -cal 1 "both" -cal 2 "both" -cal 3 "left" -ct3 "both" "both" "left"; string $dirToUse = animImportGetCurrentDir(); string $fileToUse = ($dirToUse+"import.atom"); textFieldGrp -label (uiRes("m_performImportAnim.kAnimDirectory")) -tx $fileToUse -cc ( "animImportAtomFileCallBack" ) -adjustableColumn 2 importFileWidget; separator -w 5 -style "none"; symbolButton -image "navButtonBrowse.png" -c ( "animImportBrowseForFile" ) importFileWidgetBrowser; setParent ..; importAnimSharedOptions(0); // Turn off the wait cursor (if it's on). // (Note: exportAnimSharedOptions makes calls to the containerTemplate // command which can error out. They are suppressed via "catchQuiet" // but they can still result in clearing the waitCursor state.) // if (`waitCursor -q -st`) { waitCursor -state 0; } // Step 5: Deactivate the default UI template. // =========================================== // setUITemplate -popTemplate; // Step 6: Customize the buttons. // ============================== // // Provide more descriptive labels for the buttons. This is not // necessary, but in some cases, for example, a button labelled // 'Create' may be more meaningful to the user than one labelled // 'Apply'. // // Disable those buttons that are not applicable to the option box. // // Attach actions to those buttons that are applicable to the option // box. Note that the 'Close' button has a default action attached // to it that will hide the window. If a a custom action is // attached to the 'Close' button then be sure to call the 'hide the // option box' procedure within the custom action so that the option // box is hidden properly. // 'Apply' button. // string $applyBtn = getOptionBoxApplyBtn(); string $buttonName = (uiRes("m_performImportAnim.kApply")); button -edit -label $buttonName -command ($callback + " " + $parent + " " + 1) $applyBtn; // 'Save' button. // string $saveBtn = getOptionBoxSaveBtn(); button -edit -command ($callback + " " + $parent + " " + 0 + "; hideOptionBox") $saveBtn; // 'Reset' button string $resetBtn = getOptionBoxResetBtn(); int $resetToDefaults = 1; button -edit -command ($setup + " " + $parent + " " + $resetToDefaults) $resetBtn; // Step 7: Set the option box title. // ================================= // string $optionTitle = (uiRes("m_performImportAnim.kMayaAnimImporterOptions")); setOptionBoxTitle($optionTitle); // Step 8: Customize the 'Help' menu item text. // ============================================ // setOptionBoxHelpTag( "Atom_Importer_Options" ); // Step 9: Set the current values of the option box. // ================================================= // eval ($setup + " " + $parent + " " + 0); // Step 10: Show the option box. // ============================= // showOptionBox(); } // // Procedure Name: // assembleCmd // // Description: // Construct the command that will apply the option box values. // // Input Arguments: // None. // // Return Value: // None. // proc string assembleCmd() { global string $gAnimImportCurrentProject; string $sel[] = `ls -sl`; if (0 == size($sel)) { error((uiRes("m_performImportAnim.kNothingSelected"))); } // Make sure the text field group value (i.e., filename and directory) have // been properly saved to option vars. Failure to refresh these can lead to // problems if the user enters a value in the textFieldGrp and the change command // callback is not triggered, such as in cases when focus is not lost. // animImportSetDirOptionVar; string $filename = `optionVar -q animImportFileName`; if (size($filename) == 0) error((uiRes("m_performImportAnim.kMustSpecifyFilename"))); string $dirname = animImportGetCurrentDir(); string $fullPath = ($dirname + $filename); // Check if the user specified file exists and is readable. If not, print an error and bail. int $exists = `filetest -r $fullPath`; if ($exists == false) { string $fmt = (uiRes("m_performImportAnim.kFileNotFound")); error(`format -s $fullPath $fmt`); } if (! `exists importAnimGetCommand`) source "importAnimSharedOptions"; // get the command string options to send to ATOM c++ plugin string $optionsString = `importAnimGetCommand`; string $ns = basename($filename,".atom"); string $cmd = ("file -import -type \"atomImport\" -ra true "); $cmd += ("-namespace \""+$ns+"\" "); $cmd += ("-options \""+$optionsString+"\" "); $cmd += ("\""+ $fullPath +"\""); return $cmd; } global proc string performImportAnim( int $action ) { string $cmd = ""; switch( $action ) { case 0: setOptionVars(false); $cmd = assembleCmd(); evalEcho($cmd); break; case 1: createAnimImportOptions(); break; case 2: setOptionVars(false); $cmd = assembleCmd(); break; } return $cmd; }