Maya Python API 1.0 output value to an Array

50 views
Skip to first unread message

hwee li

unread,
Dec 26, 2022, 4:46:04 AM12/26/22
to Python Programming for Autodesk Maya
hi, can I know how to get the Out value from maya.OpenMaya API correctly ?

for example, I read the Maya document about MColor object, it has a function get() which can output color value to an array.
MColor.Get()

but in python, when I tried to create an array like
array=[0.0,0.0,0.0]
or use maya api
array= MFloatArray(3)

then run the get function from a MColor object

colorObject.get(array)

maya will give me an error `
"in method 'MColor_get', argument 2 of type 'float [3]"
`
it seems that the array object I created is not the correct type for output value to write in, anyone know how to fix this, I know there is a better way in API 2.0 , but I hope to figure out how to solve this kind of output value type for API1.0 in python .

Justin Israel

unread,
Dec 26, 2022, 10:13:52 PM12/26/22
to python_in...@googlegroups.com
Working with Maya arrays and wrappers between Python and C++ is not always intuitive. Would it be easier to just grab the value as needed?
import maya.OpenMaya as omi

col = omi.MColor(1.0, 0.5, 0.25)

# tuple
rgb = col.r, col.g, col.b

# If you need MFloatArray
arr = omi.MFloatArray(3)
arr[0], arr[1], arr[2] = rgb


--
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/5d08ac61-058d-4575-b55f-f63fb20eaf40n%40googlegroups.com.

hwee li

unread,
Dec 27, 2022, 8:39:47 AM12/27/22
to Python Programming for Autodesk Maya
test

hwee li

unread,
Dec 27, 2022, 8:42:02 AM12/27/22
to Python Programming for Autodesk Maya
hi, Justin 
  thanks for your reply and your code , after I got someone's help , it is about the parameter pointer issues between C++ and Python, which needs MscriptUtil() to solve , the blow code works finally .


import maya.OpenMaya as om

mColor=om.MColor(123,222,122)

uti0=om.MScriptUtil()
uti0.createFromDouble(0.0,0.0,0.0)
ptr_0=uti0.asFloatPtr()

mColor.get(ptr_0)

print uti0.getFloatArrayItem(ptr_0,1)
print uti0.getFloatArrayItem(ptr_0,2)
print uti0.getFloatArrayItem(ptr_0,3)

Justin Israel

unread,
Dec 27, 2022, 4:00:24 PM12/27/22
to python_in...@googlegroups.com


On Wed, 28 Dec 2022, 2:42 am hwee li, <internet...@gmail.com> wrote:
hi, Justin 
  thanks for your reply and your code , after I got someone's help , it is about the parameter pointer issues between C++ and Python, which needs MscriptUtil() to solve , the blow code works finally .

All good. I was avoiding MScriptUtil because it feels like more work than needed to get the values when you can get them straight from the MColor instead of special pointer helpers. 
Glad you got a solution that works for you! 

hwee li

unread,
Dec 29, 2022, 1:14:05 AM12/29/22
to Python Programming for Autodesk Maya
yes, you are right , it increases lots coding work , I think I will try API2.0 in the future as much as possible , this time no choice since I has to deal with some legacy code .
thanks for your kind help and suggestion again .

regards
Reply all
Reply to author
Forward
0 new messages