# 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:

   - https://github.com/mottosso/cmdx/pull/9

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


On Mon, 24 Aug 2020 at 14:48, Rudi Hammad <rudiham...@gmail.com> wrote:

> 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)
>>
>>
>> --
> 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_maya+unsubscr...@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
> <https://groups.google.com/d/msgid/python_inside_maya/e0c89aac-caf0-4fa2-9bf5-4ae08e75742dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCKWxb59TGiNDkLtb_J565bPoZgpy%3Des-VmrcybRast6w%40mail.gmail.com.

Reply via email to