Files
grass-cutters/ArtSource/Blender/scripts/gc_crew_check.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

129 lines
6.4 KiB
Python

"""GRASS CUTTER - inspects the exported crew models for everything that can go wrong with a generated shell.
Mesh: holes, non-manifold edges, flipped or degenerate faces, stray islands. Map: islands, how much of it is used, texels
per metre. Skin: weights that don't add up, more than four bones on a vertex. Pose: every game pose at its extreme, with the
edges that stretch or crush past a limit named by the bone they sit on.
Run: blender -b --factory-startup --python ArtSource/Blender/scripts/gc_crew_check.py -- [hank otis ...]"""
import sys, os, math
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import bpy, bmesh
from mathutils import Vector
from gc_tripo_crew import ROSTER, MAP
import common
DEG = math.radians
STRETCH = 0.035 # metres an edge may grow or shrink before it reads as a tear
# Blender's signs, the opposite of Rot() in CrewAvatar: forward at the hip is negative X
POSES = {
"step": {"Leg_L": (-38, 0, 0), "Shin_L": (0, 0, 0), "Leg_R": (32, 0, 0), "Shin_R": (55, 0, 0), "Arm_L": (30, 0, 0), "Arm_R": (-30, 0, 0), "Fore_L": (-30, 0, 0), "Fore_R": (-30, 0, 0)},
"sit": {"Leg_L": (-85, 0, 0), "Leg_R": (-85, 0, 0), "Shin_L": (85, 0, 0), "Shin_R": (85, 0, 0), "Arm_L": (-60, 0, 0), "Arm_R": (-60, 0, 0), "Fore_L": (-25, 0, 0), "Fore_R": (-25, 0, 0), "Head": (20, 0, 0)},
"reach": {"Arm_L": (-105, 0, 25), "Arm_R": (-100, 0, -25), "Fore_L": (-45, 0, 0), "Fore_R": (-30, 0, 0), "Head": (-25, 0, 0)},
"swim": {"Leg_L": (35, 0, 0), "Leg_R": (-35, 0, 0), "Arm_L": (-110, 0, 0), "Arm_R": (70, 0, 0), "Shin_L": (30, 0, 0), "Shin_R": (30, 0, 0)},
}
def check(name):
bpy.ops.wm.read_factory_settings(use_empty=True)
path = os.path.join(common.MODELS, "SM_Crew_" + name.capitalize() + ".fbx")
bpy.ops.import_scene.fbx(filepath=path)
bad = []
o = bpy.data.objects.get("CrewBody")
rig = bpy.data.objects.get("Rig")
for need in ("CrewBody", "Rig", "Backpack", "TankFill", "Vest", "Lamp", "Hand_L", "Hand_R", "EyeBall_L", "EyeBall_R", "Lid_L", "Lid_R", "LidLow_L", "LidLow_R", "Brow_L", "Brow_R"):
if need.startswith(("EyeBall", "Lid", "Brow")) and need in bpy.data.objects and (bpy.data.objects[need].parent_bone != "Head"):
bad.append(need + " not on Head")
if need not in bpy.data.objects:
bad.append("missing " + need)
me = o.data
bm = bmesh.new()
bm.from_mesh(me)
bm.edges.ensure_lookup_table()
open_ = [e for e in bm.edges if e.is_boundary]
wild = [e for e in bm.edges if not e.is_manifold and not e.is_boundary]
thin = [f for f in bm.faces if f.calc_area() < 1e-8]
# a closed skin that encloses a positive volume faces outward all over; folds are only where it turns on itself, a brim's lip
fold = [f for f in bm.faces if sum(1 for e in f.edges for g in e.link_faces if g is not f and g.normal.dot(f.normal) < -0.6) >= 2]
volume = sum(f.verts[0].co.dot(f.verts[1].co.cross(f.verts[2].co)) / 6 for f in bm.faces if len(f.verts) == 3)
# islands of the mesh
seen, isl = set(), []
for v in bm.verts:
if v.index in seen:
continue
st, n = [v], 0
seen.add(v.index)
while st:
q = st.pop()
n += 1
for e in q.link_edges:
w = e.other_vert(q)
if w.index not in seen:
seen.add(w.index)
st.append(w)
isl.append(n)
# UV islands
uvl = bm.loops.layers.uv.active
done, uv_isl = set(), 0
for f in bm.faces:
if f.index in done:
continue
uv_isl += 1
st = [f]
done.add(f.index)
while st:
g = st.pop()
for l in g.loops:
r = l.link_loop_radial_next
h = r.face
if h is g or h.index in done:
continue
if (l[uvl].uv - r.link_loop_next[uvl].uv).length < 1e-5 and (l.link_loop_next[uvl].uv - r[uvl].uv).length < 1e-5:
done.add(h.index)
st.append(h)
uv_area = sum(abs(sum(a[uvl].uv.x * b[uvl].uv.y - b[uvl].uv.x * a[uvl].uv.y for a, b in zip(f.loops, list(f.loops)[1:] + list(f.loops)[:1]))) / 2 for f in bm.faces)
area = sum(f.calc_area() for f in bm.faces)
density = math.sqrt(uv_area * MAP * MAP / max(area, 1e-6))
print(f"{name}: {len(bm.faces)} tris {len(bm.verts)} verts | open edges {len(open_)} wild {len(wild)} degenerate {len(thin)} folds {len(fold)} volume {volume * 1000:.0f} L | islands {len(isl)} smallest {sorted(isl)[:6]} | uv islands {uv_isl} map used {uv_area * 100:.0f}% {density:.0f} texels/m", flush=True)
for e in open_[:6]:
c = (e.verts[0].co + e.verts[1].co) / 2
print(" open at", tuple(round(x, 3) for x in c))
# skin
groups = {g.index: g.name for g in o.vertex_groups}
over, off = 0, 0
bone_of = []
for v in me.vertices:
ws = sorted(((g.weight, groups[g.group]) for g in v.groups if g.weight > 1e-4), reverse=True)
if len(ws) > 4:
over += 1
if abs(sum(w for w, _ in ws) - 1) > 0.02:
off += 1
bone_of.append(ws[0][1] if ws else "?")
print(f" skin: {over} verts over 4 bones, {off} not summing to 1")
# poses
rest = [v.co.copy() for v in me.vertices]
edges = [tuple(e.vertices) for e in me.edges]
base = [(rest[a] - rest[b]).length for a, b in edges]
for pose, rots in POSES.items():
for pb in rig.pose.bones:
pb.rotation_mode = "XYZ"
pb.rotation_euler = tuple(DEG(x) for x in rots.get(pb.name, (0, 0, 0)))
bpy.context.view_layer.update()
ev = o.evaluated_get(bpy.context.evaluated_depsgraph_get()).to_mesh()
now = [v.co.copy() for v in ev.vertices]
tally, worst, at = {}, 0.0, None
for (a, b), l in zip(edges, base):
d = abs((now[a] - now[b]).length - l)
if d > worst:
worst, at = d, rest[a]
if d > STRETCH:
tally[bone_of[a]] = tally.get(bone_of[a], 0) + 1
print(f" {pose}: worst {worst * 100:.1f} cm at {tuple(round(x, 2) for x in at)}, over {STRETCH * 100:.1f} cm: {tally or 'none'}")
o.evaluated_get(bpy.context.evaluated_depsgraph_get()).to_mesh_clear()
bm.free()
if bad:
print(" MISSING:", bad)
if __name__ == "__main__":
args = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []
for nm in (args or ROSTER):
check(nm)