eval

Executes a MEL command.

string eval(string command)

command is either a command string enclosed in quote marks or a string variable containing a command.

The returned value contains command output returned by the command’s execution.

Note:

The eval command is executed at the top level MEL scope. Therefore, it recognizes global variables but does not recognize variables that you define inside a procedure (local variables). See Global and local variables for more information.

Example 1

eval("select -cl")

Executes the command select -cl, which deselects all objects in the scene. Though the return value is not used in this example, it contains the command output.

Example 2

string $cmd = "select -cl";
eval($cmd);

The first statement assigns the command string select -cl to the string variable $cmd. The second statement executes the contents of $cmd, which is the command select -cl.

Example 3

string $mycommand = "sphere";
eval($mycommand+"-r 5");

The first statement assigns the string sphere to the variable $mycommand. The second statement appends -r 5 to the string sphere and executes the complete command sphere -r 5. This creates a sphere with a radius of 5 grid units.

Example 4

string $a[];
$a = eval("ls -lights");
print($a);

The first statement defines an array of strings named $a. The second statement executes the MEL command ls -lights, then assigns the command’s output to array $a. The third statement displays the contents of $a to the Script Editor as follows:

ambientLightShape1
directionalLightShape1

Each line of command output appears on a new line. Each command output line is an array element. Maya formats array output with each array element on a new line.