From 891752a569faa225783146f4e217c2cce8a2f4ac Mon Sep 17 00:00:00 2001 From: Bruno Postle Date: Mon, 24 Feb 2025 19:56:37 +0000 Subject: [PATCH] Move blender object manipulation to inner loop create_orientation_slots() can be called a lot of times recursively, so only do creation and deletion of dummy objects when absolutely necessary --- src/bonsai/bonsai/tool/spatial.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/bonsai/bonsai/tool/spatial.py b/src/bonsai/bonsai/tool/spatial.py index 4d139c0648..e937ce5750 100644 --- a/src/bonsai/bonsai/tool/spatial.py +++ b/src/bonsai/bonsai/tool/spatial.py @@ -485,13 +485,6 @@ class Spatial(bonsai.core.tool.Spatial): active_slot = bpy.context.scene.transform_orientation_slots[0] initial_orientation = active_slot.type - # stash selected objects - active_object = bpy.context.view_layer.objects.active - selected_objects = list(bpy.context.view_layer.objects.selected) - - # bpy.ops.transform.create_orientation() requires a dummy object - bpy.ops.object.empty_add(type="PLAIN_AXES") - for product in products: placement = product.ObjectPlacement combined_matrix = ifcopenshell.util.placement.get_local_placement(placement)[:3, :3] @@ -509,18 +502,24 @@ class Spatial(bonsai.core.tool.Spatial): continue # this product has a unique orientation orientation_name = product.is_a() + "/" + product.Name + + # stash selected objects + active_object = bpy.context.view_layer.objects.active + selected_objects = list(bpy.context.view_layer.objects.selected) + # bpy.ops.transform.create_orientation() requires a dummy object + bpy.ops.object.empty_add(type="PLAIN_AXES") + bpy.ops.transform.create_orientation(name=orientation_name, overwrite=True) active_slot.type = orientation_name active_slot.custom_orientation.matrix = np.linalg.inv(combined_matrix) - # delete dummy object - bpy.ops.object.delete() - - # reinstate selected objects - for obj in selected_objects: - obj.select_set(True) - if active_object: - bpy.context.view_layer.objects.active = active_object + # delete dummy object + bpy.ops.object.delete() + # reinstate selected objects + for obj in selected_objects: + obj.select_set(True) + if active_object: + bpy.context.view_layer.objects.active = active_object if not use: active_slot.type = initial_orientation