Showing posts with label learning. Show all posts
Showing posts with label learning. Show all posts

Thursday, July 30, 2015

Stretch Node Maya API Plugin Comparisons

As mentioned in a previous post I had some woes querying the matrix data in my plugin. I did some simplified tests and have no implemented these tests into the node. The video below demonstrates a comparison between the matrix plug node and the transform plug node.

Stretch Node Python API Plugin demo from Tim Forbes on Vimeo.

Test stretch node plugin using matrix connections rather than channel connections. This video compares two test plugins briefly. One plugin using Matrix connections, the other using Transform channel connections. This is a test purely to look at different types of data handling and how I can use Mayas API to simplify node graphs for common rigging techniques.

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()

Positon From Matrix

I am currently switching my stretchNode over to work on a matrix input rather than a vector input. I was having issues when querying the dataBlock of the MPxNode to return the matrix data. Pretty much I wasn't getting the correct information. The way this connection was made was the ".worldMatrix" attribute was connected to the '.inMatrix' attribute on my stretchNode. I accesed this with:

startMXDataH = block.inputValue(stretchNodeMX.aStartMX).asMatrix()

Now this was not playing nce with me. As I went further down the track I was not getting the correct information returned.

So here's what I have..I'll delve deeper tomorrow...

def compute(self,plug,block):
#method 1
#get matrix and translate from finding the dependency node and working from there
#returns the correct t.x,t.y and t.z
#-12.0540640814 5.6978884254 7.7806477623

sNode = self.thisMObject()
plugArray = OpenMaya.MPlugArray()
depNodeFn = OpenMaya.MFnDependencyNode(sNode)
startPlug = depNodeFn.findPlug(stretchNodeMX.aStartMX)
startPlug.connectedTo(plugArray,True,False)
stObjMx = OpenMaya.MMatrix()
if plugArray.length()>0:
for i in range(0,plugArray.length()):
stObj = plugArray[i].node()
stDagNodeFn = OpenMaya.MFnDagNode(stObj)
stObjMx = stDagNodeFn.transformationMatrix()
stP = OpenMaya.MTransformationMatrix(stObjMx)
vec = stP.getTranslation(OpenMaya.MSpace.kWorld)
print vec.x,vec.y,vec.z

#method 2
#get the matrix data from the input plug and work from there
#returns the incorrrect t.x,t.y and t.z
#5.26354424712e-315 0.0 0.0078125

startMXDataH = block.inputValue(stretchNodeMX.aStartMX)
startMX = startMXDataH.asMatrix()
#just print the "translate" parts of the matrix - does not return what I'd expect.
print startMX(0,0),startMX(0,1),startMX(0,2)

#method 3
#returns the incorrrect t.x,t.y and t.z
#0.0 5.26354424712e-315 0.0

#get the transformation matrix to query the translation from there. Once again it returns the incorrect imformation
mxData = OpenMaya.MFnMatrixData()
mxData.create(startMX)
stPTransformationMatrix = mxData.transformation()
stPTranslation = stPTransformationMatrix.getTranslation(OpenMaya.MSpace.kWorld)
print stPTranslation.x,stPTranslation.y,stPTranslation.z

block.setClean(plug)

Obviously something's going screwy with my coding here, and my use of the API. But anyways, it's all about digging through it and working out whats wrong...to be continued tomorrow. Something's going wrong with either my creation of the matrix attribute and the kind of data there..but I'm pretty sure thats good. So it must be in the way I am querying the data from the dataBlock...hmmm.

TBC...