Special Topic 1:  Local and Global Procedures

(this page © 2003 Mark R. Wilkins, all rights reserved)

Like variables, procedures in MEL have scope, meaning that the conditions of how they're defined determines where they can be used.

As a rule, a procedure defined in the script editor, by hand, is global, and like a global variable, can be used anywhere in any MEL script you run, whether it's in a MEL file in your script path or defined in the script editor by typing it in.

Procedures are often defined by putting them in MEL files in the script path, usually in your maya/scripts directory.  Then, either the procedure is defined when you use the source command to load the MEL file, or the file is sourced automatically if (and only if) certain conditions explained below are met.

When you define a procedure in a MEL file, normally it cannot be used outside that file even if you source the .mel file!  For example, make a file containing this in your maya/scripts directory, and name it test.mel:

proc testing () {
  print "it worked!\n";

Now, in the script editor, execute these commands:

source "test.mel";

You'll get an error.  Even though Maya found and ran the file test.mel, it could not see the procedure testing contained within it, because that procedure (like a variable defined in a MEL file) is local to that MEL file.

Now, exit Maya and edit that MEL file to contain this:

global proc testing () {
  print "it worked!\n";

Run Maya again, then run the commands to source the file and execute the global procedure.  Now, you should get a satisfying message saying that your experiment worked.

Using the global keyword to define the procedure, then, makes it available outside the MEL file in which it's defined.

Now, exit Maya and edit the MEL file to contain this:

global proc test () {
  print "it worked!\n";

This subtle change renames the global procedure to have the same name as the MEL file (except for the .mel extension.)  Now, start Maya, and run the procedure without sourcing the file:


The procedure should run without having sourced the file.

Maya is able to automatically source .mel files in your script path if you are calling a global procedure with the same name as the containing file.  So, most of the time, you'll create at least one global procedure inside a .mel file that will serve as that script's entry point, or the first code to be executed in the script.

Procedures that you only want to use within other procedures in that MEL file can be defined with proc alone, and procedures that you want to expose to the outside world of other MEL files or the script editor should be defined with global proc.

Note that sometimes you will install a command (in the form of a procedure you've written) to execute when a user clicks a button in a user interface.  This kind of procedure is often called a button action procedure.  These commands must be declared global, even though it looks like you are using them within the same Maya file.  The reason is that all you are doing when you declare a command to associate with a button is storing the name of the command for later use.  When the button is clicked, Maya, outside that MEL file in which you defined the button, runs that command, and if the command is not global, the button will do nothing.

Questions?  Comments?  Please write to mark@melscripting.com