// =========================================================================== // 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 21 2012 // // // // // // // incrementAndSaveScene( int $force ) // // // None. // // // Called when the scene is saved and the "File" -> "Increment & Save" // menu item is clicked or hotkey "Ctrl+Alt+S" is pressed . // // Performs a non-destructive save of the current scene by taking the // version of the scene that was last saved to disk, incrementing the version // and putting it into the current scene folder. // // If the current scene has never been saved, the regular "Save As..." dialog will be opened. // Otherwise, the version in the current file name is incremented, and the file is saved with this new name. // If the new file name is an existing file, the user will be prompted to confirm that the file will be replaced. // After saving, the current Maya scene will be the renamed file. The original file will be unchanged. // // How the file version is incremented: // Maya will look at the end of the root file name for a period followed by a string of digits [0-9]. // If this pattern is not found, then Maya will append ".0001" to the root file name. // If the pattern is found, then the string of digits will be incremented, adding a digit only if necessary. // // Examples: // MyFile.mb ---> MyFile.0001.mb // MyFile.01.mb ---> MyFile.02.mb // MyFile.99.mb --> MyFile.100.mb // MyFile01.mb --> MyFile01.0001.mb // MyFile.0043.mb --> MyFile.0044.mb // MyFile.00a22.mb --> MyFile.00a22.0001.mb // // // // int $force When the argument is non-zero, the user will not be prompted for confirmation // before the file is written in the event that the file already exists. // // // // // User will be prompted for confirmation if the file already exists. // incrementAndSaveScene 0; // // // User will NOT be prompted for confirmation if the file already exists. The existing file gets overwritten. // incrementAndSaveScene 1; // // // global proc incrementAndSaveScene( int $force ) { string $sceneFullPath = `file -q -sceneName`; if ( size($sceneFullPath) == 0 ) { projectViewer SaveAs; } else if (`file -q -mf`) { string $sceneName = `match "[^/]+$" $sceneFullPath`; string $currentDir = `substring $sceneFullPath 1 (size($sceneFullPath) - size($sceneName))`; string $sceneExtension = `match "[.][^.]*$" $sceneName`; if($sceneExtension == `match "[.][0-9]+" $sceneExtension`) { $sceneExtension = ""; } string $sceneNamePrefix = `substring $sceneName 1 (size($sceneName) - size($sceneExtension))`; string $currVersionString = `match "[.][0-9]+$" $sceneNamePrefix`; $currVersionString = `match "[0-9]+" $currVersionString`; $fileVersion = 0; // Remove the current version string from the scene name prefix if($currVersionString != "") { $sceneNamePrefix = `substring $sceneNamePrefix 1 (size($sceneNamePrefix) - size($currVersionString) - 1)`; $fileVersion = $currVersionString; } string $newVersionString = $fileVersion + 1; // Make the increment-number in the filename has the same number of digits as the current version pattern. // By default, the number is 4. $digitLen = 4; if(size($currVersionString)!=0) $digitLen = size($currVersionString); for($i = size($newVersionString); $i < $digitLen; $i++) $newVersionString = "0" + $newVersionString; // Build the name of the new scene // string $newScenePath = $currentDir + $sceneNamePrefix + "." + $newVersionString + $sceneExtension; // Make sure that our script isn't going to overwrite an existing file without asking user int $doIt = true; if(!$force && `file -q -exists $newScenePath`) { string $cancel = (uiRes("m_incrementAndSaveScene.kCancel")); string $message = (uiRes("m_incrementAndSaveScene.kIncrementFileExists")); $message = `format -s $newScenePath $message`; $result = `confirmDialog -message $message -button (uiRes("m_incrementAndSaveScene.kYes")) -button $cancel -defaultButton $cancel -dismissString $cancel -cancelButton $cancel`; if ($result == $cancel) { $doIt = false; } } if( $doIt ) { int $wasDefaultExtensions = `file -q -defaultExtensions`; file -defaultExtensions false; file -rename $newScenePath; file -defaultExtensions $wasDefaultExtensions; string $fileType = "unknown"; string $fileTypes[] = `file -q -type $newScenePath`; if (size($fileTypes) > 0 ) { $fileType = $fileTypes[0]; } addRecentFile ($newScenePath, $fileType); // Print some useful information to the script editor so that users can // find out exactly what has been done // print ( (uiRes("m_incrementAndSaveScene.kPerformingIncrementalSave")) ); string $masterfile = (uiRes("m_incrementAndSaveScene.kMasterFile")); print ( `format -s $sceneFullPath $masterfile` ); string $incrementfile = (uiRes("m_incrementAndSaveScene.kIncrementFile")); print ( `format -s $newScenePath $incrementfile` ); string $cmd = "file -force -save -options \"v=0\""; evalEcho ($cmd); } } else { warning (uiRes("m_incrementAndSaveScene.kNoChangesToIncrementSave")); } }