"""Checks for gc_up_tools.py / gc_up_crew.py. Run: blender -b --factory-startup --python gc_up_check.py -- [names] fpv [names] tool + gloves composed like ViewModel.BuildRigs (62 degree overlay camera) -> Previews/_fpv.png open [names] the same with the blades at full stroke -> _fpv_open.png swing [names] the scythe at both ends of the swing -> _fpv_swA.png / _swB.png wall the seven tools hung like ProjectBuilder.SupplyWallPiece -> Previews/GC_SupplyWall.png report [names] re-import the exported FBX: parts, pivots, tris, materials, missing children """ import bpy, os, sys, math from mathutils import Vector, Matrix sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import common as C GLOVE_R = Vector((0.068, 0, -0.08)) GLOVE_L = Vector((-0.068, 0, -0.08)) HAND = 1.45 M = Matrix(((-1, 0, 0, 0), (0, 0, -1, 0), (0, 1, 0, 0), (0, 0, 0, 1))) # Unity (model convention) -> Blender # ViewModel.BuildRigs: pos, euler, scale, rear grip, hand RIGS = { "SM_Tool_NailScissors": ((0.17, -0.17, 0.32), (-14, -10, -70), 0.42, 0.0, 0.6), "SM_Tool_GardenShears": ((0.2, -0.2, 0.36), (-14, -10, -70), 0.75, 0.0, 0.75), "SM_Tool_Sickle": ((0.26, -0.27, 0.42), (-30, -16, 0), 0.2, 0.0, 0.6), "SM_Tool_Scythe": ((0.32, -0.2, 0.44), (10, -40, 0), 0.46, 0.62, 0.5), "SM_Tool_LongScythe": ((0.32, -0.2, 0.44), (10, -42, 0), 0.5, 0.6, 0.5), "SM_Tool_WideScythe": ((0.32, -0.2, 0.44), (10, -44, 0), 0.54, 0.58, 0.5), } # the hammer needs its own lines in ViewModel (the fist turns to wrap a handle that runs along the tool's forward axis) HAMMER = dict(pos=(0.28, -0.19, 0.42), eul=(-58, -14, -8), s=0.56, hand=0.72) REQ = { "SM_Tool_NailScissors": ["Body", "BladeA", "BladeB"], "SM_Tool_GardenShears": ["Body", "BladeA", "BladeB"], "SM_Tool_Shears": ["Body", "BladeA", "BladeB"], "SM_Tool_Sickle": ["Body"], "SM_Tool_Scythe": ["Body"], "SM_Tool_LongScythe": ["Body"], "SM_Tool_WideScythe": ["Body"], "SM_Tool_Hammer": ["Body"], "SM_Glove_R": ["Glove"], "SM_Glove_L": ["Glove"], "SM_Crew": ["Rig", "CrewBody", "Vest", "Helmet", "Lamp", "Backpack", "TankFill", "Boot_L", "Boot_R"], } BONES = ["Root", "Body", "Leg_L", "Leg_R", "Arm_L", "Arm_R"] WALL = ["SM_Tool_NailScissors", "SM_Tool_GardenShears", "SM_Tool_Sickle", "SM_Tool_Scythe", "SM_Tool_LongScythe", "SM_Tool_WideScythe", "SM_Tool_Hammer"] WALL_SIZE = [0.5, 0.8, 0.24, 0.46, 0.52, 0.6, 0.7] def euler(x, y, z): """Quaternion.Euler numerically: y * x * z (the left-handed sign flip is covered by the M conjugation).""" return Matrix.Rotation(math.radians(y), 4, "Y") @ Matrix.Rotation(math.radians(x), 4, "X") @ Matrix.Rotation(math.radians(z), 4, "Z") def T(v): return Matrix.Translation(Vector(v)) def to_blender(uw): return M @ uw @ M.inverted() def load(name, W, blade=0.0): """blade: ViewModel's opening angle in degrees. AngleAxis(+a, forward) on BladeA in Unity's mirrored part frame is a clockwise turn seen from Blender +Z, so BladeA's tip goes to Blender -X.""" with bpy.data.libraries.load(os.path.join(C.BLENDS, name + ".blend")) as (src, dst): dst.objects = list(src.objects) for o in dst.objects: if o is None or o.type != "MESH": continue C.link(o) b = o.matrix_basis.copy() if o.name.startswith("BladeA"): b = b @ Matrix.Rotation(math.radians(-blade), 4, "Z") elif o.name.startswith("BladeB"): b = b @ Matrix.Rotation(math.radians(blade), 4, "Z") o.matrix_world = W @ b def lights(bgcol=(0.55, 0.72, 0.42), strength=0.7): sc = bpy.context.scene for nm, en, rot in (("Key", 2.4, (50, 10, -30)), ("Fill", 0.8, (60, 0, 120)), ("Back", 0.9, (110, 0, 20))): ld = bpy.data.lights.new(nm, "SUN") ld.energy = en 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 bg = w.node_tree.nodes.get("Background") bg.inputs[0].default_value = (*bgcol, 1) bg.inputs[1].default_value = strength sc.render.engine = "BLENDER_EEVEE" def view(out, res=(960, 540)): 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) # the camera at the origin looks along Blender -Y (Unity +Z) sc.camera = cam lights() sc.render.resolution_x, sc.render.resolution_y = res sc.render.filepath = os.path.join(C.PREVIEWS, out + ".png") bpy.ops.render.render(write_still=True) def cull(names): """Each model with back faces culled, like Unity: a part that is inside-out shows up as a hole.""" for n in names: bpy.ops.wm.read_factory_settings(use_empty=True) load(n, Matrix.Identity(4)) objs = [o for o in bpy.context.scene.objects if o.type == "MESH"] for o in objs: for m in o.data.materials: if m: m.use_backface_culling = True for suf, az in (("a", -35), ("b", 145)): C.preview("_cull_" + n + "_" + suf, elev=28, azim=az, dist_mul=0.78, res=420) def fpv(name, blade=6.3, extra=None, suffix="_fpv"): bpy.ops.wm.read_factory_settings(use_empty=True) d = extra or (0, 0, 0, 0, 0, 0) if name == "SM_Tool_Hammer": h = HAMMER rig = T(h["pos"]) @ euler(*h["eul"]) load(name, to_blender(rig @ Matrix.Scale(h["s"], 4))) S = HAND * h["hand"] load("SM_Glove_R", to_blender(rig @ T((0.068 * S, 0.08 * S, 0)) @ euler(90, 0, 0) @ Matrix.Scale(S, 4))) else: pos, eu, s, rear, hand = RIGS[name] rig = T(Vector(pos) + Vector(d[:3])) @ euler(*(Vector(eu) + Vector(d[3:]))) load(name, to_blender(rig @ Matrix.Scale(s, 4)), blade=blade) load("SM_Glove_R", to_blender(rig @ T(GLOVE_R * hand) @ Matrix.Scale(HAND * hand, 4))) if rear > 0: load("SM_Glove_L", to_blender(rig @ T(Vector((0, 0, rear)) * s + GLOVE_L * hand) @ euler(0, 0, 12) @ Matrix.Scale(HAND * hand, 4))) view(name + suffix) def wall(): bpy.ops.wm.read_factory_settings(use_empty=True) W0 = T((-8.5, 0, -1.5)) @ euler(0, 90, 0) for i, n in enumerate(WALL): if not os.path.exists(os.path.join(C.BLENDS, n + ".blend")): continue p = W0 @ Vector((1.8 - i * 0.6, 0.9, 0.14)) load(n, to_blender(T(p) @ (W0.to_3x3().to_4x4()) @ euler(-90, 0, 0) @ Matrix.Scale(WALL_SIZE[i], 4))) # the pegboard bc = W0 @ Vector((0, 1.3, 0)) pb = C.cube("Pegboard", (4.4, 0.12, 2.5), (0, 0, 0), "M_Timber") # model axes: Blender Y = Unity Z, Blender Z = Unity Y C.uv_box(pb, 1.0) pb.matrix_world = to_blender(T(bc) @ W0.to_3x3().to_4x4()) sc = bpy.context.scene cam_d = bpy.data.cameras.new("c") cam_d.lens = 24 cam = C.link(bpy.data.objects.new("c", cam_d)) # a player standing on the deck, facing the wall (Unity -X from the wall) eye = Vector((-5.0, 1.15, -1.5)) target = Vector((-8.5, 1.35, -1.5)) cam.location = (M @ eye.to_4d()).to_3d() t = (M @ target.to_4d()).to_3d() cam.rotation_mode = "QUATERNION" cam.rotation_quaternion = (t - cam.location).to_track_quat("-Z", "Y") sc.camera = cam lights((0.62, 0.72, 0.84), 0.8) sc.render.resolution_x, sc.render.resolution_y = 1280, 720 sc.render.filepath = os.path.join(C.PREVIEWS, "GC_SupplyWall.png") bpy.ops.render.render(write_still=True) def report(n): bpy.ops.wm.read_factory_settings(use_empty=True) bpy.ops.import_scene.fbx(filepath=os.path.join(C.MODELS, n + ".fbx")) bpy.context.view_layer.update() found, bones, tot, mats = set(), [], 0, set() print(f"UP|{n}") for o in sorted(bpy.context.scene.objects, key=lambda o: o.name): found.add(o.name) mw = o.matrix_world org = tuple(round(x, 3) for x in mw.translation) if o.type == "ARMATURE": bones = [b.name for b in o.data.bones] for b in o.data.bones: print(f"UP| bone {b.name:<6} head {tuple(round(x, 3) for x in mw @ b.head_local)} tail {tuple(round(x, 3) for x in mw @ b.tail_local)} par {b.parent.name if b.parent else '-'}") continue if o.type != "MESH": print(f"UP| {o.type:<6} {o.name:<10} origin {org}") continue t = sum(len(f.vertices) - 2 for f in o.data.polygons) tot += t pts = [mw @ v.co for v in o.data.vertices] bb = [(round(min(p[i] for p in pts), 3), round(max(p[i] for p in pts), 3)) for i in range(3)] par = (o.parent.name + (":" + o.parent_bone if o.parent_type == "BONE" else "")) if o.parent else "-" names = [m.name for m in o.data.materials if m] mats.update(names) vg = ",".join(g.name for g in o.vertex_groups) print(f"UP| MESH {o.name:<10} origin {org} bb {bb} tris {t} par {par} vg[{vg}]") missing = [r for r in REQ.get(n, []) if r not in found] + ([b for b in BONES if b not in bones] if n == "SM_Crew" else []) unk = sorted({m.split(".")[0] for m in mats if not os.path.exists(os.path.join(C.ROOT, "Assets", "Art", "Materials", m.split(".")[0] + ".mat"))}) print(f"UP| tris {tot} | missing {missing or 'none'} | no .mat yet: {unk or 'none'}") return len(missing) args = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [] mode = args[0] if args else "report" names = args[1:] if mode == "fpv": for n in (names or list(RIGS) + ["SM_Tool_Hammer"]): fpv(n) elif mode == "open": for n in (names or ["SM_Tool_NailScissors", "SM_Tool_GardenShears"]): fpv(n, blade=18.0, suffix="_fpv_open") elif mode == "swing": for n in (names or ["SM_Tool_Scythe", "SM_Tool_WideScythe"]): fpv(n, extra=(-35 * 0.0025, 0, 0, 35 * 0.08, 35, -35 * 0.25), suffix="_fpv_swA") fpv(n, extra=(55 * 0.0025, 0, 0, 55 * 0.08, -55, 55 * 0.25), suffix="_fpv_swB") elif mode == "wall": wall() elif mode == "cull": cull(names or [n for n in REQ if n != "SM_Tool_Shears"]) else: bad = sum(report(n) for n in (names or list(REQ))) print("UP|RESULT|" + ("OK" if bad == 0 else f"MISSING {bad}"))