PDA

View Full Version : code equivalant


wrend
10-26-2010, 02:39 PM
I feel kind of ashamed asking it, but can someone kindly help me out converting the python code below to mel? My transform matrix knowledge just aint quite there, which is crap, but perhaps this will help push it along.


import maya.OpenMaya as om

vec_1 = [1, 2, 3]
vec_2 = [4, 5, 6]

cross = [vec_1[1]*vec_2[2] - vec_1[2]*vec_2[1], vec_1[2]*vec_2[0] - vec_1[0]*vec_2[2], vec_1[0]*vec_2[1] - vec_1[1]*vec_2[0]]
tMatrix = vec_1 + [0] + vec_2 + [0] + cross + [0,0,0,0,1]

mMatrix = om.MMatrix()
om.MScriptUtil.createMatrixFromList(tMatrix, mMatrix)
tmMatrix = om.MTransformationMatrix(mMatrix)
rotate = tmMatrix.eulerRotation().reorder(om.MEulerRotation.kXYZ)

This is an aim constraint. Code slightly modified from an older post on google group 'python_inside_maya'.

My problems are in ensuring construction of the matrix correctly (as done by createMatrixFromList), and then extracting the euler rotation from it.

So its basic transform matrix manipulations that I'm missing. Needs to be done by hand in mel, for a mel proc. Why mel? Because the overhead of calling a mel proc compared to a python function (in their respective environments) starts to count when many thousands of calls are strung together. A python scripted command plug was even slower than a standard func (due to overheads in cmd construction?). A node is not an option (well, it is, but not at my current level).

Much appreciated.

wrend
10-26-2010, 03:14 PM
or, really, done in python, with no helper function calls - all contructions/operations done 'by hand'. If i could get inside the functions being called here, I'd be ok, but these are 'python compiled' arent they, you cant easily open them up and have a read?

thanks.

NateH
10-26-2010, 04:26 PM
That's using the OpenMaya API. You won't be able to re-write it easily in pure mel. It would be much easier to just define a python function and call it from MEL via the python(); command.

They OpenMaya commands are compiled extensions yes, there's no access to the Maya API source code as far as I am aware. So no way to go looking through the code.

NaughtyNathan
10-26-2010, 04:26 PM
I would have thought that however slow that python code is, the MEL equivalent is likely to be between 2 to 10 times slower...?

:nathaN

wrend
10-27-2010, 01:26 AM
It would be much easier to just define a python function and call it from MEL via the python(); command.
Kinda suprising, but yeah, the cmd plug I wrapped that code with is slower than the exact same code in a vanilla python function. Its the establishment of the cmd overhead that takes the time (?). It has to instantiate the command each time it is called apparently. I did a test with their helloWorldCmd, removing the print, keeping the doIt empty with a pass. It takes ~1 second to do 10K calls to the cmd, if insead you are calling an empty func, its negligble and you need to do a million calls before you'd trust the timer (at ~0.2 seconds). I'm doing my testing from Maya's Python interpreter, if I do MEL python() calls, it gets worse of course.

I would have thought that however slow that python code is, the MEL equivalent is likely to be between 2 to 10 times slower...?
Maybe, I will test once the helper functions are written out.


All I need is the simple transform matrix maths of those functions.

It'll just be populating a matrix in the rigth way, a few multiplications, perhaps with a few trig calls and a little pi. I'm sure its elementary? Those functions are just helpers... if we could just do what they do 'in line', we'd be sorted (and could do a test between Python and MEL for their raw varaibale and mathematic manipulation speeds).


Many thanks!

NateH
10-27-2010, 03:10 AM
Hamish just posted some tutorials on this over the past few weeks: http://www.macaronikazoo.com/?p=435

wrend
10-27-2010, 05:34 AM
Nathan, superb, they should surely have the guts of what I need in there. Nice to see this guy active. If I'm a good boy, I'll come back and post results when I get to this.

Thanks man, much appreciated (I know, lmgtfy).

wrend
10-29-2010, 02:12 AM
If I'm a good boy, I'll come back and post results

Ended up being ~ 6x faster to do this 'by hand' in python, than to use the skinniest version with Python API calls. As for MEL, well, its >50x faster than the Python API in this scenario - don't discard the old puppy. Its all obvious why, but just interesting to see how significant the differences are. No mention of C++ has been made in this discussion.

CGTalk Moderation
10-29-2010, 02:12 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.