MEL How-To #18

Back · Previous · Next Maya

How do I get information for a motion curve value at a specific time?

If you're looking for the ‘.uValue’ at a specified time:

// Create a curve:

string $curve = `curve -d 3 -p -7.425213 12.202613 1.439619
  -p -7.750539 10.676282 8.273118 -p -8.401192 7.62362 21.940116
  -p 25.720537 -16.234996 -7.200271 -p -12.58179 -3.496693 -16.762239
  -p -5.70914 0.8393886 -24.073161 -p -2.272816 3.007429 -27.728622
  -k 0 -k 0 -k 0 -k 1 -k 2 -k 3 -k 4 -k 4 -k 4`;

// And a sphere:

string $sphereA[] = `polySphere -r 1 -sx 16 -sy 8 -ax 0 1 0 -tx 1 -ch 1`;
string $sphere = $sphereA[0];

// Assign the sphere to a motionPath of the curve:

string $motionPath = `pathAnimation -fractionMode true -follow false
  -startTimeU 1 -endTimeU 30 -c $curve $sphere`;

// Get the .uValue of the resultant motionPath at frame 12:

getAttr -time 12 ( $motionPath + ".uValue" );
// Result: 0.3793103 //

** Warning! The '.uValue' attribute does not respect the Working Units Preference! If your Unit Preference is set to something other than "centimeter" then the value you get will be off by a factor of 10^n. For example, if your Working Units are set to "meter" then you will have to multiply this value by 100 to get the proper result. This has been logged with A|w as Case ID #35184.

If you know the name of the curve but not the name of the associated motionPath you can use my ‘getMotionPath()’ function from Highend3D to derive the latter. It's in the "Animation" section of the MEL Scripts.

If you're looking to get keyed values along the motion path...

// Add a keyframe to the motionPath at frame 6:

currentTime 6;
// assuming Working Unit == "centimeter"
setKeyframe -v 0.4 -at uValue $motionPath;

// Now get the motion key information for this motionPath:

keyframe -q -timeChange -valueChange $motionPath;
// Result: 1 0 6 0.4 30 1.0 //

This returns a float array as pairs of numbers in the order { timeChange, valueChange }. Read as:

  At frame 1, uValue == 0.0 (start of curve).
  At frame 6, uValue == 0.4.
  At frame 30, uValue == 1.0 (end of curve).