Chapter 3: MEL Basics


click to expand

MEL is not just a small part of Maya ”MEL is Maya. Nearly every feature of Maya is accessible via the language, and the large majority of the interface is created with MEL scripts. So, how exactly do we access MEL within Maya?

The Interface: Working with MEL in the Maya UI

Within the Maya interface, there are three ways to enter and execute MEL commands. The first and perhaps most obvious is the Command Line .

The Command Line

The Command Line, seen in Figure 3.1, allows us to quickly and easily enter MEL commands one line at a time.

click to expand
Figure 3.1: The Command Line.

The Command Line, and its sibling UI element the Command Feedback area, let us quickly execute MEL commands and review any information that might be returned. Many Maya users would no doubt be surprised at the number of functions that can be more easily accomplished with one quick command than by using the interface. For example, if we wanted to turn on rotational snapping we would use the command manipRotateContext as seen in Example 3.1

Example 3.1: A basic MEL command.

 manipRotateContext -s on; 

While entering that one command at the Command Line over and over might not seem efficient, the fact that we can attach that MEL command to a hotkey or put it into a marking menu suddenly makes it much more appealing. Or, if the Command Line has the cursor s focus, we can use the Up and Down arrows to navigate through the history of commands we have executed.

Tip  

In the Maya Preferences, under Interface, we can set the Command Line to hold the cursor focus after we enter a command. By default, Maya returns the cursor focus to the main workspace. Of course, this being a book about MEL, we should know that we can also set it by entering the command optionVar -iv commandLineHoldFocus true;

The Command Shell

The Command Shell , seen in Figure 3.2, which is opened under the menu Window>General Editors>Command Shell, is essentially the Command Line and Command Feedback area all in one.

click to expand
Figure 3.2: The Command Shell.

Similar to a Unix Terminal or DOS prompt, we issue a command and receive the feedback in the same window. In most ways, the Command Line or the Script Editor (to be discussed next ) are superior ways of entering MEL commands. The Command Shell is significantly less useful than the Script Editor, takes up a lot of precious screen real estate compared to the Command Line, and offers no advantage over either. It is, however, a user interface control command ( cmdShell ) and can therefore be included in our own UI layouts, if we so desire . See Chapter 10, "Creating Tools in Maya," for details on creating our own windows and user interface elements.

The Script Editor

The Script Editor is a separate window, opened by using the menu under the Window>General Editors>Script Editor, or by using the button to the far right of the Command Feedback area. The Script Editor, seen in Figure 3.3, is split into two areas, History and Input.

click to expand
Figure 3.3: The Script Editor.

As an artist works in Maya, most of what is done within the interface is echoed within the History area. This is an excellent way to learn what different commands do, as well as learn the various flags different commands have. In fact, many people start their scripting by simply copying and pasting commands from the History area. This process, sometimes called harvesting , is done all the time by even the most experienced scripters. Only the commands that Maya considers significant are echoed within the History area, however. These are almost never all the commands issued, nor are they necessarily all the commands we might want to learn about. Three options can be set within the Script Editor to aid in the education of a scripter; the option panel is seen in Figure 3.4.

click to expand
Figure 3.4: Script Editor options.

Edit > Echo All Commands turns Maya into an echoing machine. By turning this option on, nearly everything done within Maya is echoed. Sometimes the feedback can be a little too much, and a user can suffer from command info overload. By working occasionally with the option off as well as on, a scripter can learn how Maya works on a behind the scenes level. This is how we often learn about panel construction within the user interface, as well as commands that are executed as MEL scripts, rather than MEL commands. The difference is subtle but important, and will be discussed later in this chapter.

The other two options, Show Stack Trace and Show Line Numbers, both aid in the debugging of scripts and procedures. Stack tracing follows the flow of commands from one script file to another. When we use inter-dependent scripts, stack tracing is a good idea. Show Line Numbers allows us to quickly and easily find errors in a command. The error will be reported as two numbers separated by a period, seen in Example 3.2.

Example 3.2: An error as issued by Maya.

 //  Error: Line 1.6: Syntax error  // 

The number preceding the period is the line number, or the number of lines down from the beginning of the executed code. The second number is the number of characters over from the left to the right. It basically nails down the point where Maya becomes confused . Always keep in mind that an error on one line might actually be caused by an error made on another, be it not terminating a command line or a simple misspelling.

The input area of the Script Editor lets us enter multiple commands, or even full scripts. One thing about working within the Script Editor is the different behaviors of the two Enter keys on the computer keyboard. The main Enter key behaves as would be expected in any text editor, and goes to the next line. To actually execute MEL code, there are three options. We can select Script>Execute from the Script Editor menu, use the CTRL+Enter shortcut, or use the Enter key on the numeric keypad to execute commands. If any text is highlighted in the input area, only the highlighted portion is executed. This is an excellent way to debug specific passages of code. In addition, when code is executed while a passage is highlighted, the input area of the Script Editor does not automatically clear itself, as it does when code is simply run. This is a great aid when working out the specifics for a command, saving a user from having to type it in again and again.

Now, a point of exquisite irony. For all the options that Maya offers for entering MEL commands, the majority of scripters don t use any of them to actually create scripts. Sure, the Command Line is used to quickly access a single command, and we will use the Script Editor to create quick little shelf buttons . However, for the good stuff ”the down-and-dirty stuff that probably makes a user want to go out and buy this book ”we ll want to use a dedicated text editor. Many have features dedicated to programming, and can be highly customized to work with the MEL language. Available programs vary from platform to platform, although some popular ones such as vi are available on multiple operating systems. All the scripts for this book were created in Visual SlickEdit, a text editor designed for programming in languages such as C++ and Java that is easily adapted to work with MEL.




The MEL Companion
The MEL Companion: Maya Scripting for 3D Artists (Charles River Media Graphics)
ISBN: 1584502754
EAN: 2147483647
Year: 2003
Pages: 101

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net