import argparse import hashlib import json import math import shutil import sys from pathlib import Path import bpy from mathutils import Vector OUT = Path(__file__).resolve().parent ROOT = OUT.parents[1] ASSET = ROOT / "orange_juice_2l" BLEND = ASSET / "PerfectCoffee_orange_juice_2l.blend" GLB = ASSET / "PerfectCoffee_orange_juice_2l.glb" TEXTURE = OUT / "orange_juice_wrap.png" REPORT = ASSET / "validation.json" STATE = OUT / "application_validation.json" BODY = "OrangeJuice_Bottle_Body" LABEL_MATERIAL = "OrangeJuice_Label" LABEL_UV = "UVMap_Label" Z_MIN, Z_MAX = 0.023, 0.113 def sha256(path): return hashlib.sha256(path.read_bytes()).hexdigest() def mesh_objects(): return [obj for obj in bpy.context.scene.objects if obj.type == "MESH" and not obj.name.startswith("Studio_")] def triangle_count(objects): total = 0 for obj in objects: obj.data.calc_loop_triangles() total += len(obj.data.loop_triangles) return total def label_polygons(body): return [ polygon for polygon in body.data.polygons if all(Z_MIN - 0.00001 <= body.data.vertices[index].co.z <= Z_MAX + 0.00001 for index in polygon.vertices) ] def apply_label(): assert "100%" not in (OUT / "orange_juice_wrap.svg").read_text() initial = { "source_blend_sha256": sha256(BLEND), "cap_file_hashes": { name: sha256(ASSET / name) for name in ("PerfectCoffee_orange_juice_2l_Cap.blend", "PerfectCoffee_orange_juice_2l_Cap.glb") }, } backup = OUT / "_work" backup.mkdir(exist_ok=True) if not (backup / "unlabeled.blend").exists(): shutil.copy2(BLEND, backup / "unlabeled.blend") bpy.ops.wm.open_mainfile(filepath=str(BLEND)) bpy.context.preferences.filepaths.save_version = 0 body = bpy.data.objects[BODY] initial["source_triangles"] = triangle_count(mesh_objects()) initial["source_vertices"] = len(body.data.vertices) initial["source_polygons"] = len(body.data.polygons) polygons = label_polygons(body) assert len(polygons) == 64, f"Expected 64 outer-wall label faces, got {len(polygons)}" uv = body.data.uv_layers.get(LABEL_UV) or body.data.uv_layers.new(name=LABEL_UV) body.data.uv_layers.active = uv for polygon in polygons: coordinates = [] for loop_index in polygon.loop_indices: vertex = body.data.vertices[body.data.loops[loop_index].vertex_index].co u = (math.atan2(vertex.y, vertex.x) - math.pi / 2) / math.tau % 1.0 v = (vertex.z - Z_MIN) / (Z_MAX - Z_MIN) coordinates.append((loop_index, u, v)) seam = max(value[1] for value in coordinates) - min(value[1] for value in coordinates) > 0.5 for loop_index, u, v in coordinates: if seam and u < 0.5: u += 1.0 uv.data[loop_index].uv = (u, v) image = bpy.data.images.load(str(TEXTURE), check_existing=True) image.name = "OrangeJuice_Wrap_NoPercentage" image.colorspace_settings.name = "sRGB" image.use_fake_user = True image.pack() mat = bpy.data.materials.get(LABEL_MATERIAL) or bpy.data.materials.new(LABEL_MATERIAL) mat.use_nodes = True mat.use_backface_culling = False mat.node_tree.nodes.clear() output = mat.node_tree.nodes.new("ShaderNodeOutputMaterial") shader = mat.node_tree.nodes.new("ShaderNodeBsdfPrincipled") texture = mat.node_tree.nodes.new("ShaderNodeTexImage") texture.image = image texture.interpolation = "Linear" texture.extension = "REPEAT" shader.inputs["Roughness"].default_value = 0.62 shader.inputs["Alpha"].default_value = 1.0 mat.node_tree.links.new(texture.outputs["Color"], shader.inputs["Base Color"]) mat.node_tree.links.new(shader.outputs["BSDF"], output.inputs["Surface"]) if LABEL_MATERIAL not in [slot.name for slot in body.data.materials]: body.data.materials.append(mat) material_index = [slot.name for slot in body.data.materials].index(LABEL_MATERIAL) for polygon in polygons: polygon.material_index = material_index body["label_scope"] = "Outer straight wall only; original generic artwork without percentage claims" body["label_z_range_m"] = [Z_MIN, Z_MAX] body["label_seam"] = "Back (+Y); front label centered at -Y" assert triangle_count(mesh_objects()) == initial["source_triangles"] assert len(body.data.vertices) == initial["source_vertices"] assert len(body.data.polygons) == initial["source_polygons"] collection = bpy.data.collections["Model"] bpy.ops.object.select_all(action="DESELECT") for obj in collection.all_objects: obj.select_set(True) bpy.context.view_layer.objects.active = body bpy.ops.export_scene.gltf( filepath=str(GLB), export_format="GLB", use_selection=True, export_yup=True, export_apply=True, export_texcoords=True, export_cameras=False, export_lights=False, ) scene = bpy.context.scene scene.render.filepath = str(ASSET / "preview.png") bpy.ops.render.render(write_still=True) pivot = bpy.data.objects["OrangeJuice_Cap_Pivot"] location = pivot.location.copy() pivot.location += Vector((0.045, 0, 0.055)) scene.render.filepath = str(ASSET / "preview_cap_removed.png") bpy.ops.render.render(write_still=True) pivot.location = location camera = scene.camera camera_matrix = camera.matrix_world.copy() camera_scale = camera.data.ortho_scale resolution = (scene.render.resolution_x, scene.render.resolution_y) for name, position in (("front", (0, -0.55, 0.18)), ("back", (0, 0.55, 0.18))): camera.location = position target = Vector((0, 0, 0.14)) camera.rotation_euler = (target - camera.location).to_track_quat("-Z", "Y").to_euler() camera.data.ortho_scale = 0.325 scene.render.resolution_x = 900 scene.render.resolution_y = 900 scene.render.filepath = str(ASSET / f"preview_label_{name}.png") bpy.ops.render.render(write_still=True) camera.matrix_world = camera_matrix camera.data.ortho_scale = camera_scale scene.render.resolution_x, scene.render.resolution_y = resolution scene.render.filepath = str(ASSET / "preview.png") assert sha256(BLEND) == initial["source_blend_sha256"], "Bottle changed on disk during label application; not saving" bpy.ops.wm.save_as_mainfile(filepath=str(BLEND), compress=True) for name, old_hash in initial["cap_file_hashes"].items(): assert sha256(ASSET / name) == old_hash initial["label_faces"] = len(polygons) initial["label_triangle_increase"] = 0 initial["label_png_dimensions"] = [2048, 768] initial["cap_files_untouched"] = True STATE.write_text(json.dumps(initial, ensure_ascii=False, indent=2) + "\n") report = json.loads(REPORT.read_text()) report["visual_review_passed"] = False REPORT.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n") def validate(visual_reviewed): state = json.loads(STATE.read_text()) report = json.loads(REPORT.read_text()) bpy.ops.wm.open_mainfile(filepath=str(BLEND)) body = bpy.data.objects[BODY] native_triangles = triangle_count(mesh_objects()) native_faces = label_polygons(body) label_uv = body.data.uv_layers.get(LABEL_UV) uv_loops = sum(len(polygon.loop_indices) for polygon in native_faces) native_texture = bpy.data.materials[LABEL_MATERIAL].node_tree.nodes.get("Image Texture") native_ok = ( native_triangles == state["source_triangles"] and len(body.data.vertices) == state["source_vertices"] and len(body.data.polygons) == state["source_polygons"] and len(native_faces) == 64 and uv_loops == 256 and label_uv is not None and all(polygon.material_index < len(body.data.materials) and body.data.materials[polygon.material_index].name == LABEL_MATERIAL for polygon in native_faces) and native_texture is not None and list(native_texture.image.size) == [2048, 768] and native_texture.image.packed_file is not None ) for name, old_hash in state["cap_file_hashes"].items(): assert sha256(ASSET / name) == old_hash for obj in list(bpy.context.scene.objects): if not obj.name.startswith("Studio_"): bpy.data.objects.remove(obj, do_unlink=True) for mesh in list(bpy.data.meshes): if mesh.users == 0: bpy.data.meshes.remove(mesh) for material in list(bpy.data.materials): if material.users == 0: bpy.data.materials.remove(material) bpy.ops.import_scene.gltf(filepath=str(GLB)) imported_objects = mesh_objects() glb_triangles = triangle_count(imported_objects) imported_label_faces = 0 imported_label_uv = False imported_texture_size = None for obj in imported_objects: for polygon in obj.data.polygons: material = obj.data.materials[polygon.material_index] if material and material.name == LABEL_MATERIAL: imported_label_faces += 1 imported_label_uv = imported_label_uv or bool(obj.data.uv_layers) for node in material.node_tree.nodes: if node.type == "TEX_IMAGE" and node.image: imported_texture_size = list(node.image.size) glb_ok = ( glb_triangles == state["source_triangles"] and imported_label_faces == 128 and imported_label_uv and imported_texture_size == [2048, 768] and not any(obj.name.startswith("Studio_") for obj in imported_objects) ) if glb_ok: scene = bpy.context.scene camera = scene.camera camera.location = (0, -0.55, 0.18) camera.rotation_euler = (Vector((0, 0, 0.14)) - camera.location).to_track_quat("-Z", "Y").to_euler() camera.data.ortho_scale = 0.325 scene.render.resolution_x = 900 scene.render.resolution_y = 900 scene.render.filepath = str(OUT / "preview_glb_front.png") bpy.ops.render.render(write_still=True) technical_passed = native_ok and glb_ok report.update({ "native_reopen_passed": native_ok, "glb_reimport_passed": glb_ok, "native_triangles": native_triangles, "glb_triangles": glb_triangles, "passed_triangle_budget": native_triangles <= 1000 and glb_triangles <= 1000, "technical_validation_passed": technical_passed, "generic_label_applied": True, "brand_graphics": False, "percentage_purity_claims_present": False, "label_texture": "model_assets/labels/orange_juice/orange_juice_wrap.png", "label_uv_faces": 64, "label_uv_loops": uv_loops, "label_triangle_increase": 0, "label_texture_packed": True, "glb_label_texture_verified": imported_texture_size == [2048, 768], "no_image_textures_or_labels": False, "cap_files_untouched_by_label_application": True, }) report.pop("labels_or_brand_graphics", None) if visual_reviewed: report["visual_review_passed"] = True report["visual_review_notes"] = ( "Original orange-tree wrap is centered on the front, with a clean rear seam, legible generic text, " "no 100% claim and transparent PET retained above and below the label. Independent cap and bottle geometry unchanged." ) report["passed"] = technical_passed and report.get("visual_review_passed", False) REPORT.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n") state.update({ "native_reopen_passed": native_ok, "glb_reimport_passed": glb_ok, "native_triangles": native_triangles, "glb_triangles": glb_triangles, "glb_label_faces": imported_label_faces, "glb_label_triangles": imported_label_faces, "glb_label_uv_verified": imported_label_uv, "glb_texture_dimensions": imported_texture_size, "technical_validation_passed": technical_passed, "visual_review_passed": report.get("visual_review_passed", False), "passed": report["passed"], }) STATE.write_text(json.dumps(state, ensure_ascii=False, indent=2) + "\n") summary_path = ROOT / "low_poly_consumables_validation.json" summary = json.loads(summary_path.read_text()) for asset in summary["assets"]: if asset["directory"] == "orange_juice_2l": asset.update({ "native_triangles": native_triangles, "glb_triangles": glb_triangles, "technical_validation_passed": technical_passed, "visual_review_passed": report.get("visual_review_passed", False), "passed": report["passed"], }) summary["all_technical_checks_passed"] = all(asset["technical_validation_passed"] for asset in summary["assets"]) summary["all_visual_reviews_passed"] = all(asset["visual_review_passed"] for asset in summary["assets"]) summary_path.write_text(json.dumps(summary, ensure_ascii=False, indent=2) + "\n") assert technical_passed, "Orange label validation failed" print(json.dumps(state, ensure_ascii=False, indent=2)) parser = argparse.ArgumentParser() parser.add_argument("--check", action="store_true") parser.add_argument("--visual-reviewed", action="store_true") args = parser.parse_args(sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []) if not args.check: apply_label() validate(args.visual_reviewed)