Python 获取对象的方向';s轴在Maya中指向

Python 获取对象的方向';s轴在Maya中指向,python,orientation,axis,maya,Python,Orientation,Axis,Maya,使用python,我试图确认Maya中给定的变换节点的Y轴指向上,Z轴指向前方,X轴指向侧面,如下图所示。变换节点可能是深度未知层次中另一个变换节点的子节点 我的第一个想法是使用xform旋转轴心矩阵?我不确定这是否正确,也不确定如何解释矩阵值 对于大多数对象,您可以非常简单地获得世界空间中的方向: import maya.cmds as cmds world_mat = cmds.xform(my_object, q=True, m=True, ws=True) x_axis = world

使用python,我试图确认Maya中给定的变换节点的Y轴指向上,Z轴指向前方,X轴指向侧面,如下图所示。变换节点可能是深度未知层次中另一个变换节点的子节点

我的第一个想法是使用xform旋转轴心矩阵?我不确定这是否正确,也不确定如何解释矩阵值


对于大多数对象,您可以非常简单地获得世界空间中的方向:

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]
如果希望它们以向量形式出现(以便可以对它们进行规格化或点化),可以使用maya api将它们作为向量。比如说

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)
这将包括可能对对象上的
.rotateAxis
参数所做的任何修改,因此,如果操作了视觉三脚架,则不能保证其与视觉三脚架对齐


如果您没有理由期望
.rotateAxis
已设置,这是最简单的方法。一个好的中间步骤是对非零的对象发出警告。rotateAxis,因此结果没有歧义,在任何情况下都可能不清楚正确的操作过程。

我将@theodox answer包装到一个函数中。也许这对将来的人会有帮助

def getOrientation(obj, returnType='point'):
    '''
    Get an objects orientation.

    args:
        obj (str)(obj) = The object to get the orientation of.
        returnType (str) = The desired returned value type. (valid: 'point', 'vector')(default: 'point')

    returns:
        (tuple)
    '''
    obj = pm.ls(obj)[0]

    world_matrix = pm.xform(obj, q=True, m=True, ws=True)
    rAxis = pm.getAttr(obj.rotateAxis)
    if any((rAxis[0], rAxis[1], rAxis[2])):
        print('# Warning: {} has a modified .rotateAxis of {} which is included in the result. #'.format(obj, rAxis))

    if returnType is 'vector':
        from maya.api.OpenMaya import MVector

        result = (
            MVector(world_matrix[0:3]),
            MVector(world_matrix[4:7]),
            MVector(world_matrix[8:11])
        )

    else:
        result = (
            world_matrix[0:3],
            world_matrix[4:7],
            world_matrix[8:11]
        )


    return result