bpy.types.Bone.AxisRollFromMatrix() returns bad roll value #82930

Closed
opened 2020-11-23 10:13:25 +01:00 by A. H. · 11 comments

System Information
Operating system: Win 10
Graphics card: Nvidia

Blender Version
Broken: 2.82a+, ..., 2.90
Worked: 2.81a

Short description of error
The roll value returned by bpy.types.Bone.AxisRollFromMatrix() changes between 2.81 and 2.82, and is still broken in 2.90. This means that bones created with this function have a wrong local matrix and become unusable.
The broken roll values are consistently tiny (about e-07).

good - 2.81a:

<Matrix 4x4 (-0.0891, -0.0448,  0.9950,  0.1884)
            ( 0.4410, -0.8975, -0.0009, -4.7614)
            (-0.8931, -0.4387, -0.0998,  4.2873)
            ( 0.0000,  0.0000,  0.0000,  1.0000)>

result_axis: <Vector (-0.0448, -0.8975, -0.4387)>
result_roll: 1.6745758056640625

broken - 2.82+:

<Matrix 4x4 (-0.0891, -0.0448,  0.9950,  0.1884)
            ( 0.4410, -0.8975, -0.0009, -4.7614)
            (-0.8931, -0.4387, -0.0998,  4.2873)
            ( 0.0000,  0.0000,  0.0000,  1.0000)>

result_axis: <Vector (-0.0448, -0.8975, -0.4387)>
result_roll: 8.284717296191957e-06

Exact steps for others to reproduce the error
Run the py script in the attached blend file with the blender versions mentioned above.AxisRollFromMatrix.blend

