// =========================================================================== // 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: Jan. 1997 // // Description: // setProject is used to set the current project. // // Input Arguments: // A string. If empty, the project browser comes up for the // new project. Otherwise the project is set to the given // string. // // Return Value: // None // // // // // // setproject $newProject // // // none // // // Sets the current project. If no string is specified, the // Project Browser window will open. // // // // setProject "C:/Documents and Settings/kilroy/My Documents/maya/projects/default"; // // // Check if workspace file exists in a directory proc int sp_workspaceExists(string $projectLocation) { //check if there is workspace.mel under the directory. string $path = $projectLocation + "/workspace.mel"; int $exists = `filetest -e $path`; return $exists; } global proc string sp_getProjectLocation(string $startingDirectory) // // Descrption: // pop up a fileDialog2 to let user to choose the directory for the project // { string $caption = (uiRes("m_setProject.kSetProject")); string $okCaption = (uiRes("m_setProject.kOkButtonLabel")); string $cmd = ("fileDialog2 -caption \"" + $caption + "\" -fileMode 3"); $cmd += (" -startingDirectory \"" + $startingDirectory + "\""); $cmd += (" -okCaption \"" + $okCaption + "\""); $cmd += (" -setProjectBtnEnabled 0"); string $dir[] = `eval $cmd`; string $dirPath = ""; if (0 < size($dir) && "" != $dir[0]) { $dirPath = fromNativePath($dir[0]); if( !endsWith($dirPath, "/") ) { $dirPath += "/"; } } return $dirPath; } global proc int sp_createAndSetDefaultProject(string $projectLocation, int $createDirectories) { // This routine will create a new workspace with default project // settings at the given location // The routine may generate an error, for example if the workspace file // cannot be saved. This can be caught by the caller // if they do not want to stop execution of their script as a result. // Check this is a valid directory if (!`filetest -d $projectLocation`) { // Location is not a valid directory, workspace cannot be created string$fmtString = (uiRes("m_setProject.kLocationNotDir")); string $warnMsg = (`format -s $projectLocation $fmtString`); warning ($warnMsg); return false; } // If a workspace already exists at this location do not continue // The user should be using the edit project window to change the settings // (this routine will overwrite their file) if (sp_workspaceExists($projectLocation)) { string $fmtString = (uiRes("m_setProject.kAlreadyExists")); string $warnMsg = (`format -s $projectLocation $fmtString`); warning ($warnMsg); return false; } // ---------------------------------------------- // Create the new workspace setup workspace -openWorkspace $projectLocation; // Get list of all standard file rules string $fileRules[] = np_getDefaultFileRuleWidgets(); // Create default rules and also create the directories if requested if ($createDirectories) { // Set workspace current directory to the workspace root // so relative paths created with -create are located correctly workspace -dir `workspace -q -rootDirectory`; } int $i; for ($i=0; $i 0) error($errorMsg); return false; } // ------------------------------------------------------------- return true; } proc sp_workAfterOpeningProject(string $projectLocation) // // Description: // the work after opening project, add project location to recent project, then // update the project window if it is opened. // { addRecentProject( $projectLocation ); // We also want to update the edit box if they're editing the 'current' // (the new one should now be the 'current') if ( `window -ex projectWindow` ) { // If the use is editing the project, then we want to // refresh the Edit window // If they were setting up a new one we close the newProjectWindow if ( `np_isEditModeForProjectWindow` ) projectWindow( ); else deleteUI projectWindow; // We remove the window if they were setting a new one } } global proc int sp_setLocalWorkspaceCallback (string $path) // // Description: // Open the selected workspace and make it the new local workspace. // // // { int $result = false; string $workspace = `workspace -q -fn`; string $projectLocation = $path; string $ok = (uiRes("m_setProject.kOk")); while($result != true) { // Check if valid directory if (`filetest -d $projectLocation`) { //check if there is workspace.mel under the directory. if (!sp_workspaceExists($projectLocation)) { string $tryAgain = (uiRes("m_setProject.kTryAgain")); string $cancel = (uiRes("m_setProject.kCancel")); string $createDefault = (uiRes("m_setProject.kCreateDefault")); string $messageText = (uiRes("m_setProject.kWithoutWorkspaceFileText")); $messageText = `format -s $projectLocation $messageText`; string $resultStr = `confirmDialog -message $messageText -button $tryAgain -button $createDefault -button $cancel -defaultButton $cancel -cancelButton $cancel -dismissString $cancel`; if( $resultStr == $cancel) { break; //cancel the operation, break while loop. } else if( $resultStr == $tryAgain) { // Prompt user to browse for another location // Reset the browser back to the parent directory of this location // which is where they would have selected it from. $projectLocation = sp_getProjectLocation(dirname($projectLocation)); if( "" != $projectLocation) { continue; //get the new location and start over the while loop. } else { break; //$projectLocation is empty string. break the while loop. } } else if( $resultStr == $createDefault) { // This operation may generate an error, for example if the directory // is write protected. We want to continue in any case, so catch the // error and break out of the while loop. if(catch (sp_createAndSetDefaultProject($projectLocation,false))) { string $errorMsg = getLastError(); if(size($errorMsg) > 0) confirmDialog -message $errorMsg -button $ok -defaultButton $ok -parent projectViewerWindow; break; //not a directory, break while loop. } $result = true; // break out of while loop break; } } workspace -o $projectLocation; // If the browser is up then we must reset it. // if (`window -ex projectViewerWindow`) { pv_resetWorkspace; // Force the layout to be redone. } np_resetBrowserPrefs; $result = true; } else { string $msg = (uiRes("m_setProject.kInvalidPath")); $msg = `format -s $projectLocation $msg`; confirmDialog -message $msg -button $ok -defaultButton $ok -parent projectViewerWindow; break; //not a directory, break while loop. } } // They picked a new one: if ( true == $result && $projectLocation != $workspace ) { sp_workAfterOpeningProject($projectLocation); } return $result; } global proc int sp_setLocalWorkspaceWithoutPopupDialog( string $path ) // // Description: // this routine is for the setProject with a string argument, // this situation can not pop up any dialog, only treat it as a command. // { string $workspace = `workspace -q -fn`; string $projectLocation = $path; // Check if valid directory if (`filetest -d $projectLocation`) { //check if there is workspace.mel under the directory. if (sp_workspaceExists($projectLocation)) { //workspace.mel exists, then use workspace command to open the workspace under the specified directory workspace -openWorkspace $projectLocation; } else { //print warning when workspace.mel does not exist, then create default project in this location. string $fmtString = (uiRes("m_setProject.kWorkspaceFileNotExist")); string $warnMsg = (`format -s $projectLocation $fmtString`); warning ($warnMsg); // This operation may generate an error, for example if the directory // is write protected, So catch the error and return this routine. if( catch (sp_createAndSetDefaultProject($projectLocation,false)) ) { // the default workspace cannot be created string $errorMsg = getLastError(); if(size($errorMsg) > 0) error ($errorMsg); return false; } } // If the browser is up then we must reset it. // if (`window -ex projectViewerWindow`) { pv_resetWorkspace; // Force the layout to be redone. } np_resetBrowserPrefs; } else { // Location is not a valid directory, workspace cannot be created string $fmtString = (uiRes("m_setProject.kLocationNotDirNoPopup")); string $errorMsg = (`format -s $projectLocation $fmtString`); error ($errorMsg); return false; } // They picked a new one: if ( $projectLocation != $workspace ) { sp_workAfterOpeningProject($projectLocation); } return true; } global proc setProject ( string $newProject ) // // Description: // Set the current workspace to the one selected by the user. // { // No name given - browse for it if ( size( $newProject ) == 0 ) { string $oldCurrentDir = `workspace -q -dir`; // Set the directory to the users project area. // string $wsDir = dirname( `workspace -q -fn` ); if (`file -q -ex $wsDir`) { workspace -dir $wsDir; } string $dirPath = sp_getProjectLocation($wsDir); if ("" != $dirPath) { string $cb = ("sp_setLocalWorkspaceCallback \"" + $dirPath + "\""); eval $cb; } // Restore the current directory. The project may have changed, but the cur dir // need not workspace -dir $oldCurrentDir; } // Try to set it directly from the name given else { sp_setLocalWorkspaceWithoutPopupDialog $newProject; } }