You are here: Home / Hidden_Study / Mel / Accessing Matrix Data

Accessing Matrix Data

API Only:

Here, we have functions that return either MMatrix or MTransformationMatrix objects.


Calls used in the below API examples:

import maya.OpenMaya as om
def getMDagPath(node):
    """
    Convenience function to make getting MDagPaths easier:
    Get a MDagPath for a node.  node is a string name.
    """
    selList = om.MSelectionList()
    selList.add(node)
    mDagPath = om.MDagPath()
    selList.getDagPath(0, mDagPath)
    return mDagPath

def get_MDagPath_MMatrix(node):
    """
    This will return the worldspace MMatrix of a node. node is a string name.
    """
    mDagPath = getMDagPath(node)
    mMatrix = mDagPath.inclusiveMatrix()
    return mMatrix
def get_MFnTransform_MTransformationMatrix(node):
    """
    Using a MFnTransform function, return a MTransformationMatrix for the node.
    This is the *local* matrix of the node (not worldspace).  node is a string name.
    """
    mDagPath = getMDagPath(node)
    transformFunc = om.MFnTransform(mDagPath)
    mTransformMtx = transformFunc.transformation()
    return mTransformMtx

API and getAttr / xform:

The below to functions are almost the same. The getAttr method however gives you a few more matrix querying options.


Calls used in the below API examples:

import maya.cmds as mc

def get_getAttr_MMatrix(node, matrixType):
    """
    Access the matrix attr data directly on a node.
    node : string name of node to query.
    matrixType : "matrix", "inverseMatrix", "worldMatrix", "worldInverseMatrix",
        "parentMatrix", "parentInverseMatrix", "xformMatrix"   
    return a MMatrix
    """
    mList = mc.getAttr(node+"."+matrixType)
    mMatrix = om.MMatrix()
    om.MScriptUtil.createMatrixFromList(mList, mMatrix )
    return mMatrix
def get_xform_MMatrix(node):
    """
    Access the matrix attr data directly on a node.
    This will return the worldspace MMatrix for the node.
    node : string name of node to query.
    return a MMatrix
    """
    mList = mc.xform(node, query=True, matrix=True)
    mMatrix = om.MMatrix()
    om.MScriptUtil.createMatrixFromList(mList, mMatrix )
    return mMatrix

Mel only:

Python has no built-in matrix type, that's why above we use the Maya API's version. Here we do it all in mel:

global proc matrix get_getAttr_matrix(string $node, string $matrixType){
    // Access the matrix attr data directly on a node.
    // string $node : Name of node to query.
    // string $ matrixType : "matrix", "inverseMatrix", "worldMatrix",
    //     "worldInverseMatrix", "parentMatrix", "parentInverseMatrix", 
    //     "xformMatrix"
    float $m[] =`getAttr ($node+"."+$matrixType)`;
    matrix $mtx[4][4] = <<$m[0],  $m[1],  $m[2],  $m[3];
                          $m[4],  $m[5],  $m[6],  $m[7];
                          $m[8],  $m[9],  $m[10], $m[11];  
                          $m[12], $m[13], $m[14], $m[15]>>; 
    return $mtx;
    }

// example:
matrix $myMatrix[4][4] = get_getAttr_matrix("pCube1", "worldMatrix")
// Result: << 1 0 0 0;  0 1 0 0;  0 0 1 0;  0 0 0 1 >> // 
global proc matrix get_xform_matrix(string $node){
    // returns a matrix type for the given node's world matrix.
    float $m[] = `xform -query -matrix  $node`;
    matrix $mtx[4][4] = <<$m[0],  $m[1],  $m[2],  $m[3];
                          $m[4],  $m[5],  $m[6],  $m[7];
                          $m[8],  $m[9],  $m[10], $m[11];  
                          $m[12], $m[13], $m[14], $m[15]>>; 
    return $mtx;
    }

// example:
matrix $myMatrix[4][4] = get_xform_matrix("pCube1")
// Result: << 1 0 0 0;  0 1 0 0;  0 0 1 0;  0 0 5.667197 1 >> // 
Filed under: , , ,
OpenID Login