Chapter 5: The Script File


click to expand

Overview

Now that we have a fundamental understanding of MEL procedures, commands, and how we structure them, we can consider actual scripts. A MEL script is simply a series of commands and procedures stored in a text file that ends with the extension .mel .

To the artist working on a single workstation, simply dragging MEL code to the shelf to create buttons might be enough, but what happens when those tools need to be transferred to a new computer? Using this method, there is no manageable way to distribute custom tools to a studio full of artists , let alone over the Internet, if that is the goal. By using MEL scripts, we create easily distributable files that allow the hard work of a scripter to be shared with others. More than just that, however, putting procedures into individual MEL scripts allows the creation of modular tools, and stops the repeated recreation of common procedures.

In the last chapter, we discussed the concept of a global procedure, and how those procedures act as custom commands within Maya. The question still unanswered is, how does Maya know that those commands exist? Maya actually has no clue that a procedure exists until one of two things happens ”either we execute the script or we execute the Maya command source on the script.

A short explanation is in order as to how Maya treats MEL scripts. Whenever a command is sent to Maya, whether it is at the command line, through another script, or from a menu in the Maya interface, Maya searches all the commands currently in memory to see if the command is known; if not, Maya searches for a MEL script file with the same name . For example, if we were to execute the command myProcedure at the command line, Maya looks for a command called myProcedure in memory. If it does not find it, Maya first searches the script directories for the user , then the script paths internal to Maya for a file called myProcedure.mel . Once finding that file, Maya searches for a global procedure within that script file called myProcedure , as seen in Example 5.1.

Example 5.1: The declaration of a global procedure called myProcedure, stored in a file called myProcedure.mel .

 //  Begin myProcedure.mel     global proc myProcedure ()     {         print "I am myProcedure.mel" ;     } 

If Maya cannot find a file called myProcedure.mel or if myProcedure.mel does not contain a global procedure called myProcedure , Maya will return an error.

The order of directories that Maya searches to look for a script is not arbitrary. By searching the user directories before Maya s internal directories, Maya allows us to customize the tools it provides as MEL scripts without altering the original. source

Once a procedure has been loaded into memory, Maya uses that procedure until a new procedure of the same name is sourced into memory. Sourcing is the method by which we update a procedure within Maya s memory, and is most often used during the construction of a script. Writing scripts is an iterative process, with the script essentially being built in layers . Even if we make changes to the physical .mel file, those changes are not reflected within the executable command unless the script is first sourced. While the extra step to source a file is a hassle, it aids in the quick execution of scripts, allowing them to reside in memory.




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