Skip to content
Advertisement

Get the direction an object’s axis is pointing in Maya

Using python I’m trying to confirm that a given transform node in Maya has it’s Y axis pointing up, Z axis pointing forward, and X axis pointing to the side, like in the picture below. The transform node might be a child to another transform node in a hierarchy of unknown depth.

My first thought was to use xform rotate pivot matrix? I’m not sure if this is the correct thing or how to interpret the matrix values.

enter image description here

Advertisement

Answer

For most objects, you can get the orientation in world space quite simply:

import maya.cmds as cmds

world_mat = cmds.xform(my_object, q=True, m=True, ws=True)
x_axis = world_mat[0:3]
y_axis = world_mat[4:7]
z_axis = world_mat[8:11]

If you want them in vector form (so you can normalize them or dot them) you can use the maya api to get them as vectors. So for example

import maya.cmds as cmds
import maya.api.OpenMaya as api

my_object = 'pCube1'
world_mat = cmds.xform(my_object, q=True, m=True, ws=True)
x_axis = api.MVector(world_mat[0:3])
y_axis = api.MVector(world_mat[4:7])
z_axis = api.MVector(world_mat[8:11])

# 1 = straight up, 0 = 90 degrees from up, -1 = straight down
y_up = y_axis * api.MVector(0,1,0)

This will include the any modifications that might have been made to the .rotateAxis parameter on the object so it’s not guaranteed to line up with the visual tripod if that’s been manipulated.

If you don’t have reason to expect the .rotateAxis has been set, this the easiest way to do it. A good intermediate step would be just to warn on objects that have a non-zero .rotateAxis so there is no ambiguity in the results, it might be unclear what the correct course of action there is in any case.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement