4

I have a Python script that's recursively parenting children objects to parents. I can use bpy.ops.object.parent_set(), which produces the desired outcome but is too slow for what I'm doing.

Another solution I found was to not call bpy.ops, but rather to set the parent directly, as outlined in this answer. Basically it proposes to do this kind of thing:

childObject.parent = parentObject
childObject.matrix_parent_inverse = parentObject.matrix_world.inverted()

This works really great except for that it requires a call to bpy.context.evaluated_depsgraph_get().update() first so that the parentObject's matrix_world matrix actually is correct. Although this approach is significantly faster than calling bpy.ops, it still requires a dependency graph update when it seems that all I really need is to update the parent's the matrix_world and nothing else.

Is there a way to only update the matrix_world without calling bpy.context.evaluated_depsgraph_get().update() ?

Thanks!

Anson Savage
  • 3,392
  • 3
  • 31
  • 67

1 Answers1

5

I found one solution here. As soon as any transformations are applied on the parent object (before the parenting happens I guess), then update the parent's matrices using this script:

def update_matrices(obj):
    if obj.parent is None:
        obj.matrix_world = obj.matrix_basis
else:
    obj.matrix_world = obj.parent.matrix_world * \
                       obj.matrix_parent_inverse * \
                       obj.matrix_basis

This did the job for what I needed, but I'm not sure if there's a better way! If there is, feel free to post another answer!

Anson Savage
  • 3,392
  • 3
  • 31
  • 67