From f6c34ac1961bfa054bb1439f8d8fc3447ea61944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Perdig=C3=A3o?= Date: Thu, 19 Dec 2024 09:00:08 -0300 Subject: [PATCH] Refactor `enable_constraints` for performance reasons. `context.view_layer.update()` was being called for every object in the loop. Now it is just called after all the constraints are enabled. --- src/bonsai/bonsai/tool/aggregate.py | 35 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/bonsai/bonsai/tool/aggregate.py b/src/bonsai/bonsai/tool/aggregate.py index 7fe6bd9289..fba4fa1b9a 100644 --- a/src/bonsai/bonsai/tool/aggregate.py +++ b/src/bonsai/bonsai/tool/aggregate.py @@ -123,25 +123,27 @@ class Aggregate(bonsai.core.tool.Aggregate): bpy.ops.constraint.apply(constraint=constraint.name) @classmethod - def disable_constraints(cls, part: bpy.types.Object): + def disable_constraints(cls, objs: list[bpy.types.Object]): # Disable constraints while keeping parts in the same location - matrix = part.matrix_world.copy() - constraint = next((c for c in part.constraints if c.type == "CHILD_OF"), None) - if constraint: - constraint.enabled = False - part.matrix_world = matrix + for obj in objs: + matrix = obj.matrix_world.copy() + constraint = next((c for c in obj.constraints if c.type == "CHILD_OF"), None) + if constraint: + constraint.enabled = False + obj.matrix_world = matrix @classmethod - def enable_constraints(cls, part: bpy.types.Object): + def enable_constraints(cls, objs: list[bpy.types.Object]): # Enable constraints while keeping parts in the same location - constraint = next((c for c in part.constraints if c.type == "CHILD_OF"), None) - if constraint: - constraint.enabled = True + for obj in objs: + constraint = next((c for c in obj.constraints if c.type == "CHILD_OF"), None) + if constraint: + constraint.enabled = True bpy.context.view_layer.update() - diff = part.matrix_world.translation - part.location - part.location -= diff + for obj in objs: + diff = obj.matrix_world.translation - obj.location + obj.location -= diff - @classmethod def get_aggregate_mode(cls): return bpy.context.scene.BIMAggregateProperties.in_aggregate_mode @@ -178,7 +180,8 @@ class Aggregate(bonsai.core.tool.Aggregate): else: editing_obj = props.editing_objects.add() editing_obj.obj = obj.original - tool.Aggregate.disable_constraints(obj.original) + + tool.Aggregate.disable_constraints([o.obj for o in props.editing_objects]) props.in_aggregate_mode = True return {"FINISHED"} @@ -196,9 +199,7 @@ class Aggregate(bonsai.core.tool.Aggregate): parts = ifcopenshell.util.element.get_parts(tool.Ifc.get_entity(props.editing_aggregate)) objs = [tool.Ifc.get_object(part) for part in parts] - for obj in objs: - tool.Aggregate.enable_constraints(obj) - + tool.Aggregate.enable_constraints(objs) if context.space_data.local_view: bpy.ops.view3d.localview()