"""Review of the round 5 hand items (gc_up5_tools.py, and anything else that writes into remaster_v3_tools) against the game's FBX files. In Blender: blender -b --factory-startup --python gc_up5_check.py -- views [names] old | new panels per view, materials as the game maps them, back faces culled like Unity -> remaster_v3_tools/previews/_panels blender -b --factory-startup --python gc_up5_check.py -- fpv [names] the ViewModel hold (rest and in use), 62 degree overlay camera (skipped, with a note, for an item without a hold) blender -b --factory-startup --python gc_up5_check.py -- verify [names] names, parents, origins, axes, sizes, slots, grips vs Assets/Art/Models blender -b --factory-startup --python gc_up5_check.py -- scene remaster_v3_tools/Hand_Tools_v3_Review.blend: every .blend in remaster_v3_tools, each beside its game model, in a labelled grid Outside Blender (python with Pillow): python gc_up5_check.py compose panels -> previews/_.png, old left, new right Names: any item with remaster_v3_tools/fbx/.fbx (no names: TOOLS). Items outside TOOLS get a "full" view, no FPV and a names/parents/origins check unless they are registered: from another script, import gc_up5_check as K K.register("SM_Tool_LawnMower", views=[("full", None, None, 38, -30), ("deck", (0, -0.4, 0.1), 0.2, 30, -40)], hold=((x, y, z), (ex, ey, ez), (ux, uy, uz), (uex, uey, uez), scale), req=["Body"], grips=[((x, y), "Z", z0, z1, r)]) K.views(["SM_Tool_LawnMower"]); K.fpv(["SM_Tool_LawnMower"]); K.verify(["SM_Tool_LawnMower"]) or pass the dicts straight to views(names, views=...), fpv(names, holds=...), verify(names, req=..., grips=...). """ import os, sys, math, glob HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.normpath(os.path.join(HERE, "..", "..", "..")) OUT = os.path.join(ROOT, "ArtSource", "Blender", "remaster_v3_tools") GAME = os.path.join(ROOT, "Assets", "Art", "Models") PANELS = os.path.join(OUT, "previews", "_panels") REVIEW = "Hand_Tools_v3_Review" TOOLS = ["SM_Tool_WideScythe", "SM_Tool_Scythe", "SM_Tool_GardenShears", "SM_Tool_NailScissors", "SM_Tool_Shears", "SM_Tool_Sickle", "SM_Tool_Hammer", "SM_Tool_LongScythe"] DEFAULT_VIEWS = [("full", None, None, 38, -30)] # (tag, target or None for the whole model, radius or None, elev, azim) VIEWS = { "SM_Tool_WideScythe": [("full", None, None, 38, -30), ("mount", (0.03, -1.74, -0.07), 0.17, 30, -40), ("mount_under", (0.03, -1.74, -0.09), 0.17, -30, -30), ("braces", (0.38, -1.6, -0.0), 0.45, 42, -12), ("turnbuckle", (0.43, -1.57, 0.045), 0.13, 30, 15), ("back", (0.42, -1.84, -0.05), 0.2, 52, -25), ("grip", (-0.01, -0.06, 0.0), 0.2, 12, 120), ("butt", (0.0, 0.07, 0.14), 0.15, 25, 150), ("wedge", (0.11, -1.75, -0.03), 0.07, 45, 40)], "SM_Tool_Scythe": [("full", None, None, 38, -30), ("mount", (0.02, -1.3, -0.065), 0.13, 30, -35), ("mount_under", (0.02, -1.3, -0.08), 0.13, -30, -30), ("grip", (0.0, -0.05, 0.03), 0.17, 12, 120), ("nail", (0.05, -1.17, -0.04), 0.15, 25, -60), ("butt", (0.01, 0.1, 0.1), 0.14, 25, 150), ("horn", (0.05, -0.26, 0.03), 0.15, 15, -70), ("wedge", (0.065, -1.31, -0.04), 0.06, 45, 40)], "SM_Tool_GardenShears": [("full", None, None, 60, -12), ("pivot", (0.0, -0.145, 0.0), 0.07, 50, -25), ("pivot_under", (0.0, -0.145, 0.0), 0.07, -50, -25), ("ferrule", (0.0, -0.06, 0.0), 0.08, 35, 35), ("grips", (0.0, 0.1, 0.0), 0.13, 45, -20), ("notch", (0.0, -0.32, 0.0), 0.1, 60, -10)], "SM_Tool_Shears": [("full", None, None, 60, -12), ("pivot", (0.0, -0.145, 0.0), 0.07, 50, -25), ("ferrule", (0.0, -0.06, 0.0), 0.08, 35, 35)], "SM_Tool_NailScissors": [("full", None, None, 60, -12), ("pivot", (0.0, -0.145, 0.0), 0.045, 50, -25), ("pivot_under", (0.0, -0.145, 0.0), 0.045, -50, -25), ("bows", (0.0, 0.02, 0.0), 0.11, 50, -15), ("tips", (-0.012, -0.36, 0.0), 0.08, 55, -30), ("rest", (-0.1, 0.1, 0.0), 0.06, 40, 30)], "SM_Tool_Sickle": [("full", None, None, 62, -10), ("neck", (0.0, -0.36, 0.26), 0.08, 35, -40), ("pommel", (0.0, 0.03, -0.31), 0.08, 15, 140), ("grip", (0.0, -0.02, -0.06), 0.14, 10, 70), ("blade", (-0.06, -0.95, 0.26), 0.16, 60, -20), ("spine", (0.11, -1.18, 0.26), 0.12, 30, 160)], "SM_Tool_Hammer": [("full", None, None, 64, -14), ("head", (-0.02, -0.56, 0.0), 0.16, 30, -120), ("face", (0.17, -0.56, 0.0), 0.08, 20, -60), ("claw", (-0.22, -0.53, 0.0), 0.11, 35, -150), ("eye", (0.0, -0.62, 0.0), 0.07, 70, -20), ("boss", (0.0, -0.5, 0.0), 0.1, -12, -100), ("poll", (0.16, -0.56, 0.0), 0.075, 30, -110), ("grip", (0.0, 0.12, 0.0), 0.16, 30, 40)], "SM_Tool_LongScythe": [("full", None, None, 38, -30), ("mount", (0.02, -1.52, -0.07), 0.14, 30, -35), ("mount_under", (0.02, -1.52, -0.08), 0.14, -30, -30), ("cradle", (0.25, -1.35, 0.1), 0.33, 35, -25), ("cradle_post", (0.03, -1.25, 0.14), 0.2, 15, 100), ("grip", (0.0, -0.05, 0.03), 0.17, 12, 120), ("butt", (0.02, 0.1, 0.1), 0.14, 25, 150)], } # ViewModel.Holds: pos, euler, usePos, useEuler, scale (Shears is not in the game any more: it borrows GardenShears' hold) HOLDS = { "SM_Tool_GardenShears": ((0.25, -0.14, 0.46), (-36, -18, -6), (0.22, -0.2, 0.5), (-24, -14, -6), 0.75), "SM_Tool_Shears": ((0.25, -0.14, 0.46), (-36, -18, -6), (0.22, -0.2, 0.5), (-24, -14, -6), 0.75), "SM_Tool_NailScissors": ((0.21, -0.13, 0.4), (-42, -16, -8), (0.2, -0.17, 0.46), (-28, -14, -6), 0.5), "SM_Tool_Sickle": ((0.27, -0.17, 0.46), (-35, -18, -6), (0.22, -0.19, 0.5), (-20, -14, -6), 0.34), "SM_Tool_Scythe": ((0.32, -0.1, 0.36), (9, -18, 0), (0.32, -0.11, 0.36), (11, -18, 0), 0.46), "SM_Tool_LongScythe": ((0.32, -0.1, 0.36), (9, -20, 0), (0.32, -0.11, 0.36), (11, -20, 0), 0.5), "SM_Tool_WideScythe": ((0.32, -0.1, 0.36), (9, -22, 0), (0.32, -0.11, 0.36), (11, -22, 0), 0.54), "SM_Tool_Hammer": ((0.27, -0.2, 0.42), (-68, -10, -8), (0.27, -0.2, 0.42), (-68, -10, -8), 0.42), } REQ = {"SM_Tool_WideScythe": ["Body"], "SM_Tool_Scythe": ["Body"], "SM_Tool_LongScythe": ["Body"], "SM_Tool_Sickle": ["Body"], "SM_Tool_Hammer": ["Body"], "SM_Tool_GardenShears": ["Body", "BladeA", "BladeB"], "SM_Tool_Shears": ["Body", "BladeA", "BladeB"], "SM_Tool_NailScissors": ["Body", "BladeA", "BladeB"]} # where a hand closes: (centre (a, b), axis, lo, hi, radius). Axis "Z": the centre in XY of the geometry within radius between z = lo..hi; # axis "Y": the centre in XZ between y = lo..hi. Old and new must agree within 5 mm. GRIPS = { "SM_Tool_WideScythe": [((0.03, -0.04), "Z", -0.06, 0.02, 0.06), ((-0.03, -0.62), "Z", -0.06, 0.02, 0.06)], "SM_Tool_Scythe": [((0.03, -0.04), "Z", -0.06, 0.02, 0.06), ((-0.03, -0.66), "Z", -0.06, 0.02, 0.06)], "SM_Tool_LongScythe": [((0.03, -0.04), "Z", -0.06, 0.02, 0.06), ((-0.03, -0.64), "Z", -0.06, 0.02, 0.06)], "SM_Tool_Sickle": [((0.0, 0.006), "Z", -0.13, -0.01, 0.075)], "SM_Tool_Hammer": [((0.0, 0.0), "Y", 0.02, 0.14, 0.07)], "SM_Tool_NailScissors": [((0.056, 0.06), "Z", -0.03, 0.03, 0.049), ((-0.061, 0.064), "Z", -0.03, 0.03, 0.05)], } RES = 800 def register(name, views=None, hold=None, req=None, grips=None): """Review data for an item outside TOOLS (another script's): any of its views, ViewModel hold, required objects, grip checks.""" if views is not None: VIEWS[name] = list(views) if hold is not None: HOLDS[name] = hold if req is not None: REQ[name] = list(req) if grips is not None: GRIPS[name] = list(grips) def items(): """Every finished item in remaster_v3_tools: .blend, the review blend left out.""" out = [] for p in sorted(glob.glob(os.path.join(OUT, "*.blend"))): n = os.path.splitext(os.path.basename(p))[0] if n == REVIEW or n.endswith("_Review"): continue out.append(n) return out # ======================================================================= Blender side def _bl(): import bpy from mathutils import Vector, Matrix sys.path.insert(0, HERE) import common as C import gc_up_machines # noqa: F401 (registers the P_ swatch materials) return bpy, Vector, Matrix, C def load_fbx(path, W=None): bpy, Vector, Matrix, C = _bl() before = set(bpy.data.objects) bpy.ops.import_scene.fbx(filepath=path) new = [o for o in bpy.data.objects if o not in before] bpy.context.view_layer.update() # the FBX brings its own materials (no textures): move them aside so common.mat builds the real ones under the same names for m in list(bpy.data.materials): if not m.get("gc_real") and not m.name.endswith("_imp"): m.name = m.name.split(".")[0] + "_imp" for o in new: if o.type == "MESH": me = o.data for i, m in enumerate(me.materials): if m is None: continue nm = m.name.split(".")[0] nm = nm[:-4] if nm.endswith("_imp") else nm if nm in C.MATDEFS: me.materials[i] = C.mat(nm) me.materials[i]["gc_real"] = True for m in me.materials: if m: m.use_backface_culling = True if W is not None: for o in new: if o.parent is None: o.matrix_world = W @ o.matrix_world return new def load_blend(path, W=None): """The objects of a .blend (an item with no FBX yet), linked into the scene, parentless ones moved by W.""" bpy, Vector, Matrix, C = _bl() with bpy.data.libraries.load(path) as (src, dst): dst.objects = list(src.objects) new = [] for o in dst.objects: if o is None or o.type not in ("MESH", "EMPTY", "ARMATURE"): continue bpy.context.scene.collection.objects.link(o) new.append(o) bpy.context.view_layer.update() if W is not None: for o in new: if o.parent is None: o.matrix_world = W @ o.matrix_world return new def bounds(objs): bpy, Vector, Matrix, C = _bl() mn, mx = Vector((1e9,) * 3), Vector((-1e9,) * 3) for o in objs: if o.type != "MESH": continue for v in o.data.vertices: w = o.matrix_world @ v.co mn = Vector(map(min, mn, w)) mx = Vector(map(max, mx, w)) return mn, mx def lights(bg=(0.62, 0.72, 0.84), strength=0.55): bpy, Vector, Matrix, C = _bl() sc = bpy.context.scene for nm, en, rot in (("Key", 2.2, (50, 10, -40)), ("Fill", 0.7, (60, 0, 120)), ("Rim", 1.2, (30, 0, 170))): ld = bpy.data.lights.new(nm, "SUN") ld.energy = en ld.angle = math.radians(8) lo = C.link(bpy.data.objects.new(nm, ld)) lo.rotation_euler = [math.radians(a) for a in rot] w = bpy.data.worlds.new("W") sc.world = w try: w.use_nodes = True except Exception: pass n = w.node_tree.nodes.get("Background") n.inputs[0].default_value = (*bg, 1) n.inputs[1].default_value = strength sc.render.engine = "BLENDER_EEVEE" try: sc.view_settings.view_transform = "AgX" except Exception: pass def shoot(path, target, radius, elev, azim, res=RES, ortho=False): bpy, Vector, Matrix, C = _bl() sc = bpy.context.scene cam_d = bpy.data.cameras.new("cam") cam_d.lens = 50 cam = C.link(bpy.data.objects.new("cam", cam_d)) el, az = math.radians(elev), math.radians(azim) d = radius / math.tan(math.radians(18)) * 1.05 tgt = Vector(target) cam.location = tgt + Vector((math.sin(az) * math.cos(el), -math.cos(az) * math.cos(el), math.sin(el))) * d cam.rotation_euler = (tgt - cam.location).to_track_quat("-Z", "Y").to_euler() cam_d.clip_start = d * 0.01 cam_d.clip_end = d * 10 + 10 sc.camera = cam sc.render.resolution_x = sc.render.resolution_y = res sc.render.filepath = path bpy.ops.render.render(write_still=True) bpy.data.objects.remove(cam) def new_path(n): """The round 5 model of n: its FBX, else its .blend.""" p = os.path.join(OUT, "fbx", n + ".fbx") return p if os.path.exists(p) else os.path.join(OUT, n + ".blend") def built(names): """The names that have a round 5 model; a note for the others.""" out = [] for n in names: if os.path.exists(new_path(n)): out.append(n) else: print(f"V5|{n}: no round 5 model in {OUT}, skipped") return out def load_any(path, W=None): return load_blend(path, W) if path.endswith(".blend") else load_fbx(path, W) def views(names, views=None): """views: {name: [(tag, target, radius, elev, azim)]} over VIEWS; an item with none gets DEFAULT_VIEWS.""" bpy, Vector, Matrix, C = _bl() os.makedirs(PANELS, exist_ok=True) table = dict(VIEWS, **(views or {})) for n in built(names): # the frame of each view comes from the new model, so old and new are shot from exactly the same camera bpy.ops.wm.read_factory_settings(use_empty=True) objs = load_any(new_path(n)) mn, mx = bounds(objs) full = ((mn + mx) / 2, (mx - mn).length / 2 * 0.72) only, tags = os.environ.get("GC5_ONLY"), os.environ.get("GC5_VIEWS") for which, path in (("old", os.path.join(GAME, n + ".fbx")), ("new", new_path(n))): if only and which != only: continue if not os.path.exists(path): print(f"V5|{n}: no {which} model ({path}), skipped") continue bpy.ops.wm.read_factory_settings(use_empty=True) load_any(path) lights() for tag, tgt, rad, el, az in table.get(n, DEFAULT_VIEWS): if tags and tag not in tags.split(","): continue t, r = (full if tgt is None else (Vector(tgt), rad)) shoot(os.path.join(PANELS, f"{n}_{tag}_{which}.png"), t, r, el, az) def euler(x, y, z): bpy, Vector, Matrix, C = _bl() return Matrix.Rotation(math.radians(y), 4, "Y") @ Matrix.Rotation(math.radians(x), 4, "X") @ Matrix.Rotation(math.radians(z), 4, "Z") def fpv(names, holds=None): """The ViewModel rig in Blender space, the overlay camera at the origin looking down Blender -Y (Unity +Z). holds: {name: (pos, euler, usePos, useEuler, scale)} over HOLDS. An item without a hold is skipped with a note.""" bpy, Vector, Matrix, C = _bl() M = Matrix(((-1, 0, 0, 0), (0, 0, -1, 0), (0, 1, 0, 0), (0, 0, 0, 1))) os.makedirs(PANELS, exist_ok=True) table = dict(HOLDS, **(holds or {})) for n in built(names): if n not in table: print(f"V5|{n}: no ViewModel hold, no FPV shot (pass holds= or register(name, hold=...))") continue pos, eu, upos, ueu, s = table[n] for pose, (p_, e_) in (("fpv", (pos, eu)), ("fpv_use", (upos, ueu))): for which, path in (("old", os.path.join(GAME, n + ".fbx")), ("new", new_path(n))): if not os.path.exists(path): print(f"V5|{n}: no {which} model, skipped") continue bpy.ops.wm.read_factory_settings(use_empty=True) rig = Matrix.Translation(Vector(p_)) @ euler(*e_) @ Matrix.Scale(s, 4) W = M @ rig @ M.inverted() objs = load_any(path) open_ = 0.35 * 18.0 if pose == "fpv" else 18.0 for o in objs: if o.parent is not None: continue b = o.matrix_world.copy() if o.name.startswith("BladeA"): b = b @ Matrix.Rotation(math.radians(-open_), 4, "Z") elif o.name.startswith("BladeB"): b = b @ Matrix.Rotation(math.radians(open_), 4, "Z") o.matrix_world = W @ b sc = bpy.context.scene cam_d = bpy.data.cameras.new("c") cam_d.sensor_fit = "VERTICAL" cam_d.angle = math.radians(62) cam_d.clip_start = 0.01 cam = C.link(bpy.data.objects.new("c", cam_d)) cam.rotation_euler = (math.radians(90), 0, math.pi) sc.camera = cam lights((0.55, 0.72, 0.42), 0.7) sc.render.resolution_x, sc.render.resolution_y = 1280, 720 sc.render.filepath = os.path.join(PANELS, f"{n}_{pose}_{which}.png") bpy.ops.render.render(write_still=True) def snapshot(path): """objects -> {name: (type, parent, loc, rot, scale, bbox min, bbox max, tris, [materials])}""" bpy, Vector, Matrix, C = _bl() bpy.ops.wm.read_factory_settings(use_empty=True) load_any(path) if path.endswith(".blend") else bpy.ops.import_scene.fbx(filepath=path) bpy.context.view_layer.update() out = {} for o in bpy.context.scene.objects: mn, mx = bounds([o]) if o.type == "MESH" else (Vector(), Vector()) tris = sum(len(p.vertices) - 2 for p in o.data.polygons) if o.type == "MESH" else 0 mats = [m.name.split(".")[0] for m in o.data.materials if m] if o.type == "MESH" else [] out[o.name] = (o.type, o.parent.name if o.parent else None, tuple(o.matrix_world.translation), tuple(o.matrix_world.to_euler()), tuple(o.matrix_world.to_scale()), tuple(mn), tuple(mx), tris, mats) return out def nib_axis(path, c, z0=-0.06, z1=0.02, r=0.06, axis="Z"): """Centre of the geometry round a grip point where the fist closes. axis "Z": (x, y) of what lies within r of c = (x, y) between z0 and z1; axis "Y": (x, z) of what lies within r of c = (x, z) between y = z0 and z1. Returns (a, b, half extent) or None.""" bpy, Vector, Matrix, C = _bl() bpy.ops.wm.read_factory_settings(use_empty=True) load_any(path) if path.endswith(".blend") else bpy.ops.import_scene.fbx(filepath=path) bpy.context.view_layer.update() xs, ys = [], [] for o in bpy.context.scene.objects: if o.type != "MESH": continue for v in o.data.vertices: w = o.matrix_world @ v.co a, b, h = (w.x, w.y, w.z) if axis == "Z" else (w.x, w.z, w.y) if z0 < h < z1 and (a - c[0]) ** 2 + (b - c[1]) ** 2 < r * r: xs.append(a) ys.append(b) if not xs: return None return ((min(xs) + max(xs)) / 2, (min(ys) + max(ys)) / 2, (max(xs) - min(xs)) / 2) def verify(names, req=None, grips=None): """req: {name: [object names]} over REQ (default: the game model's own objects); grips: {name: [(c, axis, lo, hi, r)]} over GRIPS.""" bad = 0 sys.path.insert(0, HERE) rq, gr = dict(REQ, **(req or {})), dict(GRIPS, **(grips or {})) for n in built(names): gp = os.path.join(GAME, n + ".fbx") new = snapshot(new_path(n)) print(f"V5|{n}") if not os.path.exists(gp): print(f"V5| (no game model: {sorted(new)} not compared)") old = {} else: old = snapshot(gp) if set(old) != set(new): print(f"V5| FAIL objects differ: old {sorted(old)} new {sorted(new)}") bad += 1 for k in sorted(set(old) & set(new)): o, w = old[k], new[k] dloc = max(abs(a - b) for a, b in zip(o[2], w[2])) drot = max(abs(a - b) for a, b in zip(o[3], w[3])) dsc = max(abs(a - b) for a, b in zip(o[4], w[4])) ok = o[0] == w[0] and o[1] == w[1] and dloc < 1e-4 and drot < 1e-4 and dsc < 1e-4 bad += 0 if ok else 1 ext = lambda s: tuple(round(b - a, 3) for a, b in zip(s[5], s[6])) print(f"V5| {'ok ' if ok else 'FAIL'} {k:<7} type {w[0]} parent {w[1]} origin {tuple(round(x, 4) for x in w[2])} (d {dloc:.1e}) rot d {drot:.1e} " f"scale d {dsc:.1e}") print(f"V5| bbox old {tuple(round(x, 3) for x in o[5])}..{tuple(round(x, 3) for x in o[6])} size {ext(o)}") print(f"V5| bbox new {tuple(round(x, 3) for x in w[5])}..{tuple(round(x, 3) for x in w[6])} size {ext(w)}") print(f"V5| tris {o[7]} -> {w[7]}") extra = sorted(set(w[8]) - set(o[8])) nomat = sorted(m for m in set(w[8]) if not os.path.exists(os.path.join(ROOT, "Assets", "Art", "Materials", m + ".mat"))) print(f"V5| slots old {sorted(set(o[8]))}") print(f"V5| slots new {sorted(set(w[8]))} new slots: {extra or 'none'} without a .mat: {nomat or 'none'}") if nomat: bad += 1 missing = [r for r in rq.get(n, sorted(old)) if r not in new] if missing: print(f"V5| FAIL missing {missing}") bad += 1 if not old: continue for g in gr.get(n, []): c, axis, lo, hi, r = g a = nib_axis(gp, c, lo, hi, r, axis) b = nib_axis(new_path(n), c, lo, hi, r, axis) d = math.hypot(a[0] - b[0], a[1] - b[1]) if a and b else 9 ok = d < 0.005 bad += 0 if ok else 1 fmt = lambda q: tuple(round(x, 4) for x in q) if q else None print(f"V5| {'ok ' if ok else 'FAIL'} grip at {c} ({axis} {lo}..{hi}): old centre {fmt(a)} new centre {fmt(b)} (d {d:.4f})") print("V5|RESULT|" + ("OK" if bad == 0 else f"{bad} problem(s)")) return bad def scene(names=None, row_width=6.0, gap=0.35): """Every item in remaster_v3_tools (or `names`): the round 5 model on the left of each cell, the game's model on its right, a label under each and the item's name under the cell; cells packed in rows. The 3D view in material preview, looking over the grid.""" bpy, Vector, Matrix, C = _bl() from mathutils import Quaternion # noqa: F401 names = names or items() bpy.ops.wm.read_factory_settings(use_empty=False) for o in list(bpy.data.objects): bpy.data.objects.remove(o) def label(col, text, loc, size): cu = bpy.data.curves.new("lbl_" + text, "FONT") cu.body = text cu.size = size t = bpy.data.objects.new("Label_" + text, cu) t.location = loc col.objects.link(t) return t cells = [] for n in names: parts = [] for which, path in (("v5", new_path(n)), ("game", os.path.join(GAME, n + ".fbx"))): if not os.path.exists(path): continue col = bpy.data.collections.new(f"{n}_{which}") bpy.context.scene.collection.children.link(col) objs = load_any(path) for o in objs: for c in list(o.users_collection): c.objects.unlink(o) col.objects.link(o) o.name = f"{o.name}_{which}" if not o.name.endswith(which) else o.name for m in (o.data.materials if o.type == "MESH" else []): if m: m.use_backface_culling = False bpy.context.view_layer.update() mn, mx = bounds(objs) parts.append((which, col, objs, mn, mx)) if parts: cells.append((n, parts)) # pack the cells in rows along +X, rows going back along +Y; the rows about as wide as a 16:9 view wants them sizes = [(sum(mx.x - mn.x for _, _, _, mn, mx in parts) + gap * (len(parts) - 1), max(mx.y - mn.y for _, _, _, mn, mx in parts) + 0.25) for n, parts in cells] if sizes: area = sum((w + gap * 2) * (d + gap * 1.5) for w, d in sizes) row_width = max(row_width, max(w for w, d in sizes), math.sqrt(area * 1.9)) x, y, row_d = 0.0, 0.0, 0.0 lo, hi = Vector((1e9, 1e9, 0)), Vector((-1e9, -1e9, 0)) for n, parts in cells: widths = [(mx.x - mn.x) for _, _, _, mn, mx in parts] w_cell = sum(widths) + gap * (len(parts) - 1) d_cell = max(mx.y - mn.y for _, _, _, mn, mx in parts) + 0.25 if x > 0 and x + w_cell > row_width: x, y, row_d = 0.0, y + row_d + gap * 1.5, 0.0 cx = x for (which, col, objs, mn, mx), w in zip(parts, widths): off = Vector((cx - mn.x, y + 0.25 - mn.y, 0.0)) for o in objs: if o.parent is None: o.matrix_world = Matrix.Translation(off) @ o.matrix_world label(col, "round 5" if which == "v5" else "game", (cx, y + 0.1, 0.0), 0.05) cx += w + gap title = bpy.data.collections.get("Labels") or bpy.data.collections.new("Labels") if title.name not in bpy.context.scene.collection.children: bpy.context.scene.collection.children.link(title) label(title, n.replace("SM_Tool_", "").replace("SM_", ""), (x, y - 0.06, 0.0), 0.1) lo = Vector((min(lo.x, x), min(lo.y, y), 0)) hi = Vector((max(hi.x, x + w_cell), max(hi.y, y + d_cell), 0)) x += w_cell + gap * 2 row_d = max(row_d, d_cell) w = bpy.data.worlds.new("W") bpy.context.scene.world = w try: w.use_nodes = True w.node_tree.nodes["Background"].inputs[0].default_value = (0.62, 0.72, 0.84, 1) except Exception: pass ld = bpy.data.lights.new("Key", "SUN") ld.energy = 3.0 lo_ = bpy.data.objects.new("Key", ld) lo_.rotation_euler = [math.radians(a) for a in (50, 10, -40)] bpy.context.scene.collection.objects.link(lo_) tgt = (lo + hi) / 2 if cells else Vector() W_, D_ = (hi.x - lo.x, hi.y - lo.y) if cells else (3.0, 3.0) # the viewport's 50 mm lens on a 16:9 view: about 19.8 degrees either side across, 11.4 up and down; looking down at 60 degrees dist = 1.12 * max(W_ / 2 / math.tan(math.radians(19.8)), D_ * 0.87 / 2 / math.tan(math.radians(11.4)), 1.5) eye = tgt + Vector((0.0, -0.5, 0.866)) * dist look = (tgt - eye).normalized() for scr in bpy.data.screens: for area in scr.areas: for sp in area.spaces: if sp.type != "VIEW_3D": continue sp.shading.type = "MATERIAL" sp.clip_start = 0.005 sp.clip_end = 1000 sp.overlay.show_floor = False r3 = sp.region_3d r3.view_perspective = "PERSP" r3.view_rotation = look.to_track_quat("-Z", "Y") r3.view_location = tgt r3.view_distance = (tgt - eye).length path = os.path.join(OUT, REVIEW + ".blend") bpy.ops.wm.save_as_mainfile(filepath=path, check_existing=False) print(f"V5|scene: {len(cells)} item(s) -> {path}: " + ", ".join(n for n, _ in cells)) # ======================================================================= compose (outside Blender) def compose(keys=None): """Old | new panels side by side. keys: only panel keys starting with one of these (names, or name_view).""" from PIL import Image, ImageDraw, ImageFont try: font = ImageFont.truetype("arial.ttf", 26) small = ImageFont.truetype("arial.ttf", 20) except Exception: font = small = ImageFont.load_default() made = [] for fn in sorted(os.listdir(PANELS)): if not (fn.endswith("_old.png") or fn.endswith("_new.png")): continue key = fn[:-len("_old.png")] if keys and not any(key.startswith(k) for k in keys): continue a, b = os.path.join(PANELS, key + "_old.png"), os.path.join(PANELS, key + "_new.png") if fn.endswith("_new.png") and os.path.exists(a): continue # done from the _old side if not os.path.exists(b): continue B = Image.open(b).convert("RGB") A = Image.open(a).convert("RGB") if os.path.exists(a) else Image.new("RGB", B.size, (60, 62, 66)) w, h = A.size bar = 44 im = Image.new("RGB", (w * 2 + 8, h + bar), (30, 32, 36)) im.paste(A, (0, bar)) im.paste(B, (w + 8, bar)) d = ImageDraw.Draw(im) d.text((14, 9), "game (round 4)" if os.path.exists(a) else "no game model", fill=(230, 230, 230), font=font) d.text((w + 22, 9), "round 5", fill=(255, 214, 120), font=font) d.text((w * 2 + 8 - 14 - d.textlength(key, font=small), 13), key, fill=(170, 170, 170), font=small) out = os.path.join(OUT, "previews", key + ".png") im.save(out) made.append(out) print("\n".join(made)) def _names(args): got = [a for a in args if not a.startswith("-")] return got or TOOLS if __name__ == "__main__": args = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else sys.argv[1:] mode = args[0] if args else "views" names = _names(args[1:]) if mode == "compose": compose([a for a in args[1:] if not a.startswith("-")] or None) elif mode == "views": views(names) elif mode == "fpv": fpv(names) elif mode == "verify": verify(names) elif mode == "scene": scene([a for a in args[1:] if not a.startswith("-")] or None)