Files
grass-cutters/ArtSource/Blender/scripts/verify_farm_tools.py
NiemandKG d1c3761e58 Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and
Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date.
Images, audio, models and binaries go through Git LFS (.gitattributes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:40 +06:00

152 lines
6.6 KiB
Python

"""Checks for gc_farm_tools.py. Run: blender -b --factory-startup --python verify_farm_tools.py -- [names]
1. Re-imports each exported FBX and reports parts, pivots, spin axes, tris, materials; every part the game looks up must exist.
2. Renders SM_x_fpv.png: the tool and gloves composed exactly like ViewModel.BuildRigs (positions, rotations, scales and the
62 degree view-model camera), so grip fit and screen coverage can be judged without running Unity.
"""
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
REQ = {
"SM_Tool_HandPump": ["Body", "Plunger", "Nozzle"],
"SM_Tool_SlurpGun": ["Body", "Fill", "Fan", "Nozzle"],
"SM_Tool_HydroCannon": ["Body", "Fan", "Nozzle"],
"SM_Tool_MegaSiphon": ["Body", "Ring1", "Ring2", "Nozzle"],
"SM_Glove_R": ["Glove"],
"SM_Glove_L": ["Glove"],
"SM_Crew": ["Rig", "CrewBody", "Vest", "Helmet", "Lamp", "Backpack", "TankFill", "Boot_L", "Boot_R"],
"SM_Gadget_Tablet": ["Body", "Screen"],
}
BONES = ["Root", "Body", "Leg_L", "Leg_R", "Arm_L", "Arm_R"]
# ViewModel.BuildRigs: pos, euler, scale, rear grip
RIGS = {
"SM_Tool_HandPump": ((0.19, -0.2, 0.46), (-2, -4, 0), 0.6, 0.0),
"SM_Tool_SlurpGun": ((0.2, -0.21, 0.48), (-2, -4, 0), 0.56, 0.0),
"SM_Tool_HydroCannon": ((0.2, -0.22, 0.5), (-1, -4, 0), 0.5, 0.33),
"SM_Tool_MegaSiphon": ((0.21, -0.23, 0.54), (-1, -4, 0), 0.46, 0.25),
}
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
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):
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)
o.matrix_world = W @ o.matrix_basis # matrix_world of an appended object is stale; these tool parts have no parents
def view(name, 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
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 = (0.55, 0.72, 0.42, 1)
bg.inputs[1].default_value = 0.7
sc.render.engine = "BLENDER_EEVEE"
sc.render.resolution_x, sc.render.resolution_y = res
sc.render.filepath = os.path.join(C.PREVIEWS, name + "_fpv.png")
bpy.ops.render.render(write_still=True)
def fpv(name):
bpy.ops.wm.read_factory_settings(use_empty=True)
pos, eu, s, rear = RIGS[name]
rig = T(pos) @ euler(*eu)
load(name, to_blender(rig @ Matrix.Scale(s, 4)))
load("SM_Glove_R", to_blender(rig @ T(GLOVE_R * s) @ Matrix.Scale(HAND * s, 4)))
if rear > 0:
load("SM_Glove_L", to_blender(rig @ T(Vector((0, 0, rear)) * s + GLOVE_L * s) @ euler(0, 0, 12) @ Matrix.Scale(HAND * s, 4)))
view(name)
def fpv_tablet():
bpy.ops.wm.read_factory_settings(use_empty=True)
rig = T((0, -0.27, 0.46)) @ euler(28, 0, 0)
load("SM_Gadget_Tablet", to_blender(rig @ Matrix.Scale(0.75, 4)))
load("SM_Glove_R", to_blender(rig @ T((0.135, 0.015, -0.015)) @ euler(0, 0, -80) @ Matrix.Scale(HAND * 0.6, 4)))
load("SM_Glove_L", to_blender(rig @ T((-0.135, 0.015, -0.015)) @ euler(0, 0, 80) @ Matrix.Scale(HAND * 0.6, 4)))
view("SM_Gadget_Tablet")
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"FARM|{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"FARM| 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)}")
continue
if o.type != "MESH":
print(f"FARM| {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)]
y = (mw.to_3x3() @ Vector((0, 1, 0))).normalized()
z = (mw.to_3x3() @ Vector((0, 0, 1))).normalized()
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"FARM| MESH {o.name:<10} origin {org} localY {tuple(round(c, 2) for c in y)} localZ {tuple(round(c, 2) for c in z)} 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"FARM| tris {tot} | missing {missing or 'none'} | no .mat yet: {unk or 'none'}")
return len(missing)
names = (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []) or list(REQ)
bad = sum(report(n) for n in names)
for n in names:
if n in RIGS:
fpv(n)
elif n == "SM_Gadget_Tablet":
fpv_tablet()
print("FARM|RESULT|" + ("OK" if bad == 0 else f"MISSING {bad}"))