Mel Script - fileBrowser

 

 

 

Maya is very rarely used in isolation, for example you may also be using gimp/photoshop for textures, shake/after effects for compositing, prman (renderman) for rendering etc. It is therefore of great importance to be able to create simple file dialogs to locate files.

If you look in the mel documentation that comes with Maya, two procedures are listed to create a file dialog, fileDialog and fileBrowserDialog. Unfortunately they are both rubbish. Luckily, hidden away in a dark undocumented corner of Maya is a little script called fileBrowser.mel.

It turns out that this undocumented command is what we want for file dialogs...

 

 

 

 

Example 1 : A simple file dialog for opening a file

A common place you will see a while loop is when you are reading a file. Since we do not know the size of the file, we need to loop until we reach the end.

 


		// This procedure is called when the 'Open' button of the dialog is clicked.
		// The procedure recieves the name of the file and it's extension
		proc int onOpen(string $filename,string $type) {
		   print($filename+"\n");
		   print($type+"\n");
		   return true;
		}
		
		// The final parameter indicates the type of file dialog. 0=Open File Dialog
		// The 1st parameter is a function to call when OK is pressed.
		// The 2nd parameter is the text to appear on the OK button.
		// The 3rd parameter is the type of file, eg "mb", "ma" etc.
		fileBrowser( "onOpen", "Open", "", 0 );

 

 

 

Example 2 : A simple file dialog for saving a file

We only need to change the final parameter for fileBrowser to get a save dialog.

 


		// This procedure is called when the 'Save' button of the dialog is clicked.
		// The procedure recieves the name of the file and it's extension
		proc int onSave(string $filename,string $type) {
		   print($filename+"\n");
		   print($type+"\n");
		   return true;
		}
		
		// The final parameter indicates the type of file dialog. 1=Save File Dialog
		// The 1st parameter is a function to call when OK is pressed.
		// The 2nd parameter is the text to appear on the OK button.
		// The 3rd parameter is the type of file, eg "mb", "ma" etc.
		fileBrowser( "onSave", "Save", "", 0 );

 

 

 

Example 3 : Browsing for a folder

fileBrowser can also browse for folders.

 


		// This procedure is called when the 'Open' button of the dialog is clicked.
		// The procedure recieves the name of the file and it's extension
		proc int onOk(string $dirpath,string $type) {
		   print($dirpath+"\n");
		   return true;
		}
		
		// The final parameter indicates the type of file dialog. 4=Folder Dialog
		// The 1st parameter is a function to call when OK is pressed.
		// The 2nd parameter is the text to appear on the OK button.
		// The 3rd parameter is the type of file, somewhat meaningless here
		fileBrowser( "onOk", "Text", "", 4 );