Change Matrix Representation and Access in Python to Conform with Standard Notation #29534

Closed
opened 2011-12-08 06:30:47 +01:00 by Andrew Hale · 8 comments
Member

%%%It is often a point of confusion among new users that matrices don't appear to behave correctly within Blender. It is often cited that internally Blender stores matrices in column major format, while typically users are expecting row major format. However this does not fully explain the issues around matrices. For instance, if a user were to enter:

m=Matrix(((1, 2), (3, 4)))

They would receive the following upon inspecting m:

Matrix(((1.0, 2.0),

         (3.0, 4.0)))

Which appears as expected and may be accessed as expected with m[row][column] notation. However, should a vector be entered, e.g.,

v=Vector((1, 1))

and then multiplied by the matrix, the incorrect result would be formed,

m*v == Vector((4.0, 6.0))

whereas the user would expect

m*v == Vector(( m- [x]- [x] * v- [x] + m- [x]- [x] * v- [x], m- [x]- [x] * v- [x] + m- [x]- [x] * v- [x] )) = Vector((3.0, 7.0))

This issue arises because the Python Representation -> Internal Data conversion is performed incorrectly for the representation shown in the console.

This patch rectifies the issue by ensuring that the conversion from Python representation to internal storage and back again accounts for the differing notations used by each. This also means that no internal functions for calulating matrix products, additions etc are modified, only the initialisation and representation of matrices in Python.

Thanks,
Andrew%%%

%%%It is often a point of confusion among new users that matrices don't appear to behave correctly within Blender. It is often cited that internally Blender stores matrices in column major format, while typically users are expecting row major format. However this does not fully explain the issues around matrices. For instance, if a user were to enter: m=Matrix(((1, 2), (3, 4))) They would receive the following upon inspecting m: Matrix(((1.0, 2.0), ``` (3.0, 4.0))) ``` Which appears as expected and may be accessed as expected with m[row][column] notation. However, should a vector be entered, e.g., v=Vector((1, 1)) and then multiplied by the matrix, the incorrect result would be formed, m*v == Vector((4.0, 6.0)) whereas the user would expect m*v == Vector(( m- [x]- [x] * v- [x] + m- [x]- [x] * v- [x], m- [x]- [x] * v- [x] + m- [x]- [x] * v- [x] )) = Vector((3.0, 7.0)) This issue arises because the Python Representation -> Internal Data conversion is performed incorrectly for the representation shown in the console. This patch rectifies the issue by ensuring that the conversion from Python representation to internal storage and back again accounts for the differing notations used by each. This also means that no internal functions for calulating matrix products, additions etc are modified, only the initialisation and representation of matrices in Python. Thanks, Andrew%%%
Author
Member

Changed status to: 'Open'

Changed status to: 'Open'
Author
Member

%%%While using the patched matrices, an error in matrix/vector multiplication was uncovered for non-square matrices.

m = Matrix(((1, 1, 1), (2, 2, 2)))
v = Vector((1, 1, 1))

m * v == Vector((3.0, 6.0, -12289734))

This is because resultant vector was not being sized properly but merely taking its size from the input vector which is bad. It should have size == mat1->col_size

This needs fixing.%%%

%%%While using the patched matrices, an error in matrix/vector multiplication was uncovered for non-square matrices. m = Matrix(((1, 1, 1), (2, 2, 2))) v = Vector((1, 1, 1)) m * v == Vector((3.0, 6.0, -12289734)) This is because resultant vector was not being sized properly but merely taking its size from the input vector which is bad. It should have size == mat1->col_size This needs fixing.%%%

%%%I don't think this reasoning is correct. Creating and printing matrices uses a list of columns. So the rows are (1, 3) and (2, 4), and a multiplication with vector (1, 1) gives (4, 6).%%%

%%%I don't think this reasoning is correct. Creating and printing matrices uses a list of columns. So the rows are (1, 3) and (2, 4), and a multiplication with vector (1, 1) gives (4, 6).%%%
Author
Member

%%%I've attached a new patch which accounts for recent changes in mathutils. This patch also rectifies the problem with matrix/vector multiplication noted above. In addition, it also wraps the translation component of a matrix into matrix.translation but only for 4x4 matrices. This is especially useful for scripts that originally used matrix- [x] to access the translation component.

Thanks,
Andrew%%%

%%%I've attached a new patch which accounts for recent changes in mathutils. This patch also rectifies the problem with matrix/vector multiplication noted above. In addition, it also wraps the translation component of a matrix into matrix.translation but only for 4x4 matrices. This is especially useful for scripts that originally used matrix- [x] to access the translation component. Thanks, Andrew%%%
Author
Member

%%%Small fix for most recent SVN%%%

%%%Small fix for most recent SVN%%%
Author
Member

%%%Final patch which is separated from some fixes already applied by Campbell.%%%

%%%Final patch which is separated from some fixes already applied by Campbell.%%%

%%%applied, closing.%%%

%%%applied, closing.%%%

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#29534
No description provided.