cntrl/tools/blender/build_agent.py

604 lines
26 KiB
Python

# tools/blender/build_agent.py
# FBC: NIGHT SHIFT — S17: reference-based, skinned FBC agent (the player).
#
# Run inside Blender 5.x:
# exec(open("/home/dandovlatov/Documents/cntrl/tools/blender/build_agent.py").read())
#
# Builds a ~10k-tri agent from the Control reference (yellow helmet, glowing
# respirator mask, pale coveralls, tactical harness, radio, rifle with a
# red-tipped barrel), rigs it with an 18-bone armature, authors Idle / Run /
# Dash / Fire / Stagger actions and exports one skinned GLB with animations.
# CPU skinning in raylib needs no bone attributes in actor.vs (see TECHNICAL.md).
import math
import os
import re
import sys
import bpy
from mathutils import Euler, Vector
REPO = "/home/dandovlatov/Documents/cntrl"
MODEL_DIR = os.path.join(REPO, "assets", "models")
BLEND_DIR = os.path.join(REPO, "assets", "blender")
CONCEPT_DIR = os.path.join(REPO, "docs", "concept")
for d in (MODEL_DIR, BLEND_DIR, CONCEPT_DIR):
os.makedirs(d, exist_ok=True)
sys.path.insert(0, os.path.join(REPO, "tools", "blender"))
import importlib
import actor_kit as K
importlib.reload(K)
K.clear_scene()
P = K.PALETTE
P.update({
"coverall": "#6f7f6d", "coverall_dk": "#566452", "coverall_sh": "#414c3e",
"helmet": "#c8a312", "helmet_dk": "#91740c", "helmet_lt": "#d8b729",
"mask": "#242724", "mask_lt": "#3b413b", "glass": "#162020",
"filter": "#2c302e", "black": "#151719", "leather": "#1d201e",
"patch": "#d8d6c8", "visor": "#ff3a06", "visor_hot": "#ffc061",
"board": "#f5c518", "hiss_red": "#c1121f",
})
def panel(root, name, w, h, t, x, y, z, value, rot=(math.pi / 2, 0, 0), ao=0.06):
outline = [(-w / 2, -h / 2), (w / 2, -h / 2),
(w / 2, h / 2), (-w / 2, h / 2)]
obj = K.plate(root, name, outline, 0.0, t, 1.0, value, ao=ao)
mat = Euler(rot).to_matrix()
shift = Vector((x, y, z))
for v in obj.data.vertices:
v.co = shift + mat @ v.co
obj.data.update()
return obj
# ------------------------------------------------------------------- geometry
def build_agent():
r = K.new_actor("Agent")
# Head: gas mask under a worn FBC helmet.
K.blob(r, "head", (0, 0, 1.68), (0.195, 0.185, 0.225), P["mask"],
subdiv=3, warp=0.07, seed=11, ao=0.04)
panel(r, "mask_face", 0.31, 0.30, 0.035, 0.0, -0.183, 1.67,
P["mask_lt"])
panel(r, "visor_band", 0.315, 0.16, 0.022, 0.0, -0.252, 1.735,
P["visor"], ao=0.0)
panel(r, "visor_hot", 0.275, 0.055, 0.01, 0.0, -0.266, 1.745,
P["visor_hot"], ao=0.0)
panel(r, "visor_glow", 0.20, 0.05, 0.008, 0.0, -0.268, 1.65,
P["visor"], ao=0.0)
for side in (-1, 1):
lens = K.ring(r, "lens_%d" % side, (0.0, 0.0, 0.0), 0.058, 0.014,
P["steel_dk"], segments=14)
for v in lens.data.vertices:
v.co = Euler((math.pi / 2, 0, 0)).to_matrix() @ v.co + Vector(
(side * 0.112, -0.267, 1.665))
lens.data.update()
glass = K.plate(r, "glass_%d" % side, K.octagon(0.05, 0.05),
0.0, 0.012, 1.0, P["glass"], ao=0.0)
for v in glass.data.vertices:
v.co = Euler((math.pi / 2, 0, 0)).to_matrix() @ v.co + Vector(
(side * 0.112, -0.279, 1.665))
glass.data.update()
K.blob(r, "filter_%d" % side, (side * 0.095, -0.245, 1.57),
(0.055, 0.05, 0.055), P["filter"], subdiv=2, seed=21 + side)
rim = K.ring(r, "filter_rim_%d" % side, (0.0, 0.0, 0.0),
0.043, 0.011, P["steel_dk"], segments=12)
for v in rim.data.vertices:
v.co = Euler((math.pi / 2, 0, 0)).to_matrix() @ v.co + Vector(
(side * 0.095, -0.30, 1.57))
rim.data.update()
K.tube(r, "strap_chin_%d" % side,
[(side * 0.20, 0.02, 1.83), (side * 0.21, -0.05, 1.70),
(side * 0.13, -0.19, 1.58)], [0.024, 0.022, 0.020],
P["black"], sides=7)
K.tube(r, "hose", [(0.0, -0.25, 1.54), (0.05, -0.28, 1.43),
(0.12, -0.25, 1.33)], [0.036, 0.032, 0.028],
P["black"], sides=8)
K.loft(r, "helmet", [
{"z": 1.80, "r": (0.235, 0.225), "n": 24},
{"z": 1.88, "r": (0.252, 0.238), "n": 24, "fold": (0.012, 5)},
{"z": 1.96, "r": (0.235, 0.222), "n": 24, "fold": (0.015, 6)},
{"z": 2.02, "r": (0.175, 0.168), "n": 24},
{"z": 2.05, "r": (0.075, 0.075), "n": 24},
], P["helmet"], ao=0.07)
K.ring(r, "helmet_band", (0.0, 0.0, 1.815), 0.238, 0.016,
P["helmet_dk"], segments=24)
for side in (-1, 1):
K.tube(r, "helmet_seam_%d" % side,
[(side * 0.06, -0.225, 1.86),
(side * 0.14, -0.19, 1.94),
(side * 0.10, -0.10, 2.02)], [0.008, 0.008, 0.007],
P["helmet_dk"], sides=5)
K.plate(r, "helmet_brim", K.octagon(0.265, 0.195), 1.795, 1.82, 0.94,
P["helmet_dk"], shift=(0, -0.155))
for i, (x, y) in enumerate(((0.10, 0.19), (-0.10, 0.19), (0.0, 0.22))):
K.blob(r, "helmet_worn_%d" % i, (x, y, 1.94), (0.055, 0.05, 0.035),
P["helmet_lt"], subdiv=1, seed=30 + i, ao=0.0)
K.tube(r, "neck", [(0, 0, 1.50), (0, 0, 1.62)], [0.10, 0.105],
P["coverall_sh"], sides=10)
# Torso: pale coverall, harness, radio, patch, belt and pouches.
K.loft(r, "torso", [
{"z": 0.90, "r": (0.27, 0.185), "n": 20},
{"z": 1.00, "r": (0.30, 0.205), "n": 20, "fold": (0.02, 5)},
{"z": 1.14, "r": (0.33, 0.225), "n": 20, "fold": (0.03, 6)},
{"z": 1.26, "r": (0.345, 0.232), "n": 20, "fold": (0.035, 6)},
{"z": 1.36, "r": (0.355, 0.238), "n": 20, "fold": (0.025, 5)},
{"z": 1.45, "r": (0.35, 0.23), "n": 20},
{"z": 1.53, "r": (0.30, 0.20), "n": 20},
{"z": 1.59, "r": (0.25, 0.17), "n": 20},
], P["coverall"], ao=0.10)
K.plate(r, "vest", K.octagon(0.345, 0.23), 1.00, 1.47, 0.94,
P["coverall_dk"], shift=(0, -0.02), ao=0.11)
K.plate(r, "vest_back", K.octagon(0.34, 0.22), 1.04, 1.44, 0.94,
P["coverall_dk"], shift=(0, 0.025), ao=0.11)
for side in (-1, 1):
K.tube(r, "harness_%d" % side,
[(side * 0.32, -0.08, 1.52), (side * 0.22, -0.24, 1.30),
(side * 0.16, -0.22, 1.05)], [0.042, 0.044, 0.040],
P["black"], sides=8)
K.plate(r, "chest_pouch_%d" % side, K.octagon(0.095, 0.06), 1.16, 1.28,
0.92, P["leather"], shift=(side * 0.21, -0.245))
K.plate(r, "belt_pouch_%d" % side, K.octagon(0.095, 0.07), 0.84, 0.97,
0.95, P["leather"], shift=(side * 0.26, -0.18))
patch = K.plate(r, "patch", K.octagon(0.085, 0.085), 0.0, 0.012, 1.0,
P["patch"], ao=0.0)
for v in patch.data.vertices:
v.co = Euler((0, 0, math.pi / 2)).to_matrix() @ v.co + Vector(
(0.42, -0.045, 1.43))
patch.data.update()
mark = K.plate(r, "patch_mark", K.octagon(0.05, 0.05), 0.0, 0.014, 1.0,
P["hiss_red"], ao=0.0)
for v in mark.data.vertices:
v.co = Euler((0, 0, math.pi / 2)).to_matrix() @ v.co + Vector(
(0.43, -0.055, 1.43))
mark.data.update()
K.loft(r, "belt", [
{"z": 0.87, "r": (0.31, 0.21), "n": 16},
{"z": 0.97, "r": (0.32, 0.22), "n": 16},
], P["black"], ao=0.05)
K.plate(r, "buckle", K.octagon(0.055, 0.04), 0.90, 0.95, 1.0,
P["steel_dk"], shift=(0, -0.225), ao=0.0)
K.loft(r, "radio", [
{"z": 1.36, "r": (0.08, 0.06), "n": 10, "c": (0.32, 0.10)},
{"z": 1.55, "r": (0.075, 0.055), "n": 10, "c": (0.32, 0.10)},
], P["black"])
K.tube(r, "antenna", [(0.32, 0.10, 1.55), (0.35, 0.11, 1.86)],
[0.012, 0.007], P["steel_dk"], sides=6)
for z in (1.06, 1.24, 1.42):
K.tube(r, "seam_%g" % z,
[(-0.30, -0.225, z), (0.0, -0.245, z),
(0.30, -0.225, z)], [0.008, 0.008, 0.008],
P["coverall_sh"], sides=5)
# Arms: sleeves, padded elbows, gloves and individual fingers.
for side in (-1, 1):
K.blob(r, "shoulder_%d" % side, (side * 0.40, -0.01, 1.46),
(0.13, 0.14, 0.125), P["coverall_dk"], subdiv=2, seed=40 + side)
K.loft(r, "upperarm_%d" % side, [
{"z": 1.46, "r": (0.112, 0.112), "n": 16, "c": (side * 0.41, -0.02)},
{"z": 1.36, "r": (0.107, 0.107), "n": 16, "c": (side * 0.42, -0.07)},
{"z": 1.26, "r": (0.10, 0.10), "n": 16, "c": (side * 0.425, -0.12)},
{"z": 1.19, "r": (0.094, 0.094), "n": 16, "c": (side * 0.43, -0.17)},
{"z": 1.14, "r": (0.088, 0.088), "n": 16, "c": (side * 0.43, -0.20)},
], P["coverall"], ao=0.06)
K.blob(r, "elbow_%d" % side, (side * 0.43, -0.22, 1.14),
(0.09, 0.095, 0.085), P["leather"], subdiv=2, seed=50 + side)
K.tube(r, "forearm_%d" % side, [
(side * 0.43, -0.23, 1.14), (side * 0.40, -0.36, 1.15),
(side * 0.35, -0.49, 1.16), (side * 0.31, -0.60, 1.17),
], [0.09, 0.084, 0.077, 0.07], P["coverall"], sides=10)
K.ring(r, "cuff_%d" % side, (side * 0.31, -0.60, 1.17), 0.075,
0.012, P["coverall_dk"], segments=12)
K.blob(r, "glove_%d" % side, (side * 0.30, -0.69, 1.17),
(0.085, 0.095, 0.09), P["black"], subdiv=2, seed=60 + side)
for f in range(4):
x = side * (0.27 + f * 0.03)
K.tube(r, "finger_%d_%d" % (side, f),
[(x, -0.74, 1.17), (x, -0.81, 1.16), (x, -0.84, 1.14)],
[0.024, 0.02, 0.016], P["black"], sides=7)
K.tube(r, "thumb_%d" % side,
[(side * 0.265, -0.68, 1.22), (side * 0.23, -0.76, 1.20)],
[0.029, 0.022], P["black"], sides=7)
# Legs: coverall thighs, knee pads, shin, laced boots.
for side in (-1, 1):
K.loft(r, "thigh_%d" % side, [
{"z": 0.88, "r": (0.15, 0.16), "n": 16, "c": (side * 0.16, 0)},
{"z": 0.78, "r": (0.148, 0.158), "n": 16, "c": (side * 0.165, 0)},
{"z": 0.68, "r": (0.14, 0.15), "n": 16, "c": (side * 0.17, 0)},
{"z": 0.58, "r": (0.13, 0.14), "n": 16, "c": (side * 0.175, 0)},
{"z": 0.50, "r": (0.115, 0.125), "n": 16, "c": (side * 0.18, 0)},
], P["coverall"], ao=0.09)
K.blob(r, "knee_%d" % side, (side * 0.18, -0.08, 0.51),
(0.125, 0.09, 0.11), P["leather"], subdiv=2, seed=70 + side)
K.loft(r, "shin_%d" % side, [
{"z": 0.50, "r": (0.115, 0.125), "n": 16, "c": (side * 0.18, 0)},
{"z": 0.40, "r": (0.11, 0.118), "n": 16, "c": (side * 0.185, 0)},
{"z": 0.30, "r": (0.105, 0.11), "n": 16, "c": (side * 0.19, 0)},
{"z": 0.20, "r": (0.098, 0.10), "n": 16, "c": (side * 0.19, 0)},
{"z": 0.13, "r": (0.09, 0.095), "n": 16, "c": (side * 0.19, 0)},
], P["coverall_dk"], ao=0.09)
K.loft(r, "boot_%d" % side, [
{"z": 0.015, "r": (0.15, 0.225), "n": 12,
"c": (side * 0.19, -0.08)},
{"z": 0.09, "r": (0.14, 0.20), "n": 12,
"c": (side * 0.19, -0.06)},
{"z": 0.20, "r": (0.12, 0.14), "n": 12, "c": (side * 0.19, 0)},
], P["black"], ao=0.06)
K.plate(r, "sole_%d" % side, K.octagon(0.155, 0.23), 0.0, 0.025, 0.98,
P["leather"], shift=(side * 0.19, -0.08))
K.tube(r, "thigh_strap_%d" % side,
[(side * 0.06, -0.12, 0.72), (side * 0.18, -0.17, 0.70),
(side * 0.30, -0.10, 0.68)], [0.028, 0.03, 0.026],
P["leather"], sides=6)
for lace in range(4):
K.tube(r, "lace_%d_%d" % (side, lace),
[(side * 0.13, -0.22, 0.05 + lace * 0.035),
(side * 0.25, -0.22, 0.05 + lace * 0.035)],
[0.008, 0.008], P["coverall_sh"], sides=5)
HOLSTER_SIDE = -1
K.plate(r, "holster", K.octagon(0.095, 0.065), 0.65, 0.85, 0.9,
P["black"], shift=(HOLSTER_SIDE * 0.33, 0.10))
K.tube(r, "holster_strap",
[(HOLSTER_SIDE * 0.33, 0.10, 0.85), (HOLSTER_SIDE * 0.20, -0.02, 0.92)],
[0.025, 0.022], P["leather"], sides=6)
# Service Weapon: a clearly boxy rifle carried across the chest. No round
# muzzle bulb — the red rounds are flat bands on a rectangular brake.
def rect(x0, x1, y0, y1):
return [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
K.plate(r, "rifle_receiver", rect(0.05, 0.27, -0.62, -0.12),
1.09, 1.25, 1.0, P["black"], ao=0.06)
K.plate(r, "rifle_rail", rect(0.08, 0.24, -0.58, -0.16),
1.25, 1.27, 1.0, P["steel_dk"], ao=0.0)
K.plate(r, "rifle_stock", rect(0.04, 0.27, -0.14, 0.06),
1.12, 1.23, 0.94, P["leather"], ao=0.06)
K.plate(r, "rifle_grip", rect(0.10, 0.21, -0.29, -0.17),
0.92, 1.12, 0.86, P["black"], ao=0.06)
K.plate(r, "rifle_magazine", rect(0.09, 0.22, -0.52, -0.32),
0.84, 1.10, 0.92, P["steel_dk"], ao=0.06)
K.plate(r, "rifle_foregrip", rect(0.09, 0.21, -0.76, -0.62),
0.97, 1.12, 0.9, P["black"], ao=0.06)
K.plate(r, "rifle_barrel", rect(0.12, 0.20, -0.98, -0.62),
1.15, 1.20, 1.0, P["steel_dk"], ao=0.0)
K.plate(r, "rifle_brake", rect(0.11, 0.21, -1.06, -0.98),
1.14, 1.21, 1.0, P["steel_dk"], ao=0.0)
for i, y in enumerate((-1.02, -1.05)):
K.plate(r, "rifle_red_round_%d" % i, rect(0.115, 0.205, y - 0.01, y + 0.01),
1.135, 1.215, 1.0, P["hiss_red"], ao=0.0)
K.plate(r, "rifle_sight", rect(0.11, 0.19, -0.35, -0.25),
1.27, 1.34, 1.0, P["black"], ao=0.0)
K.plate(r, "rifle_sight_lens", rect(0.125, 0.175, -0.33, -0.27),
1.34, 1.345, 1.0, P["teal"], ao=0.0)
for i in range(4):
K.plate(r, "rifle_vent_%d" % i, rect(0.27, 0.272, -0.22 - i * 0.075,
-0.20 - i * 0.075),
1.14, 1.20, 1.0, P["steel_dk"], ao=0.0)
# Angle the whole weapon across the body so it reads as a carried rifle
# instead of a centred cylinder, and move the weapon bone with it.
pivot = Vector((0.16, -0.15, 1.17))
turn = Euler((0.0, 0.0, -math.radians(32))).to_matrix()
for obj in list(r.children):
if obj.type == "MESH" and obj.name.lower().startswith("rifle"):
for v in obj.data.vertices:
v.co = pivot + turn @ (v.co - pivot)
obj.data.update()
for i, (x, y, z, tilt) in enumerate([
(0.12, 0.22, 1.40, (0.3, 0.2, 0.4)),
(-0.10, 0.23, 1.24, (-0.4, 0.1, -0.3)),
(0.05, 0.24, 1.10, (0.2, -0.3, 0.1)),
]):
K.crystal(r, "taint_%d" % i, (x, y, z), 0.13, P["hiss_red"], sides=5,
seed=70 + i, tilt=tilt)
# The yellow selection ring stays planted at the feet (weighted to root).
K.ring(r, "base", (0, 0, 0.055), 0.50, 0.035, P["board"])
K.plate(r, "base_disc", K.octagon(0.46), 0.0, 0.025, 1.0, P["black"])
return r
# ------------------------------------------------------------------- rigging
BONES = [
("root", (0, 0, 0.0), (0, 0, 0.20), None),
("hips", (0, 0, 0.90), (0, 0, 1.04), "root"),
("spine", (0, 0, 1.04), (0, 0, 1.26), "hips"),
("chest", (0, 0, 1.26), (0, 0, 1.48), "spine"),
("neck", (0, 0, 1.50), (0, 0, 1.62), "chest"),
("head", (0, 0, 1.62), (0, 0, 1.98), "neck"),
("weapon", (0.15, -0.25, 1.17), (-0.20, -0.75, 1.17), "chest"),
]
for SIDE, SIGN in (("L", -1), ("R", 1)):
BONES += [
("upperarm." + SIDE, (SIGN * 0.43, 0, 1.46),
(SIGN * 0.50, -0.04, 1.09), "chest"),
("forearm." + SIDE, (SIGN * 0.50, -0.04, 1.09),
(SIGN * 0.54, -0.08, 0.78), "upperarm." + SIDE),
("hand." + SIDE, (SIGN * 0.54, -0.08, 0.78),
(SIGN * 0.54, -0.20, 0.62), "forearm." + SIDE),
("thigh." + SIDE, (SIGN * 0.17, 0, 0.88),
(SIGN * 0.18, 0, 0.51), "hips"),
("shin." + SIDE, (SIGN * 0.18, 0, 0.51),
(SIGN * 0.19, 0, 0.13), "thigh." + SIDE),
("foot." + SIDE, (SIGN * 0.19, 0, 0.13),
(SIGN * 0.19, -0.20, 0.04), "shin." + SIDE),
]
def build_rig():
data = bpy.data.armatures.new("AgentRig")
arm = bpy.data.objects.new("AgentRig", data)
bpy.context.scene.collection.objects.link(arm)
bpy.ops.object.select_all(action="DESELECT")
arm.select_set(True)
bpy.context.view_layer.objects.active = arm
bpy.ops.object.mode_set(mode="EDIT")
for name, head, tail, parent in BONES:
b = data.edit_bones.new(name)
b.head, b.tail = head, tail
if parent:
b.parent = data.edit_bones[parent]
bpy.ops.object.mode_set(mode="OBJECT")
return arm
def side_of(name, obj):
m = re.search(r"(?:^|_)(-1|1)(?=_|$)", name)
if m:
return "R" if m.group(1) == "1" else "L"
xs = [v.co.x for v in obj.data.vertices]
return "R" if (sum(xs) / max(len(xs), 1)) > 0 else "L"
def group_for(name, obj):
n = name.lower()
s = side_of(n, obj)
if "base" in n:
return "root"
if any(k in n for k in ("helmet", "visor", "mask", "head", "hose", "filter",
"lens", "glass", "chin", "neck")):
return "head"
if "rifle" in n or "weapon" in n or "muzzle" in n or "magazine" in n:
return "weapon"
if any(k in n for k in ("upperarm", "shoulder", "patch")):
return "upperarm." + s
if any(k in n for k in ("forearm", "elbow", "cuff")):
return "forearm." + s
if any(k in n for k in ("glove", "finger", "thumb", "hand")):
return "hand." + s
if any(k in n for k in ("thigh", "knee")):
return "thigh." + s
if any(k in n for k in ("shin", "boot", "sole", "lace")):
return "shin." + s
zs = [v.co.z for v in obj.data.vertices]
return "chest" if (sum(zs) / max(len(zs), 1)) > 1.14 else "hips"
def assign_groups(root):
for obj in list(root.children):
if obj.type != "MESH":
continue
primary = group_for(obj.name, obj)
groups = {primary: []}
for v in obj.data.vertices:
z = v.co.z
if primary == "upperarm." + side_of(obj.name, obj) and 1.06 < z < 1.18:
groups[primary].append((v.index, (1.18 - z) / 0.12))
groups.setdefault("forearm." + side_of(obj.name, obj), []).append(
(v.index, (z - 1.06) / 0.12))
elif primary == "forearm." + side_of(obj.name, obj) and 1.06 < z < 1.18:
groups[primary].append((v.index, (z - 1.06) / 0.12))
groups.setdefault("upperarm." + side_of(obj.name, obj), []).append(
(v.index, (1.18 - z) / 0.12))
elif primary == "thigh." + side_of(obj.name, obj) and 0.45 < z < 0.57:
groups[primary].append((v.index, (z - 0.45) / 0.12))
groups.setdefault("shin." + side_of(obj.name, obj), []).append(
(v.index, (0.57 - z) / 0.12))
elif primary == "shin." + side_of(obj.name, obj) and 0.45 < z < 0.57:
groups[primary].append((v.index, (0.57 - z) / 0.12))
groups.setdefault("thigh." + side_of(obj.name, obj), []).append(
(v.index, (z - 0.45) / 0.12))
else:
groups[primary].append((v.index, 1.0))
for bone, entries in groups.items():
vg = obj.vertex_groups.get(bone) or obj.vertex_groups.new(name=bone)
for index, weight in entries:
if weight > 1e-4:
vg.add([index], min(1.0, weight), "REPLACE")
# ----------------------------------------------------------------- animation
def reset_pose(bones):
for pb in bones:
pb.rotation_mode = "XYZ"
pb.rotation_euler = (0.0, 0.0, 0.0)
pb.location = (0.0, 0.0, 0.0)
def pose_idle(pb, t):
breath = math.sin(t * 2.0 * math.pi)
pb["chest"].rotation_euler.x = 0.015 + 0.018 * breath
pb["head"].rotation_euler.z = 0.025 * math.sin(t * math.pi)
pb["upperarm.L"].rotation_euler.x = -0.03
pb["upperarm.R"].rotation_euler.x = 0.03
def pose_run(pb, t):
phase = t * 2.0 * math.pi
s = math.sin(phase)
pb["root"].location.z = 0.035 * abs(math.sin(2.0 * phase))
pb["chest"].rotation_euler.x = 0.20
pb["head"].rotation_euler.x = -0.10
pb["thigh.L"].rotation_euler.x = 0.62 * s
pb["thigh.R"].rotation_euler.x = -0.62 * s
pb["shin.L"].rotation_euler.x = 0.55 * max(0.0, -s)
pb["shin.R"].rotation_euler.x = 0.55 * max(0.0, s)
pb["upperarm.L"].rotation_euler.x = -0.28 * s
pb["upperarm.R"].rotation_euler.x = 0.18 * s
pb["forearm.L"].rotation_euler.x = 0.35
pb["forearm.R"].rotation_euler.x = 0.42
def pose_dash(pb, t):
pb["root"].location.z = 0.02
pb["chest"].rotation_euler.x = 0.42
pb["head"].rotation_euler.x = -0.18
pb["thigh.L"].rotation_euler.x = 0.80
pb["thigh.R"].rotation_euler.x = -0.55
pb["shin.L"].rotation_euler.x = 0.30
pb["upperarm.L"].rotation_euler.x = -0.55
pb["upperarm.R"].rotation_euler.x = -0.35
pb["forearm.L"].rotation_euler.x = 0.30
pb["forearm.R"].rotation_euler.x = 0.30
def pose_fire(pb, t):
kick = math.exp(-9.0 * t)
pb["weapon"].rotation_euler.x = -0.22 * kick
pb["chest"].rotation_euler.x = 0.06 * kick
pb["upperarm.R"].rotation_euler.x = 0.16 * kick
pb["forearm.R"].rotation_euler.x = 0.20 * kick
pb["head"].rotation_euler.x = -0.05 * kick
def pose_stagger(pb, t):
fade = 1.0 - t
pb["chest"].rotation_euler.x = -0.30 * fade
pb["head"].rotation_euler.x = -0.35 * fade
pb["upperarm.L"].rotation_euler.x = -0.35 * fade
pb["upperarm.R"].rotation_euler.x = 0.30 * fade
def make_actions(arm):
specs = [
("Idle", 1, 48, pose_idle),
("Run", 1, 36, pose_run),
("Dash", 1, 18, pose_dash),
("Fire", 1, 16, pose_fire),
("Stagger", 1, 20, pose_stagger),
]
arm.animation_data_create()
made = []
for name, start, end, fn in specs:
act = bpy.data.actions.new(name)
act.use_fake_user = True # keep unassigned clips in the saved .blend
act.use_frame_range = True
act.frame_start, act.frame_end = start, end
arm.animation_data.action = act
for frame in range(start, end + 1):
reset_pose(arm.pose.bones)
fn(arm.pose.bones, (frame - start) / max(1, end - start))
for pb in arm.pose.bones:
pb.keyframe_insert(data_path="rotation_euler", frame=frame,
group=name)
if pb.name == "root":
pb.keyframe_insert(data_path="location", frame=frame,
group=name)
made.append((name, act))
print("[anim] %-8s frames %d-%d" % (name, start, end))
arm.animation_data.action = made[0][1]
return dict(made)
# ------------------------------------------------------------------- build it
root = build_agent()
arm = build_rig()
assign_groups(root)
mesh = K.merge_actor(root, "agent")
tris = K.triangle_count(root)
print("[budget] agent %d tris (cap 12000) %s"
% (tris, "OK" if tris <= 12000 else "OVER"))
assert tris <= 12000, "agent over triangle budget"
mod = mesh.modifiers.new("AgentSkin", "ARMATURE")
mod.object = arm
mesh.parent = arm
actions = make_actions(arm)
out = os.path.join(MODEL_DIR, "agent.glb")
bpy.ops.object.select_all(action="DESELECT")
arm.select_set(True)
mesh.select_set(True)
bpy.context.view_layer.objects.active = arm
bpy.ops.export_scene.gltf(
filepath=out,
export_format="GLB",
use_selection=True,
export_apply=False,
export_yup=True,
export_materials="EXPORT",
export_skins=True,
export_animations=True,
export_animation_mode="ACTIONS",
export_bake_animation=True,
export_frame_range=True,
export_cameras=False,
export_lights=False,
export_extras=False,
)
print("[export] %s %d bytes, %d bones, %d actions"
% (out, os.path.getsize(out), len(BONES), len(actions)))
# Showcase: the rig in its idle pose, plus a tight detail camera.
scene = bpy.context.scene
arm.animation_data.action = actions["Idle"]
scene.frame_set(12)
world = bpy.data.worlds.new("AgentWorld")
scene.world = world
world.use_nodes = True
bg = next(n for n in world.node_tree.nodes if n.type == "BACKGROUND")
bg.inputs["Color"].default_value = (*K.rgb("#101a16"), 1.0)
bg.inputs["Strength"].default_value = 0.8
for name, loc, power, col in (
("Key", (-3, -7, 6), 2400, "#e8f0e2"),
("Rim", (3, 4, 5), 1400, "#4ec9b0"),
("VisorFill", (0, -4, 2.2), 700, "#ff4a12"),
):
data = bpy.data.lights.new(name, "AREA")
data.energy = power
data.shape = "DISK"
data.size = 6
data.color = K.rgb(col)
obj = bpy.data.objects.new(name, data)
scene.collection.objects.link(obj)
obj.location = loc
obj.rotation_euler = (Vector((0, 0, 1.1)) - Vector(loc)).to_track_quat(
"-Z", "Y").to_euler()
for name, loc, lens, target in (
("AgentCam", (0.2, -6.5, 3.6), 44, (0, 0, 1.1)),
("AgentDetail", (-1.3, -3.0, 2.3), 50, (0, -0.05, 1.55)),
):
data = bpy.data.cameras.new(name)
cam = bpy.data.objects.new(name, data)
scene.collection.objects.link(cam)
cam.location = loc
cam.rotation_euler = (Vector(target) - Vector(loc)).to_track_quat(
"-Z", "Y").to_euler()
data.lens = lens
if name == "AgentCam":
scene.camera = cam
engines = [i.identifier for i in bpy.types.RenderSettings.bl_rna.properties[
"engine"].enum_items]
scene.render.engine = "BLENDER_EEVEE_NEXT" if "BLENDER_EEVEE_NEXT" in engines else engines[0]
scene.render.resolution_x = 1000
scene.render.resolution_y = 1100
scene.render.image_settings.file_format = "PNG"
scene.render.filepath = os.path.join(CONCEPT_DIR, "agent_reference.png")
bpy.ops.render.render(write_still=True)
scene.camera = bpy.data.objects["AgentDetail"]
scene.render.filepath = os.path.join(CONCEPT_DIR, "agent_detail.png")
bpy.ops.render.render(write_still=True)
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(BLEND_DIR, "fbc_agent.blend"))
print("[done] agent rig + animations exported")