Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Rigify bone auto mapper

Mark11b592f08f00
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.ts
index 9f20c11..9ba27ab 100644
--- a/src/retarget/AnimationRetargetService.ts
+++ b/src/retarget/AnimationRetargetService.ts
@@ -206,7 +206,8 @@ export class AnimationRetargetService {
if (target_mapping_type === TargetBoneMappingType.Mixamo) {
source_rig.fromConfig(HumanChainConfig.mesh2motion_config)
target_rig.fromConfig(HumanChainConfig.mixamo_config)
- } else if (target_mapping_type === TargetBoneMappingType.Custom) {
+ } else if (target_mapping_type === TargetBoneMappingType.Custom ||
+ target_mapping_type === TargetBoneMappingType.Rigify) {
const custom_source_config = HumanChainConfig.build_custom_source_config(this.get_bone_mappings())
const custom_target_config = HumanChainConfig.build_custom_target_config(custom_source_config, this.get_bone_mappings())
diff --git a/src/retarget/bone-automap/BoneAutoMapper.ts b/src/retarget/bone-automap/BoneAutoMapper.ts
index 6ebece4..820d47a 100644
--- a/src/retarget/bone-automap/BoneAutoMapper.ts
+++ b/src/retarget/bone-automap/BoneAutoMapper.ts
@@ -1,6 +1,7 @@
import { Bone, Group, Object3D, Scene, SkinnedMesh } from 'three'
import { BoneCategoryMapper } from './BoneCategoryMapper'
import { MixamoMapper } from './MixamoMapper'
+import { RigifyMapper } from './RigifyMapper'
import { TargetBoneMappingType } from '../steps/StepBoneMapping'
import { AnimationRetargetService } from '../AnimationRetargetService'
@@ -87,6 +88,12 @@ export class BoneAutoMapper {
return mappings
}
+ if (retarget_service.get_target_mapping_type() === TargetBoneMappingType.Rigify) {
+ console.log('Target skeleton appears to be a Rigify rig, performing direct name mapping...')
+ mappings = RigifyMapper.map_rigify_bones(source_bones_meta, target_bones_meta)
+ return mappings
+ }
+
// Match bones within each category
const categories: BoneCategory[] = [
BoneCategory.Torso,
@@ -213,24 +220,25 @@ export class BoneAutoMapper {
private static normalize_bone_name (bone_name: string): string {
let name = bone_name.toLowerCase()
- // Remove spaces and underscores
- name = name.replace(/\s+/g, '')
- name = name.replace(/_/g, '')
-
// Remove common prefixes
name = name.replace(/^def-/g, '') // Blender DEF prefix
- name = name.replace(/^mixamorig/g, '') // Mixamo prefix
+ name = name.replace(/^org-/g, '') // Rigify ORG prefix
+ name = name.replace(/^mixamorig:?/g, '') // Mixamo prefix (with optional colon)
- // Remove common suffixes
- name = name.replace(/\.\d+$/g, '') // Blender numeric suffixes (e.g., ".001")
+ // Remove spaces, underscores, dots, hyphens, colons
+ name = name.replace(/[\s_.\-:]/g, '')
// Remove L/R identifiers (we use side detection instead)
name = name.replace(/l$/g, '')
name = name.replace(/r$/g, '')
- // Remove number identifiers (we use order detection instead)
+ // Remove trailing numbers (segment indices like ".001" or "01")
name = name.replace(/\d+$/g, '')
+ // Strip another trailing l/r in case digits sat between letter and number (e.g. "thumb01l")
+ name = name.replace(/l$/g, '')
+ name = name.replace(/r$/g, '')
+
return name
}
diff --git a/src/retarget/bone-automap/BoneCategoryMapper.ts b/src/retarget/bone-automap/BoneCategoryMapper.ts
index 8c76bc9..287f60d 100644
--- a/src/retarget/bone-automap/BoneCategoryMapper.ts
+++ b/src/retarget/bone-automap/BoneCategoryMapper.ts
@@ -1,4 +1,4 @@
-import { BoneCategory, BoneMetadata, BoneSide } from './BoneAutoMapper'
+import { BoneMetadata, BoneSide } from './BoneAutoMapper'
/**
* BoneCategoryMapper - Handles category-specific bone mapping logic
@@ -112,7 +112,8 @@ export class BoneCategoryMapper {
}
/**
- * Performs exact name matching between source and target bones
+ * Match target bones to source bones by normalized name + compatible side.
+ * Each source bone may only be used once.
* @param source_bones - Array of source bone metadata
* @param target_bones - Array of target bone metadata
* @param category_mappings - Map to store the bone name mappings
@@ -120,16 +121,39 @@ export class BoneCategoryMapper {
private static perform_exact_name_matching (source_bones: BoneMetadata[],
target_bones: BoneMetadata[], category_mappings: Map<string, string>): void {
+ const used_source_names = new Set<string>(category_mappings.values())
+
for (const target_bone_meta of target_bones) {
- // Skip if already mapped
if (category_mappings.has(target_bone_meta.name)) continue
- for (const source_bone_meta of source_bones) {
- if (source_bone_meta.name === target_bone_meta.name) {
- category_mappings.set(target_bone_meta.name, source_bone_meta.name)
- break
- }
+ // Prefer same-side, exact-normalized match first
+ let match: BoneMetadata | undefined = source_bones.find(sb =>
+ !used_source_names.has(sb.name) &&
+ sb.normalized_name === target_bone_meta.normalized_name &&
+ BoneCategoryMapper.sides_compatible(sb.side, target_bone_meta.side)
+ )
+
+ // Fallback: raw-name match (preserves prior behavior for identical names)
+ if (match === undefined) {
+ match = source_bones.find(sb =>
+ !used_source_names.has(sb.name) &&
+ sb.name === target_bone_meta.name
+ )
+ }
+
+ if (match !== undefined) {
+ category_mappings.set(target_bone_meta.name, match.name)
+ used_source_names.add(match.name)
}
}
}
+
+ private static sides_compatible (a: BoneSide, b: BoneSide): boolean {
+ if (a === b) return true
+ if (a === BoneSide.Unknown || b === BoneSide.Unknown) return true
+ if (a === BoneSide.Center || b === BoneSide.Center) {
+ return a === BoneSide.Center && b === BoneSide.Center
+ }
+ return false
+ }
}
diff --git a/src/retarget/bone-automap/RigifyMapper.ts b/src/retarget/bone-automap/RigifyMapper.ts
new file mode 100644
index 0000000..ecd29a4
--- /dev/null
+++ b/src/retarget/bone-automap/RigifyMapper.ts
@@ -0,0 +1,118 @@
+import { type BoneMetadata } from './BoneAutoMapper'
+
+/**
+ * RigifyMapper - Direct bone name mapping for Blender Rigify rigs
+ * Source: Mesh2Motion skeleton
+ * Target: Rigify deform bones (DEF- prefix)
+ */
+export class RigifyMapper {
+ // Mesh2Motion bone name -> Rigify deform bone name
+ private static readonly BONE_MAP: Record<string, string> = {
+ // Torso
+ pelvis: 'DEF-spine',
+ spine_01: 'DEF-spine.001',
+ spine_02: 'DEF-spine.002',
+ spine_03: 'DEF-spine.003',
+ neck_01: 'DEF-spine.004',
+ head: 'DEF-spine.006',
+
+ // Left Arm
+ clavicle_l: 'DEF-shoulder.L',
+ upperarm_l: 'DEF-upper_arm.L',
+ lowerarm_l: 'DEF-forearm.L',
+ hand_l: 'DEF-hand.L',
+
+ // Right Arm
+ clavicle_r: 'DEF-shoulder.R',
+ upperarm_r: 'DEF-upper_arm.R',
+ lowerarm_r: 'DEF-forearm.R',
+ hand_r: 'DEF-hand.R',
+
+ // Left Leg
+ thigh_l: 'DEF-thigh.L',
+ calf_l: 'DEF-shin.L',
+ foot_l: 'DEF-foot.L',
+ ball_l: 'DEF-toe.L',
+
+ // Right Leg
+ thigh_r: 'DEF-thigh.R',
+ calf_r: 'DEF-shin.R',
+ foot_r: 'DEF-foot.R',
+ ball_r: 'DEF-toe.R',
+
+ // Left Hand Fingers
+ thumb_01_l: 'DEF-thumb.01.L',
+ thumb_02_l: 'DEF-thumb.02.L',
+ thumb_03_l: 'DEF-thumb.03.L',
+ index_01_l: 'DEF-f_index.01.L',
+ index_02_l: 'DEF-f_index.02.L',
+ index_03_l: 'DEF-f_index.03.L',
+ middle_01_l: 'DEF-f_middle.01.L',
+ middle_02_l: 'DEF-f_middle.02.L',
+ middle_03_l: 'DEF-f_middle.03.L',
+ ring_01_l: 'DEF-f_ring.01.L',
+ ring_02_l: 'DEF-f_ring.02.L',
+ ring_03_l: 'DEF-f_ring.03.L',
+ pinky_01_l: 'DEF-f_pinky.01.L',
+ pinky_02_l: 'DEF-f_pinky.02.L',
+ pinky_03_l: 'DEF-f_pinky.03.L',
+
+ // Right Hand Fingers
+ thumb_01_r: 'DEF-thumb.01.R',
+ thumb_02_r: 'DEF-thumb.02.R',
+ thumb_03_r: 'DEF-thumb.03.R',
+ index_01_r: 'DEF-f_index.01.R',
+ index_02_r: 'DEF-f_index.02.R',
+ index_03_r: 'DEF-f_index.03.R',
+ middle_01_r: 'DEF-f_middle.01.R',
+ middle_02_r: 'DEF-f_middle.02.R',
+ middle_03_r: 'DEF-f_middle.03.R',
+ ring_01_r: 'DEF-f_ring.01.R',
+ ring_02_r: 'DEF-f_ring.02.R',
+ ring_03_r: 'DEF-f_ring.03.R',
+ pinky_01_r: 'DEF-f_pinky.01.R',
+ pinky_02_r: 'DEF-f_pinky.02.R',
+ pinky_03_r: 'DEF-f_pinky.03.R'
+ }
+
+ // True if any bone name starts with "DEF-" (Rigify deform prefix)
+ static is_target_valid_skeleton (bone_names: string[]): boolean {
+ return bone_names.some(name => /^def-/i.test(name))
+ }
+
+ /**
+ * Normalize a Rigify bone name for tolerant matching.
+ * Different exporters strip or keep dots/underscores (e.g. Blender's GLTF exporter
+ * turns "DEF-spine.001" into "DEF-spine001", "DEF-upper_arm.L" into "DEF-upper_armL").
+ * Lowercase + strip dots/underscores/hyphens so all variants compare equal.
+ */
+ private static loose_key (name: string): string {
+ return name.toLowerCase().replace(/[._\-]/g, '')
+ }
+
+ static map_rigify_bones (source_bones: BoneMetadata[], target_bones: BoneMetadata[]): Map<string, string> {
+ const mappings = new Map<string, string>()
+
+ // Build a loose-keyed lookup of target bones (first match wins on collisions)
+ const target_by_loose_key = new Map<string, BoneMetadata>()
+ for (const tb of target_bones) {
+ const key = RigifyMapper.loose_key(tb.name)
+ if (!target_by_loose_key.has(key)) {
+ target_by_loose_key.set(key, tb)
+ }
+ }
+
+ for (const source_bone of source_bones) {
+ const expected_target_name: string | undefined = this.BONE_MAP[source_bone.name]
+ if (expected_target_name === undefined) continue
+
+ const target_bone = target_by_loose_key.get(RigifyMapper.loose_key(expected_target_name))
+ if (target_bone !== undefined) {
+ mappings.set(target_bone.name, source_bone.name)
+ }
+ }
+
+ console.log(`Rigify mapping complete: ${mappings.size} bones mapped`)
+ return mappings
+ }
+}
diff --git a/src/retarget/steps/StepBoneMapping.ts b/src/retarget/steps/StepBoneMapping.ts
index b18fe29..281fdeb 100644
--- a/src/retarget/steps/StepBoneMapping.ts
+++ b/src/retarget/steps/StepBoneMapping.ts
@@ -1,12 +1,14 @@
import { type Scene, type SkinnedMesh } from 'three'
import { BoneAutoMapper } from '../bone-automap/BoneAutoMapper.ts'
import { MixamoMapper } from '../bone-automap/MixamoMapper.ts'
+import { RigifyMapper } from '../bone-automap/RigifyMapper.ts'
import { AnimationRetargetService } from '../AnimationRetargetService.ts'
import { Mesh2MotionMapper } from '../bone-automap/Mesh2MotionMapper.ts'
// when we are auto-mapping, keep track of what rig type we matched target against
export enum TargetBoneMappingType {
Mixamo = 'mixamo',
+ Rigify = 'rigify',
Mesh2Motion = 'mesh2motion',
Custom = 'custom', // trying to figure out bone mappings with bone name analysis
None = 'none'
@@ -364,6 +366,9 @@ export class StepBoneMapping extends EventTarget {
if (mapping_type === TargetBoneMappingType.Mixamo) {
this.auto_bone_map_match_display.style.display = 'inline'
this.auto_bone_map_match_display.textContent = '✨ Mixamo'
+ } else if (mapping_type === TargetBoneMappingType.Rigify) {
+ this.auto_bone_map_match_display.style.display = 'inline'
+ this.auto_bone_map_match_display.textContent = '✨ Rigify'
} else if (mapping_type === TargetBoneMappingType.Mesh2Motion) {
this.auto_bone_map_match_display.style.display = 'inline'
this.auto_bone_map_match_display.textContent = '✨ Mesh2Motion'
@@ -421,8 +426,11 @@ export class StepBoneMapping extends EventTarget {
const retarget_service: AnimationRetargetService = AnimationRetargetService.getInstance()
// see if target bones follow a known template
- if (MixamoMapper.is_target_valid_skeleton(this.get_target_bone_names())) {
+ const target_bone_names = this.get_target_bone_names()
+ if (MixamoMapper.is_target_valid_skeleton(target_bone_names)) {
retarget_service.set_target_mapping_type(TargetBoneMappingType.Mixamo)
+ } else if (RigifyMapper.is_target_valid_skeleton(target_bone_names)) {
+ retarget_service.set_target_mapping_type(TargetBoneMappingType.Rigify)
} else if (Mesh2MotionMapper.is_source_bones_same_as_target(
this.get_source_bone_names(),
this.get_target_bone_names()