Does anyone have any good matrix functions?

Just checking if anyone else is annoyed by the lack of these tools in mel. Coming from max, where maxscript has all sorts of cool transformation methods (basically the same as are found in the maya api, which I can't use, but made availible to scripters as well), it's a bit frustrating not to be able to transform matrix variables without manipulating objects in the scene. I like my scripts to change the scene as little as possible, and not create temporary nodes etc when it calculates.

So, I'm thinking maybe someone out there has made a library of functions and is willing to share, but this might just be wishful thinking.

These are some of the functions I'm talking about, that would be really useful to have:

[ul][*]vector -> float [3], float [3] -> vector (simple enough, already written them)
[*]vector * matrix4 (exists - pointMatrixMult command)
[*]matrix4 * matrix4 (this I've already written too, would be glad to share if anyone wants, just a few lines long)
[*]invert matrix4 (would be really useful)
[*]euler -> quaternion, euler -> axis+angle, quaternion -> euler, quaternion -> axis+angle, axis+angle -> euler, axis+angle -> quaternion (all essential)
[*]rotate matrix4 by rotation (euler, quaternion or axis+angle, - if I had the above conversions, I would only need one type of rotation here, preferably quaternion since they are the most stable imo)
[/list]

Yeah, I think those are the important ones.[/ul]

most important support function is float 16 to matrix as you get certain trivial solutions to work thisway.

Also matrix to float 16 to make connections to nodes available.

Well the functions were neatly lined up in older versions of maya so i can understan that you dont know they allready exsist.

built in maya

vector * matrix4
matrix4 * matrix4 // builtin just multiply matrix with matrix type and it much faster than any script will ever be.

invert matrix4 // builtin as node, do same thing as pointMatrix mult wich is a scirpt look at it. but mostly tngs that can supply you with matrix can give you their inverse in maya too. Also the pontmatrix mult is the wrong function!

then notes:

the rotation functions are a bit wierd since they would all yeld the same matrix

quarteronion IS axis+angle except ot is coded into the representation for validation

euler simple, construct the matrix inrto a transform and rotate than read back.

The best would be:

The super usefull killer ap is Feed matrix to transform NODALY.

PS Mel isnt realy the calculation workhorse of maya but rether the node networks are, quite many mel functions are references to temporarily created and then deleted nodes.

Well I certainly need to understand this better. Thanks for the quick reply (had a feeling it would be you if anyone).

This helps me a lot.

Didn't know you could multiply matrices, and conversion between matrices, float arrays and vectors is quite trivial.

I thought creating nodes and getting/setting attributes in the dg was much costlier memory- and timewise than performing simple math operations in mel, but I guess I was wrong, besides, if evererybody's doing it...

Didn't get what you mean by pontmatrix mult being the wrong function...

Yes, quaternion is axis angle, but it's normalized which makes it harder to read and construct than axis+angle, but since mel doesn't even support quats, then there's no point in converting either.

Anyway, thanks for the help, once I get into this way of thinking (doing math with nodes instead of commands) I think I can overcome a lot of problems.

besides, if evererybody's doing it

yeah well the fact that everyone does something doesnet make the right, refeer to the normal discussion on mentalray forum. They expect one thing to happen not understanding or even wanting to understand that theres more than one way of skinning the fish that all fit tthe bill nicely.

Besides nodes realy start to shine if you can reuse the network many times in your script. But ia i said pontmatrix mult actualy creates a temporaray node linkage and it is a basic maya script so it cant be all that expensive if maya devteam uses it regulary. Or in fact you too, thinking it is mel while it is a node lonkage in all actuality if you look at it closely.

But if you look closely at the script they supply you with the worong answer, atleast wrong form what i asumed. Test it.

Very interesing, did a test:

// made a cube and moved, rotated and scaled it. Got this world matrix:
float $matrix1 [] = {0.317846, 0.194673, 0.308345, 0,
0.720666, 1.149498, -1.468608, 0,
-1.147928, 1.235169, 0.403477, 0,
0, 5.433526, -4.682081, 1};
// got the position of one of the verts in local space using xform -q -os -t:
float $localPoint1 [3] = {-2.5, 2.5, 2.5};
// the following should output the position of the vert in world space.
// but when I queried the position of the vert using xform -q -ws -t,
// I got the result -1.86277 10.908508 -8.115769, i.e. not the same as the result of
// the following:
float $worldPoint1 [3] = pointMatrixMult ($localPoint1, $matrix1);
// Result: -1.86277 5.474985 -3.43369 //
This, however, works like a charm:
matrix $matrix2 [4] [4] = <<0.317846, 0.194673, 0.308345, 0;
0.720666, 1.149498, -1.468608, 0;
-1.147928, 1.235169, 0.403477, 0;
0, 5.433526, -4.682081, 1>>;
matrix $localPoint2 [1] [4] = <>;
matrix $worldPoint2 [1] [4] = $localPoint2 * $matrix2;
// Result: <> //
Is it because of this line in the pointMatrixMult.mel script?
setAttr ($multNode+".vectorMultiply") true;
(it obviously is, because when I commented that line out, it gave me the correct result).

I'm not good enought at math to fully know why this is, (I kind of understand that vector-matrix multiplication is different from point-matrix multiplication because vectors have direction and magnitude), but I'm glad I found out. Cheers.

Ok, I did some timing tests and I got some very interesting results.

I used four methods - the default pointMatrixMult procedure (with the above mentioned line commented out so the result is correct), and three which I coded, and iterated them. At each iteration I gave the input matrix (as float array) and point (as float array) random values. You can get the test script here:
pointMatrixMultTimeTest.txt
(copy and paste into script editor).

the output is as follows:

Number of iterations: 100000

Method 1: pointMatrixMult - unoptimized node creation + deletion (node is created at every iteration).
Result: 0.4570506982, -3.989602981, 2.072971347
Time: 57.8

Method 2: pointMatrixMultNode - optimized node creation + deletion (node is only created once).
Result: 1.947885851, 1.192570663, 1.787191145
Time: 18.89

Method 3: pointMatrixMultMel - simple mel math operations.
Result: -1.699344621, 1.621976277, -0.1305417622
Time: 2

Method 3: pointMatrixMultMM - matrix multiplications with matrix conversions.
Result: -1.392984755, 2.293708704, -2.327621804
Time: 2.74

/edit1: there's a typo in the output (the last method is of course supposed to be method 4), - this doesn't affect the calculations though./edit1

/edit2. I tested without random assignment first, using the same matrix and point for all iterations of all 4 methods. I added the random assignment in case one of the methods benefited from getting the same values over and over, but it didn't seem to matter to the time proportionality./edit2

You can see outputs for 10000 and 1000 iterations too here (the times are pretty proportional):
pointMatrixMultTimeTestOutputs.txt
The tests were made after a fresh system restart.

It seems that the procedure which just uses simple mel math operations (float addition and multiplication) is the winner. The matrix conversion method gets second price, and is almost as fast. Then there's a big leap to the bronze, the optimized node method (seems that getAttr and setAttr are actually quite slow), 10 times slower than the winner, and then the jumbo, Alias' own procedure, which is 30 to 50 times slower than the simple math method (in one test with 100000 iterations, where I'd done the test a few times, this took 100 s to calculate!).

So, it might be a good idea for me to read up on some math and write those procedures in my first post after all.

Do you use set attr? and get attr? to process the networks since they are dfenomenaly expensive operations!

If you use a direct connection to a expression that puts your values to whatever you need you will speed things up with a HUGE deal, as getAttr forces maya to reevaluate the ENTIRE graph. So you might end up calculating the thing 2 times as opposed to 1.

Anyway get matrix playground and gert the functions form there

ps share the testbech so that i can veryfy thet it is indeed optimaly done!

I tend not to, unless I don't know any other way of getting a value. But Alias' pointMatrixMult procedure uses both getAttr and setAttr. Didn't know getAttr was that expensive though. Thanks for the tip .

Aha! didn't know that. But calculating with nodes is still faster than expressions in a network right?
Anyway, sometimes you need to script something that does shitloads of calculations once and you can't use connections (e.g. when building caches for fluids, just to give an example). So it's good to have an alternative.

Are you kidding??? That is EXACTLY what I was looking for . I feel kind of stupid now for not doing a search for "matrix" in the mel section. Well, I've learned a lot from this thread so it sure wasn't for nothing.

You mean my system specs?
p4 2.4 ghz, 512 mb ram. XP.