Get positional information from one object and apply it to another

Question:

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?

Asked By: Heelaque

||

Answers:

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 )
Answered By: Ennakard

Something which is also possible with those commands :

pos = cmds.xform('pSphere1', ws=True, q=True, t=True)
cmds.setAttr('pSphere2.t', *pos)
Answered By: DrWeeny
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.