Method for finding exact world transforms of objects

So in Maya, I have two objects, and I’d like to move the target object to the destination object’s world space transform. Short of tracing all the transforms up to the world, is there a way I can do this more elegantly?

I’ve been using this method in my rigging scripts that creates a locator, parent constrains it to the destination object, and parents the target object to that locator and zeroes out the transforms. I then delete the parent constraint and the locator and move on to whatever I have to with the target object. This has been working fine, but I’m wondering if there isn’t an analogous way to move one object (whose transforms are frozen) to the exact world space transforms of another object, which may itself be frozen or in the hierarchy of another transform.

I’ve tried using xform, move, the setTranslation/setTransform methods of the PyNode… nothing seems to work, and I’m wondering if I’m doing something wrong because it just seems like such a common use case.

Does anyone have any advice on this?

xform is what you want.
I generally use it like this:


import pymel.core as pm
parent_pos = pm.xform(parent_obj, query=True, worldSpace=True, matrix=True)
pm.xform(child_obj, worldSpace=True, m= parent_pos )

I’ve used the full flag names for demo purposes.

There are loads of ways to do this in code, but this is what I’ve used previously.

If you want position only, you can replace matrix with translation.

Hope that works for you. :slight_smile:

or

 cmds.delete(parentConstraint( <target>, <object>, mo=False) )

[QUOTE=Theodox;21551]or

 cmds.delete(parentConstraint( <target>, <object>, mo=False) )

[/QUOTE]

Hehe, exactly. Loads of ways to do it! I tend to do it this way too cause it’s one less line of code!

If I’m trying to avoid using constraints (for instance, if the object I need to move is keyframed), I use setTranslation on the pynodes. There’s a handy worldSpace flag, just like the xform command, I just feel that it reads a little more straightforward than xform at a glance:


obj = pm.PyNode(<object>)
target = pm.PyNode(<target>)
obj.setTranslation( target.getTranslation( ws=True ), ws=True )

You can set and get the translation in world space - super handy. PyMel has a lot of sweet hidden flags for those sorts of commands.

1 Like

where is the docs on that command, when i search for it i get this
http://download.autodesk.com/us/maya/2011help/PyMel/generated/classes/pymel.core.nodetypes/pymel.core.nodetypes.Transform.html?highlight=settranslation#pymel.core.nodetypes.Transform.setTranslation

it has a space option, but even when set to world i still cant move a object to worldSpace coords with it.

the best i came up with was this


pos = obj.getRotatePivot()
obj.translate.set(-1 * pos.x, -1 * pos.y, -1 * pos.z)

i use that for centering objects with frozen transforms, the idea can be adpated to move thigns in worldspace too

God how many times has this been asked on the Autodesk Beta forum and yet STILL Maya ships with no simple snap function!! All methods above are valid, one issue you may get is that if you have offset pivots on controllers and you’re trying to align them. If you want a simple snap function then there’s one in the Red9 pack, also capable of snapping entire rigs to other rig world space transforms which is damn handy some times.

Mark