diff --git a/design/figure-preview/README.md b/design/figure-preview/README.md index a6a5feedb..3dafb7487 100644 --- a/design/figure-preview/README.md +++ b/design/figure-preview/README.md @@ -60,10 +60,13 @@ For captures without a window, create the output directory before running a foot through the floor, a joint changing speed too suddenly or moving too fast, a knee bent backwards, a hand through the body or an abrupt camera change. It exits nonzero when it finds any. -- `--shot DIR PRACTICE ITEM SECONDS... [man]` writes chosen moments, and +- `--shot DIR PRACTICE ITEM SECONDS... [man] [turn=DEGREES]` writes chosen + moments with an optional camera orbit, and `--video DIR PRACTICE ITEM FROM TO [man]` thirty frames a second of a stretch, hair swinging; `--trace PRACTICE ITEM FROM TO` prints the right arm's joints and hand frame 120 times a second. +- `--rig-pose PRACTICE ITEM SECONDS [man]` prints the root transform and the + model's bind and posed humanoid bone transforms as tab-separated rows. ## Known limits @@ -72,6 +75,24 @@ For captures without a window, create the output directory before running ## Motion review +Use Blender to inspect the VRM rig at an exact preview pose. The following +example opens the man's Workout high-knee pose at 0.4 seconds and produces +three orthographic renders and a packed Blender file. Run from this folder +with `ziran` and `blender` on `PATH`. + +```sh +practices=$(ziran pkg path practices --locked) +out=build/blender-review +mkdir -p "$out" +./build/app/figure_preview --rig-pose 2 8 0.4 man > "$out/pose.tsv" +blender -b --python-exit-code 1 --python blender_pose_review.py -- \ + "$practices/shared/figure/man.vrm" "$out/pose.tsv" "$out" --save-blend +``` + +The importer applies the exported bone transforms to the GLB bind rig and +approximates native skinning. Use `--shot` to review native cloth draping, +hair springs, and slipper calibration. + `review.py` writes resumable before/after videos for the four practices and a contact sheet with both figures under `build/practice-motion-review/`. It also renders all twelve Workout exercises at three moments each and every diff --git a/design/figure-preview/blender_pose_review.py b/design/figure-preview/blender_pose_review.py new file mode 100644 index 000000000..580ff81d6 --- /dev/null +++ b/design/figure-preview/blender_pose_review.py @@ -0,0 +1,143 @@ +"""Inspect the selected VRM's bind rig at a pose exported by --rig-pose.""" + +import argparse +import csv +from math import atan2, cos, degrees, pi, sin +from pathlib import Path +import shutil +import sys +from tempfile import TemporaryDirectory + +import bpy +from mathutils import Matrix, Quaternion, Vector + + +GLTF_TO_BLENDER = Matrix.Rotation(pi / 2, 4, 'X') + + +def read_pose(path): + with path.open(newline='') as stream: + rows = csv.reader(stream, delimiter='\t') + label, x, y, z, yaw, scale = next(rows) + assert label == 'root' + root = (Matrix.Translation((float(x), float(y), float(z))) @ + Matrix.Rotation(float(yaw), 4, 'Y') @ Matrix.Scale(float(scale), 4)) + bones = {} + for label, name, *raw in rows: + assert label == 'bone' and len(raw) == 19 and name not in bones + values = list(map(float, raw)) + x, y, z, qx, qy, qz, qw = values[:7] + rest = Matrix.Translation((x, y, z)) @ Quaternion((qw, qx, qy, qz)).to_matrix().to_4x4() + matrix_rows = [values[start:start + 4] for start in (7, 11, 15)] + posed = Matrix((*matrix_rows, (0, 0, 0, 1))) + bones[name] = rest, posed + return root, bones + + +def inspect_joint_motion(bones): + for side in ('L', 'R'): + def joint(role): + return bones[f'J_Bip_{side}_{role}'] + + chest_rest, chest_pose = bones['J_Bip_C_UpperChest'] + shoulder_rest, shoulder_pose = joint('Shoulder') + chest = chest_pose @ chest_rest.inverted() + shoulder = shoulder_pose @ shoulder_rest.inverted() + collar_motion = degrees(chest.to_quaternion().rotation_difference(shoulder.to_quaternion()).angle) + elbow_rest, elbow_pose = joint('LowerArm') + hand_rest, hand_pose = joint('Hand') + thumb_rest, _ = joint('Thumb1') + finger_rest, _ = joint('Middle1') + palm = (thumb_rest.translation - hand_rest.translation).cross( + finger_rest.translation - hand_rest.translation).normalized() + axis = (hand_pose.translation - elbow_pose.translation).normalized() + + def across(turn): + rotated = turn.to_3x3() @ palm + return (rotated - axis * rotated.dot(axis)).normalized() + + forearm = across(elbow_pose @ elbow_rest.inverted()) + hand = across(hand_pose @ hand_rest.inverted()) + roll = degrees(atan2(axis.dot(forearm.cross(hand)), forearm.dot(hand))) + print(f'{side} clavicle/chest change {min(collar_motion, 360 - collar_motion):.1f}°, wrist axial change {roll:.1f}°') + + +def apply_pose(armature, bones): + rest = {bone.name: bone.bone.matrix_local.copy() for bone in armature.pose.bones} + armature_world = armature.matrix_world.copy() + to_armature = armature_world.inverted() @ GLTF_TO_BLENDER + from_armature = to_armature.inverted() + for bone in sorted(armature.pose.bones, key=lambda current: len(current.parent_recursive)): + if bone.name in bones: + bind, posed = bones[bone.name] + bone.matrix = to_armature @ posed @ bind.inverted() @ from_armature @ rest[bone.name] + bpy.context.view_layer.update() + + +def render_angles(out, root, angles): + scene = bpy.context.scene + bpy.ops.object.camera_add() + camera = bpy.context.object + scene.camera = camera + camera.data.type = 'ORTHO' + camera.data.ortho_scale = 2.05 + scene.render.engine = 'BLENDER_WORKBENCH' + scene.render.resolution_x = 544 + scene.render.resolution_y = 544 + scene.render.image_settings.file_format = 'PNG' + scene.display.shading.light = 'STUDIO' + scene.display.shading.color_type = 'MATERIAL' + scene.display.shading.show_cavity = True + scene.display.shading.cavity_type = 'WORLD' + target = (GLTF_TO_BLENDER @ root).translation + Vector((0.0, 0.0, 0.72)) + for angle in angles: + yaw = angle * pi / 180 + camera.location = target + 4 * Vector((sin(yaw), -cos(yaw), 0.30)) + camera.rotation_euler = (target - camera.location).to_track_quat('-Z', 'Y').to_euler() + scene.render.filepath = str(out / f'angle-{angle:+d}.png') + bpy.ops.render.render(write_still=True) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('model', type=Path) + parser.add_argument('pose', type=Path) + parser.add_argument('output', type=Path) + parser.add_argument('--angles', type=int, nargs='+', default=[0, 60, -60]) + parser.add_argument('--save-blend', action='store_true') + args = parser.parse_args(sys.argv[sys.argv.index('--') + 1:]) + args.output.mkdir(parents=True, exist_ok=True) + out = args.output.resolve() + # VRM 0.x is a GLB container. Blender's standard glTF importer opens + # the original bytes with the GLB extension and ignores VRM metadata. + bpy.ops.wm.read_factory_settings(use_empty=True) + with TemporaryDirectory(dir=out, prefix='.vrm-') as working: + glb = Path(working) / 'source.glb' + shutil.copyfile(args.model, glb) + bpy.ops.import_scene.gltf(filepath=str(glb)) + objects = list(bpy.context.scene.objects) + armature, = [obj for obj in objects if obj.type == 'ARMATURE'] + meshes = [obj for obj in objects if obj.type == 'MESH'] + man = all('PracticeSkirt' not in obj.data.name for obj in meshes) + print(f'{args.model.name}: {len(armature.data.bones)} bind bones, ' + f'{len(meshes)} meshes, {sum(len(obj.data.vertices) for obj in meshes)} vertices') + for obj in meshes: + for modifier in obj.modifiers: + if modifier.type == 'ARMATURE': + affine = 'PracticeClothSlippers' in obj.data.name or (man and 'Practice' in obj.data.name) + modifier.use_deform_preserve_volume = not affine + root, bones = read_pose(args.pose) + inspect_joint_motion(bones) + apply_pose(armature, bones) + change = GLTF_TO_BLENDER @ root @ GLTF_TO_BLENDER.inverted() + for obj in objects: + if obj.parent is None: + obj.matrix_world = change @ obj.matrix_world + render_angles(out, root, args.angles) + if args.save_blend: + bpy.ops.file.pack_all() + bpy.ops.wm.save_as_mainfile(filepath=str(out / 'pose.blend')) + + +if __name__ == '__main__': + main() diff --git a/design/figure-preview/figure_preview.zi b/design/figure-preview/figure_preview.zi index 80680c70b..552a56854 100644 --- a/design/figure-preview/figure_preview.zi +++ b/design/figure-preview/figure_preview.zi @@ -183,27 +183,56 @@ main :: (argc: s32, argv: **u8) -> s32 { return 0 } if argc >= 6 && TextFromCString(argv[1]) == "--shot" { - // --shot DIR PRACTICE ITEM SECONDS... [man]: chosen moments. + // --shot DIR PRACTICE ITEM SECONDS... [man] [turn=DEGREES] times: [32]float64 count: s64 man := false + turn: float64 for index: 5..argc - 1 { word := TextFromCString(argv[index]) if word == "man" { man = true + } else if word.count > 5 && word[:5] == "turn=" { + degrees, ok := ParseFloat64(word[5:]) + if !ok || !Finite64(degrees) { + return 1 + } + turn = degrees * (FIGURE_PI / 180.0) } else if count < 32 { value, ok := ParseFloat64(word) - times[count] = ifx ok then value else 0.0 + if !ok || !Finite64(value) { + return 1 + } + times[count] = value count += 1 } } practice_number, practice_ok := ParseFloat64(TextFromCString(argv[3])) item_number, item_ok := ParseFloat64(TextFromCString(argv[4])) - if !practice_ok || !item_ok { + if !practice_ok || !item_ok || count == 0 { return 1 } return PreviewShots(*preview_backdrops, *preview_library, TextFromCString(argv[2]), practices, - cast(s32)practice_number, cast(s32)item_number, times[0:count], man) + cast(s32)practice_number, cast(s32)item_number, times[0:count], man, turn) + } + if argc >= 2 && TextFromCString(argv[1]) == "--rig-pose" { + if argc < 5 || argc > 6 || (argc == 6 && TextFromCString(argv[5]) != "man") { + return 1 + } + practice, practice_ok := ParseFloat64(TextFromCString(argv[2])) + item, item_ok := ParseFloat64(TextFromCString(argv[3])) + seconds, time_ok := ParseFloat64(TextFromCString(argv[4])) + if !practice_ok || !Finite64(practice) || practice < 0 || practice > 3 || + !item_ok || !Finite64(item) || item < 0 || + item >= cast(float64)PreviewItemCount(*preview_library, cast(s32)practice) || !time_ok || !Finite64(seconds) { + return 1 + } + practice_index := cast(s32)practice + item_index := cast(s32)item + if cast(float64)practice_index != practice || cast(float64)item_index != item { + return 1 + } + return PreviewRigPose(*preview_library, practices, practice_index, item_index, seconds, argc == 6) } if argc >= 3 && TextFromCString(argv[1]) == "--capture" { return PreviewCapture(*preview_backdrops, *preview_library, TextFromCString(argv[2]), practices) diff --git a/design/figure-preview/preview_capture.zi b/design/figure-preview/preview_capture.zi index b17af0bcd..047f93d94 100644 --- a/design/figure-preview/preview_capture.zi +++ b/design/figure-preview/preview_capture.zi @@ -3,11 +3,13 @@ // flat figure when asked. #import "figure_math" #import "figure_joints" +#import "figure_model" #import "figure_finish" #import "preview_practices" #import "preview_flat" #import "preview_figure" #import "kryon/scene" +#import "kryon/scene_gltf" #import "sun_salutation_rules" #import "workout_rules" #import "std/file" @@ -206,7 +208,7 @@ PreviewVideo :: (backdrops: *PreviewBackdrops, library: *PreviewLibrary, directo // Writes the woman (or the man, MAN) at each of SECONDS of PRACTICE's ITEM // into DIRECTORY as SECONDS.png, for looking at chosen moments. PreviewShots :: (backdrops: *PreviewBackdrops, library: *PreviewLibrary, directory: string, practices: string, - practice: s32, item: s32, seconds: []float64, man: bool) -> s32 { + practice: s32, item: s32, seconds: []float64, man: bool, turn: float64 = 0.0) -> s32 { picture: Raster.Image defer VecFree(picture.pixels) figure: PreviewFigure @@ -219,7 +221,7 @@ PreviewShots :: (backdrops: *PreviewBackdrops, library: *PreviewLibrary, directo moment := PreviewSample(library, practice, item, at) width: s32 = ifx practice == PREVIEW_ASTRAL then 900 else 600 height: s32 = ifx practice == PREVIEW_ASTRAL then 600 else 800 - view := PreviewViewOf(*moment, 0.0, 0.0, 1.0, cast(float64)width / cast(float64)height) + view := PreviewViewOf(*moment, turn, 0.0, 1.0, cast(float64)width / cast(float64)height) digits: [32]u8 stem: [1024]u8 named: [1024]u8 @@ -235,6 +237,47 @@ PreviewShots :: (backdrops: *PreviewBackdrops, library: *PreviewLibrary, directo return failed } +// One exact native pose in glTF model space for inspecting the bind rig in +// Blender. The runtime-rendered shots also include the native cloth pass. +PreviewRigPose :: (library: *PreviewLibrary, practices: string, practice: s32, item: s32, + seconds: float64, man: bool) -> s32 { + if practice < 0 || practice > 3 || item < 0 || item >= PreviewItemCount(library, practice) || + seconds < 0.0 || seconds > PreviewDuration(library, practice, item) { + return 1 + } + scene: Scene + SceneInit(*scene) + defer SceneFree(*scene) + model: FigureModel + defer FigureModelRelease(*model) + path: [1200]u8 + file := ifx man then "/shared/figure/man.vrm" else "/shared/figure/woman.vrm" + if !FigureModelLoad(*model, *scene, PreviewJoin(path[:], practices, file)) { + return 1 + } + moment := PreviewSample(library, practice, item, seconds) + if !FigureModelPose(*model, *scene, *moment.body, seconds) { + return 1 + } + root := scene.nodes[model.instance.root_node] + print("root\t%\t%\t%\t%\t%\n", root.position.x, root.position.y, root.position.z, model.yaw, root.scale.x) + for role: 0..FIGURE_BONES - 1 { + node := model.bones[role] + if node < 0 { + continue + } + source := *model.model.nodes[node] + name := TextView(source.name[:]) + at := model.rest_at[node] + rest := model.rest_turn[node] + m := model.instance.node_world[node].m + print("bone\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\n", + name, at.x, at.y, at.z, rest.x, rest.y, rest.z, rest.w, + m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11]) + } + return 0 +} + #scope_file // The second of the item where her pelvis is lowest: the deepest stance. PreviewLowest :: (library: *PreviewLibrary, practice: s32, item: s32) -> float64 { diff --git a/design/figure-preview/ziran.lock b/design/figure-preview/ziran.lock index 497912064..bd267d408 100644 --- a/design/figure-preview/ziran.lock +++ b/design/figure-preview/ziran.lock @@ -46,15 +46,6 @@ "ref": "f5f186bb13569ef13338f81366ff63fb9b9282f6", "url": "https://github.com/ziranlang/avif.git" }, - { - "commit": "fa25dceb78462dac3a9899b895be0526f2241523", - "dependencies": {"kryon": "pbc3b5919c300641a"}, - "dependency_refs": {"kryon": "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91"}, - "id": "p426e7d71ec12461d", - "name": "practices", - "ref": "fa25dceb78462dac3a9899b895be0526f2241523", - "url": "https://git.waozi.xyz/waozi/practices.git" - }, { "commit": "1aadfad932c98c069a1204261b1856f81f3bc199", "dependencies": {}, @@ -82,6 +73,15 @@ "ref": "bd278217e2bac754d868b3d6502eac4257658d4b", "url": "https://github.com/ziranlang/av1.git" }, + { + "commit": "acd695550fa74178e4264bb4774f96cc9d776f0e", + "dependencies": {"kryon": "pbc3b5919c300641a"}, + "dependency_refs": {"kryon": "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91"}, + "id": "p645c65373d66d423", + "name": "practices", + "ref": "acd695550fa74178e4264bb4774f96cc9d776f0e", + "url": "https://git.waozi.xyz/waozi/practices.git" + }, { "commit": "360553150917f352e49e1beb78917370792ccaef", "dependencies": {}, @@ -128,8 +128,8 @@ } ], "root": { - "dependencies": {"kryon": "pbc3b5919c300641a", "practices": "p426e7d71ec12461d"}, - "dependency_refs": {"kryon": "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91", "practices": "fa25dceb78462dac3a9899b895be0526f2241523"}, + "dependencies": {"kryon": "pbc3b5919c300641a", "practices": "p645c65373d66d423"}, + "dependency_refs": {"kryon": "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91", "practices": "acd695550fa74178e4264bb4774f96cc9d776f0e"}, "name": "figure_preview" }, "toolchain": { diff --git a/design/figure-preview/ziran.toml b/design/figure-preview/ziran.toml index 26229184e..7660cb0b7 100644 --- a/design/figure-preview/ziran.toml +++ b/design/figure-preview/ziran.toml @@ -15,4 +15,4 @@ ref = "43e6ea3d76ded207e2be93ff82b2a609c3cfeb91" # The shared figure and its media (anatomy, textures, Tai Chi course). [dependencies.practices] git = "https://git.waozi.xyz/waozi/practices.git" -ref = "fa25dceb78462dac3a9899b895be0526f2241523" +ref = "acd695550fa74178e4264bb4774f96cc9d776f0e"