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>
138 lines
6.2 KiB
Python
138 lines
6.2 KiB
Python
"""GRASS CUTTER - first-person arms cut from the crew themselves: SM_Arm_Hank_R, SM_Arm_Hank_L and so on.
|
|
|
|
Each crew member's own hand, forearm and sleeve, faces, texture and all, taken off SM_Crew_<Name> as it hangs at rest.
|
|
They come out in the frame ViewModel holds a fist in: the fist round the origin, the forearm running up +Y toward the
|
|
elbow, the rod through the fist along +Z (little finger to thumb, so forward as the arm hangs). Past the elbow the sleeve
|
|
is drawn out longer than it is, so that it leaves the screen instead of ending in the middle of it.
|
|
Run (after gc_tripo_crew.py has written the crew):
|
|
blender -b --factory-startup --python ArtSource/Blender/scripts/gc_crew_arms.py -- [hank otis ...]
|
|
"""
|
|
import sys, os, math
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
import bpy
|
|
from mathutils import Vector, Matrix
|
|
import common
|
|
from common import reset, link, preview, MODELS, TEXTURES
|
|
|
|
ROSTER = ("hank", "otis", "lin", "rustam", "marcus", "gus", "wade", "kenji")
|
|
TAKE = 0.5 # how much of a vertex must belong to the arm for it to come along
|
|
STRETCH = 3.2 # the upper arm, drawn out this many times
|
|
SLEEVE = 0.7 # how much of the upper arm, elbow to shoulder, is kept
|
|
HAND_FROM = 0.012 # metres back up the forearm from the wrist where the hand is taken to start
|
|
|
|
|
|
def load(name):
|
|
reset()
|
|
bpy.ops.import_scene.fbx(filepath=os.path.join(MODELS, "SM_Crew_" + name.capitalize() + ".fbx"))
|
|
body = next(o for o in bpy.context.scene.objects if o.type == "MESH" and o.vertex_groups.get("Fore_R"))
|
|
rig = next(o for o in bpy.context.scene.objects if o.type == "ARMATURE")
|
|
return body, rig
|
|
|
|
|
|
def arm(body, rig, side, name):
|
|
"""The arm on one side as a new mesh in the fist's frame."""
|
|
me = body.data
|
|
W = body.matrix_world
|
|
g = {body.vertex_groups[n].index for n in ("Arm_" + side, "Fore_" + side)}
|
|
share = [sum(e.weight for e in v.groups if e.group in g) for v in me.vertices]
|
|
bones = rig.data.bones
|
|
sh = rig.matrix_world @ bones["Arm_" + side].head_local
|
|
el = rig.matrix_world @ bones["Arm_" + side].tail_local
|
|
wr = rig.matrix_world @ bones["Fore_" + side].tail_local
|
|
down = (wr - sh).normalized()
|
|
keep = [share[i] >= TAKE for i in range(len(me.vertices))]
|
|
P = [W @ v.co for v in me.vertices]
|
|
# the fist: the middle of what hangs past the wrist
|
|
t_wr = (wr - sh).dot(down) - HAND_FROM
|
|
hand = [P[i] for i in range(len(P)) if keep[i] and (P[i] - sh).dot(down) > t_wr]
|
|
lo = Vector(map(min, *hand)) if len(hand) > 1 else hand[0]
|
|
hi = Vector(map(max, *hand)) if len(hand) > 1 else hand[0]
|
|
c = (lo + hi) / 2
|
|
Y = -down
|
|
Z = Vector((0, -1, 0))
|
|
Z = (Z - Y * Z.dot(Y)).normalized()
|
|
X = Y.cross(Z)
|
|
R = Matrix((X, Y, Z)) # rows: world to fist frame
|
|
y_el = (el - c).dot(Y)
|
|
# the shoulder end is ragged where the arm's weights give out: the sleeve is cut level well short of it
|
|
y_cut = y_el + SLEEVE * (sh - el).length
|
|
keep = [keep[i] and (R @ (P[i] - c)).y < y_cut for i in range(len(P))]
|
|
|
|
def place(p):
|
|
q = R @ (p - c)
|
|
if q.y > y_el:
|
|
q.y = y_el + (q.y - y_el) * STRETCH
|
|
return q
|
|
|
|
faces = [p for p in me.polygons if all(keep[i] for i in p.vertices)]
|
|
used = sorted({i for p in faces for i in p.vertices})
|
|
remap = {i: k for k, i in enumerate(used)}
|
|
nm = bpy.data.meshes.new(name)
|
|
nm.from_pydata([place(P[i]) for i in used], [], [[remap[i] for i in p.vertices] for p in faces])
|
|
nm.update()
|
|
# the same corners in the same order: carry the UVs, the normals (turned into the new frame) and the material
|
|
src_uv = me.uv_layers.active.data
|
|
uv = nm.uv_layers.new(name="UVMap").data
|
|
normals = []
|
|
Rn = R @ W.to_3x3()
|
|
k = 0
|
|
for p in faces:
|
|
for li in p.loop_indices:
|
|
uv[k].uv = src_uv[li].uv
|
|
normals.append((Rn @ me.corner_normals[li].vector).normalized())
|
|
k += 1
|
|
for m in me.materials:
|
|
nm.materials.append(m)
|
|
for p, q in zip(nm.polygons, faces):
|
|
p.material_index = q.material_index
|
|
nm.normals_split_custom_set(normals)
|
|
o = link(bpy.data.objects.new(name, nm))
|
|
lengths = (round((wr - sh).length, 3), round(y_el, 3), round(max(v.co.y for v in nm.vertices), 3))
|
|
print(name, "faces", len(faces), "of", len(me.polygons), "arm, elbow, sleeve end", lengths, flush=True)
|
|
return o
|
|
|
|
|
|
def textured(o, name):
|
|
"""Only for the picture: the crew texture on the material, as the game has it."""
|
|
path = os.path.join(TEXTURES, "T_Crew_" + name.capitalize() + ".png")
|
|
for m in o.data.materials:
|
|
m.use_nodes = True
|
|
nt = m.node_tree
|
|
bsdf = next(n for n in nt.nodes if n.type == "BSDF_PRINCIPLED")
|
|
tex = nt.nodes.new("ShaderNodeTexImage")
|
|
tex.image = bpy.data.images.load(path)
|
|
nt.links.new(tex.outputs["Color"], bsdf.inputs["Base Color"])
|
|
|
|
|
|
def export_one(o, name):
|
|
for x in bpy.context.scene.objects:
|
|
x.select_set(x == o)
|
|
bpy.ops.export_scene.fbx(filepath=os.path.join(MODELS, name + ".fbx"), use_selection=True, axis_forward="-Z", axis_up="Y",
|
|
apply_scale_options="FBX_SCALE_ALL", bake_space_transform=False,
|
|
use_mesh_modifiers=True, mesh_smooth_type="OFF", add_leaf_bones=False,
|
|
object_types={"MESH"}, path_mode="STRIP", use_custom_props=False)
|
|
|
|
|
|
def main(names):
|
|
for nm in names:
|
|
body, rig = load(nm)
|
|
made = [(arm(body, rig, s, "SM_Arm_%s_%s" % (nm.capitalize(), s)), s) for s in ("R", "L")]
|
|
for x in list(bpy.context.scene.objects):
|
|
if x not in [o for o, _ in made]:
|
|
bpy.data.objects.remove(x)
|
|
for o, s in made:
|
|
export_one(o, o.name)
|
|
for o, s in made:
|
|
o.hide_render = False
|
|
for x, _ in made:
|
|
x.hide_render = x != o
|
|
if nm == names[0] or os.environ.get("GC_ALLPREV"):
|
|
textured(o, nm)
|
|
preview(o.name, elev=20, azim=60 if s == "R" else -60, res=520, bg=(0.45, 0.52, 0.48), frame=[o.name])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
args = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []
|
|
common.PREVIEWS = os.environ.get("GC_PREVIEWS", common.PREVIEWS)
|
|
main(args or list(ROSTER))
|