MAYA Python Script , getting a position of an selected object using GetAttr

Chico Spans picture Chico Spans · Nov 11, 2014 · Viewed 16.3k times · Source

I am trying to make a simple "allign tool" in maya using Python script, and this is how far I got

import maya.cmds as cmds

selected = cmds.ls(selection=True)

for all in selected:

    cmds.getAttr('Cube.translateX')

And this seems to get the X position of the object names cube in the scene, However I would like it to get the translate of any object I selected.

I hope someone can help out, thanks

Answer

kartikg3 picture kartikg3 · Nov 11, 2014

In the string 'Cube.translateX', you need to have the selected object's name in place of 'Cube'. We do this by a simple string formatting here using the %s format:

import maya.cmds as cmds

selected = cmds.ls(selection=True)

for item in selected:
    translate_x_value = cmds.getAttr("%s.translateX" % item)
    # do something with the value. egs:
    print translate_x_value

Hope that helped.