Advertisement

You're blocking ads, which pay for BlenderNation. Read about other ways to support us.

Changes to Python Matrices for Blender 2.62

28

Andrew Hale describes changes that were recently made to the way Python handles matrices.

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 4x4 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.

28 Comments

    • Nabil Stendardo on

      That is like saying: The year 1900 is considered a leap year in Microsoft Office (which in reality wasn't - divisible by 100 but not by 400) - so it should be like that everywhere else. Worldwide mathematicians use conventions (such as the row--column convention), and it's not because one piece of software (even if blender uses it) does it differently, that we should do the same. I really welcome this change.

    • Campbell Barton on

      In blenders C code its still OpenGL - theres a division between a user-friendly python module and our internal C code.

  1. There are times when I read something I think I understand only to quickly realize that someone(s) much MUCH, smarter than I wrote it. 

    This is one of those times.

    All I know is something wasn't doing what it was suppose to and you guys made it better. Which .. well done. Now the matrix can be better and the machines won't win ... or .. something.

    • Campbell Barton on

      yep, some (though many scripts that do matrix maths turned out not to be effected).
      For the most part we found updates are not all that difficult.

  2. The new conventions sounded a bit confusing at first, but I think I get it (I work in the Numerical Linear Algebra field btw). So under the hood the matrices are stored as row-major and that's the way you can access them now? That's important because of performance issues. You don't want to jump around in memory.

    • Internally (i.e. in C) the matrices are stored in column major format which is a carry over from OpenGL. Since all arithmetic with matrices is done in C, we can make sure to access contiguous memory where possible. However, you are correct, when looping over all elements in Python you will not access contiguous elements since rows are accessed first. Given the relative speeds of attribute access in Python vs memory access, I would be surprised if any slowdown was noticeable.

        • Yes, however, OpenGL uses column major layout in a linear array. So accessing as for row major is equivalent  to transposing the matrix, thus to access the elements correctly in C you need to use matrix[col][row]

          •  Alright I misunderstood. You meant internally in Blender it's column-major. Not how C stores a T x[2][2].

  3. Thank you so much Andrew and Campbell. You are really important to the community, specially for those like me, who never made the effort to start programing :-)

  4. Just me ranting....
    So if openGL uses matrix[col][row]
    as said in the faqs [1] people suggested [2] you could set
    the transpose parameter of glUniformMatrix to true to pass matrices in row-order format,
    but that it is much more convenient to use it like matrix[col][row],
    since it is more easy,
    why didn't become matrix[col][row] the standard in programming?
    Like in C++, although you can just print the matrix the way you like,
    anyway.

    1.http://www.opengl.org/resources/faq/technical/transformations.htm
    2.http://stackoverflow.com/questions/8092172/the-use-of-column-major-format-in-the-spec-and-blue-book

    • how is column-major order more convenient? Row-major is the standard in mathematics and it's been like that years before 
      even computers arrived. Why? Just cause, probably.

      • Because you can access the base vectors easily. It was described in the second link.

        Anyway, I find this a stupid move. I'm glad I only have my exporter code in Python. And even that I found extra painful to adjust to the ever changing Python base in Blender. It's starting to feel like the good ole 3D studio again lol.

        •  It can indeed be useful to access columns as you describe. To this end
          you can get and set columns using matrix.col[0], matrix.col[1] etc. This can also allow you to index by column first if you really want to, without transposing the matrix. That is, matrix.col[1][3] is the element in the second column and fourth row.

    • Don't give up! :)
      There is an excellent series of lectures by prof. Gilbert Strang on youtube, search for "MIT 18.06 Linear Algebra". Watch it and (most importantly) do some exercises on paper and you will get it pretty fast.

    • That's pretty much the case.
      However, it's a special table of numbers that has some simple operations defined on it, like addition and multiplication.

    • Lawrence D’Oliveiro on

      It’s a rectangular block of numbers, plus operations defined on pairs of blocks.

      Matrix multiplication is a particularly interesting operation. Combine matrix multiplication with 4×4 matrices, and you can represent any linear transformation in 3D space—that is, any transformation in which straight lines remain straight lines.

      I put together a description of linear transformations (carefully avoiding any mention of matrices) here: http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Coordinate_Transformations

        • Thank you gentlemen, I didn't knew that there was so much theory in the wikibooks.
          Also it is astonishing how much open information some universities give people.
          In youtube you will find quite a lot detailed videos by stanford and mit,
          which will give you so much insight, that you will be able to learn the subject without
          having to inscribe in a local university. Aside from the directions what to learn and
          the ability to ask the lecturers question directly that maybe in a local university are common.
          (Is this correct grammar my english is a bit rusty :-))
          All hail to open knowledge, so we can learn and study together with fewer barriers
          than ever before, so that we can live together in wisdom, prosperity and hopefully
          harmony peace and love :-)
          Ok this is Off-Topic. But I like to dicuss so much.
          Greetings Dani C

Leave A Reply

To add a profile picture to your message, register your email address with Gravatar.com. To protect your email address, create an account on BlenderNation and log in when posting a message.

Advertisement

×