View Full Version : MEL: position of an object over the last 3 frames

04 April 2003, 03:34 PM

Any idea how i can access the position of an object over the last 3 frames in a MEL runtime expression evaluated perframe.


04 April 2003, 04:55 PM
Use the time flag on getAttr to evaluate the object translation at a previous frame:
float $pos[3] = `getAttr -time ( time - 1) myObject.t`;


04 April 2003, 05:48 PM
thx very much

04 April 2003, 07:48 PM
Your use of "runtime expression" makes me wonder if you're doing this in a particle expression? But then you said "per frame," so I'm confused.

Doing a getAttr like this in an expression can be very dicey, because Maya won't necessarily execute the expression if the actual input attributes (the ones you access without a getAttr) haven't changed. This will give you funky behavior whenever you have two identical input states in a row, because the results of the getAttr will be changing but the inputs will not and the expression will not execute.

Also, if you're doing this in a particle expression, you may want to think of another approach because it will be verrrry slow. (Another approach might be to use userVectors to cache old positions.)

-- Mark

04 April 2003, 09:21 PM
When using "getAttr -time" to get values in an expression you can still get good updates by simply assigning the attributes to a dummy variable, i.e..
float $pos[3] = `getAttr -time (frame - 10 ) myObject.t`;
float $dummyX = myObject.tx;
float $dummyY = myObject.ty;
float $dummyZ =;

This creates connections from the object translate to your expression, which insures that the expression updates when the object's translate changes.


04 April 2003, 10:21 PM
i also saw a solution on the 'fast animation rigs' dvd

it uses a frameCache node. it gives you access to specefic values in an animation curve. on the dvd jason created it using MEL. i don't remember the specifics on how to create the node, but maybe you can find it in the MEL reference.

04 April 2003, 10:34 PM
Yea im using a rigid body with some particles in the same scene, the particles are colored depending upon the position of the rigid body of the last few frames. So i prolly got a bit confised with my terminology.

04 April 2003, 10:56 PM
Duncan's point is excellent, but I think I wasn't perfectly clear:

In the event where tx, ty, and tz do not change for a couple of frames in a row but there has been recent change (within the window of past frames being examined by getAttr) you still won't get proper updates with that solution, because the results of the getAttrs will be different but the values of the actual input connections will not themselves be changing.

As for frameCache, I played around with that for a while and got very inconsistent results -- I wonder if there's some magic dance one has to do to make it work properly?

-- Mark

04 April 2003, 12:13 AM
Mark, I think it should update every frame if alwaysEvaluate is turned on for the expression( the default setting ).


04 April 2003, 02:24 AM
hehe oops! Is that the default setting? :D

Anyway, as long as that works and you are careful not to getAttr on something that requires massive amounts of calculation (attrs fed by animation curves may be OK, attrs fed by a mess of IK-driven deformers acting on geometry may not be) it should be fine, I agree.

-- Mark

04 April 2003, 10:05 AM
hmm.. does that work at all?

does maya really evaluate the current position of a rigid body/particle system when you query it?
it would need to do a runup for each queried frame which would make that expression terribly slow.

to get it work you can either create a cache for the particles and/or bake the dynamics simulation for the rigid body.

another thing would be creating 3 new per particle attributes which you fill with the position values from the frames in a runtime expression.. but i think what you needed is for the rigid body



04 April 2003, 10:12 AM
oops again, I didn't read carefully enough to note that he was trying to get the values of dynamics data as opposed to object attribute values.

-- Mark

04 April 2003, 04:06 PM
Yes. Rebo if you need to get the value of rigid body positions at different points in time, then you should first bake the rigid body solution before attempting to use "getAttr -time".


04 April 2003, 01:51 AM
Thanx guys, i have this clearer in my head now.

CGTalk Moderation
01 January 2006, 01:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.