import bpy import mathutils def calculate_global_bounds(selected_objects): if not selected_objects: return None min_x, min_y, min_z = float('inf'), float('inf'), float('inf') max_x, max_y, max_z = float('-inf'), float('-inf'), float('-inf') found_any_mesh = False for obj in selected_objects: if obj.type != 'MESH': continue found_any_mesh = True matrix = obj.matrix_world for corner in obj.bound_box: world_corner = matrix @ mathutils.Vector(corner) min_x = min(min_x, world_corner.x) min_y = min(min_y, world_corner.y) min_z = min(min_z, world_corner.z) max_x = max(max_x, world_corner.x) max_y = max(max_y, world_corner.y) max_z = max(max_z, world_corner.z) if not found_any_mesh: return None return { 'min': mathutils.Vector((min_x, min_y, min_z)), 'max': mathutils.Vector((max_x, max_y, max_z)), 'size': mathutils.Vector((max_x - min_x, max_y - min_y, max_z - min_z)) } def create_aligned_boundary_cube(bounds): if not bounds: return None min_v = bounds['min'] size = bounds['size'] mesh = bpy.data.meshes.new("Stocker_Boundary") obj = bpy.data.objects.new("Stocker_Boundary", mesh) bpy.context.collection.objects.link(obj) verts = [ (0, 0, 0), (size.x, 0, 0), (size.x, -size.y, 0), (0, -size.y, 0), (0, 0, size.z), (size.x, 0, size.z), (size.x, -size.y, size.z), (0, -size.y, size.z) ] faces = [ (0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7) ] mesh.from_pydata(verts, [], faces) mesh.update() # Place origin at Back-Left (Min X, Max Y, Min Z) obj.location = (bounds['min'].x, bounds['max'].y, bounds['min'].z) return obj if __name__ == "__main__": print("Phase 2: Geometry (Origin Alignment) logic loaded.")