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.

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

Example 5

Suppose you’ve created a MEL script file named bunk.mel in your Maya scripts directory and it contains this procedure:

global proc string bunk()
{
string $fog;
if (rand(2) < 1)
	$fog = "particle";
else
	$fog = "sphere";
return $fog;
}

Further suppose you create this expression:

string $name = bunk();
eval($name);
print($name);

The first expression statement executes the bunk() procedure in the bunk.mel script file. In the bunk procedure, the if-else statement generates a random floating point value between 0 and 2, then compares its value to 1.

If the value is less than 1, the statement assigns the MEL command string particle to $fog. If the value is greater than 1, $fog receives the command string sphere.

The procedure finishes executing and passes the value of $fog back to the calling procedure, bunk() in the expression. This assigns the command string to the variable $name.

The eval function executes the command string stored in the $name. For example, the statement might execute particle, which creates a particle at the origin of the workspace.

The fourth statement displays the contents of $name, for example, particle.

The expression executes each frame and creates a new particle or sphere.