World space coordinates

Hi,

I’m trying to get the position of a mesh in world space and I’m wondering why xform doesn’t work.
cmds.xform( mesh, q=True, a=True, translation=True )

My mesh is not at the origin but yet the command gives me (0,0,0). Do I automatically lose the world space coord if I freeze it out? If so, what should I be using to get it back?

Thanks!

The a (absolute) flag doesn’t affect queries–it’s only for performing transformations.

You can query the worldSpace pivot (rotate or scale) of an object (ws=True, rp/sp=True) to get the pivot’s worldSpace coordinate.

There may be another way, but that’s what I use.

If you freeze transforms, the geometry is moved relative to the local coordinate space ( you can see this by selecting the shape and checking the ‘CV’s - click to show’ area) . The position of the frozen object really is ‘0,0,0’, although the pivot is offset to make it appear as if were in the same place. Xform is reporting the truth (you can check it by looking at the values in the attribute editor) – the object is now at 0,0,0.

You can freeze rotations and scales without losing the position by turning off the ‘translate’ option in the freeze transform dialog or makeIdentity (apply = True, t=False, r=True, s=True).

If you have a frozen object and you want to get back the lost transform:

  1. get the current pivot position with xform (q=True, rp=True, ws=True)
  2. move the object by (-1 * the pivot position), which centers it at origin
  3. freeze transform again, resetting the verts
  4. move it back to the orginal pivot position with xform(t=(?,?,?), a=True)

Hey,

Here’s a python script I’ve written as part of my own little tool suite which does exactly that (multiple object support)


"""
take all selected objects and make the transform "correct" in worldspace
"""
import maya.cmds as cmds

def reset_world_transform():
    selection=cmds.ls(selection=True)
    print str(selection)
    
    for node in selection:    
    
        print (node)
        cmds.select(node)
        #freeze transformation
    	cmds.makeIdentity(apply=True, t=1, s=1, n=0)   
        #move to world 0
        cmds.move ( 0,0,0, [node], rpr=True)
        #store resulting transformation
        position=cmds.xform(q=1, t=1, ws=1)
        #change stored position to inverse version of itself
        position[0]=(position[0]-(position[0]*2))
        position[1]=(position[1]-(position[1]*2))
        position[2]=(position[2]-(position[2]*2))
        #freeze transformation
        cmds.makeIdentity(apply=True, t=1, s=1, n=0) 
        #move to original world position, with correct transformation intact
        cmds.move (position[0],position[1],position[2], [node], r=True)    
        cmds.delete( node, ch=True )
    
    cmds.select(selection, r=1)

Hope it helps!