**System Information** Operating system: Win 10 Graphics card: Nvidia **Blender Version** Broken: 2.82a+, ..., 2.90 Worked: 2.81a **Short description of error** The roll value returned by bpy.types.Bone.AxisRollFromMatrix() changes between 2.81 and 2.82, and is still broken in 2.90. This means that bones created with this function have a wrong local matrix and become unusable. The broken roll values are consistently tiny (about e-07). good - 2.81a: ``` <Matrix 4x4 (-0.0891, -0.0448, 0.9950, 0.1884) ( 0.4410, -0.8975, -0.0009, -4.7614) (-0.8931, -0.4387, -0.0998, 4.2873) ( 0.0000, 0.0000, 0.0000, 1.0000)> ``` result_axis: `<Vector (-0.0448, -0.8975, -0.4387)>` result_roll: **1.6745758056640625** broken - 2.82+: ``` <Matrix 4x4 (-0.0891, -0.0448, 0.9950, 0.1884) ( 0.4410, -0.8975, -0.0009, -4.7614) (-0.8931, -0.4387, -0.0998, 4.2873) ( 0.0000, 0.0000, 0.0000, 1.0000)> ``` result_axis: `<Vector (-0.0448, -0.8975, -0.4387)>` result_roll: **8.284717296191957e-06** **Exact steps for others to reproduce the error** Run the py script in the attached blend file with the blender versions mentioned above.[AxisRollFromMatrix.blend](https://archive.blender.org/developer/F9358709/AxisRollFromMatrix.blend)
Author

Added subscriber: @csnyfan

Added subscriber: @csnyfan

This issue was referenced by 8330e19cb2

This issue was referenced by 8330e19cb2e7faca9884e34a8f46564250c8c29a

Added subscribers: @angavrilov, @mano-wii

Added subscribers: @angavrilov, @mano-wii

I'm not familiar with this area and I do not know what results to expect.
But this change occurred in the commit d1657b40.
So, @angavrilov, could you shed some light here?

I'm not familiar with this area and I do not know what results to expect. But this change occurred in the commit d1657b40. So, @angavrilov, could you shed some light here?

Well, this matrix is flipped with negative scale (determinant is -1), so technically AxisRollFromMatrix has no solution - there is no possible axis and roll it could return so that matrix == MatrixFromAxisRoll(*AxisRollFromMatrix(matrix)). The old code seemed to work because it was very simple and based on a lot of assumptions, while the new code includes a full conversion to quaternions.

Internally this is a non-issue for the actual armature code because rest pose cannot have scale, and may only potentially cause problems in a degenerate case for B-Bones, which were the cause of that fix in the first place due to previous math having issues in a way less degenerate case.

Personally I would lean to making the function error out if determinant is negative (or just documenting the restriction for the sake of performance), because there isn't a single 'best' way to unflip a matrix, so doing that is best left to the caller.

Well, this matrix is flipped with negative scale (determinant is -1), so technically AxisRollFromMatrix has no solution - there is no possible axis and roll it could return so that `matrix == MatrixFromAxisRoll(*AxisRollFromMatrix(matrix))`. The old code seemed to work because it was very simple and based on a lot of assumptions, while the new code includes a full conversion to quaternions. Internally this is a non-issue for the actual armature code because rest pose cannot have scale, and may only potentially cause problems in a degenerate case for B-Bones, which were the cause of that fix in the first place due to previous math having issues in a way less degenerate case. Personally I would lean to making the function error out if determinant is negative (or just documenting the restriction for the sake of performance), because there isn't a single 'best' way to unflip a matrix, so doing that is best left to the caller.

@angavrilov, would it be a good idea to set an error message in these cases?
Something like:

  if (!mat3_vec_to_roll(mat, r_axis, &tmp_roll)) {
     BKE_report(reports, RPT_ERROR, "Impossible to determine the rool of this matrix. Make sure it is not negative");
  }
@angavrilov, would it be a good idea to set an error message in these cases? Something like: ``` if (!mat3_vec_to_roll(mat, r_axis, &tmp_roll)) { BKE_report(reports, RPT_ERROR, "Impossible to determine the rool of this matrix. Make sure it is not negative"); } ```
Author

This is good to know - I wasn't aware that the function needed a positive determinant since the same data just worked on the old blender.
Raising an error and documenting it seems like the best solution.

This is good to know - I wasn't aware that the function needed a positive determinant since the same data just worked on the old blender. Raising an error and documenting it seems like the best solution.

There is no error return - I didn't check, but this probably fails the matrix to quaternion conversion and outputs a zero rotation quaternion. To do a check you would actually have to compute the determinant of the matrix. Also, a sheared matrix isn't great either. The math behind this function basically expects a so called 'special orthogonal matrix', meaning there should be no scaling or shear, so the easiest solution without a performance impact is to adjust the documentation string.

If you really want to do a check it would be something like:

normalize_m3(matrix);
if (determinant_m3_array(matrix) < 0.9999f) { error "matrix is flipped or has shear" }
There is no error return - I didn't check, but this probably fails the matrix to quaternion conversion and outputs a zero rotation quaternion. To do a check you would actually have to compute the determinant of the matrix. Also, a sheared matrix isn't great either. The math behind this function basically expects a so called '*special orthogonal matrix*', meaning there should be no scaling or shear, so the easiest solution without a performance impact is to adjust the documentation string. If you really want to do a check it would be something like: ``` normalize_m3(matrix); if (determinant_m3_array(matrix) < 0.9999f) { error "matrix is flipped or has shear" } ```

Would this be a good description?:
"Convert a rotational matrix to the axis + roll representation. Note that the resulting roll value is unpredictable if the matrix has shear or is negative"

Would this be a good description?: `"Convert a rotational matrix to the axis + roll representation. Note that the resulting roll value is unpredictable if the matrix has shear or is negative"`

According to wikipedia, "negative matrix" means all matrix elements are negative, so you have to say "has negative determinant" to be correct. In practical terms it means it mirrors/flips the object.

According to wikipedia, "negative matrix" means all matrix elements are negative, so you have to say "has negative determinant" to be correct. In practical terms it means it mirrors/flips the object.

Changed status from 'Needs Triage' to: 'Resolved'

Changed status from 'Needs Triage' to: 'Resolved'
Germano Cavalcante self-assigned this 2020-11-25 17:55:39 +01:00
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
4 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#82930
No description provided.