[Bf-committers] Python MathUtils API matrix multiplication order bug fixed - check your scripts!

Benoit Bolsee benoit.bolsee at online.be
Tue Sep 8 00:42:16 CEST 2009

Hi,

I fixed a disturbing bug in the Python MathUtils library where the
multiplication order or matrix and vector had to be reversed from the
canonical order to give correct result. Some script may have
implementated work around that should be removed. This is only
applicable to 2.5 scripts for the moment, I guess it's better not to
touch 2.49. The commit log explains in details what needs to be done.

/benoit
==========================
Fix bug in Mathutil with matrix/matrix and matrix/vector multiplication
order.

MathUtil matrix type follows Blender convention of column major storage.
This means that the elements on one column are contiguous in memory.
Vectors are one dimensional arrays that can be considered in row or in
column but the Blender convention is column so vector should only be
considered as column. This means that the only logical multiplication
operation between matrix and vector is matrix * vector.

This convention is respected in all parts of MathUtil except in
matrix/matrix and matrix/vector multiplication where the row major
convention was assumed, which in the end is equivalent in reversing the
order of multiplication.

This is clearly a bug and must be corrected but the side effect is that
it will break the scripts using these operations. Script writers who
implemented work arounds:

1) change order of matrix/vector multiplication.
vec2 = vec1 * mat1

This must be changed to the normal order:
vec2 = mat1 * vec1

2) change order of matrix/matrix multiplication
(with matl a local transform in matw reference)
mat3 = matl * matw

This must be changed to the normal order:
mat3 = matw * matl

3) transpose before an after the multiplication
matl.transpose()
matw.transpose()
mat3 = matw * matl
mat3.transpose()

This must be changed to:
mat3 = matw * matl;