import bpy import bmesh import hashlib import json from pathlib import Path from mathutils import Vector from mathutils.bvhtree import BVHTree OUT = Path(__file__).resolve().parent MAIN = OUT/'PerfectCoffee_Minex_Mechanism.blend' SOURCE_HASH = hashlib.sha256(MAIN.read_bytes()).hexdigest() SPECS = { 'residue': { 'file': 'PerfectCoffee_Residue_Bin.blend', 'preview': 'preview_residue_bin.png', 'report': 'residue_bin_validation.json', 'objects': ['Residue_Collection_Box', 'Residue_Box_Open_Rim'], 'offset': (-0.0625, -0.068, 0), 'camera': ((-0.42, 0.54, 0.42), (0, 0, 0.17), 0.510), }, 'juice': { 'file': 'PerfectCoffee_Juice_Bin.blend', 'preview': 'preview_juice_bin.png', 'report': 'juice_bin_validation.json', 'objects': ['Juice_Collection_Box_Contoured', 'Juice_Bin_Contoured_Rim'], 'offset': (0.101, -0.087, 0), 'camera': ((-0.38, 0.50, 0.32), (0, 0, 0.14), 0.430), }, 'pair': { 'file': 'PerfectCoffee_Collector_Pair.blend', 'preview': 'preview_collector_pair.png', 'report': 'collector_pair_validation.json', 'objects': ['Residue_Collection_Box', 'Residue_Box_Open_Rim', 'Juice_Collection_Box_Contoured', 'Juice_Bin_Contoured_Rim'], 'offset': (0.01925, -0.0775, 0), 'camera': ((-0.46, 0.62, 0.43), (0, 0.015, 0.16), 0.560), }, } def aim(obj, target): obj.rotation_euler = (Vector(target)-obj.location).to_track_quat('-Z', 'Y').to_euler() def center(obj): points = [obj.matrix_world @ Vector(corner) for corner in obj.bound_box] return sum(points, Vector((0, 0, 0))) / len(points) def area(name, location, energy, size, target): data = bpy.data.lights.new(name, 'AREA') data.energy = energy data.size = size obj = bpy.data.objects.new(name, data) bpy.context.scene.collection.objects.link(obj) obj.location = location aim(obj, target) def studio(kind, spec): scene = bpy.context.scene scene.unit_settings.system = 'METRIC' scene.unit_settings.length_unit = 'METERS' scene.render.engine = 'CYCLES' scene.cycles.samples = 48 scene.cycles.use_denoising = True scene.render.resolution_x = 1200 scene.render.resolution_y = 1000 scene.render.resolution_percentage = 100 scene.render.image_settings.file_format = 'PNG' scene.render.image_settings.color_mode = 'RGB' scene.view_settings.view_transform = 'AgX' scene.view_settings.look = 'AgX - Medium High Contrast' world = bpy.data.worlds.new('Collector_Studio_World') world.use_nodes = True world.node_tree.nodes['Background'].inputs['Color'].default_value = (0.26, 0.30, 0.33, 1) world.node_tree.nodes['Background'].inputs['Strength'].default_value = 0.40 scene.world = world bpy.ops.mesh.primitive_cube_add(size=1, location=(0, 0, 0.0365)) ground = bpy.context.object ground.name = 'Studio_Ground_Not_Part_Of_Product' ground.dimensions = (200, 200, 0.005) bpy.ops.object.transform_apply(location=False, rotation=False, scale=True) material = bpy.data.materials.new('Studio_Neutral') material.diffuse_color = (0.12, 0.15, 0.17, 1) material.use_nodes = True material.node_tree.nodes['Principled BSDF'].inputs['Base Color'].default_value = (0.12, 0.15, 0.17, 1) material.node_tree.nodes['Principled BSDF'].inputs['Roughness'].default_value = 0.75 ground.data.materials.append(material) area('Studio_Key', (-0.30, 0.40, 0.65), 12, 0.30, (0, 0, 0.16)) area('Studio_Fill', (0.40, 0.10, 0.45), 9, 0.26, (0, 0, 0.14)) area('Studio_Rim', (-0.20, -0.40, 0.50), 10, 0.24, (0, 0, 0.16)) location, target, scale = spec['camera'] data = bpy.data.cameras.new('VALIDATION_Camera') camera = bpy.data.objects.new('VALIDATION_Camera', data) scene.collection.objects.link(camera) camera.location = location aim(camera, target) data.type = 'ORTHO' data.ortho_scale = scale data.clip_start = 0.001 data.clip_end = 100 scene.camera = camera for screen in bpy.data.screens: for area_ in screen.areas: if area_.type == 'VIEW_3D': space = area_.spaces.active space.region_3d.view_perspective = 'CAMERA' space.shading.type = 'MATERIAL' space.overlay.show_extras = False space.clip_start = 0.001 space.clip_end = 100 def evaluated_bin(obj): ev = obj.evaluated_get(bpy.context.evaluated_depsgraph_get()) mesh = ev.to_mesh() points = [ev.matrix_world @ vertex.co for vertex in mesh.vertices] faces = [list(poly.vertices) for poly in mesh.polygons] bm = bmesh.new() bm.from_mesh(mesh) non_manifold = sum(not edge.is_manifold for edge in bm.edges) volume = bm.calc_volume(signed=True) bm.free() ev.to_mesh_clear() tree = BVHTree.FromPolygons(points, faces) return non_manifold, volume, tree def export_part(kind): spec = SPECS[kind] source_object_names = list(spec['objects']) bpy.ops.wm.read_factory_settings(use_empty=True) bpy.context.preferences.filepaths.save_version = 0 with bpy.data.libraries.load(str(MAIN), link=False) as (source, target): target.objects = spec['objects'] appended = [obj for obj in target.objects if obj is not None] assert len(appended) == len(spec['objects']) for obj in appended: bpy.context.scene.collection.objects.link(obj) obj.parent = None obj.matrix_parent_inverse.identity() obj.animation_data_clear() obj.location = spec['offset'] obj.rotation_euler = (0, 0, 0) obj.hide_render = False obj.hide_viewport = False studio(kind, spec) scene = bpy.context.scene scene['acceptance_view'] = kind scene['source_model'] = MAIN.name scene['source_sha256'] = SOURCE_HASH scene['geometry_note'] = 'Collector geometry is appended from the assembled juicer, not independently redrawn' about = bpy.data.texts.new('ABOUT_Collector_Acceptance') about.write(f'Independent {kind} collector acceptance. Source: {MAIN.name}. Only collector meshes, rims and studio objects are included.\n') bpy.ops.object.select_all(action='DESELECT') appended[0].select_set(True) bpy.context.view_layer.objects.active = appended[0] scene.frame_set(1) bpy.context.view_layer.update() path = OUT/spec['file'] bpy.ops.wm.save_as_mainfile(filepath=str(path), compress=True) bpy.ops.wm.open_mainfile(filepath=str(path)) scene = bpy.context.scene scene.frame_set(1) bpy.context.view_layer.update() juice = bpy.data.objects.get('Juice_Collection_Box_Contoured') residue = bpy.data.objects.get('Residue_Collection_Box') primary = residue if kind == 'residue' else juice non_manifold, volume, primary_tree = evaluated_bin(primary) center_error = Vector((center(primary).x,center(primary).y,0)).length if kind != 'pair' else Vector(((center(juice)+center(residue)).x/2,(center(juice)+center(residue)).y/2,0)).length report = { 'file': spec['file'], 'preview': spec['preview'], 'source_blend': MAIN.name, 'source_blend_sha256': SOURCE_HASH, 'acceptance_view': kind, 'source_objects': source_object_names, 'object_count': len(appended), 'center_error_m': center_error, 'rim_height_range_m': primary['rim_max_z_m']-primary['rim_min_z_m'], 'non_manifold_edges': non_manifold, 'volume_m3': volume, 'linked_library_count': len(bpy.data.libraries), 'missing_external_assets': [image.filepath for image in bpy.data.images if image.source == 'FILE' and not image.packed_file and not Path(bpy.path.abspath(image.filepath)).exists()], 'machine_parts_excluded': all(name not in bpy.data.objects for name in ['Cabinet_Main_Body', 'Chamber_Rear_Wall', 'Chamber_Lower_Backplate', 'Residue_Service_Door', 'Collection_Deck_Assembly', 'Fixed_Control_Pod_Assembly', 'Lower_Service_Cover_Assembly']), 'depth_m': primary.dimensions.y, 'source_depth_matches': abs(primary.dimensions.y-primary['depth_m']) < 0.0001, 'front_shallow_collector': primary.dimensions.y < 0.180, 'visual_review_passed': False, } if kind == 'pair': residue_left = min((residue.matrix_world @ Vector(corner)).x for corner in residue.bound_box) juice_right = max((juice.matrix_world @ Vector(corner)).x for corner in juice.bound_box) report['collector_wall_gap_m'] = residue_left-juice_right report['close_pair_without_divider'] = abs(report['collector_wall_gap_m']-0.007) < 0.0001 if kind in ['juice', 'pair']: handle_x = center(juice).x-0.060 handle_y = 0.060+spec['offset'][1] handle_hit = primary_tree.ray_cast(Vector((handle_x, handle_y, 0.128)), Vector((1, 0, 0)), 0.060)[0] upper_hit = primary_tree.ray_cast(Vector((handle_x, handle_y, 0.175)), Vector((1, 0, 0)), 0.060)[0] report['handle_through_cut_passed'] = handle_hit is None and upper_hit is not None if kind in ['residue','pair']: residue_tree = primary_tree if kind == 'residue' else evaluated_bin(residue)[2] pull_x = 0.0625+spec['offset'][0] pull_y = 0.169+spec['offset'][1] pull_hit = residue_tree.ray_cast(Vector((pull_x,pull_y,0.165)),Vector((0,-1,0)),0.030)[0] wall_hit = residue_tree.ray_cast(Vector((pull_x,pull_y,0.180)),Vector((0,-1,0)),0.030)[0] report['integrated_front_pull_passed'] = pull_hit is None and wall_hit is not None report['technical_checks_passed'] = ( report['object_count'] == len(spec['objects']) and report['center_error_m'] < 0.0001 and report['rim_height_range_m'] > 0.012 and report['non_manifold_edges'] == 0 and report['volume_m3'] > 0 and report['linked_library_count'] == 0 and not report['missing_external_assets'] and report['machine_parts_excluded'] and report['source_depth_matches'] and report['front_shallow_collector'] and report.get('handle_through_cut_passed', True) and report.get('integrated_front_pull_passed', True) and report.get('close_pair_without_divider', True) ) report['passed'] = report['technical_checks_passed'] assert report['passed'], f'{kind} collector export validation failed: {report}' scene.render.filepath = str(OUT/spec['preview']) bpy.ops.render.render(write_still=True) (OUT/spec['report']).write_text(json.dumps(report, ensure_ascii=False, indent=2)+'\n', encoding='utf-8') print('COLLECTOR_EXPORT_COMPLETE '+json.dumps(report), flush=True) if __name__ == '__main__': for part in ['residue', 'juice', 'pair']: export_part(part)