Python: obj.parent = new_parent_obj, clears obj.matrix_parent_inverse for no reason. #62431

Closed
opened 2019-03-10 22:14:33 +01:00 by Jose Conseco · 7 comments

I want to change object parent form Cube to Suzane, so that it won't move (matrix_basis, and matrix_world should be the same).
We search for new obj.matrix_parent_inverse2, in code below. It should work (proof on bottom), but last line clears new obj.matrix_parent_inverse

def set_new_parent_bug(obj, new_parent):
     new_m_p_inv = new_parent.matrix_world.inverted() * obj.parent.matrix_world * obj.matrix_parent_inverse
     obj.matrix_parent_inverse = new_m_p_inv
     obj.parent = new_parent # this clears m_parent_inv !! BUG?

In blend file I have script with broken function set_new_parent_bug(), and workaround function set_new_parent() that backups obj.matrix_parent_inverse, so that after changing parent and it being cleared to Identity Matrix, we can still use backup for calculation of new matrix_parent_inverse (for new parent)

Parenting_bug.blend
But affects both blender 2.8 and 2.79
In short using obj.parent = xxx, should not clear obj.matrix_parent_inverse

And below proof that the math is valid (we search for new matrix_parent_inverse so that objects stays in place).
mat_world should be same with old parent and new parent

old_m_w == new_m_w
old_parent_m_w @ obj.m_p_inv1 @ obj.m_basis  ==  new_parent_m_w @ **obj.m_p_inv2** @ obj.m_basis
new_parent_m_w.inv() @ old_parent_m_w @ obj.m_p_inv1 @ obj.m_basis  ==  new_parent_m_w.inv() @ new_parent_m_w @ **obj.m_p_inv2** @ obj.m_basis
new_parent_m_w.inv() @ old_parent_m_w @ obj.m_p_inv1   ==   **obj.m_p_inv2**
I want to change object parent form Cube to Suzane, so that it won't move (matrix_basis, and matrix_world should be the same). We search for new obj.matrix_parent_inverse2, in code below. It should work (proof on bottom), but last line clears new obj.matrix_parent_inverse ``` def set_new_parent_bug(obj, new_parent): new_m_p_inv = new_parent.matrix_world.inverted() * obj.parent.matrix_world * obj.matrix_parent_inverse obj.matrix_parent_inverse = new_m_p_inv obj.parent = new_parent # this clears m_parent_inv !! BUG? ``` In blend file I have script with broken function **set_new_parent_bug()**, and workaround function **set_new_parent()** that backups obj.matrix_parent_inverse, so that after changing parent and it being cleared to Identity Matrix, we can still use backup for calculation of new matrix_parent_inverse (for new parent) [Parenting_bug.blend](https://archive.blender.org/developer/F6796732/Parenting_bug.blend) But affects both blender 2.8 and 2.79 In short using obj.parent = xxx, should not clear obj.matrix_parent_inverse And below proof that the math is valid (we search for new matrix_parent_inverse so that objects stays in place). mat_world should be same with old parent and new parent ``` old_m_w == new_m_w old_parent_m_w @ obj.m_p_inv1 @ obj.m_basis == new_parent_m_w @ **obj.m_p_inv2** @ obj.m_basis new_parent_m_w.inv() @ old_parent_m_w @ obj.m_p_inv1 @ obj.m_basis == new_parent_m_w.inv() @ new_parent_m_w @ **obj.m_p_inv2** @ obj.m_basis new_parent_m_w.inv() @ old_parent_m_w @ obj.m_p_inv1 == **obj.m_p_inv2** ```
Author

Added subscriber: @JoseConseco

Added subscriber: @JoseConseco

Added subscriber: @brecht

Added subscriber: @brecht

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Brecht Van Lommel self-assigned this 2019-03-11 14:40:52 +01:00

You can manually set matrix_parent_inverse to the old value if you need to, but I don't think preserving is the most commonly useful behavior.

Even if we did want to change it, it would break backward compatibility and I don't think it's worth it.

You can manually set `matrix_parent_inverse` to the old value if you need to, but I don't think preserving is the most commonly useful behavior. Even if we did want to change it, it would break backward compatibility and I don't think it's worth it.
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Might be misunderstanding, but if you only want to change parent without moving, why not just do something like:

obj.parent = new_parent
obj.matrix_parent_inverse = new_parent.matrix_world.inverted()
Might be misunderstanding, but if you only want to change parent without moving, why not just do something like: ``` obj.parent = new_parent obj.matrix_parent_inverse = new_parent.matrix_world.inverted() ```
Author

@lichtwerk, this will work only if for old parent:
obj.matrix_parent_inverse would be same as obj.parent.matrix_world.inverted() (which is true only at time of parenting, as long as we don't move parent).
then obj.matrix_parent_inverse * obj.parent.matrix_world == Identity Matrix. And obj.m_w == basically obj.matrix_basis
But as soon as we move obj.parent, then above equation wont give you identity matrix, and obj.matrix_world will change (child will follow change of parent obj transform).
To test you can open blend file attached to task, and before running the script offset first parent object (cube). You will see, that with your method child object won't stay in one place.

@lichtwerk, this will work only if for old parent: obj.matrix_parent_inverse would be same as obj.parent.matrix_world.inverted() (which is true only at time of parenting, as long as we don't move parent). then obj.matrix_parent_inverse * obj.parent.matrix_world == Identity Matrix. And obj.m_w == basically obj.matrix_basis But as soon as we move obj.parent, then above equation wont give you identity matrix, and obj.matrix_world will change (child will follow change of parent obj transform). To test you can open blend file attached to task, and before running the script offset first parent object (cube). You will see, that with your method child object won't stay in one place.
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#62431
No description provided.