query world position of objects at time (after changing scene linear units)

query world position of objects at time (after changing scene linear units)

bmagner888
Enthusiast Enthusiast
1,253 Views
3 Replies
Message 1 of 4

query world position of objects at time (after changing scene linear units)

bmagner888
Enthusiast
Enthusiast

Hello,
How can I query the world space position at a specific time in the same units as the scene?  I do not want to have to switch the currentTime because in heavy scenes that can cause significant slowness because Maya may have lots of other stuff to calculate just by switching frames.

I'm trying to animate a gravity simulation.  To obtain the object's initial velocity I need to know an object's world position (regardless of hierarchy or translate values) at time1 and at time2.  My method was to do the following:

 

import pymel.core as pm

thisObject  = pm.ls(sl=True)[0]
world_matrix = pm.getAttr(thisObject +'.worldMatrix', t= time1)

p1 = world_matrix[3]

y1 = p1[1]

world_matrix = pm.getAttr(thisObject +'.worldMatrix', t= time2)

p2 = world_matrix[3]

y2 = p2[1]


v = (y2-y1)/(time2-time1)

However:  it's not calculating correctly if I change the scene units from cm to meters.  This can lead to my velocity calculation being off by a factor of 100, as the world matrix still returns cm values when all the other calculations are done in meters.  When things get moved around, it can also lead to incorrect negative values where the object's worldMatrix position is different from its actual position:

 

p1 = pm.getAttr(thisObject +'.worldMatrix')[3] #-->[347.522, -58.995, 0.0, 1.0]

p0 = pm.xform(thisObject , q=True, rp=True, ws=True, a=True) #--> [-2.158, 0.4704, 0.0]

p1 != p0


I don't really care that these two  query methods don't match, but I need a reliable method of querying worldspace positions at different times (which is impossible with xform) that yield results that are consistent with the scene's linear units.

Ideally, everything would be built in the right units, but we all know that usually doesn't always happen in a production environment.

 

0 Likes
1,254 Views
3 Replies
Replies (3)
Message 2 of 4

mcw0
Advisor
Advisor

getAttr has a time option...oops, I see you already know that.  Since Maya works internally in cm, you will just have to account for that in the return values.

0 Likes
Message 3 of 4

bmagner888
Enthusiast
Enthusiast

I actually found a solution.  The trick is to create a ghost locator via script that you can point constrain to your target object and delete when script is complete.  This will trick maya into telling you where the locator is at any time using getAttr, which for the locator in worldspace (unparented) returns world space coordinates. 

ws_loc = pm.spaceLocator()
pointConst = pm.pointConstraint(thisObject, ws_loc,offset=(0,0,0))
p1 = pm.getAttr(ws_loc+'.translate', t=time1)

p2 = pm.getAttr(ws_loc+'.translate', t=time2)
pm.delete(ws_loc)

It would still be nice if Maya had a better built-in function for querying worldspace coordinates.  It's only been, what, 24 years since Maya's been around?

0 Likes
Message 4 of 4

mcw0
Advisor
Advisor

Locators have world position attribute.  You can just parent a locator under the node you want to track.

0 Likes