Mel Script - The Basics




The entire user interface of Maya is driven by the Maya Embedded Language (mel for short). To begin with, open with script editor by clicking on the button in the bottom right hand corner of Maya....






The Script Editor

The script editor is the way in Maya that is available to edit and run mel scripts. The top pane of the editor is a results window where output from your scripts will displayed, or compilation errors. The bottom pane is where you edit your scripts.







In the bottom pane, type....


... and then press the little enter key (or control+enter if you are on a laptop). That should have executed the sphere command to create you a NURBS sphere within the Maya Scene.

In the script editor you have to use the little enter key to execute scripts, the large enter key is a newline needed when using a text editor. You may have noticed that after executing the script, the contents of the script editor vanished. Well, this is slightly annoying since you may still be in the process of working on the script.

To avoid this, simply highlight the lines of code to execute (ctrl+a is good for this) and then press the little enter key. The script will be executed as before however the code will remain in the script editor for further editing.







Useful Script Editor Menu Options

On the file menu we can open a file for editing, source (execute) a script, or save the current script either to the shelf or to a file.





On the edit menu we have the standard edit options you'd expect, but you also have 3 options at the bottom to clear the history (top pane), clear the input (bottom pane) or simply clear both.





On the script menu, there are two settings you'll be wanting to turn on (trust me, you will). They are, "Show Line Numbers" and "Show Stack Trace". The first option will print the line number that a compilation error occurred on; the second will print some info about where the script went wrong at runtime. Without these two options on, debugging scripts is almost impossible.





Some people may have assumed that turning on the line numbers would give you line numbers down the side of the script editor. No such luck i'm afraid. The best method I can suggest is to work in an external text editor such as kate(linux) or textpad(Win32) to do your editing and save the script to the hard drive. By using source script from the file menu you can run the script (or alternatively use the source command).

Update: Maya 8 has now added line numbers to the script editor. If you use the Ctrl+Shift+L shortcut, you can now get two sets of line numbers if you really want. The latter set of line numbers may actually be more useful since it actually handles the case of selecting text and executing.






Within your scripts, it is useful to write small reminders, or comments to describe what the code is doing. // indicates that the text following it until the end of the line is a comment and not part of the code.



		// print some text to the results window. \n indicates a newline
		print( "hello world\n" );



Random Script Editor Shortcuts

There are a few useful hotkey shortcuts for the mel script editor within Maya, it's just that people don't seem to know they exist!



	ctrl + shift + '>' - Increase font size

	ctrl + shift + '<' - Decrease font size

	ctrl + shift + '=' - Toggle font size

	ctrl + shift + 'L' - line numbers for selected text (press a few times)

	ctrl + 'E' - Aligns text to the center.

	ctrl + 'L' - Aligns text to the left.

	ctrl + 'R' - Aligns text to the right.

	ctrl + '1', or ctrl + '2', or ctrl + '5' - change font spacing
	ctrl + return - executes the current script.	
	ctrl + backspace - delete a word.	
	ctrl + left cursor - skip back a word	
	ctrl + right cursor - skip forward a word	
	ctrl + return - executes the current script (useful for laptops!).	
	ctrl + Middle Mouse Wheel - use scrolling to control font size

	ctrl + shift + 'a' - Toggles selected text between lower & upper case

	ctrl + shift + 'm' - Execute selected code then deletes it