Show in Contents
Add to Favorites
Home: Autodesk Maya Online Help
How do I execute a statement created at runtime?
Scripting and syntax
How can I stop a MEL script that is running?
What is the difference between eval, backquotes,
and ()?
Commands and procedures are executed in the
same way. The following examples illustrate this point.
proc float myTime(string $dummyFlag, float $time) {return $time;}
currentTime -e 1;
myTime -e 1;
currentTime "-e" "1";
myTime "-e" "1";
currentTime("-e", 1);
myTime("-e", 1);
To execute a command or procedure and get the
return value you could use the eval,
``, or ( ) syntax as the
following examples illustrate.
string $transforms[];
$transforms = eval("ls -type transform");
$transforms = `ls -type transform`;
$transforms = ls("-type", "transform");
There are important things to remember when
using each of these types of syntax. Below are the key differences
between them. Read them so you know which one to use.
eval
- Allows for delayed evaluation. Normally
when a script is executed, if a command is not defined, Maya will
still try to execute it.
For example, if you try to execute a script
that first loads a plug-in and then immediately executes it, the
script will fail when the plug-in command is executed. This is because
Maya initially evaluates the script to check for commands that it
does not know. However, if the plug-in command is executed using
the eval syntax then the script will not fail.
- Can
embed commands. For example: eval("sphere;
cone; ls");
- Entire
command, including its arguments, must be a single string. For example: eval("ls -type transform");
backquotes
- Immediate evaluation.
- Can
not embed commands.
- Do
not need to put string arguments in quotes. For example: string $trans2[] = `ls -type transform`;
- Can
not use this syntax as a stand-alone command because it’s intended for
assigning the result of one command or building another command. For
example, you can not do the following: `ls
-type transform`;
( )
- Immediate evaluation.
- Can
not embed commands.
- Must
put string arguments in quotes. For example, if $faces represents
the number of faces in an object, you might use ( ) to build a string
to print out.
print("This object has
" + $faces + " faces \n");