Small Arrays and OpenMaya API

39 views
Skip to first unread message

Nicolas Chaverou

unread,
Mar 25, 2020, 10:20:15 AM3/25/20
to python_in...@googlegroups.com
Hey guys,

That's probably a silly question but I've been wondering this for a while and always figured something else, but now i'm kinda stucked. Using Maya 2018 / Windows

How do get/set small arrays within the Python API ?
For example, MfnTransform has the following function prototype:
MStatus getScale (double scale[3]) const

And when calling it from the Python API returns the same prototype:

in method 'MFnTransform_getScale', argument 2 of type 'double [3]' //


According to SWIG documentation (which Autodesk used for its Python bindings), one should use tuples to do it (but it has to be implemented on the API side)

Here is what I tried:


import maya.OpenMaya as om

selList = om.MSelectionList()
selList.add('joint1')
oNode = om.MObject()
selList.getDependNode(0, oNode)
joint = om.MFnTransform(oNode)
scale = (0, 0, 0)
joint.getScale(scale)

But I always get the following error:

# TypeError: in method 'MFnTransform_getScale', argument 2 of type 'double [3]' //


Tried with [], [0, 0, 0], om.MFloatArray(), None... but was never able to query that property correctly. What's the supposed way of making this work ? Or does it just nit work at all and I'm obliged to use PyMel for those ?

Any insights appreciated
N.

Nicolas Chaverou

unread,
Mar 25, 2020, 10:39:10 AM3/25/20
to python_in...@googlegroups.com
Ok, inspecting the API with the good old dir command, I figured a MScriptUtil_setFloat3ArrayItem entry

Problem solved.
Here's the fixed code for reference:

import maya.OpenMaya as om
selList = om.MSelectionList()
selList.add('joint1')
oNode = om.MObject()
selList.getDependNode(0, oNode)
joint = om.MFnTransform(oNode)

# create reference array
util = maya.OpenMaya.MScriptUtil()
util.createFromDouble(0.0, 0.0, 0.0)  
ptr = util.asDoublePtr()
joint.getScale(ptr)

scale = [util.getDoubleArrayItem(ptr, 0), util.getDoubleArrayItem(ptr, 1), util.getDoubleArrayItem(ptr, 2)]
print(scale)

As the documentation mentions:
This class is admittedly cumbersome to use but it provides a way of building parameters and accessing return values for methods which would not normally be accessible from Python.

Cumbersome is the right word indeed.
Will probably hide this with some binding functions in my module

N.

Marcus Ottosson

unread,
Mar 25, 2020, 1:28:45 PM3/25/20
to python_in...@googlegroups.com

This is why god created API 2.0…

from maya.api import OpenMaya as om

sl = om.MSelectionList()
sl.add('joint1')
node = sl.getDependNode(0)
joint = om.MFnTransform(node)
print(joint.scale())

And why I created cmdx...

import cmdx
joint = cmdx.encode("joint1")
joint["scale"] = [1, 2, 3]

--
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/CAFS5DCZtXtL8yiTgS_X5oj_3gvGuzsExhe_YtKjf4k6rNEvS5g%40mail.gmail.com.

Nicolas Chaverou

unread,
Mar 27, 2020, 5:49:40 AM3/27/20
to python_in...@googlegroups.com
Hi Marcus,

Somehow I missed cmdx!! Will totally take a look!
I looked at API 2.0 but as, for some obscure reasons, it still does not include MfnIkJoint (it's been 5years now since its release, and still not in the 2021 preview), this is a no go for me and I don't like switching api within the same function...

Thanks for the tips!



Marcus Ottosson

unread,
Mar 27, 2020, 11:21:48 AM3/27/20
to python_in...@googlegroups.com
It's true, 2.0 is lacking alot of what 1.0 has. I suspect 1.0 is an automated affair, updated alongside updates to the C++ API. Whereas 2.0 is a manual affair, someone needs to be assigned that task, which doesn't seem to be a priority. Pity.

The few things it does though, like manipulating the scene, is quite solid and faster to both write and compute, so if you can (like in this example) I definitely recommend it. :+1:

Reply all
Reply to author
Forward
0 new messages