Hi,
I know this can be done using cmds.setAttr(type="matrix"), but I am trying to set the values of amatrix plug using OpenMaya, but I got stuck at the end line. Of the code below
matList = [18, 24, 7, 0,
54, 17, 1, 0,
22, 34, 8, 0,
11, 78, 6, 1]
mScr = OpenMaya.MScriptUtil()
MMat = OpenMaya.MMatrix()
mScr.createMatrixFromList(matList, MMat)
cmds.joint(n="myJoint")
mslist = OpenMaya.MSelectionList()
mslist.add("myJoint")
mobj = OpenMaya.MObject()
mslist.getDependNode(0, mobj)
mfndepnode = OpenMaya.MFnDependencyNode(mobj)
mfndepnode = OpenMaya.MFnDependencyNode(mobj)
matPlug = mfndepnode.findPlug("worldMatrix")
# What I though could workd is passing th matrix plug to a MFnMatrixData, to set the value, but this line below fails
mfn = OpenMaya.MFnMatrixData(matPlug.asMObject())
#...so where to go from here
Any ideas?
thank you
matList = [18, 24, 7, 0,
54, 17, 1, 0,
22, 34, 8, 0,
11, 78, 6, 1]
mScr = OpenMaya.MScriptUtil()
MMat = OpenMaya.MMatrix()
mScr.createMatrixFromList(matList, MMat)
cmds.joint(n="myJoint")
mslist = OpenMaya.MSelectionList()
mslist.add("myJoint")
mobj = OpenMaya.MObject()
mslist.getDependNode(0, mobj)
mfndepnode = OpenMaya.MFnDependencyNode(mobj)
mfndepnode = OpenMaya.MFnDependencyNode(mobj)
matPlug = mfndepnode.findPlug("worldMatrix")
matPlug_ind0 = matPlug.elementByLogicalIndex(0)
mfnMatData = OpenMaya.MFnMatrixData(matPlug_ind0.asMObject())
mfnMatData.set(MMat) # No errors, but nothing is happening
Glad I found this, I’m heading down a similar route. However I couldn’t get it working.
There are two problems here I think.
worldMatrixWriting to the worldMatrix seems odd; I wouldn’t expect that to affect anything, and that the attribute is more of a pre-computed value representing the matrices of itself and all parent nodes. Did you actually manage to write to that? :O
Writing to any matrix however does come in handy, for example if you create your own matrix attribute, especially via Python API 2.0 (rather than 1.0 as per your example)
from maya import cmds
from maya.api import OpenMaya as om
# Matrix to write
tm = om.MTransformationMatrix()
tm.setTranslation(om.MVector(1, 2, 3), om.MSpace.kObject)
mat = tm.asMatrix()
node = cmds.createNode("transform")
mlist = om.MSelectionList()
mlist.add(node)
mobj = mlist.getDependNode(0)
# Create Attribute
mattr = om.MFnMatrixAttribute()
attr = mattr.create("myMatrix", "mm")
mattr.readable = True
mattr.writable = True
mattr.storable = True
fn = om.MFnDependencyNode(mobj)
fn.addAttribute(attr)
So far so good, we’ve got a vanilla node with a custom matrix attribute. But now watch as neither API nor cmds is able to actually modify this value.
# Failed edit attempt 1
cmds.setAttr("%s.myMatrix" % node, list(mat), type="matrix")
# Failed edit attempt 2
mplug = fn.findPlug("myMatrix", True)
matrixData = om.MFnMatrixData(mplug.asMObject())
matrixData.set(mat) # Nothing happens
What are we missing here? :O
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/4e3be844-fe3c-431d-bd41-3d492ec2ce2d%40googlegroups.com.
from maya import cmds
from maya.api import OpenMaya as om
# Matrix to write
tm = om.MTransformationMatrix()
tm.setTranslation(om.MVector(1, 2, 3), om.MSpace.kObject)
mat = tm.asMatrix()
node = cmds.createNode("transform")
mlist = om.MSelectionList()
mlist.add(node)
mobj = mlist.getDependNode(0)
# Create Attribute
mattr = om.MFnMatrixAttribute()
attr = mattr.create("myMatrix", "mm")
mattr.readable = True
mattr.writable = True
mattr.storable = True
fn = om.MFnDependencyNode(mobj)
fn.addAttribute(attr)
import maya.OpenMaya as OpenMaya
mobj = apiObjects.getMObject("transform1") # (<-- this a custom method I have to get mobject, you can use any one you like)
mfnDepNode = OpenMaya.MFnDependencyNode(mobj)
mplug = mfnDepNode.findPlug("myMatrix")
mMat = OpenMaya.MMatrix() # <-- test matrix that will be set
# This block is how I set the matrix plug
mfnMatData = OpenMaya.MFnMatrixData()
matMObj = mfnMatData.create(mMat)
mplug.setMObject(matMObj)
# My broken version
mplug.set(mat)
# Your working version
mplug.setMObject(matMObj)
That was it! That is great, thanks Rudi.
A shameless plug for a relevant project:
import cmdx
node = cmdx.createNode("transform")
node["translate"] = (1, 2, 3)
node["rotate", cmdx.Degrees] = (20, 30, 40)
# Create a new matrix attribute
node["myMatrix"] = cmdx.Matrix()
# Store current world matrix in this custom attribute
node["myMatrix"] = node["worldMatrix"][0].asMatrix()
And a full API 2.0 example for completeness.
from maya import cmds
from maya.api import OpenMaya as om
# Matrix to write
tm = om.MTransformationMatrix()
tm.setTranslation(om.MVector(1, 2, 3), om.MSpace.kObject)
mat = tm.asMatrix()
node = cmds.createNode("transform")
mlist = om.MSelectionList()
mlist.add(node)
mobj = mlist.getDependNode(0)
# Create Attribute
mattr = om.MFnMatrixAttribute()
attr = mattr.create("myMatrix", "mm")
mattr.readable = True
mattr.writable = True
mattr.storable = True
fn = om.MFnDependencyNode(mobj)
fn.addAttribute(attr)
# Edit Attribute
mplug = fn.findPlug("myMatrix", True)
matrixData = om.MFnMatrixData()
matobj = matrixData.create(mat)
mplug.setMObject(matobj)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e0c89aac-caf0-4fa2-9bf5-4ae08e75742dn%40googlegroups.com.