Mel
Curves & Expression Animation


return to main index



Introduction

If an expression is assigned to the cv's (control vertices) of a curve their positions can be dynamically changed from one frame to the next. As a consequence the curve will continually change. This tutorial shows how a mel script can assign an expression to one or more cv's of a curve. The mel script, listing 1, shows the basic technique of accessing a cv and assigning an expression to it.


Listing 1


proc wiggleLast(string $name)
{
string $cv[] = `ls -flatten ($name + ".cv[0:3]")`;
string $exp = "";
$exp += "setAttr \"" + $cv[3] + ".xValue\" (sin(frame));";
$exp += "setAttr \"" + $cv[3] + ".yValue\" (sin(frame));";
expression -s $exp -ae 1;
}
// Create a curve  
string $obj = `curve -d 3 -p 0 0 0 -p 1 0 0 
                          -p 2 0 0 -p 3 0 0`;
// Assign the expression
wiggleLast($obj);

The point of interest in the script is the use of the ls proceedure to generate an array of names of the cv's with indexes from zero to 3. Although not all cv's are required, referencing all of them is done so that the last cv can be accessed via its absolute index of 3. Had the ls command been used in this way,

    string $cv[] = `ls -flatten ($name + ".cv[3:3]")`;

The $cv array would have had only one item, consequently lines 5 and 6 would have had to reference the name of the cv in this way.

    "setAttr \"" + $cv[0] + ".xValue\" (sin(frame));";
    "setAttr \"" + $cv[0] + ".yValue\" (sin(frame));";

Using index 3 in the way shown in listing 1 makes the code easier to understand. The command setAttr is used twice to modify the x and y values of a cv.


Listing 2


proc wiggleAll(string $name)
{
string $exp = "";
string $cv[] = `ls -flatten ($name + ".cv[0:3]")`;
  
// cv[0] remains "fixed"
for($n = 1; $n <= 3; $n++)
    {
    string $item = $cv[$n] + ".yValue";
    string $trig = "sin(" + $n + " * frame)";
    $exp += "setAttr \"" + $item + "\" (" + $trig + ");\n";
    }
expression -s $exp -ae 1;
print($exp + "\n");
}
  
string $obj = `curve -d 3 -p 0 0 0 -p 1 0 0 
                          -p 2 0 0 -p 3 0 0`;
wiggleAll($obj);

The second mel script, listing 2, also "grabs" the names of all the cv's but uses a for-loop to assign an expression to each cv. The script takes advantage of the pre-defined variable frame (no dollar symbol) that can be used within an expression - frame cannot be accessed directly for a users proc.



Figure 1




© 2002- Malcolm Kesson. All rights reserved.