What is non-relative version of Transform.translateBy?



I’m working with pymel and am trying to spread selected object inside the range of 10 units.

def spread():
    selList = pm.ls(sl=True)
    for obj in selList:
        rangeX = random.randint(-10, 10)
        rangeZ = random.randint(-10, 10)
        obj.translateBy((rangeX, 0, rangeZ))

This works but it’s relative to current translate of the object. Suppose if I run this function 5 times, each time it would calculate the 10 unit translate from the current position it is moved.

Previously, I was working with maya.cmds and used setAttr for translation of the object, and it worked fine.

setAttr(obj + ".translateX", rangeX)

But now I want to use pymel; for sake of it’s pythonic-ness.


You can among other things do

obj.setAttr('translateX', rangeX)
pm.setAttr(obj.translateX, rangeX)

Then you have the xform command and all kinds of ways to position items.


You can also use this method, using a pm.datatypes.Vector or a list or tuple:

obj.setTranslation([rangeX, 0, rangeZ], space='world')

And when you use this to get positions,

obj.getTranslation(space='world') # or whatever space you want

then you can use all the pm.datatypes.Vector methods on the result.


+1 for .setTranslation use it in my pymel code.