import bpy import bmesh import json from pathlib import Path from mathutils import Vector from mathutils.bvhtree import BVHTree OUT = Path(__file__).resolve().parent SOURCE = OUT.parent / "PerfectCoffee_milk_2l.blend" bpy.ops.wm.open_mainfile(filepath=str(SOURCE)) bpy.context.preferences.filepaths.save_version = 0 scene = bpy.context.scene body = bpy.data.objects["Milk_Bottle_Body"] cap = bpy.data.objects["Milk_Cap"] pivot = bpy.data.objects["Milk_Cap_Pivot"] root = bpy.data.objects["Milk2L_Root"] def bvh(obj): return BVHTree.FromPolygons( [obj.matrix_world @ vertex.co for vertex in obj.data.vertices], [list(polygon.vertices) for polygon in obj.data.polygons], ) def triangles(obj): obj.data.calc_loop_triangles() return len(obj.data.loop_triangles) bpy.context.view_layer.update() bm = bmesh.new() bm.from_mesh(body.data) non_manifold = sum(not edge.is_manifold for edge in bm.edges) euler = len(bm.verts) - len(bm.edges) + len(bm.faces) bm.free() body_eval = body.evaluated_get(bpy.context.evaluated_depsgraph_get()) factor = root["neck_outer_radius_m"] / 0.018 side_hit = body_eval.ray_cast(Vector((0.1, -0.005 * factor, 0.165)), Vector((-1, 0, 0)), distance=0.2) center_hit = body_eval.ray_cast(Vector((0, -0.1, 0.165)), Vector((0, 1, 0)), distance=0.2) handle_path = [Vector((point[0] * factor, point[1] * factor, 0.165)) for point in root["handle_path_xy"]] blocked_segments = [] for index, (start, end) in enumerate(zip(handle_path[:-1], handle_path[1:])): direction = end - start hit = body_eval.ray_cast(start, direction.normalized(), distance=direction.length + 0.000001) if hit[0]: blocked_segments.append({"segment": index, "point": list(hit[1])}) arc_path_clear = not blocked_segments bottom_probe = handle_path[6] bottom_hit = body_eval.ray_cast(Vector((bottom_probe.x, bottom_probe.y, 0.213)), Vector((0, 0, -1)), distance=0.12) mouth_vertices = [vertex.co for vertex in body.data.vertices if abs(vertex.co.z - root["mouth_z_m"]) < 0.000001] mouth_center = sum(mouth_vertices, Vector()) / len(mouth_vertices) cap_center = pivot.matrix_world.translation alignment_error = Vector((mouth_center.x - cap_center.x, mouth_center.y - cap_center.y)).length mouth_center_error = Vector((mouth_center.x, mouth_center.y)).length lower_vertices = [vertex.co for vertex in body.data.vertices if abs(vertex.co.z - root["lower_segment_top_z_m"]) < 0.000001] lower_width = max(point.x for point in lower_vertices) - min(point.x for point in lower_vertices) lower_depth = max(point.y for point in lower_vertices) - min(point.y for point in lower_vertices) intersections = bvh(body).overlap(bvh(cap)) old_cap_location = pivot.location.copy() old_cap_z = cap.matrix_world.translation.z pivot.location.z += 0.04 bpy.context.view_layer.update() removal_distance = cap.matrix_world.translation.z - old_cap_z pivot.location = old_cap_location bpy.context.view_layer.update() side_entry = bool(side_hit[0]) and side_hit[1].x < 0.061 * factor and side_hit[1].x > -0.061 * factor center_blocked = bool(center_hit[0]) and center_hit[1].y < 0 limited_bottom = bool(bottom_hit[0]) and 0.115 < bottom_hit[1].z < 0.135 report = { "source_blend": SOURCE.name, "test_blend": "MilkCap_AssemblyTest.blend", "assembled_triangles": triangles(body) + triangles(cap), "triangle_limit": 1000, "body_non_manifold_edges": non_manifold, "body_euler_characteristic": euler, "expected_body_genus_for_corner_grip": 1, "expected_euler_characteristic": 0, "square_lower_section_width_m": lower_width, "square_lower_section_depth_m": lower_depth, "square_lower_section_passed": abs(lower_width - lower_depth) < 0.000001, "lower_segment_top_z_m": root["lower_segment_top_z_m"], "shoulder_start_height_ratio": root["shoulder_start_height_ratio"], "shoulder_start_height_passed": 0.79 <= root["shoulder_start_height_ratio"] <= 0.81, "mouth_center_error_m": mouth_center_error, "mouth_centered_passed": mouth_center_error < 0.000001, "shoulder_to_mouth_ratio": root["shoulder_to_mouth_ratio"], "upper_shoulder_termination_passed": root["shoulder_to_mouth_ratio"] <= 1.25, "handle_construction": root["handle_construction"], "handle_cavity_overlap_ml": root["handle_cavity_overlap_ml"], "handle_cavity_separation_passed": root["handle_cavity_overlap_ml"] < 0.001, "side_pocket_entry_point_m": list(side_hit[1]) if side_hit[0] else None, "side_pocket_entry_passed": side_entry, "central_body_stop_point_m": list(center_hit[1]) if center_hit[0] else None, "no_full_depth_tunnel_passed": center_blocked, "handle_path_blocked_segments": blocked_segments, "arched_handle_path_clear_passed": arc_path_clear, "handle_aperture_width_m": root["handle_aperture_width_m"], "handle_aperture_height_m": root["handle_aperture_height_m"], "handle_aperture_size_passed": root["handle_aperture_width_m"] >= 0.015 and root["handle_aperture_height_m"] >= 0.08, "pocket_bottom_point_m": list(bottom_hit[1]) if bottom_hit[0] else None, "limited_height_pocket_passed": limited_bottom, "integral_corner_handle_passed": arc_path_clear and center_blocked and limited_bottom and non_manifold == 0 and euler == 0, "handle_is_separate_object": False, "cap_body_triangle_intersections": len(intersections), "cap_body_intersection_passed": not intersections, "cap_radial_clearance_m": root["cap_radial_clearance_m"], "cap_radial_clearance_passed": root["cap_radial_clearance_m"] >= 0.0005, "cap_top_clearance_m": root["cap_top_clearance_m"], "cap_top_clearance_passed": root["cap_top_clearance_m"] >= 0.0004, "cap_axis_alignment_error_m": alignment_error, "cap_axis_alignment_passed": alignment_error < 0.00001, "cap_removal_distance_m": removal_distance, "cap_removal_passed": abs(removal_distance - 0.04) < 0.00001, "mouth_height_m": root["mouth_z_m"], "cap_base_height_m": root["cap_base_z_m"], "preview_assembled": "assembled_closeup.png", "preview_exploded": "exploded_closeup.png", "preview_handle": "handle_corner.png", "preview_top": "top_view.png", "preview_handle_top_detail": "handle_top_detail.png", "top_section_height_m": 0.165, } checks = [value for key, value in report.items() if key.endswith("_passed")] report["passed"] = all(checks) and report["assembled_triangles"] <= 1000 (OUT / "validation.json").write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n") assert report["passed"], json.dumps(report, ensure_ascii=False) camera = scene.camera original_matrix = camera.matrix_world.copy() original_scale = camera.data.ortho_scale original_filepath = scene.render.filepath scene.render.resolution_x = 900 scene.render.resolution_y = 900 neck_target = Vector((cap_center.x, cap_center.y, 0.263)) camera.location = neck_target + Vector((0.085, -0.13, 0.060)) camera.rotation_euler = (neck_target - camera.location).to_track_quat("-Z", "Y").to_euler() camera.data.ortho_scale = 0.090 scene.render.filepath = str(OUT / "assembled_closeup.png") bpy.ops.render.render(write_still=True) pivot.location.z += 0.04 exploded_target = Vector((cap_center.x, cap_center.y, 0.282)) camera.location = exploded_target + Vector((0.095, -0.15, 0.035)) camera.rotation_euler = (exploded_target - camera.location).to_track_quat("-Z", "Y").to_euler() camera.data.ortho_scale = 0.120 scene.render.filepath = str(OUT / "exploded_closeup.png") bpy.ops.render.render(write_still=True) pivot.location = old_cap_location corner_target = Vector((0.034 * factor, 0.030 * factor, 0.166)) camera.location = corner_target + Vector((0.20, 0.22, 0.14)) camera.rotation_euler = (corner_target - camera.location).to_track_quat("-Z", "Y").to_euler() camera.data.ortho_scale = 0.155 scene.render.filepath = str(OUT / "handle_corner.png") bpy.ops.render.render(write_still=True) section_mesh = body.data.copy() bm = bmesh.new() bm.from_mesh(section_mesh) bmesh.ops.bisect_plane( bm, geom=list(bm.verts) + list(bm.edges) + list(bm.faces), dist=0.000001, plane_co=(0, 0, 0.165), plane_no=(0, 0, 1), clear_inner=False, clear_outer=True, ) cut_edges = [edge for edge in bm.edges if edge.is_boundary] outline_data = bpy.data.curves.new("Handle_Level_Outline", "CURVE") outline_data.dimensions = "3D" outline_data.resolution_u = 1 outline_data.bevel_depth = 0.00035 outline_data.bevel_resolution = 0 for edge in cut_edges: spline = outline_data.splines.new("POLY") spline.points.add(1) for point, vertex in zip(spline.points, edge.verts): point.co = (*vertex.co, 1) bm.free() bpy.data.meshes.remove(section_mesh) section_material = bpy.data.materials.new("Handle_Section_Contrast") section_material.diffuse_color = (0.025, 0.055, 0.08, 1) section_material.use_nodes = True section_material.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.025, 0.055, 0.08, 1) section_material.node_tree.nodes["Principled BSDF"].inputs["Roughness"].default_value = 0.8 outline_data.materials.append(section_material) section = bpy.data.objects.new("Handle_Level_Section_Preview", outline_data) scene.collection.objects.link(section) section.matrix_world = body.matrix_world.copy() body.hide_render = True cap.hide_render = True top_target = Vector((0, 0, 0.165)) camera.location = (0, 0, 0.60) camera.rotation_euler = (top_target - camera.location).to_track_quat("-Z", "Y").to_euler() camera.data.ortho_scale = 0.155 scene.render.filepath = str(OUT / "top_view.png") bpy.ops.render.render(write_still=True) detail_target = Vector((0.043, 0.035, 0.165)) camera.location = (0.043, 0.035, 0.60) camera.rotation_euler = (detail_target - camera.location).to_track_quat("-Z", "Y").to_euler() camera.data.ortho_scale = 0.068 scene.render.filepath = str(OUT / "handle_top_detail.png") bpy.ops.render.render(write_still=True) bpy.data.objects.remove(section, do_unlink=True) body.hide_render = False cap.hide_render = False camera.matrix_world = original_matrix camera.data.ortho_scale = original_scale scene.render.filepath = original_filepath scene.render.resolution_x = 720 scene.render.resolution_y = 720 bpy.ops.wm.save_as_mainfile(filepath=str(OUT / "MilkCap_AssemblyTest.blend"), compress=True) print(json.dumps(report, ensure_ascii=False, indent=2))