MEL How-To #93

Back · Previous · Next Maya

How do I create a file dialog for which I can define the default starting directory?

For Windows NT:

For a file-selection dialog the 'fileDialog' command provides the functionality of specifying a default starting location and filetype.

string $selectedFile = `fileDialog -dm "D:\\joe\\mayastuff\\*.mb;*.ma"`;

For a folder-selection dialog you can use the ‘fileBrowserDialog’ command, but it does not have any direct means to define the starting directory. You can do it, however, with a little help from the ‘workspace’ command.

global proc browseForFolder( string $startFolder )
  string $mayaFolder;

  // Get current directory.. we'll restore this when we're done
  $mayaFolder = `workspace -q -dir`;

  // Set the current directory to our own.
  // Note that this will set Maya's workspace folder even
  // if the specified folder does not exist.
  workspace -dir $startFolder;

  // Present the user with a folder-selection dialog.
  // It will default to $startFolder.
  // The $mayaFolder is included as the first argument for the callback.
  fileBrowserDialog -mode 4
    -fileCommand ( "browseForFolderCallback \"" + $mayaFolder + "\"" )
    -actionName "Pick Your Folder";

global proc browseForFolderCallback( string $mayaFolder, string $result, string $type )
  // Do whatever you need to with the $result
  print ( "Folder selection: " + $result + "\n" );

  // Restore the original folder path
  workspace -dir $mayaFolder;


browseForFolder( "C:/projects/gameData/" );


I know of only one command that will allow you to define the default directory for a file dialog. The command is not in the docs, but you can investigate it under the file:


You need to set a global variable to define the default starting location; e.g.:

global proc myFileBrowserCB( string $file, string $fileType )
  print ( "File selected: " + $file + "\n" );

global proc myFileBrowser()
  global string $gDefaultFileBrowserDir;
  $gDefaultFileBrowserDir = "/usr/joe/mayastuff/";

   fileBrowser( "myFileBrowserCB", "Open Joe's File", "mayaBinary", 0 );

Suggestion to A|w: Please consolidate all 'fileDialog', etc. functions into one command that works universally under UNIX and NT: default starting folder, file- or folder-selection, file selection mask, allowance for entering filenames which do not yet exist (e.g. save dialogs), definable dialog titles and button labels, proper return status and/or callbacks for success/failure/no selection.

Monday, April 10, 2000