read c++ MVectorArray by python

43 views
Skip to first unread message

pety20...@gmail.com

unread,
Mar 18, 2019, 1:26:39 PM3/18/19
to Python Programming for Autodesk Maya
hi, how i can read or how i can access to MVectorArray of C++ code by python code?
right now, i convert C++ MVectorArray to String and run it by executePythonCommand() func

( MGlobal::executePythonCommand(c++MVectorArrayToString); )

but,i Want a faster way...

i think, convert C++ MVectorArray to String, is not good way!(in big arrays)

Justin Israel

unread,
Mar 18, 2019, 5:27:16 PM3/18/19
to python_in...@googlegroups.com
MVectorArray in python is a SWIG object wrapping the C++ type. I am not sure of a way to pass a pointer to the C++ MVectorArray and 'wrap' it in Python. But that would be the lowest overhead if it were possible since you wouldn't need to copy. 
The hacky workaround is to serialize it to something like /dev/shm/myvector.json and let python deserialize it. That incurs the cost of having to encode/decode, but avoids the huge literal string call (I am not sure of the cost differences here). 

I'm probably missing some other option to solve this.

--
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/58b6b569-f8cf-418c-828b-7c6c1577c61c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fruit...@gmail.com

unread,
Apr 10, 2019, 3:14:21 PM4/10/19
to Python Programming for Autodesk Maya
If this array is the result of another function called in cpp, maybe you can access it with ctypes and get the result in python.
If you want to get your cpp variable content on the fly, then I'd go for Justin's solution (but first I'd try to double check there is no better structure for what I want to achieve)

Justin Israel

unread,
Apr 10, 2019, 4:27:13 PM4/10/19
to python_in...@googlegroups.com
You could use ctypes but you would also need to write some C wrapper functions that can take void* and operate on the member functions and operators of the MVectorArray. Ctypes only uses C binary interface calling conventions. But the minimum functionality could be exposed such as a count(ptr) and a index(ptr, i) so that in ctypes you could for loop over the range of indices. 

If you want to get your cpp variable content on the fly, then I'd go for Justin's solution (but first I'd try to double check there is no better structure for what I want to achieve)

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

pety20...@gmail.com

unread,
Apr 16, 2019, 9:26:48 AM4/16/19
to Python Programming for Autodesk Maya
thanks!
I use node and save the c++ MVectorArray in the node attribute(attribute type=MFnData::kVectorArray).Then get it by python api.
This method is suitable for my work.
It's much faster than executePythonCommand func :)

Reply all
Reply to author
Forward
0 new messages