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

  1. 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.

  2. Can embed commands. For example: eval("sphere; cone; ls");
  3. Entire command, including its arguments, must be a single string. For example: eval("ls -type transform");

backquotes

  1. Immediate evaluation.
  2. Can not embed commands.
  3. Do not need to put string arguments in quotes. For example: string $trans2[] = `ls -type transform`;
  4. 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`;

( )

  1. Immediate evaluation.
  2. Can not embed commands.
  3. 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");