Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

simplify retargeting logic

Scott Petrovic573db9d92144
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.ts
index a5f5101..08f50ca 100644
--- a/src/retarget/AnimationRetargetService.ts
+++ b/src/retarget/AnimationRetargetService.ts
@@ -142,7 +142,9 @@ export class AnimationRetargetService {
// Create a track for each target bone this source bone maps to. Will mostly just rename
// a bone with the mapping
target_bone_names.forEach((target_bone_name) => {
- const new_track_name = RetargetUtils.create_track_name(target_bone_name, track_property)
+ // track name is in the format of "boneName.property"
+ const new_track_name = `${target_bone_name}.${track_property}`
+
const times_copy = Float32Array.from(track.times as ArrayLike<number>)
const values_copy = Float32Array.from(track.values as ArrayLike<number>)
diff --git a/src/retarget/RetargetUtils.ts b/src/retarget/RetargetUtils.ts
index dea88ca..034ad54 100644
--- a/src/retarget/RetargetUtils.ts
+++ b/src/retarget/RetargetUtils.ts
@@ -132,14 +132,6 @@ export class RetargetUtils {
}
}
- /**
- * Create a track name in the format expected by Three.js
- * For named bones, use: BoneName.property
- */
- static create_track_name (bone_name: string, property: string): string {
- return `${bone_name}.${property}`
- }
-
/**
* Parse a track name to extract bone name and property (e.g., "quaternion", "position", "scale")
* Handles various formats like "boneName.property" or ".bones[boneName].property"
@@ -186,6 +178,22 @@ export class RetargetUtils {
return reverse_mappings
}
+ /**
+ * Create a reverse mapping for one-to-one use cases: source bone name -> target bone name.
+ * If multiple targets map to the same source, the first mapping wins.
+ */
+ static reverse_bone_mapping_one_to_one (bone_mappings: Map<string, string>): Map<string, string> {
+ const reverse_mapping = new Map<string, string>()
+
+ bone_mappings.forEach((source_bone_name, target_bone_name) => {
+ if (!reverse_mapping.has(source_bone_name)) {
+ reverse_mapping.set(source_bone_name, target_bone_name)
+ }
+ })
+
+ return reverse_mapping
+ }
+
/**
* Clone a skeleton into a detached working copy for retargeting.
*
diff --git a/src/retarget/human-retargeting/HumanChainConfig.ts b/src/retarget/human-retargeting/HumanChainConfig.ts
index ab52260..79c3316 100644
--- a/src/retarget/human-retargeting/HumanChainConfig.ts
+++ b/src/retarget/human-retargeting/HumanChainConfig.ts
@@ -1,5 +1,6 @@
+import { RetargetUtils } from '../RetargetUtils.ts'
-
+// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class HumanChainConfig {
// Master list of human bone/joint names that we can use and part of the Mesh2Motion rig
// this will always be the source config we start with for retargeting
@@ -43,14 +44,6 @@ export class HumanChainConfig {
fingersPinkyR: ['mixamorigRightHandPinky1', 'mixamorigRightHandPinky2', 'mixamorigRightHandPinky3', 'mixamorigRightHandPinky4']
}
- // TODO: Have this algorithm work for ALL humanoid rigs, not just Mixamo
- // right now they just use the simple bone mapping...which will have issues with bone roll
- // the retargeting algorithm needs a source config and a target config for it to work
- // this configuration will store the "chains" of joints for both source and target rigs
- // we have all the source joints since that is the Mesh2Motion rigs, but we might not
- // have all the target joints. We will need to effectively clone this "master" config and
- // modify it to only includes bones that are part of the mapping
-
// then we can duplicate that source config to a target config. We can go through the bone
// mapping and swap out all the source bone names for the target bone names
@@ -59,7 +52,6 @@ export class HumanChainConfig {
const base_source_config = structuredClone(HumanChainConfig.mesh2motion_config)
const flat_source_bone_names: string = this.flat_bone_name_list(bone_mapping.values()) // values store the Mesh2Motion bones
- // TODO: Go through each chain and bone. If the bone has a bone mapping, keep it, if not, replace the value with an empty string
for (const chain_name in base_source_config) {
const bone_names_in_chain = base_source_config[chain_name]
for (let i = 0; i < bone_names_in_chain.length; i++) {
@@ -92,8 +84,8 @@ export class HumanChainConfig {
}
public static build_custom_target_config (source_config: Record<string, string[]>, bone_mapping: Map<string, string>): Record<string, string[]> {
- // swap the keys with value since it puts the Mesh2Motion bone names as values
- const reverse_bone_mapping = new Map(Array.from(bone_mapping.entries()).map(([key, value]) => [value, key]))
+ // swap key/value so source (Mesh2Motion) bone names can resolve to target rig names
+ const reverse_bone_mapping = RetargetUtils.reverse_bone_mapping_one_to_one(bone_mapping)
// our source config will only have the bones that need to be mapped. Non-mapped bones will be empty strings
const custom_target_config: Record<string, string[]> = structuredClone(source_config)