Calling procedures

 
 
 

Using a defined MEL procedure is the same as using any other MEL command or function. To execute the above MEL procedure helloValue, enter the name of the MEL procedure in a script, the Script Editor, or the Command Line.

helloValue( 1, "Jake" );
// Result: Hello Jake, number 1 //

The helloValue procedure requires an integer and a string argument in order to be successfully called. Another way to execute a procedure does not use parentheses or commas. For example, to execute the helloValue procedure this way, enter the following:

helloValue 7 "Torq";
// Result: Hello Torq, number 7 //

Calling external procedures

If Maya encounters a command without a definition, it searches through the script paths for a MEL script with the same name as the command (minus the .mel extension on the file name).

If it finds the file, it declares all the global MEL procedures within that file, and if the procedure you called exists in the file, it executes.

For example, you have a file sayWhat.mel in one of the script folders with the following contents:

// This is a local procedure
// that is only visible to the code in this file.
proc red5() {print("red5 standing by...\n");}
// This is a global procedure. When this file
// is sourced, this procedure will become
// available.
global proc GoGo() {print("GoGo online\n");}
// This procedure has the same name as this file
// (without .mel on the end).
global proc sayWhat() {print("sayWhat online\n");}

Now if you try to call the function sayWhat on the command line:

  1. Since there is no internal command sayWhat, Maya searches through all its script paths for a file called sayWhat or sayWhat.mel.
  2. If it finds the file sayWhat.mel script in one of the script directories, Maya checks whether a procedure with the name you tried to call exists in the file. If it does, Maya calls the procedure. In this example, a procedure named sayWhat exists in the file and so it executes and prints:
sayWhat online
  1. Since the GoGo global procedure has been declared, you could now type GoGo in the Command Line or Script Editor to execute the procedure.
    Note

    When you create and save a new MEL script, you must source it before externally calling a procedure coded in your new script. This is because Maya sources scripts at startup and on command, not when you try to call an external procedure from the Command Line or Script Editor. To source the script, you can use the File > Source Script command in the Script Editor, or you can restart Maya. For more information about sourcing scripts, see Script files.

Creative Commons License Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License