Hi All,
I thought I would post this thread to update everyone on some changes to matrices for Blender 2.62.
Introduction
It has often been a point of some confusion among new Blender users that matrices do not behave as is “normal” in mathematics. The common response to this is that Blender uses column major storage for matrices. While this did cause some of the issues, the situation was further complicated by the fact that printing matrices shows the matrix columns as rows.
While the issue of column major storage is important in C where the layout of values in memory is pertinent, in Python, layout in memory is not nearly as important. As such, so as to comply with the standard mathematical of indexing the row first, some changes have been made in SVN r42816. The changes are been detailed below.
Uneffected
Most uses of matrices are uneffected by the changes. This includes;
- Matrix Classmethods such as Matrix.Rotation(), Matrix.Translation() etc.
- Matrix multiplication with vectors and other matrices.
- Matrix methods such as matrix.identity(), matrix.transpose(), matrix.to_translation() etc
In most scripts this will mean the majority, if not all, uses of matrices will be unchanged.
Changes
Element Indexing
The biggest change to matrices is that now rows are indexed before the column. As such, consider the following matrix:
matrix = [1 2 3]
[4 5 6]
[7 8 9]
So to access 4, we take the second row, first column:
matrix[1][0] == 4
Previously, the column index came first.
Matrix Initialisation
This is also important for manually initialising the matrix yourself, previously a list of all columns was supplied. Now a list of rows is supplied. For the matrix above, to create this matrix in Blender you would now do the following:
matrix = Matrix(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
Previously you would create it as:
matrix = Matrix(((1, 4, 7), (2, 5, 8), (3, 6, 9)))
Matrix Column Access
Since the order has been changed,
matrix[0]
now returns the first row instead of the first column as it did previously.
However, if you require access to the matrix columns you can do so as follows
matrix.col[0]
Matrix Translation Component
If you need to get or set the translation component of a 4 x 4 matrix, then you can now use,
matrix.translation
Previously the translation component was accessed by using:
matrix[3][0:3]
Matrix Length
The length of a matrix (as calculated by len()) now returns the number of rows in the matrix, previously this was the number of columns.
Effect on Scripts
These changes will effect the function of some scripts which access matrix elements or initialise matrices manually. However, in updating many of the scripts included with Blender, many scripts were found not to need any changes at all. See the link below for a cheat sheet for script writers and list of gotchas which apply.
Additional Information
For additional information including a cheat sheet, see here http://wiki.blender.org/index.php/User:TrumanBlending/Matrix_Indexing
Cheers,
Truman