Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Try to be more flexible when finding root motion bone with retargeter

Scott Petrovic050fe268af00
diff --git a/src/retarget/human-retargeting/Retargeter.ts b/src/retarget/human-retargeting/Retargeter.ts
index 2cbdb8f..a770f74 100644
--- a/src/retarget/human-retargeting/Retargeter.ts
+++ b/src/retarget/human-retargeting/Retargeter.ts
@@ -114,6 +114,9 @@ export class Retargeter {
// Storage for keyframe data per bone
const bone_data = new Map<string, { times: number[], positions: number[], quaternions: number[] }>()
+ // Only one bone carries root motion. Figure out which one before we sample
+ const root_motion_bone_name: string | null = this.root_motion_bone_name()
+
// Initialize storage for each bone in the target skeleton
this.tarRig.skel.bones.forEach((bone) => {
bone_data.set(bone.name, {
@@ -161,10 +164,8 @@ export class Retargeter {
)
tracks.push(quat_track)
- // add root motion track
- // TODO: this needs to be a bit smarter to know which track
- // has the root bone (hard-coding where Mixamo stores root motion)
- if (bone_name.toLowerCase().trim().includes('hips')) {
+ // add root motion track for the one bone that actually gets translated
+ if (bone_name === root_motion_bone_name) {
const pos_track = new THREE.VectorKeyframeTrack(
`${bone_name}.position`,
data.times,
@@ -179,6 +180,39 @@ export class Retargeter {
return tracks
}
+ /**
+ * Find the target bone that carries root motion.
+ * The pelvis chain is the only chain that gets translated (see applyScaledTranslation),
+ * so whatever bone the user mapped there is the one with position data worth baking.
+ * Falls back to name matching for rigs that never got a pelvis mapping.
+ * @returns bone name, or null if we cannot figure one out
+ */
+ private root_motion_bone_name (): string | null {
+
+ // Check if the pelvis chain exists and has bones mapped
+ const pelvis_chain: RigItem[] = this.tarRig.chains.pelvis
+ if (pelvis_chain !== undefined && pelvis_chain.length > 0) {
+ const bone: THREE.Bone | undefined = this.tarRig.skel.bones[pelvis_chain[0].idx]
+ if (bone !== undefined) {
+ return bone.name
+ }
+ }
+
+ // no pelvis chain mapped...guess from common naming conventions instead
+ const root_motion_names: string[] = ['pelvis', 'hips', 'hip']
+ const fallback_bone: THREE.Bone | undefined = this.tarRig.skel.bones.find((bone) => {
+ const name = bone.name.toLowerCase().trim()
+ return root_motion_names.some(candidate => name.includes(candidate))
+ })
+
+ if (fallback_bone === undefined) {
+ console.warn('Retargeter: Could not find a root motion bone. Baked animation will have no position track')
+ return null
+ }
+
+ return fallback_bone.name
+ }
+
/**
* Retargets a whole chain of bones (like an arm or leg) by matching the swing and twist
* orientation of each source bone to its corresponding target bone, one-to-one, along the chain.