"""Export R04 geometry without saving or modifying its authoritative .blend. Run with Blender --background --python this.py -- . Collapse tiny engineering parts by material for the operator viewport; retain thirteen cell bodies, thirteen original cell label faces with distinct dynamic material IDs, and separate static printed labels. """ import hashlib import json import sys from pathlib import Path import bpy output = Path(sys.argv[sys.argv.index("--") + 1]) scene = bpy.context.scene scene.frame_set(1) depsgraph = bpy.context.evaluated_depsgraph_get() roots = [c for c in bpy.data.collections if c.name == "IN" or c.name.startswith("OUT |")] if len(roots) != 2: raise ValueError("Expected R04 IN / OUT collection structure") objects = set(o for c in roots for o in c.all_objects) outer_objects = set(o for c in roots if c.name.startswith("OUT |") for o in c.all_objects) groups = {} for obj in sorted(objects, key=lambda o: o.name): if obj.type not in {"MESH", "CURVE", "FONT"} or obj.hide_render: continue # Replace only the two static glyph objects on each original side label. cell = next( (c.name[:3] for c in obj.users_collection if c.name.startswith("C") and "| cell" in c.name), None, ) if cell and obj.name in {cell + " Side index", cell + " Side note"}: continue display = cell is not None and obj.name == cell + " Side index label" evaluated = obj.evaluated_get(depsgraph) mesh = evaluated.to_mesh() if mesh is None: continue matrix = obj.matrix_world for polygon in mesh.polygons: # Keep the existing outward face, in its original world position. if display and (matrix.to_3x3() @ polygon.normal).y > -0.99: continue material = mesh.materials[polygon.material_index] if mesh.materials else None name = material.name if material else "default" label = obj.type == "FONT" or name.startswith(("PRINT", "Print")) glass = name == "CASE | bead-blasted graphite" and any( c.name.startswith(("02 |", "03 |")) for c in obj.users_collection ) role = ( "bms/labels" if label else "bms/cells" if name == "Cell / blue polymer sleeve" else "bms/glass" if glass else "bms/" + name ) part = ( ("cells" if cell else "enclosure" if obj in outer_objects else "electronics") if label else cell if role == "bms/cells" else obj.name if name == "CASE | bead-blasted graphite" else None ) if name == "CASE | dielectric liner" and obj.name.startswith("SLOT BACKER |"): part = "left" if obj.name.endswith(" -1") else "right" if obj.name.startswith("FRAME |"): part = obj.name if display: role, part = "bms/cell-display/" + cell, None key = (role, part) group = groups.setdefault( key, {"vertices": [], "faces": [], "smooth": [], "material": material, "indices": {}} ) face = [] for index in polygon.vertices: vertex_key = (obj.name, index) if vertex_key not in group["indices"]: group["indices"][vertex_key] = len(group["vertices"]) group["vertices"].append(tuple(matrix @ mesh.vertices[index].co)) face.append(group["indices"][vertex_key]) group["faces"].append(face) group["smooth"].append(polygon.use_smooth) evaluated.to_mesh_clear() bpy.ops.object.select_all(action="DESELECT") collection = bpy.data.collections.new("Mission Core battery export") scene.collection.children.link(collection) materials = {} for (role, cell), data in groups.items(): if role not in materials: material = data["material"].copy() if data["material"] else bpy.data.materials.new(role) material.name = role materials[role] = material mesh = bpy.data.meshes.new(role + ("/" + cell if cell else "")) mesh.from_pydata(data["vertices"], [], data["faces"]) mesh.materials.append(materials[role]) for polygon, smooth in zip(mesh.polygons, data["smooth"], strict=True): polygon.use_smooth = smooth obj = bpy.data.objects.new(mesh.name, mesh) collection.objects.link(obj) obj.select_set(True) # A deterministic cube UV layout makes the shared texture controls useful. export_objects = list(collection.objects) bpy.ops.object.select_all(action="DESELECT") for obj in export_objects: if obj.name.startswith("bms/cell-display/"): uv = obj.data.uv_layers.new(name="Cell display UV") xs = [v.co.x for v in obj.data.vertices] zs = [v.co.z for v in obj.data.vertices] for polygon in obj.data.polygons: for loop in polygon.loop_indices: co = obj.data.vertices[obj.data.loops[loop].vertex_index].co uv.data[loop].uv = ( (co.x - min(xs)) / (max(xs) - min(xs)), (co.z - min(zs)) / (max(zs) - min(zs)), ) continue obj.select_set(True) bpy.context.view_layer.objects.active = obj bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.select_all(action="SELECT") bpy.ops.uv.cube_project(cube_size=0.1) bpy.ops.object.mode_set(mode="OBJECT") obj.select_set(False) for obj in export_objects: obj.select_set(True) output.parent.mkdir(parents=True, exist_ok=True) bpy.ops.export_scene.gltf( filepath=str(output), export_format="GLB", use_selection=True, export_animations=False, export_extras=False, export_cameras=False, export_lights=False, ) report = { "source": Path(bpy.data.filepath).name, "source_sha256": hashlib.sha256(Path(bpy.data.filepath).read_bytes()).hexdigest(), "model": output.name, "sha256": hashlib.sha256(output.read_bytes()).hexdigest(), "bytes": output.stat().st_size, "groups": len(groups), "cell_bodies": sum(role == "bms/cells" for role, _ in groups), "objects": [obj.name for obj in export_objects], "materials": list(materials), "cell_displays": sum(role.startswith("bms/cell-display/") for role, _ in groups), "uv": "cube_projection; original cell display faces use planar X/Z UV", "source_modified": False, } output.with_suffix(".json").write_text(json.dumps(report, indent=2) + "\n") print(json.dumps(report))