Matrix equality on bones do not match with python FK / IK Snaping script #117111

Open
opened 2024-01-14 13:41:41 +01:00 by Damien Monteillard · 3 comments

System Information
Operating system: Linux-6.1.67-gentoo-x86_64-Intel-R-_Core-TM-i7-6700K_CPU@_4.00GHz-with-glibc2.37 64 Bits, X11 UI
Graphics card: NVIDIA GeForce RTX 2070 SUPER/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 535.146.02

Blender Version
Broken: version: 4.0.2, branch: blender-v4.0-release, commit date: 2023-12-05 07:41, hash: 9be62e85b727
Worked:

Short description of error
Matrix equality on bones do not match with python FK / IK Snaping script
I need to use a bad concept of python function code to solve those equality

def execute(self, context):
    # Bad solution with bpy.ops.wm.redraw_timer with a For loop!
    for i in range(0, 18):
        IK2FK_Leg(context)
        bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
    return {'FINISHED'}

Exact steps for others to reproduce the error
Download FK_IK_matrix_equality_bone.tar.gz
Open the file FK_IK_matrix_equality_bone.blend

With this python writing

def execute(self, context):
    IK2FK_Leg(context)
    return {'FINISHED'}

Press IK to FK Snap button
You need to press multiple times to have a good bone matrix equality.

My function should be 3 bones equality of matrix:

def IK2FK_Leg(self):
    bone_IK.matrix = bone_FK.matrix
    bone_1_IK.matrix = bone_1_FK.matrix
    bone_2_IK.matrix = bone_2_FK.matrix
    bone_3_IK.matrix = bone_3_FK.matrix

Sometimes blender crash!

If you try to choose FK to IK snap button this is the same problem.

**System Information** Operating system: Linux-6.1.67-gentoo-x86_64-Intel-R-_Core-TM-_i7-6700K_CPU_@_4.00GHz-with-glibc2.37 64 Bits, X11 UI Graphics card: NVIDIA GeForce RTX 2070 SUPER/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 535.146.02 **Blender Version** Broken: version: 4.0.2, branch: blender-v4.0-release, commit date: 2023-12-05 07:41, hash: `9be62e85b727` Worked: **Short description of error** Matrix equality on bones do not match with python FK / IK Snaping script I need to use a bad concept of python function code to solve those equality ```Py def execute(self, context): # Bad solution with bpy.ops.wm.redraw_timer with a For loop! for i in range(0, 18): IK2FK_Leg(context) bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) return {'FINISHED'} ``` **Exact steps for others to reproduce the error** Download FK_IK_matrix_equality_bone.tar.gz Open the file FK_IK_matrix_equality_bone.blend With this python writing ```Py def execute(self, context): IK2FK_Leg(context) return {'FINISHED'} ``` Press IK to FK Snap button You need to press multiple times to have a good bone matrix equality. My function should be 3 bones equality of matrix: ```Py def IK2FK_Leg(self): bone_IK.matrix = bone_FK.matrix bone_1_IK.matrix = bone_1_FK.matrix bone_2_IK.matrix = bone_2_FK.matrix bone_3_IK.matrix = bone_3_FK.matrix ``` Sometimes blender crash! If you try to choose FK to IK snap button this is the same problem.
Damien Monteillard added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2024-01-14 13:41:42 +01:00
No description provided.

While the crash needs to be investigated. The script provided does not show a good usage of PoseBone.matrix.

The PoseBone.matrix attribute contains the final 4x4 matrix after parents, constraints, and drivers have been applied. When you set a matrix to PoseBone.matrix, Blender internally extracts the matrix_basis from this matrix and applies it. However, the child bones' matrices need to be updated to correctly extract the ideal matrix_basis for the other bones. This update only happens after the script runs, which is why you are observing the inconsistency.

To work around this, you can force updates by adding the following lines after setting the bone matrices:

bone_1_FK.matrix = bone_1_IK.matrix
bpy.context.view_layer.depsgraph.update()
bone_2_FK.matrix = bone_2_IK.matrix
bpy.context.view_layer.depsgraph.update()
bone_3_FK.matrix = bone_3_IK.matrix

Alternatively, if parent relationships, constraints, and drivers are not crucial in your case, you can set the matrix_basis directly instead:

bone_1_FK.matrix_basis = bone_1_IK.matrix_basis
bone_2_FK.matrix_basis = bone_2_IK.matrix_basis
bone_3_FK.matrix_basis = bone_3_IK.matrix_basis
While the crash needs to be investigated. The script provided does not show a good usage of `PoseBone.matrix`. The `PoseBone.matrix` attribute contains the final 4x4 matrix after parents, constraints, and drivers have been applied. When you set a matrix to `PoseBone.matrix`, Blender internally extracts the `matrix_basis` from this matrix and applies it. However, the child bones' matrices need to be updated to correctly extract the ideal `matrix_basis` for the other bones. This update only happens after the script runs, which is why you are observing the inconsistency. To work around this, you can force updates by adding the following lines after setting the bone matrices: ```py bone_1_FK.matrix = bone_1_IK.matrix bpy.context.view_layer.depsgraph.update() bone_2_FK.matrix = bone_2_IK.matrix bpy.context.view_layer.depsgraph.update() bone_3_FK.matrix = bone_3_IK.matrix ``` Alternatively, if parent relationships, constraints, and drivers are not crucial in your case, you can set the `matrix_basis` directly instead: ```py bone_1_FK.matrix_basis = bone_1_IK.matrix_basis bone_2_FK.matrix_basis = bone_2_IK.matrix_basis bone_3_FK.matrix_basis = bone_3_IK.matrix_basis ```

Please read through Can I redraw during script execution? in the Python manual. Especially the part about using bpy.ops.wm.redraw_timer():

scripts that use this hack will not be considered for inclusion in Blender and any issue with using it will not be considered a bug, there is also no guaranteed compatibility in future releases.

So please provide an example of the problem that does not use this operator.

Please read through [Can I redraw during script execution?](https://docs.blender.org/api/current/info_gotcha.html#can-i-redraw-during-script-execution) in the Python manual. Especially the part about using `bpy.ops.wm.redraw_timer()`: > scripts that use this hack will not be considered for inclusion in Blender and **any issue with using it will not be considered a bug**, there is also no guaranteed compatibility in future releases. So please provide an example of the problem that does not use this operator.
Sybren A. Stüvel added
Status
Needs Information from User
and removed
Status
Confirmed
labels 2024-04-25 17:00:30 +02: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
No Assignees
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#117111
No description provided.