Thursday, November 9, 2006

simple dynamics in MEL

I've often wondered how to build an expression in Maya/MEL that would implement some simple dynamics. For instance, if I want an attribute to be dependent on an object's velocity, how can I get the velocity?

I learned yesterday that you can use the "keyframe" command within an expression to query for the value of an animation curve at a different time.
This isn't an ultimate answer, but it provides a good tool to getting closer.

So here's some code for determining the frame rate of change of an attribute. In this case, pSphere1's tx attribute.

// get the previous frame
$newt = frame - 1;

// query pSphere1's tx anim curve from that frame
$oldx = `keyframe -at tx -t $newt -q -eval pSphere1`;

// this is ugly, but it turns the array result into a float.
// someone please show me how to do this "properly"!
float $ox = $oldx[0];

// now compute the difference
$delta = $ox - pSphere1.translateX;

(and now for search indexing, since these are the keywords I was searching for to find this answer: velocity mel expression dynamics)