Wednesday, July 29, 2015

Maya API Matrix Research

So after my last post I have delved into Maya to test out some Matrix data queries to determine where my node was going wrong. The test I set up in Maya was simple.
Build a locator. Move it somewhere other than the origin (origin is fine but the data would just read 0.0,0.0,0.0. I wanted something in there as it makes it easier to verify that the data is returning correctly.

I then started writing some API code that would:
#get the active selection
#loop through selected nodes and for each node
     #find the plug for the matrix attribute
     #get that plug as matrix data
     #extract the transformation data
     #print the x,y and z coordinates.

I chose this test as that is exactly what is going on in my stretch node. In the node the .matrix attr is connected into the node, and from that plug I try to retrieve the x,y and z position. However it is returning incorrectly.

So the code in my Maya test scene is working, so the error must be in the creation of the node attribute, or the way in which I extract the data from the data block. Anyways, here is the code I used to the the t.x,t.y and t.z of a selected object from the matrix plug. I hope you find it useful.

import maya.OpenMaya as OpenMaya
dagPathFn = OpenMaya.MDagPath()
#get active selection
mSelList = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(mSelList)
selItr = OpenMaya.MItSelectionList(mSelList)
#iterate through selection and get the world space positon from the matrix plug
while not selItr.isDone():
     selItr.getDagPath(dagPathFn)
     nObj = dagPathFn.node()
     #we got the node from the dagPathFn#now we get the dependency node
     objDepNodeFn = OpenMaya.MFnDependencyNode(nObj)
     #find the .matrix plug
     mxPlug = objDepNodeFn.findPlug('matrix')
     print mxPlug.name()
     print objDepNodeFn.name()
     #get plug as MObject which we will attach MFnMatrixData to.
     mxObj = mxPlug.asMObject()
     mxDataFn = OpenMaya.MFnMatrixData(mxObj)
     #query the transformation, returns MTransformationMatrix
     trfnMX = mxDataFn.transformation()
     #get the translate in world space
     v = trfnMX.getTranslation(OpenMaya.MSpace.kWorld)
     #print it out!
     print v.x,v.y,v.z
     selItr.next()

No comments:

Post a Comment