In this script example, there are two functions, one that gets a worldMatrix of an object and one that decomposes a matrix into translation, rotation, and scale. I think I've annotated it pretty well, but a big thing to note is that if you freeze translation on an object, it won't return the correct world values for the object you input.
Just change the nodeName to your object name and run the script.
import maya.cmds as cmds import maya.api.OpenMaya as OpenMaya import math import sys def getMatrix(node): ''' Gets the world matrix of an object based on name. ''' #Selection list object and MObject for our matrix selection = OpenMaya.MSelectionList() matrixObject = OpenMaya.MObject() #Adding object selection.add(node) #New api is nice since it will just return an MObject instead of taking two arguments. MObjectA = selection.getDependNode(0) #Dependency node so we can get the worldMatrix attribute fnThisNode = OpenMaya.MFnDependencyNode(MObjectA) #Get it's world matrix plug worldMatrixAttr = fnThisNode.attribute( "worldMatrix" ) #Getting mPlug by plugging in our MObject and attribute matrixPlug = OpenMaya.MPlug( MObjectA, worldMatrixAttr ) matrixPlug = matrixPlug.elementByLogicalIndex( 0 ) #Get matrix plug as MObject so we can get it's data. matrixObject = matrixPlug.asMObject( ) #Finally get the data worldMatrixData = OpenMaya.MFnMatrixData( matrixObject ) worldMatrix = worldMatrixData.matrix( ) return worldMatrix def decompMatrix(node,matrix): ''' Decomposes a MMatrix in new api. Returns an list of translation,rotation,scale in world space. ''' #Rotate order of object rotOrder = cmds.getAttr('%s.rotateOrder'%node) #Puts matrix into transformation matrix mTransformMtx = OpenMaya.MTransformationMatrix(matrix) #Translation Values trans = mTransformMtx.translation(OpenMaya.MSpace.kWorld) #Euler rotation value in radians eulerRot = mTransformMtx.rotation() #Reorder rotation order based on ctrl. eulerRot.reorderIt(rotOrder) #Find degrees angles = [math.degrees(angle) for angle in (eulerRot.x, eulerRot.y, eulerRot.z)] #Find world scale of our object. scale = mTransformMtx.scale(OpenMaya.MSpace.kWorld) #Return Values return [trans.x,trans.y,trans.z],angles,scale #If we're in the main namespace run our stuffs! if __name__ == '__main__': #Defining object name. nodeName = 'yourName' #Get Matrix mat = getMatrix(nodeName) #Decompose matrix matDecomp = decompMatrix(nodeName,mat) #Print our values sys.stdout.write('\n---------------------------%s---------------------------\n'%nodeName) sys.stdout.write('\nTranslation : %s' %matDecomp[0]) sys.stdout.write('\nRotation : %s' %matDecomp[1]) sys.stdout.write('\nScale : %s\n' %matDecomp[2])
You could very easily get an MMatrix using maya.cmds by just doing:
mat = OpenMaya.MMatrix(cmds.getAttr('YourNode.worldMatrix'))
But this will only work with the new API, not the old.
|
just what I was looking for. I assume this method is much faster than getAttr Translate/Rotate and xform. Kind of new to this programming stuff.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteunfortunately that is not api 2.
ReplyDeleteapi 2 would be:
import maya.api.OpenMaya as OpenMayaApi
node = OpenMayaApi.MFnDependencyNode(OpenMayaApi.MSelectionList().add(node).getDependNode(0))
plug_object = OpenMayaApi.MPlug(node.findPlug('worldMatrix', 0)).elementByLogicalIndex(0).asMObject()
matrix = OpenMayaApi.MFnMatrixData(plug_object).matrix()
broken down into more lines to make it more readable, but you get the idea.
This comment has been removed by the author.
ReplyDeleteTitanium headers - TITADICART®
ReplyDeleteTitanium headers titanium price per ounce by TITADICART®. The Tritanium® headers of our apple watch titanium vs aluminum Tritanium® baoji titanium have titanium earrings sensitive ears become part of our growing portfolio of ti 89 titanium calculator Tritanium® products
d749f9dvseb434 horse dildo,sex chair,penis rings,wholesale sex toys,dildo,cheap sex toys,huge dildos,horse dildo,male sexy toys c110d6yodkx526
ReplyDeleteg559f6sdpcs329 couples sexy toys,male masturbator,horse dildo,vibrators,real dolls,wholesale sex toys,male sex dolls,dildos,black dildos c447i5rptcl998
ReplyDeletewq192 wholesale nfl jerseys,Cheap Jerseys from china,Cheap Jerseys from china,wholesale jerseys,Cheap Jerseys free shipping,cheap jerseys,wholesale nfl jerseys,Cheap Jerseys china,wholesale jerseys rt375
ReplyDelete