0

Actually i'am new to api and am trying to get the translation values(x,y,z) but the problem is i cant get when i specify only "translate" instead of "translateX", "translateY", "translateZ" in every separate line. is there any way to get what i actually want?

here's the code:

import maya.OpenMaya as om

selected = om.MSelectionList()
om.MGlobal.getActiveSelectionList(selected)

obj = om.MObject()
selected.getDependNode(0,obj)

print(om.MFnDependencyNode(obj).findPlug("translateX").asFloat())
print(om.MFnDependencyNode(obj).findPlug("translateY").asFloat())
print(om.MFnDependencyNode(obj).findPlug("translateZ").asFloat())

thank you...

joojaa
  • 4,354
  • 1
  • 27
  • 45
Anvesh Chary
  • 65
  • 1
  • 11
  • 1
    You may want to tell people why you chose to use maya api calls for this when you could do this in one line with maya scripting interfaces i one line. – joojaa Apr 20 '15 at 10:32
  • iam trying to develop a small plugin and i also recently started to learn python api... – Anvesh Chary Apr 20 '15 at 11:33

2 Answers2

2

The translate attribute is a compound attribute. In the Maya API, you have to individually query each child attribute of a compound attribute in order to retrieve the complete value of the compound attribute.

But the MEL getAttr() command can retrieve the value of the translate attribute all at once. Since you are using Python, you can mix MEL commands and calls to the Maya API together in the same script:

import maya.OpenMaya as om
import maya.cmds as cmds

selected = om.MSelectionList()
om.MGlobal.getActiveSelectionList(selected)

obj = om.MObject()
selected.getDependNode(0,obj)

depNodeName = om.MFnDependencyNode(obj).name()

print(cmds.getAttr(depNodeName + '.translate')[0])
Irving Moy
  • 331
  • 3
  • 5
  • yeah i can use getAttr() command in this python api but what if i use c++ api? should i query each child attribute? – Anvesh Chary Apr 20 '15 at 11:29
  • @AnveshChary Yes, c++ is a more explicit language than python, so it would not feel weird. Nearly all c++ out there is like this. Even the examples are like this. Its jsut python sense that makes it feel weird. In most real programming languages you write about 40 pages of text for same thing that takes python 1 line. – joojaa Apr 20 '15 at 12:27
  • @AnveshChary Yes, you will need to query each child attribute when you use the C++ version of the Maya API. I don't see a method in the MPlug class which returns an MVector object; that would have allowed you to get all 3 values in the translate attribute in one call. However, if you get the transformation matrix for the object, you can get the value of the translation as a MVector with MTransformationMatrix::getTranslation(). – Irving Moy Apr 20 '15 at 18:35
0
object = 'cube1'
# Get the transform matrix as a list of 16 floats
m_list = cmds.xform(object, query=True, matrix=True)
# Create the MMatrix object
m = om.MMatrix(m_list) 
mt = om.MTransformationMatrix(mt)

translate = mt.translation(om.MSpace.kWorld)
print(translate.x, translate.y, translate.z)
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 29 '22 at 12:23