Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Create a bone map for a mixamo style human skeleton

Scott Petrovice294823259d8
diff --git a/src/retarget/BoneAutoMapper.ts b/src/retarget/BoneAutoMapper.ts
index 353ad42..811efde 100644
--- a/src/retarget/BoneAutoMapper.ts
+++ b/src/retarget/BoneAutoMapper.ts
@@ -1,5 +1,6 @@
import { Bone, Group, Object3D, SkinnedMesh } from 'three'
import { BoneCategoryMapper } from './BoneCategoryMapper'
+import { MixamoMapper } from './MixamoMapper'
/**
* Bone categories for grouping bones by anatomical area
@@ -69,6 +70,15 @@ export class BoneAutoMapper {
console.log('Source bones metadata:', source_bones_meta)
console.log('Target bones metadata:', target_bones_meta)
+ // if the target is a mixamo rig and our skeleton type is human, we can do a direct name mapping
+ // without worrying about guessing
+ const is_target_mixamo_rig: boolean = target_bones_meta.some(bone => bone.name.toLowerCase().includes('mixamorig'))
+ if (is_target_mixamo_rig) {
+ console.log('Target skeleton appears to be a Mixamo rig, performing direct name mapping...')
+ mappings = MixamoMapper.map_mixamo_bones(source_bones_meta, target_bones_meta)
+ return mappings
+ }
+
// Match bones within each category
const categories: BoneCategory[] = [
BoneCategory.Torso,
@@ -317,11 +327,7 @@ export class BoneAutoMapper {
/**
* Normalize bone names for comparison by:
- * - Converting to lowercase
- * - Removing common prefixes/suffixes
- * - Standardizing separators
- * - Removing side indicators (since we have that info separately)
- * - Standardizing numeric suffixes
+ * This will help us later when done auto-mapping and we want more similar names
*/
private static normalize_bone_name (bone_name: string, category: BoneCategory, side: BoneSide): string {
let normalized = bone_name.toLowerCase()
@@ -350,17 +356,7 @@ export class BoneAutoMapper {
// Apply category-specific normalizations
if (category === BoneCategory.Hands) {
// Standardize finger naming variations
- normalized = normalized.replace(/\b(thumb|index|middle|ring|pinky|pinkie)\b/g, (match) => {
- const finger_map: Record<string, string> = {
- 'thumb': 'thumb',
- 'index': 'index',
- 'middle': 'middle',
- 'ring': 'ring',
- 'pinky': 'pinky',
- 'pinkie': 'pinky'
- }
- return finger_map[match] || match
- })
+ // potentially do more here later with hand normalization
} else if (category === BoneCategory.Legs) {
// Standardize leg bone naming variations
normalized = normalized.replace(/\b(upperleg|upleg|thigh)\b/g, 'thigh')
diff --git a/src/retarget/MixamoMapper.ts b/src/retarget/MixamoMapper.ts
new file mode 100644
index 0000000..0e4d57f
--- /dev/null
+++ b/src/retarget/MixamoMapper.ts
@@ -0,0 +1,138 @@
+import { type BoneMetadata } from './BoneAutoMapper'
+
+/**
+ * MixamoMapper - Direct bone name mapping for Mixamo rigs
+ * Source: Mesh2Motion skeleton (hardcoded names)
+ * Target: Mixamo skeleton (mixamorig: prefix)
+ */
+export class MixamoMapper {
+ /**
+ * Direct mapping: Mesh2Motion bone name -> Mixamo bone name
+ */
+ private static readonly BONE_MAP: Record<string, string> = {
+ // Torso
+ 'DEF-hips': 'mixamorigHips',
+ 'DEF-spine001': 'mixamorigSpine',
+ 'DEF-spine002': 'mixamorigSpine1',
+ 'DEF-spine003': 'mixamorigSpine2',
+ 'DEF-neck': 'mixamorigNeck',
+ 'DEF-head': 'mixamorigHead',
+ 'DEF-headtip': 'mixamorigHeadTop_End',
+
+ // Left Arm
+ 'DEF-shoulderL': 'mixamorigLeftShoulder',
+ 'DEF-upper_armL': 'mixamorigLeftArm',
+ 'DEF-forearmL': 'mixamorigLeftForeArm',
+ 'DEF-handL': 'mixamorigLeftHand',
+
+ // Right Arm
+ 'DEF-shoulderR': 'mixamorigRightShoulder',
+ 'DEF-upper_armR': 'mixamorigRightArm',
+ 'DEF-forearmR': 'mixamorigRightForeArm',
+ 'DEF-handR': 'mixamorigRightHand',
+
+ // Left Leg
+ 'DEF-thighL': 'mixamorigLeftUpLeg',
+ 'DEF-shinL': 'mixamorigLeftLeg',
+ 'DEF-footL': 'mixamorigLeftFoot',
+ 'DEF-toeL': 'mixamorigLeftToeBase',
+ 'DEF-toe_tipL': 'mixamorigLeftToe_End',
+
+ // Right Leg
+ 'DEF-thighR': 'mixamorigRightUpLeg',
+ 'DEF-shinR': 'mixamorigRightLeg',
+ 'DEF-footR': 'mixamorigRightFoot',
+ 'DEF-toeR': 'mixamorigRightToeBase',
+ 'DEF-toe_tipR': 'mixamorigRightToe_End',
+
+ // Left Hand Fingers - Thumb
+ 'DEF-thumb01L': 'mixamorigLeftHandThumb1',
+ 'DEF-thumb02L': 'mixamorigLeftHandThumb2',
+ 'DEF-thumb03L': 'mixamorigLeftHandThumb3',
+ 'DEF-thumb_04_tipL': 'mixamorigLeftHandThumb4',
+
+ // Left Hand Fingers - Index
+ 'DEF-f_index01L': 'mixamorigLeftHandIndex1',
+ 'DEF-f_index02L': 'mixamorigLeftHandIndex2',
+ 'DEF-f_index03L': 'mixamorigLeftHandIndex3',
+ 'DEF-f_index04_tipL': 'mixamorigLeftHandIndex4',
+
+ // Left Hand Fingers - Middle
+ 'DEF-f_middle01L': 'mixamorigLeftHandMiddle1',
+ 'DEF-f_middle02L': 'mixamorigLeftHandMiddle2',
+ 'DEF-f_middle03L': 'mixamorigLeftHandMiddle3',
+ 'DEF-f_middle04_tipL': 'mixamorigLeftHandMiddle4',
+
+ // Left Hand Fingers - Ring
+ 'DEF-f_ring01L': 'mixamorigLeftHandRing1',
+ 'DEF-f_ring02L': 'mixamorigLeftHandRing2',
+ 'DEF-f_ring03L': 'mixamorigLeftHandRing3',
+ 'DEF-f_ring04_tipL': 'mixamorigLeftHandRing4',
+
+ // Left Hand Fingers - Pinky
+ 'DEF-f_pinky01L': 'mixamorigLeftHandPinky1',
+ 'DEF-f_pinky02L': 'mixamorigLeftHandPinky2',
+ 'DEF-f_pinky03L': 'mixamorigLeftHandPinky3',
+ 'DEF-f_pinky04_tipL': 'mixamorigLeftHandPinky4',
+
+ // Right Hand Fingers - Thumb
+ 'DEF-thumb01R': 'mixamorigRightHandThumb1',
+ 'DEF-thumb02R': 'mixamorigRightHandThumb2',
+ 'DEF-thumb03R': 'mixamorigRightHandThumb3',
+ 'DEF-thumb04_tipR': 'mixamorigRightHandThumb4',
+
+ // Right Hand Fingers - Index
+ 'DEF-f_index01R': 'mixamorigRightHandIndex1',
+ 'DEF-f_index02R': 'mixamorigRightHandIndex2',
+ 'DEF-f_index03R': 'mixamorigRightHandIndex3',
+ 'DEF-f_index04_tipR': 'mixamorigRightHandIndex4',
+
+ // Right Hand Fingers - Middle
+ 'DEF-f_middle01R': 'mixamorigRightHandMiddle1',
+ 'DEF-f_middle02R': 'mixamorigRightHandMiddle2',
+ 'DEF-f_middle03R': 'mixamorigRightHandMiddle3',
+ 'DEF-f_middle04_tipR': 'mixamorigRightHandMiddle4',
+
+ // Right Hand Fingers - Ring
+ 'DEF-f_ring01R': 'mixamorigRightHandRing1',
+ 'DEF-f_ring02R': 'mixamorigRightHandRing2',
+ 'DEF-f_ring03R': 'mixamorigRightHandRing3',
+ 'DEF-f_ring04_tipR': 'mixamorigRightHandRing4',
+
+ // Right Hand Fingers - Pinky
+ 'DEF-f_pinky01R': 'mixamorigRightHandPinky1',
+ 'DEF-f_pinky02R': 'mixamorigRightHandPinky2',
+ 'DEF-f_pinky03R': 'mixamorigRightHandPinky3',
+ 'DEF-f_pinky04_tipR': 'mixamorigRightHandPinky4'
+ }
+
+ /**
+ * Map Mesh2Motion bones to Mixamo bones
+ * @param source_bones - Mesh2Motion skeleton bones
+ * @param target_bones - Mixamo skeleton bones
+ * @returns Map of target bone name -> source bone name
+ */
+ static map_mixamo_bones (source_bones: BoneMetadata[], target_bones: BoneMetadata[]): Map<string, string> {
+ const mappings = new Map<string, string>()
+
+ console.log('=== MIXAMO DIRECT MAPPING ===')
+
+ // For each source bone (Mesh2Motion), find matching target bone (Mixamo)
+ for (const source_bone of source_bones) {
+ const expected_mixamo_name = this.BONE_MAP[source_bone.name]
+
+ if (expected_mixamo_name) {
+ // Find target bone with this Mixamo name
+ const target_bone = target_bones.find(tb => tb.name === expected_mixamo_name)
+
+ if (target_bone) {
+ mappings.set(target_bone.name, source_bone.name)
+ console.log(`Mapped: ${target_bone.name} -> ${source_bone.name}`)
+ }
+ }
+ }
+
+ console.log(`Mixamo mapping complete: ${mappings.size} bones mapped`)
+ return mappings
+ }
+}