Zweiter Durchlauf für Geometrie und Outfit: Referenzbild per Tripo-Image-Edit auf ein petrolfarbenes Kürkleid umgestellt (20 Credits), daraus ein Ultra-Image→3D (geometry_quality=detailed, 50 Credits). - v008: 59.488 Dreiecke, 1,7 m, 222 Control-/30 Deform-Bones, Glide unverändert per Rotations-Retargeting - Lider, Ohren und Haaransatz deutlich besser als v007; Finger bleiben verschmolzen - Manifest-Eintrag auf Version 2 (v008), Skater3D-Fallback und check_export.sh nachgezogen - tools/blender/build_skater.py: Rig, Retarget und Export in einem Befehl, reproduziert die manuelle GLB byte-identisch - Lizenz-Log um Bild-Edit und Ultra-Lauf ergänzt, Doku und AGENTS.md aktualisiert
350 lines
12 KiB
Python
350 lines
12 KiB
Python
"""Baut aus einem Tripo-GLB einen geriggten Spielcharakter mit Glide-Clip.
|
||
|
||
Ein Befehl, zwei Blender-Läufe:
|
||
|
||
blender --python tools/blender/build_skater.py -- \
|
||
/pfad/model.glb /pfad/ausgabe.glb \
|
||
--source-rig rinkplanner/assets/external/characters/skater_figure_glide_v006.glb
|
||
|
||
AutoRigPro braucht eine GUI (es greift auf bpy.context.space_data zu) und
|
||
läuft deshalb nur im Vordergrund-Lauf. Das Retargeten und der glTF-Export
|
||
laufen danach in einem zweiten, headless Prozess: der glTF-Importer verträgt
|
||
sich nicht mit einer zweiten, gleichnamigen Armatur in derselben Datei, die
|
||
Trennung ist der zuverlässige Weg.
|
||
|
||
Ablauf:
|
||
1. Mesh auf 1,70 m skalieren, Füße auf Z=0, Blick -Y (AutoRigPro-Vorne).
|
||
2. AutoRigPro-Smart-Modus, Marker setzen, Rig erkennen, Match to Rig, Bind.
|
||
3. Zwischenstand als .blend speichern.
|
||
4. Headless: Quell-Rig importieren, Clip per Rotations-Retargeting
|
||
übertragen (keine Translationen, sonst wandert die Figur), Export mit
|
||
ausschließlich den gewichteten Deform-Bones.
|
||
|
||
Marker-Kalibrierung: die Vorgaben sind für eine 1,70-m-Figur in A-Pose
|
||
gemessen und werden mit der tatsächlichen Höhe skaliert. Weicht die Pose stark
|
||
ab, eine eigene Marker-Datei übergeben:
|
||
|
||
{"root": [0.0, 0.0, 0.95], "neck": [...], ...}
|
||
|
||
Schlüssel: root, neck, chin, head_tip, shoulder, elbow, hand, hand_tip,
|
||
thigh, knee, foot. Koordinaten in Metern, X = links, -Y = vorn, Z = hoch.
|
||
"""
|
||
|
||
import argparse
|
||
import json
|
||
import math
|
||
import subprocess
|
||
import sys
|
||
import traceback
|
||
|
||
import bpy
|
||
from mathutils import Matrix
|
||
|
||
REFERENCE_HEIGHT = 1.70
|
||
|
||
DEFAULT_MARKERS = {
|
||
"root": (0.000, 0.000, 0.950),
|
||
"neck": (0.000, 0.000, 1.500),
|
||
"chin": (0.000, -0.090, 1.560),
|
||
"head_tip": (0.000, 0.000, 1.700),
|
||
"shoulder": (0.155, 0.020, 1.370),
|
||
"elbow": (0.210, 0.010, 1.100),
|
||
"hand": (0.300, -0.060, 0.860),
|
||
"hand_tip": (0.340, -0.100, 0.760),
|
||
"thigh": (0.090, 0.000, 0.920),
|
||
"knee": (0.100, 0.000, 0.470),
|
||
"foot": (0.100, 0.000, 0.090),
|
||
}
|
||
|
||
# Quell-Bone -> Ziel-Bone. Beide Rigs stammen aus AutoRigPro und teilen die
|
||
# Namen; die Twist-Helfer der Produktionsfigur werden mitgenommen.
|
||
BONE_MAP = {
|
||
"root.x": "root.x",
|
||
"spine_01.x": "spine_01.x",
|
||
"spine_02.x": "spine_02.x",
|
||
"spine_03.x": "spine_03.x",
|
||
"neck.x": "neck.x",
|
||
"head.x": "head.x",
|
||
"shoulder.l": "shoulder.l",
|
||
"shoulder.r": "shoulder.r",
|
||
"arm_stretch.l": "arm_stretch.l",
|
||
"arm_stretch.r": "arm_stretch.r",
|
||
"forearm_stretch.l": "forearm_stretch.l",
|
||
"forearm_stretch.r": "forearm_stretch.r",
|
||
"hand.l": "hand.l",
|
||
"hand.r": "hand.r",
|
||
"thigh_stretch.l": "thigh_stretch.l",
|
||
"thigh_stretch.r": "thigh_stretch.r",
|
||
"leg_stretch.l": "leg_stretch.l",
|
||
"leg_stretch.r": "leg_stretch.r",
|
||
"foot.l": "foot.l",
|
||
"foot.r": "foot.r",
|
||
"toes_01.l": "toes_01.l",
|
||
"toes_01.r": "toes_01.r",
|
||
"thigh_twist.l": "thigh_twist.l",
|
||
"thigh_twist.r": "thigh_twist.r",
|
||
"leg_twist.l": "leg_twist.l",
|
||
"leg_twist.r": "leg_twist.r",
|
||
"forearm_twist.l": "forearm_twist.l",
|
||
"forearm_twist.r": "forearm_twist.r",
|
||
}
|
||
|
||
|
||
def parse_args():
|
||
argv = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []
|
||
parser = argparse.ArgumentParser(prog="build_skater.py")
|
||
parser.add_argument("input", help="GLB mit dem Rohmesh")
|
||
parser.add_argument("output", help="Ziel-GLB")
|
||
parser.add_argument("--source-rig", required=True,
|
||
help="GLB mit Skelett und dem zu übernehmenden Clip")
|
||
parser.add_argument("--clip", default="Glide")
|
||
parser.add_argument("--height", type=float, default=REFERENCE_HEIGHT)
|
||
parser.add_argument("--markers", help="JSON mit Marker-Positionen")
|
||
parser.add_argument("--work-dir", default="/tmp/opencode/build_skater")
|
||
parser.add_argument("--phase", choices=["auto", "rig", "retarget"], default="auto")
|
||
parser.add_argument("--blend", help="Zwischenstand, den die Rig-Phase schreibt")
|
||
return parser.parse_args(argv)
|
||
|
||
|
||
def context_override():
|
||
win = bpy.context.window_manager.windows[0]
|
||
area = next(a for a in win.screen.areas if a.type == "VIEW_3D")
|
||
region = next(r for r in area.regions if r.type == "WINDOW")
|
||
return dict(window=win, screen=win.screen, area=area, region=region)
|
||
|
||
|
||
def markers_for(height, path):
|
||
if path:
|
||
with open(path, "r", encoding="utf-8") as handle:
|
||
data = json.load(handle)
|
||
return {key: tuple(float(v) for v in value) for key, value in data.items()}
|
||
factor = height / REFERENCE_HEIGHT
|
||
return {key: tuple(component * factor for component in value)
|
||
for key, value in DEFAULT_MARKERS.items()}
|
||
|
||
|
||
def fit_mesh(path, height):
|
||
bpy.ops.wm.read_homefile(use_empty=True)
|
||
bpy.ops.import_scene.gltf(filepath=path)
|
||
mesh = next(o for o in bpy.data.objects if o.type == "MESH")
|
||
|
||
mesh.data.transform(mesh.matrix_world)
|
||
mesh.matrix_world = Matrix.Identity(4)
|
||
mesh.data.transform(Matrix.Rotation(-math.pi / 2, 4, "Z"))
|
||
|
||
coords = [v.co for v in mesh.data.vertices]
|
||
low = min(c.z for c in coords)
|
||
high = max(c.z for c in coords)
|
||
mesh.data.transform(Matrix.Scale(height / (high - low), 4))
|
||
|
||
coords = [v.co for v in mesh.data.vertices]
|
||
low_x = min(c.x for c in coords)
|
||
high_x = max(c.x for c in coords)
|
||
low_y = min(c.y for c in coords)
|
||
high_y = max(c.y for c in coords)
|
||
low_z = min(c.z for c in coords)
|
||
mesh.data.transform(Matrix.Translation(
|
||
(-(low_x + high_x) / 2, -(low_y + high_y) / 2, -low_z)))
|
||
|
||
width = high_x - low_x
|
||
depth = high_y - low_y
|
||
if width < depth:
|
||
print("WARNUNG: Mesh ist breiter in Y als in X – Blickrichtung prüfen "
|
||
"(AutoRigPro erwartet -Y).", file=sys.stderr)
|
||
print(f"MESH: {width:.3f} breit, {depth:.3f} tief, {height:.3f} hoch")
|
||
return mesh
|
||
|
||
|
||
def build_rig(mesh, markers):
|
||
scene = bpy.context.scene
|
||
bpy.ops.object.select_all(action="DESELECT")
|
||
mesh.select_set(True)
|
||
bpy.context.view_layer.objects.active = mesh
|
||
|
||
with bpy.context.temp_override(**context_override()):
|
||
bpy.ops.id.get_selected_objects("EXEC_DEFAULT")
|
||
|
||
scene.arp_smart_sym = True
|
||
scene.arp_fingers_enable = False
|
||
scene.arp_bind_engine = "PSEUDO_VOXELS"
|
||
|
||
with bpy.context.temp_override(**context_override()):
|
||
for name in markers:
|
||
bpy.ops.id.add_marker("EXEC_DEFAULT", body_part=name)
|
||
|
||
for name, location in markers.items():
|
||
marker = bpy.data.objects.get(name + "_loc")
|
||
if marker is None:
|
||
raise RuntimeError(f"Marker {name} wurde nicht angelegt")
|
||
marker.location = location
|
||
|
||
with bpy.context.temp_override(**context_override()):
|
||
bpy.ops.id.go_detect("EXEC_DEFAULT")
|
||
|
||
rig = bpy.data.objects.get("rig")
|
||
if rig is None:
|
||
raise RuntimeError("AutoRigPro hat kein Rig erzeugt – Marker prüfen")
|
||
|
||
with bpy.context.temp_override(**context_override()):
|
||
bpy.ops.arp.match_to_rig("EXEC_DEFAULT")
|
||
|
||
bpy.ops.object.select_all(action="DESELECT")
|
||
mesh.select_set(True)
|
||
rig.select_set(True)
|
||
bpy.context.view_layer.objects.active = rig
|
||
with bpy.context.temp_override(**context_override()):
|
||
bpy.ops.arp.bind_to_rig("EXEC_DEFAULT")
|
||
|
||
weighted = {group.name for group in mesh.vertex_groups}
|
||
print(f"RIG: {len(rig.data.bones)} Bones, {len(weighted)} gewichtete Gruppen")
|
||
return rig, weighted
|
||
|
||
|
||
def hierarchy_order(rig):
|
||
order = []
|
||
|
||
def walk(bone, depth):
|
||
order.append((depth, bone.name))
|
||
for child in bone.children:
|
||
walk(child, depth + 1)
|
||
|
||
for bone in rig.data.bones:
|
||
if bone.parent is None:
|
||
walk(bone, 0)
|
||
return [name for _, name in order]
|
||
|
||
|
||
def phase_rig(args):
|
||
markers = markers_for(args.height, args.markers)
|
||
mesh = fit_mesh(args.input, args.height)
|
||
build_rig(mesh, markers)
|
||
bpy.ops.wm.save_as_mainfile(filepath=args.blend)
|
||
print(f"BLEND: {args.blend}")
|
||
|
||
|
||
def phase_retarget(args):
|
||
bpy.ops.wm.open_mainfile(filepath=args.blend)
|
||
rig = bpy.data.objects["rig"]
|
||
mesh = next(o for o in bpy.data.objects
|
||
if o.type == "MESH" and 'tripo' in o.name.lower())
|
||
weighted = {group.name for group in mesh.vertex_groups}
|
||
|
||
bpy.ops.import_scene.gltf(filepath=args.source_rig)
|
||
source = next(o for o in bpy.data.objects if o.type == "ARMATURE" and o is not rig)
|
||
action = bpy.data.actions.get(args.clip)
|
||
if action is None:
|
||
raise RuntimeError(f"Clip {args.clip} fehlt im Quell-GLB")
|
||
source.animation_data.action = action
|
||
start, end = int(action.frame_range[0]), int(action.frame_range[1])
|
||
|
||
rig.animation_data_create()
|
||
baked = bpy.data.actions.new(args.clip)
|
||
rig.animation_data.action = baked
|
||
|
||
order = [n for n in hierarchy_order(rig) if n in weighted and n in BONE_MAP.values()]
|
||
for name in order:
|
||
back = next(src for src, dst in BONE_MAP.items() if dst == name)
|
||
if source.pose.bones.get(back) is None:
|
||
raise RuntimeError(f"Quell-Bone {back} fehlt im Quell-Rig")
|
||
|
||
scene = bpy.context.scene
|
||
for frame in range(start, end + 1):
|
||
scene.frame_set(frame)
|
||
bpy.context.view_layer.update()
|
||
|
||
world = {}
|
||
for name in order:
|
||
src_name = next(src for src, dst in BONE_MAP.items() if dst == name)
|
||
rest_src = source.data.bones[src_name].matrix_local.to_3x3()
|
||
rest_tgt = rig.data.bones[name].matrix_local.to_3x3()
|
||
delta = source.pose.bones[src_name].matrix.to_3x3() @ rest_src.inverted()
|
||
world[name] = (delta @ rest_tgt).to_quaternion()
|
||
|
||
posed_parent = {}
|
||
for name in order:
|
||
bone = rig.data.bones[name]
|
||
rest_tgt = bone.matrix_local.to_3x3()
|
||
parent = bone.parent
|
||
parent_rot = (posed_parent[parent.name] if parent and parent.name in posed_parent
|
||
else (parent.matrix_local.to_3x3() if parent else Matrix.Identity(3)))
|
||
rest_local = (parent.matrix_local.to_3x3().inverted() @ rest_tgt) if parent else rest_tgt
|
||
basis = rest_local.inverted() @ parent_rot.inverted() @ world[name].to_matrix()
|
||
pose_bone = rig.pose.bones[name]
|
||
pose_bone.rotation_mode = "QUATERNION"
|
||
pose_bone.rotation_quaternion = basis.to_quaternion()
|
||
pose_bone.keyframe_insert("rotation_quaternion", frame=frame)
|
||
posed_parent[name] = world[name].to_matrix()
|
||
|
||
scene.frame_start = start
|
||
scene.frame_end = end
|
||
print(f"CLIP: {args.clip}, Frames {start}-{end}, {len(order)} Bones")
|
||
|
||
for obj in list(bpy.data.objects):
|
||
if obj.type == "ARMATURE" and obj is not rig:
|
||
for child in list(obj.children):
|
||
bpy.data.objects.remove(child, do_unlink=True)
|
||
bpy.data.objects.remove(obj, do_unlink=True)
|
||
for obj in list(bpy.data.objects):
|
||
if obj.type == "MESH" and obj is not mesh and not obj.name.startswith("cs_"):
|
||
bpy.data.objects.remove(obj, do_unlink=True)
|
||
|
||
for bone in rig.data.bones:
|
||
bone.use_deform = bone.name in weighted
|
||
|
||
for other in list(bpy.data.actions):
|
||
if other is not baked:
|
||
bpy.data.actions.remove(other)
|
||
baked.name = args.clip
|
||
baked.use_fake_user = True
|
||
|
||
bpy.ops.object.select_all(action="DESELECT")
|
||
mesh.select_set(True)
|
||
rig.select_set(True)
|
||
bpy.context.view_layer.objects.active = rig
|
||
|
||
bpy.ops.export_scene.gltf(
|
||
filepath=args.output,
|
||
export_format="GLB",
|
||
use_selection=True,
|
||
export_animations=True,
|
||
export_frame_range=True,
|
||
export_def_bones=True,
|
||
export_anim_single_armature=True,
|
||
export_apply=True,
|
||
export_yup=True,
|
||
)
|
||
print(f"EXPORT: {args.output}")
|
||
|
||
|
||
def run_gui_phase():
|
||
args = parse_args()
|
||
args.blend = args.blend or args.work_dir.rstrip("/") + "/build_skater.blend"
|
||
try:
|
||
phase_rig(args)
|
||
subprocess.run(
|
||
[bpy.app.binary_path, "-b", "--python", __file__, "--",
|
||
args.input, args.output, "--source-rig", args.source_rig,
|
||
"--clip", args.clip, "--phase", "retarget", "--blend", args.blend],
|
||
check=True,
|
||
)
|
||
except Exception:
|
||
traceback.print_exc()
|
||
finally:
|
||
bpy.ops.wm.quit_blender()
|
||
|
||
|
||
def main():
|
||
args = parse_args()
|
||
if args.phase == "retarget":
|
||
args.blend = args.blend or args.work_dir.rstrip("/") + "/build_skater.blend"
|
||
phase_retarget(args)
|
||
return
|
||
if bpy.app.background:
|
||
raise SystemExit(
|
||
"AutoRigPro braucht eine GUI. Aufruf ohne -b, mit DISPLAY:\n"
|
||
" blender --python tools/blender/build_skater.py -- <in.glb> <out.glb> --source-rig <rig.glb>"
|
||
)
|
||
bpy.app.timers.register(run_gui_phase, first_interval=0.5)
|
||
|
||
|
||
main()
|