Search code examples
pythonmaya

Get positional information from one object and apply it to another


I am trying to query the location of an object (x,y,z coordinates) with xform, and then set the values harvested from xform to use in a setAttr command to influence the translation of a different object.

pos = cmds.xform('pSphere1', r=True, ws=True, q=True, t=True )
print(pos)
cmds.setAttr('pSphere2', tx=pos[0], ty=pos[1], tz=pos[2])

The print command is providing me with the correct coordinates however the setAttr command isn't picking them up and using them.

I'm getting the error:

Error: TypeError: file line 1: Invalid flag 'tx'

Is this something to do with the 'data type' of the xform being "linear" and the setAttr being something else? If so, how do I work around or convert?


Solution

  • You are supposed to use it like that :

        cmds.setAttr('pSphere2.tx', pos[0])
    

    and your query should be

        pos = cmds.xform('pSphere1', ws=True, q=True, t=True )
    

    To apply you can also do

        cmds.xform('pSphere2', ws=True, t=pos )