Changes to Python Matrices for Blender 2.62

Andrew Hale writes:

For quite some time Blender has received complaints from people acquainted with matrix math regarding Python matrices in Blender. Having had some experience in Python scripting and wanting to become involved in C development I decided to investigate this myself. After much investigation on the matter and discussion with Blender developers and script writers alike, many of the most common complaints were identified and possible solutions found. Campbell Barton and I then set about rectifying these issues.

During the process of modifying the code we also discovered some other opportunities for improvement in matrices. Perhaps the most confusing of the problems discovered, was that on printing a matrix to the console, columns were shown as rows. While there were some good reasons for this, it was the root of much confusion and has now been rectified. Using print(matrix) will correctly show columns as columns.

While correcting the print() function for matrices requires only relatively minor changes to the code, tackling the root of the matrix issues required somewhat more significant changes. The main issue relating to matrices was the order of access to a matrix element. Each element in a matrix can be located by the use of two indices, the number of the row and column which it is in. Ordinarily in matrix math, the element is accessed by listing the row first and the column second. This convention is also followed by NumPy and many other packages used for matrix math. However, this was not previously followed by matrices in Blender.

This change also meant that indexing only the first index (i.e. matrix[0]) accesses a row whereas previously this accessed a column. In order to still permit access to columns two new attributes were added; matrix.row and matrix.col. Also added was the ability to access the translation component of a 4×4 matrix via matrix.translation.

It is hoped that these changes will help to standardise the mathutils, thereby simplifying the transition for those experienced in other matrix math packages.

While these changes affect access to matrix elements and manual matrix creation, no matrix methods, classmethods or matrix arithmetic are affected. For hints and tips on coping with the changes there is a thread on BlenderArtists relating to the changes for Blender 2.62.

Advertisement