@hackbuild/retarget
v0.3.2
Published
Humanoid motion interchange format, clip retargeting between any rigs, BVH, VMD, VPD, VRMA and Unity clip readers, motion analysis that tags clips. Rust core in wasm.
Readme
@hackbuild/retarget
Move clips between any rigs through one normalized humanoid space, with readers for BVH, VMD, VPD, VRMA and Unity text clips.
Browser status: the demo page has run in Chrome on a BVH walk written in code; no real clip file has been loaded in a browser yet.
What it does
- Defines
HumanoidMotion, an interchange format of per bone rotations in the normalized space and hip travel in hip heights, so one motion fits a child and a tall adult. - Maps and measures any skeleton (facing, units, floor, A or T pose) and retargets clips onto and off it with
RetargetRig. - Reads BVH, MikuMikuDance VMD and VPD, VRM animation (VRMA) and Unity text clips of transform curves, solving legs keyed only by foot IK.
- Tags a motion by what the body does (wave, squat, nod, spin, walk, dance, idle and more) and packs it into a compact binary form.
- Retargets three.js
AnimationClips between scenes, or copies a driver rig's pose onto a target every frame.
Install
npm install @hackbuild/retargetIt depends on @hackbuild/rig for the bone mapper. three.js is an optional peer, needed only for the /three subpath.
| subpath | needs | what |
|---|---|---|
| @hackbuild/retarget | none | the format, the rig, BVH and VMD parsers, analysis |
| @hackbuild/retarget/formats | none | VMD with face tracks, VPD, Unity text clips |
| @hackbuild/retarget/three | three 0.186.1 or later (optional peer) | scene skeletons, AnimationClip in and out, live retarget, VRMA |
| @hackbuild/retarget/wasm | none | the wasm file, to pass to load() as a URL |
| @hackbuild/retarget/inline | none | the wasm as base64 |
| @hackbuild/retarget/iife | THREE global before it loads, for every part | single file build, global HackbuildRetarget, wasm inlined |
Quick start
import { analyzeMotion, load, packMotion, parseBvh, restForRetarget, RetargetRig } from "@hackbuild/retarget";
await load();
// pick any BVH file: its bones map onto the normalized humanoid by name
const input = document.body.appendChild(document.createElement("input"));
input.type = "file";
input.accept = ".bvh";
input.onchange = async () => {
const file = input.files?.[0];
if (!file) return;
const read = parseBvh(await file.text());
// a t pose kept in frame 0, or a rest no body stands in, gives way to a t pose to measure by
const { skeleton, clip } = restForRetarget(read.skeleton, read.clip);
const rig = new RetargetRig(skeleton); // meters per unit is guessed from the rest height
const motion = rig.toHumanoid(clip, { settle: true });
const found = analyzeMotion(motion);
console.log(found.duration.toFixed(2), "s,", found.tags.join(" "), packMotion(motion).byteLength, "bytes packed");
rig.dispose();
};Live example
The demo page plays a BVH walk recorded on another rig, in centimeters, on the normalized humanoid, and takes your own file: hackbuild-motion/retarget. The suite runs it too: hackbuild-suite retarget.
Concepts
the normalized humanoid space
Every motion is stored as VRM 1.0 bone names, rotations relative to a T pose, +Z forward, meters, feet at y = 0. Hip travel is in multiples of hip height, so a half size body moves half as far. packMotion and unpackMotion give the compact binary form (HBM1, f32, about 16 bytes per bone per frame).
a rig maps and measures
new RetargetRig(skeleton) maps bone names through @hackbuild/rig, finds the facing, the unit and the floor, and gives each humanoid bone a rest alignment toward the reference T pose. An A pose source, a model facing -Z, centimeters or scrambled bind orientations make no difference. toHumanoid(clip) reads a clip into the space, bind(motion) writes a motion onto the rig, retargetClip does both at once, and retargetLimb gives an end effector target for a limb of another length. Pass { unit } when you know meters per source unit.
rest and floor
restForRetarget(skeleton, clip) picks the rest to measure a parsed file by; call it between parseBvh and new RetargetRig. restFromFirstFrame turns a T pose kept in frame 0 into the rest, as some BVH releases do. restFromPose builds a T pose from the clip when the file rests in no pose a body stands in, as where every bone rests along its joint's local axis and the standing pose lives in the rotations: the frame standing straightest on the file's world up, each bone turned the shortest way onto its T pose direction, so each limb keeps the twist the motion gives it; the rest translations stay the file's. settleOnFloor (or toHumanoid(clip, { settle: true })) moves a clip a few centimetres off the floor down onto it and leaves an airborne loop alone.
readers
parseBvh takes any channel order and keeps End Sites as helper joints. parseVmd reads MikuMikuDance at 30 fps with per channel bezier easing and Shift-JIS names through TextDecoder, and solves legs keyed only by foot IK bones with two bone IK. From /formats: readVmd returns the motion and its face as VRM expression tracks, parseVpd reads a pose, and parseUnityAnim with convertUnityGeneric reads a Unity text clip of transform curves. Unity humanoid muscle clips are refused: their zero pose depends on per avatar rotations Unity does not publish, so export from a generic rig or as VRMA instead.
three.js
import { retargetAnimation } from "@hackbuild/retarget/three";
const clip = retargetAnimation(source.animations[0]!, source.scene, avatar.scene);
mixer.clipAction(clip).play();poseFrame(clip, scene, frame) puts one frame of a bound clip on a scene's nodes with no mixer, so your own clock drives it, for instance to read each frame of a pose as it plays. LiveRetarget copies a driver rig's pose onto a target every frame, so a clip library on the driver plays on any model. readVrma reads a VRM animation by the file's own humanoid table with three's GLTFLoader; pickTake picks a take by name, else mixamo.com, else the longest, and firstPlayable tries a file's format variants until one plays.
analysis
analyzeMotion returns duration, loop and pose flags, mean head pitch, energy in hip heights per second, and tags: hands up, cheer, wave, raise hand, squat, crouch, jump, spin, turn, walk, bow, nod, shake head, clap, hand to face, pose, dance, idle.
API overview
| export | what it does |
|---|---|
| load | load the wasm core once; call before anything else |
| RetargetRig | map and measure a skeleton, read and write motions |
| HumanoidMotion | the interchange format |
| parseBvh | read a BVH file |
| parseVmd | read a MikuMikuDance VMD file |
| retargetClip | a clip from one rig onto another in one call |
| analyzeMotion | tags and measures of a motion |
| packMotion | the compact binary form |
| readVmd | a VMD as motion and face tracks |
| parseUnityAnim | read a Unity text clip |
| retargetAnimation | a three.js clip from one scene onto another |
| poseFrame | one frame of a clip on a scene's nodes, no mixer |
| LiveRetarget | copy a driver's pose onto a target every frame |
| readVrma | read a VRM animation file |
The full reference is in docs/api/retarget.
Models and weights
No model, clip or weight ships here. The clips and models you load keep their own licences.
| input | where it loads from | licence | |---|---|---| | BVH, VMD, VPD, VRMA, Unity clips | a file or URL you give | the file's own | | three.js scenes and clips | your loader | the model's own |
Browser support
| needs | Chrome | Safari | Firefox | phone |
|---|---|---|---|---|
| WebAssembly with SIMD128 | yes (measured, Chrome 150) | 16.4 and later (vendor) | yes (vendor) | unknown |
| TextDecoder("shift_jis") for VMD and VPD names | yes (vendor) | yes (vendor) | yes (vendor) | unknown |
No WebGPU, workers or wasm threads. From file:// use dist/retarget.iife.js, which carries the wasm inlined and reads THREE from the page, or call load() with no argument. In Node, Shift-JIS decoding needs full ICU, or pass your own decode to parseVmd.
Errors and troubleshooting
| message | cause | what to do |
|---|---|---|
| @hackbuild/retarget: wasm not loaded. call load() first. | a parser or rig call before load() resolved | await load() once first |
| this .vpd is a posing tool pose of unity muscle values, which is not read. save it as a vrma. | the JSON .vpd of a posing tool | export the pose as VRMA |
| not a vpd pose file | the file does not start with Vocaloid Pose Data | check the file |
| this vpd has no bones | the pose names no bone | check the file |
| not a text unity .anim. save the project with force text serialization. | a binary Unity clip | set Unity's asset serialization to force text and save again |
| this .anim is a humanoid muscle clip, which is not read. export it from a generic rig or as a vrma. | a Unity humanoid muscle clip | export from a generic rig or as VRMA |
| no curve in this .anim drives a humanoid bone | no curve path maps to a humanoid bone | check the clip's bone paths |
| no take named "<take>" in this file | pickTake got a name the file lacks | pick one of the file's take names |
| this file holds no animation. it may be a model, not a motion. | the file has no clip | load a motion file |
| no VRMC_vrm_animation humanoid in this file. it may be a model, not a vrma. | readVrma got a file without the VRMA extension | load a .vrma file |
| this vrma holds no animation | the VRMA has no clip | check the file |
Works with
- @hackbuild/rig maps bone names to the humanoid and holds the expression table the face tracks use.
- @hackbuild/avatar loads models into the same normalized space.
- @hackbuild/presence plays the face tracks and layers clips.
- @hackbuild/motion-match and @hackbuild/bake take retargeted clips.
Changelog and licences
Changes are in the repository CHANGELOG. Code is MIT (LICENSE); this README is CC-BY-4.0 with its code samples also MIT. No clips or models ship here; files you load keep their own licences.
What is verified, and what is not
Verified on this machine
| check | result | label |
|---|---|---|
| cargo test -p hackbuild-retarget | 20 of 20 pass, clippy clean natively and for wasm32 | counted |
| npx vitest run packages/retarget | 23 of 23 pass, one a fast-check property over the binary format | counted |
| a rig facing -Z, in centimeters, A pose arms, scrambled bind orientations, 5 seeded motions of 12 frames | bind then read back gives the same rotations within 1e-6 rad and hip offsets within 1e-9; limbs point as the reference body's within 1e-9 | computed |
| a half size body | hip travel halves | computed |
| a VMD left arm with no key, a 5 unit center key | arm 35 degrees below horizontal, hips move 0.4 m | computed |
| a BVH arm keyed 60 degrees down | reads 60 degrees in the normalized space | computed |
| a three.js clip on a 1 m body onto a 60 times larger body facing the other way | same arm angle in the body frame and same share of hip height, within 1e-6; LiveRetarget limb directions within 1e-6 | computed |
| poseFrame on that larger, turned body, middle and past the end frames, with and without keyed hips | every node's rotation and position equal the mixer's at that frame's time, within 1e-5 and 1e-4 | computed |
| a BVH root with zero OFFSET starting crouched, hips at 60 on a 90 leg | rests at 90 | computed |
| the reference body as a file with joint orients baked into its motion (every joint's frame turned, a rest of no rotations that no body stands in), 6 frames with one standing | restFromPose builds a rest that reads every frame as the original body does, within 1e-6 rad, hips within 1e-6, though the standing frame leans and is off the path, frame 0 lies down and frame 5 bows twisted; a T pose and a scrambled A pose rest are kept | computed |
| real BVH files in a research probe, not committed: a CMU walk (the cgspeed release) and a walk and a bow from Bandai Namco Research Motiondataset 1 (CC BY-NC 4.0), onto a person, read back as G1 joint angles | restForRetarget takes the first frame for the CMU file and builds the rest for the Bandai files; the robot's joints fit the person within 3.4 to 7.2 degrees mean (the bow deepest), against 22 to 23 mean and 76 to 79 worst for the Bandai files on their own rest; the Bandai walk keeps a level path, its hips 0.02 hip heights from their start after 8 of travel | measured |
| VMD foot IK key 0.1 hip heights forward, body 0.15 lower | foot on target within 1 mm, knee in front of the hip to ankle line | computed |
| a foot IK target 3 hip heights down | straight leg of 0.83 m, no NaN | computed |
| a face only VMD of 45 frames | 46 frames; of two blink morphs the one with more keys plays | computed |
| a VRMA arm raise of 60 degrees | reads 60, hips travel 0.5 m, aa track 0 to 1 | computed |
| golden trace (seeded BVH and foot IK VMD through parse, retarget, floor, analysis and bind) | bit identical between the native and the wasm build | measured |
Not verified by anything
- Real files: beyond the three BVH files above and seven VRMA clips (in the humanoid package's probe), no Mixamo FBX, UAL glTF, VRoid VRM, MMD VMD or VPD, or Unity clip was loaded; the readers and rigs in the tests are synthetic.
restFromPosetakes each limb's twist from one frame, the most upright one with the arms nearest the side plane. A file whose every frame holds the arms out in front, or twisted, gets a rest twisted the same way; no such file was tried.- Foot IK ignores the VMD IK on and off frames and the parent of all bone.
- Unity curves sample linearly; tangents are dropped. A generic rig whose bones rest with non identity rotations reads wrong.
- Sorting clips into classes (idle, talk, gesture, locomotion) from file names is not written.
- Non uniform scale on an armature node, and scale animation.
- Motion in a real browser at real frame rate, judged by a person.
Size budget
| file | gzip (measured) | ceiling |
|---|---|---|
| dist/index.js (main entry, imports external) | 160,029 bytes | 168,030 bytes |
| dist/wasm/retarget_bg.wasm | 114,705 bytes | 120,440 bytes |
| dist/retarget.iife.js (single file build) | 190,740 bytes | 200,277 bytes |
Measured 2026-12-02 by node tools/lint-packages.mjs (gzip level 9) from the packed
tarball. Generated from tools/size-budget.json; CI fails when a file moves more than
5 percent from its row.
