need advice on matrix vector multiplication

fireside 141 Jun 15, 2013 at 00:28

I’m trying to change this line
target= Mathutils.VecMultMat(cam_vec, e2)

to a now overloaded vec * matrix

The problem is that I looked up multiplying a vector by a matrix and it says the vector must have the same number of members as rows in the matrix.
It’s a 3 member vector and a 4X4 matrix.

Also, when I do this in Blender, it’s not commutative. vec * matrix is different from matrix * vec , but both return a 3 member vector

Is it safe to replace with cam_vec * e2? or do I try to dig out an old copy of blender and compare results?

4 Replies

Please log in or register to post a reply.

fireside 141 Jun 15, 2013 at 00:58

Wait. I don’t even know what kind of matrix it made.

I tried to do this:
e1.to_matrix and it says
built in method to_matrix at some memory location, and it won’t let me multiply it by a vector.
So, it sort of made a matrix out of a Euler vector, but it won’t show me what it is, just where it is. Here’s the code, can someone explain what it’s doing.

matrix = obj.getMatrix()

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

for a little more background, this is preparing camera information for a 3ds file.
This page describes a blender euler and the to_matrix function:


It’s multiplying a location vector by a 3X3 rotation matrix and calling that the target.

Reedbeta 167 Jun 15, 2013 at 01:56

It’s saying toMatrix is a function, so you need to call it: e1.toMatrix() <– note the parentheses. That should give you the actual matrix.

As for the 3-component vector vs 4x4 matrix, I’m confused - the docs say toMatrix returns a 3x3 matrix. However, you can probably fix it by appending a 1.0 to the vector to make it 4 components. That’s so it can do translation, basically. You can just ignore the 4th component of the result; it should always be 1.0.

It’s probably supposed to be vec * matrix, not matrix * vec. From what I can tell from googling, the old API used VecMultMat for vec * matrix, and MatMultVec for matrix * vec.

fireside 141 Jun 15, 2013 at 02:33

Thanks Reedbeta. The replacement function from the old one is to_matrix() in the new version. I left out the parenthesis and it made it into an object in memory and then I panicked. I think i’ll have to download an old version and do some tests on the overload. I think it’s supposed to be a 3x3 matrix so it can be multiplied by a 3 member vector. I should probably download an old version and make sure the overload is the same with some tests. I was looking at some pages on how to make a 3X3 rotation matrix from 3 angles and I did not want to go down that road.

TheHermit 101 Aug 13, 2013 at 06:35

I’ve seen 3d vectors and rotation matrices expressed as 4d for the sake of being able to implement translations as well (you can’t do a translation of a 3-vector with a 3x3 matrix). If this is the case in your problem, I believe the standard thing to do is to have the fourth component of the vector always be ‘1’. It sounds like you found an answer to your problem, but if you run into something like this in the future its something you can try.

If you need to do some arbitrary rotations, I’d suggest learning about quaternions though. They’re easier to use in interface design than Euler angles, because you can always take a given orientation of the object and easily ‘add’ new rotations to it continuously. If you ever want to animate it, quaternions also animate more smoothly, and I believe they avoid the problem of gimbal-lock around the z-axis that you can sometimes get with Euler angles. Here is a link to a Gamasutra article about quaterion rotation.