Matrix question

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Jun 12, 2013 at 22:44

Can someone explain what the “toMatrix” part of this code is doing? It’s python. Don’t know where the “toMatrix” function came from but I’m trying to update an old blender script.

            cam_loc=obj.getLocation()
            e1=Mathutils.Euler([obj.RotX, obj.RotY, obj.RotZ])
            e2=e1.toMatrix()
            cam_vec=Vector([obj.LocX, obj.LocY, obj.LocZ])
            target= Mathutils.VecMultMat(cam_vec, e2)

Oh, duh, it came from mathutils. I was using a different euler function. Why to a matrix, though? I suppose because they want to end up with a matrix. Ok, so the rotation is one line of the matrix, right? which one? and will that work if I get those numbers from the matrix? I think the matrix is 4 across. There isn’t a obj.RotX anymore that I can find.

edit: there is an object.rotation_euler which gives 3 numbers. It seems repetitive, but it should work.

Never mind.

Hate 3d math.

6 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jun 12, 2013 at 23:22

The euler angles aren’t going to be in one line of the matrix, no. The matrix will be made by multiplying together an X rotation, a Y rotation, and a Z rotation, each of which is made of some sin/cos factors in different places.

They’re converting the euler angles to a matrix so they can apply the rotation to cam_vec.

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Jun 13, 2013 at 00:02

So an x rotation isn’t a single float number?
obj.rotation_euler returns Euler((0.0,0.0,0.0),’XYZ’) I think that’s the new way to get rotation as there is no object.rotx that I can find.
Would I be able to use those numbers for the mathUtil. Euler(Xrot,Yrot,Zrot) so I can call the e1.toMatrix function?

That doesn’t work anyway. It says a Euler object doesn’t have a toMatrix() function.

OK, they changed the function to “to_matrix()”

but is my logic wrong, or are they talking about something else? The Euler function takes 3 numbers. I saw the rotationx cosine/ whatever matrix you were talking about also. I thought there was a rotation row on a 3X3 matrix or 3X4 whatever the euler matrix was.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jun 13, 2013 at 00:39

An x rotationmatrixisn’t a single number, no. It has the sine and cosine of the angle in it. There’s no simple relationship between Euler angles and the components of a general rotation matrix. The formulas to convert are pretty yucky, but you can find them online.

I don’t know Blender, but if the object has a rotation_euler property then that’s probably the RotX, RotY, RotZ you’re looking for, just stored as a vector instead of 3 separate values. I don’t know about the toMatrix() function, that’s something from Blender’s math library. Probably it just got renamed or moved to a different object or something.

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Jun 13, 2013 at 00:45

Yeah, it was renamed. I’m pretty sure I have the replacements and it should work however it was meant to.

6837d514b487de395be51432d9cdd078
0
TheNut 179 Jun 13, 2013 at 06:35

Blender generally stores all transformations as a matrix internally, so these operations are in fact converting from a matrix to euler, and then back into a matrix again (eww). There are some exceptions, for example when you do keyframe animations you can decide what data format is used. Nevertheless, always fetch the transformation matrices from Blender directly. If you look at Blender’s docs for objects, you’ll see they have a few matrices for you to use. matrix_basis, matrix_local, matrix_world, etc.

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Jun 13, 2013 at 14:53

The problem is I don’t understand what they are doing so I’d probably screw it up. This isn’t anything overly difficult. It only getting the floor, camera, and lights for a 2.5d game. If it absolutely does not work when I translate, I’ll have to understand it.