// =========================================================================== // 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 atom template creation // // // This string is used as an optionVar value for the // directory when we want to indicate that the preference is to // use the current project's data directory. // global string $gAtomTemplateCurrentProject = "CurrentProject"; global int $gAtomTemplateSetFileWithBrowser= 0; global proc atomTemplateWidgetsEnable() // // Description: // These are the widgets that get enabled and // disabled based on the certain states. Put them // here so they're all in one place. // { int $useField = `checkBoxGrp -q -value1 atomTemplateCreateView`; textFieldGrp -e -enable $useField viewWidget; } global proc setAtomTemplateSharedOptionVars(int $forceFactorySettings) { if( $forceFactorySettings || !`optionVar -exists atomTemplateHierarchy` || (`optionVar -q atomTemplateHierarchy` == 0)) { optionVar -intValue atomTemplateHierarchy 1; } if( $forceFactorySettings || !`optionVar -exists atomTemplateCreateView` ) optionVar -intValue atomTemplateCreateView 1; if( $forceFactorySettings || !`optionVar -exists atomTemplateChannels` ) { // 1 = keyable, 2 = channel box // optionVar -intValue atomTemplateChannels 1; } if( $forceFactorySettings || !`optionVar -exists atomTemplateChannels` ) { string $value = ""; optionVar -stringValue atomTemplateViewName $value; } } global proc atomTemplateUpdateSharedOptions() { // Hierarchy // if (`radioButtonGrp -exists hierarchyGrp`) { radioButtonGrp -e -sl `optionVar -q atomTemplateHierarchy` hierarchyGrp; } // Create View values // if (`checkBoxGrp -exists atomTemplateCreateView`) { checkBoxGrp -e -value1 `optionVar -q atomTemplateCreateView` atomTemplateCreateView; } //View Name if (`textFieldGrp -q -exists viewWidget`) { textFieldGrp -e -tx `optionVar -q atomTemplateViewName` viewWidget; } // Channels // if (`radioButtonGrp -exists channelBoxGrp`) { int $channels = `optionVar -q atomTemplateChannels`; radioButtonGrp -e -select $channels channelBoxGrp; } } global proc atomTemplateSharedOptionsCallback() { if (`radioButtonGrp -exists hierarchyGrp`) { optionVar -iv atomTemplateHierarchy `radioButtonGrp -q -sl hierarchyGrp`; optionVar -iv atomTemplateCreateView `checkBoxGrp -q -value1 atomTemplateCreateView`; optionVar -iv atomTemplateChannels `radioButtonGrp -q -select channelBoxGrp`; optionVar -stringValue atomTemplateViewName `textFieldGrp -q -tx viewWidget`; } } global proc int atomTemplateGetHierarchy() { atomTemplateSharedOptionsCallback(); return `optionVar -q atomTemplateHierarchy`; } global proc int atomTemplateGetCreateView() { atomTemplateSharedOptionsCallback(); return `optionVar -q atomTemplateCreateView`; } // Channels: 1 = selected, 2 = channel box // global proc int atomTemplateGetChannels() { atomTemplateSharedOptionsCallback(); return `optionVar -q atomTemplateChannels`; } global proc string atomTemplateGetViewName() { atomTemplateSharedOptionsCallback(); return `optionVar -q atomTemplateViewName`; } global proc string atomTemplateGetChannelAttributes() // // Description: // Sets the opts string for all of the shared options. // { string $attrOptions; string $main[]; string $shape[]; string $history[]; int $i; catch( $main = `channelBox -q -sma mainChannelBox` ); catch( $shape = `channelBox -q -ssa mainChannelBox` ); catch( $history = `channelBox -q -sha mainChannelBox` ); for( $i = 0; $i < size( $main ); $i++ ) { $attrOptions=($attrOptions + " -at " + $main[$i]); } for( $i = 0; $i < size( $shape ); $i++ ) { $attrOptions = ( $attrOptions + " -at = " + $shape[$i]); } for( $i = 0; $i < size( $history ); $i++ ) { $attrOptions = ( $attrOptions + " -at " + $history[$i]); } if( size( $attrOptions ) == 0 ) { $cmd = ""; warning (uiRes("m_createATOMTemplate.kNoChannelSelected")); } else $attrOptions += " "; return $attrOptions; } global proc atomTemplateSharedOptions() { checkBoxGrp -numberOfCheckBoxes 1 -label "" -label1 (uiRes("m_createATOMTemplate.kCreateView")) -value1 `optionVar -query atomTemplateCreateView` -cc "atomTemplateWidgetsEnable" atomTemplateCreateView; textFieldGrp -label (uiRes("m_createATOMTemplate.kView")) -adjustableColumn 2 viewWidget; radioButtonGrp -numberOfRadioButtons 2 -label (uiRes("m_createATOMTemplate.kHierarchy")) -label1 (uiRes("m_createATOMTemplate.kSelected")) -label2 (uiRes("m_createATOMTemplate.kBelow")) -select 1 -enable true hierarchyGrp; radioButtonGrp -numberOfRadioButtons 2 -label (uiRes("m_createATOMTemplate.kChannels")) -label1 (uiRes("m_createATOMTemplate.kAllKeyable")) -cc1 "atomTemplateWidgetsEnable" -label2 (uiRes("m_createATOMTemplate.kFromChannelBox")) -cc2 "atomTemplateWidgetsEnable" -select 1 -enable true channelBoxGrp; } global proc string atomTemplateGetCurrentDir() // // Return a string corresponding to the current directory preference. // Use the current project's data directory by default. // { global string $gAtomTemplateCurrentProject; // first find the current project directory, if it is defined // string $currentProj = (`workspace -q -rd`+"data"); // check what the user has as their preference // if (`optionVar -exists atomTemplateDirName`) { string $templateVar = `optionVar -q atomTemplateDirName`; if ($templateVar != $gAtomTemplateCurrentProject) { $currentProj = $templateVar; } } return $currentProj; } global proc atomTemplateSetDirOptionVar() // // Set the optionVar based on the user's entry in the option box // { global string $gAtomTemplateCurrentProject; if ( `textFieldGrp -q -en exportFileWidget` ) { string $value = `textFieldGrp -q -tx exportFileWidget`; if ( $value != "" ) { string $filename = basename($value,""); optionVar -stringValue atomTemplateFileName $filename; string $dir = dirname($value); string $currentProj = (`workspace -q -rd`+"data\/"); if ($currentProj == $dir) { $value = $gAtomTemplateCurrentProject; } optionVar -sv atomTemplateDirName $dir; } else { optionVar -stringValue atomTemplateFileName ""; optionVar -sv atomTemplateDirName $gAtomTemplateCurrentProject; } } } global proc int atomTemplateSetFile( string $file, string $type ) { textFieldGrp -e -tx $file exportFileWidget; atomTemplateSetDirOptionVar(); string $filename = basename($file,""); optionVar -stringValue atomTemplateFileName $filename; return 1; } global proc atomTemplateBrowseForFile() { string $callBack = "atomTemplateSetFile "; string $action = (uiRes("m_createATOMTemplate.kSetDir")); string $startInDir; $startInDir = `textFieldGrp -q -tx exportFileWidget`; if ( size( $startInDir ) == 0 ) { $startInDir = `internalVar -uwd`; } else { $startInDir = dirname($startInDir); } if ( `file -q -ex $startInDir` ) { workspace -dir $startInDir; } global int $gAtomTemplateSetFileWithBrowser; $gAtomTemplateSetFileWithBrowser= 1; string $template = (uiRes("m_createATOMTemplate.kTemplateFiles")); string $filter = $template + " (*.template)"; string $caption = (uiRes("m_createATOMTemplate.kATOMTemplate")); string $fileCmd = ("fileDialog2 "); $fileCmd += (" -caption \"" + $caption + "\""); //$fileCmd += (" -okCaption \"" + $options[0] + "\""); $fileCmd += (" -fileMode 0"); $fileCmd += (" -startingDirectory \"" + $startInDir + "\""); $fileCmd += (" -returnFilter 0"); $fileCmd += (" -fileFilter \"" + $filter + "\""); $fileCmd += (" -selectFileFilter \"" + $template + "\""); string $file[] = `eval $fileCmd`; if (0 < size($file) && "" != $file[0]) { string $path = fromNativePath($file[0]); string $cmd = ($callBack + "\"" + $path + "\""); if (1 < size($file) && "" != $file[1]) { string $filterType = "template"; $cmd += (" \"" + $filterType + "\""); } else { $cmd += (" \"\""); } eval $cmd; } } proc setOptionVars(int $forceFactorySettings) { global string $gAtomTemplateCurrentProject; if( $forceFactorySettings || !`optionVar -exists atomTemplateDirName`) { optionVar -sv atomTemplateDirName "CurrentProject"; } if( $forceFactorySettings || !`optionVar -exists atomTemplateFileName`) { optionVar -sv atomTemplateFileName ""; } setAtomTemplateSharedOptionVars($forceFactorySettings); } global proc atomTemplateSetup (string $parent, int $forceFactorySettings) { setOptionVars($forceFactorySettings); setParent $parent; string $dirToUse = atomTemplateGetCurrentDir(); string $filename = `optionVar -q atomTemplateFileName`; textFieldGrp -e -tx ($dirToUse+"\/"+$filename) exportFileWidget; atomTemplateUpdateSharedOptions(); separator; atomTemplateWidgetsEnable(); } global proc atomTemplateCallback(string $parent, int $doIt) { // update the UI for shared options // atomTemplateSharedOptionsCallback(); if ($doIt) { createATOMTemplate 0; } } proc createAtomTemplateOptions() { // Name of the command for this option box // string $commandName = "atomTemplate"; // Build the option box "methods" // string $callback = ($commandName + "Callback "); string $setup = ($commandName + "Setup"); // 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 200 5 50 -adjustableColumn 1 -cal 1 "both" -cal 2 "both" -cal 3 "left" -ct3 "both" "both" "left"; string $dirToUse = atomTemplateGetCurrentDir(); string $fileToUse = ($dirToUse+"template.template"); textFieldGrp -label (uiRes("m_createATOMTemplate.kAnimDirectory")) -tx $fileToUse -adjustableColumn 2 exportFileWidget; separator -w 5 -style "none"; symbolButton -image "navButtonBrowse.png" -c ( "atomTemplateBrowseForFile" ) exportFileWidgetBrowser; setParent ..; atomTemplateSharedOptions(); // Turn off the wait cursor. // 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_createATOMTemplate.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_createATOMTemplate.kMayaAtomTemplateOptions")); setOptionBoxTitle($optionTitle); // Step 8: Customize the 'Help' menu item text. // ============================================ // setOptionBoxHelpTag( "Atom_Template_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 $gAtomTemplateCurrentProject; global int $gAtomTemplateSetFileWithBrowser; if (`textFieldGrp -q -exists exportFileWidget`) { // Since the changed callback is not invoked if the user does not // hit enter, update the value now // atomTemplateSetDirOptionVar(); // If the user just typed in a path without using the browser, // then check if we need to post a message about overwriting a file. // if (0 == $gAtomTemplateSetFileWithBrowser) { string $postFile = `optionVar -q atomTemplateFileName`; string $postDir = atomTemplateGetCurrentDir(); string $path = ($postDir+"\/"+$postFile); if( `filetest -f $path` ) { string $msg = (uiRes("m_createATOMTemplate.kOverwriteMessage")); string $ok = (uiRes("m_createATOMTemplate.kYes")); string $cancel = (uiRes("m_createATOMTemplate.kNo")); $msg = `format -s $postFile $msg`; string $overwrite = `confirmDialog -title (uiRes("m_createATOMTemplate.kReplaceFileDialog")) -message $msg -button $ok -button $cancel -defaultButton $cancel -cancelButton $cancel`; if( $overwrite == $cancel ) { return ""; } } } } $gAtomTemplateSetFileWithBrowser= 0; string $dirname = `optionVar -query atomTemplateDirName`; if ($dirname == $gAtomTemplateCurrentProject) { $dirname = (`workspace -q -rd`+"data\/"); } string $filename = `optionVar -query atomTemplateFileName`; if (size($filename) == 0) { error((uiRes("m_createATOMTemplate.kMustSpecifyFilename"))); return ""; } string $buffer[]; tokenize $filename "." $buffer; string $templateName = $buffer[0]; $filename = ($dirname + "\/" + $filename); //still need to make sure the filename is not a directory //otherwise crashes will occur on MacOS or Unix. if ( `filetest -d $filename` ) { error((uiRes("m_createATOMTemplate.kTemplateIsEmpty"))); return ""; } int $createView = atomTemplateGetCreateView(); string $viewName = atomTemplateGetViewName(); if($createView ==1 && size($viewName) == 0){ error((uiRes("m_createATOMTemplate.kMustSpecifyViewname"))); return ""; } int $includeHierarchy = atomTemplateGetHierarchy(); string $keyable = ""; string $hierarchy = ""; if($includeHierarchy==2) $hierarchy = " -useHierarchy "; //based on channel box either do all keyable or get channels int $useChannelBox = atomTemplateGetChannels(); if($useChannelBox==1) $keyable = " -allKeyable "; else $keyable = atomTemplateGetChannelAttributes(); string $cmd = ""; if($createView) { //when creating a view if the template file doesn't // exist we create it first, otherwise we make sure //it's loaded int $filefound = `filetest -f $filename`; if($filefound == 0){ $cmd += "containerTemplate -force "; $cmd += " -fromSelection "; $cmd += $hierarchy; $cmd += $keyable; $cmd += $templateName; $cmd += "; containerTemplate -save -fileName "; $cmd += ("\""+$filename+"\"") + " " + $templateName + ";"; } else{ //load it $cmd += "containerTemplate -silent -force -load -fileName "; $cmd += ("\""+$filename+"\"") + " " + $templateName + ";"; } //create view $cmd += "containerTemplate -force "; $cmd += " -edit -layoutMode 1 -addView "; $cmd += $viewName + " "; } else { //if not creating a view just create the template $cmd += "containerTemplate -force "; } $cmd += " -fromSelection "; $cmd += $hierarchy; $cmd += $keyable; $cmd += $templateName; //finally we always save the template to disk $cmd += "; containerTemplate -save -fileName "; $cmd += ("\""+$filename+"\"") + " " + $templateName + ";"; return $cmd; } global proc string createATOMTemplate( int $action ) { string $cmd = ""; switch( $action ) { case 0: setOptionVars(false); $cmd = assembleCmd(); evalEcho($cmd); break; case 1: createAtomTemplateOptions(); break; case 2: setOptionVars(false); $cmd = assembleCmd(); break; } return $cmd; }