Setting a matrix's plug value usin OpenMaya

208 views
Skip to first unread message

Rudi Hammad

unread,
May 3, 2020, 12:59:16 AM5/3/20
to Python Programming for Autodesk Maya
Hi,

I know this can be done using cmds.setAttr(type="matrix"), but I am trying to set the values of a
matrix 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

Rudi Hammad

unread,
May 3, 2020, 1:48:00 PM5/3/20
to Python Programming for Autodesk Maya
Ok, I realized that I had to give the element 0 because it is an array. So with this I don't get any errors now:

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

..but still, the method .set() is not doing anything. Not sure why...

Rudi Hammad

unread,
May 6, 2020, 2:36:55 AM5/6/20
to Python Programming for Autodesk Maya
never mind, I got.
I just had to pass te matrix to a MFnMatrixData first and create it.
Cheers

Marcus Ottosson

unread,
Aug 24, 2020, 4:33:30 AM8/24/20
to python_in...@googlegroups.com

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.

  1. Write to any matrix
  2. Write to the worldMatrix

Writing 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.

Rudi Hammad

unread,
Aug 24, 2020, 9:48:27 AM8/24/20
to Python Programming for Autodesk Maya
Hey marcus,
I use OpenMaya 1, not sure about OpenMaya 2. This is what I do:
(pasted the code below because pastebin link was giving me an error)
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)

Marcus Ottosson

unread,
Aug 24, 2020, 3:05:31 PM8/24/20
to python_in...@googlegroups.com
# 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.
Reply all
Reply to author
Forward
0 new messages