Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Refactor bone metadata generation a bit

Scott Petrovic4fe00910a025
diff --git a/src/retarget/bone-automap/BoneAutoMapper.ts b/src/retarget/bone-automap/BoneAutoMapper.ts
index 1f90de7..cbdf21e 100644
--- a/src/retarget/bone-automap/BoneAutoMapper.ts
+++ b/src/retarget/bone-automap/BoneAutoMapper.ts
@@ -65,15 +65,18 @@ export class BoneAutoMapper {
return new Map<string, string>()
}
- // Extract bone data from both skeletons
- // this also contains the parent bone relationship
- // which will help us later when doing auto-mapping calculations
- const source_parent_map: Map<string, string | null> = BoneAutoMapper.extract_source_bone_parent_map(source_armature)
- const target_parent_map: Map<string, string | null> = BoneAutoMapper.extract_target_bone_parent_map(target_armature)
-
// Create metadata for both source and target bones
- const source_bones_meta: BoneMetadata[] = BoneAutoMapper.create_all_bone_metadata(source_parent_map)
- const target_bones_meta: BoneMetadata[] = BoneAutoMapper.create_all_bone_metadata(target_parent_map)
+ const retarget_service: AnimationRetargetService = AnimationRetargetService.getInstance()
+ let source_bones_meta: BoneMetadata[] = []
+ let target_bones_meta: BoneMetadata[] = []
+
+ if (retarget_service.get_source_armature().children.length > 0) {
+ source_bones_meta = BoneAutoMapper.create_all_bone_metadata(retarget_service.get_source_armature(), true)
+ }
+
+ if (retarget_service.get_target_armature().children.length > 0) {
+ target_bones_meta = BoneAutoMapper.create_all_bone_metadata(retarget_service.get_target_armature(), false)
+ }
console.log('\n=== FINAL BONE METADATA ===')
console.log('Source bones metadata:', source_bones_meta)
@@ -146,62 +149,51 @@ export class BoneAutoMapper {
return mappings
}
- /**
- * Extract parent relationships for source skeleton (Mesh2Motion armature)
- * @param source_armature - Source skeleton armature
- * @returns Map of bone name -> parent bone name (or null if root)
- */
- private static extract_source_bone_parent_map (source_armature: Group): Map<string, string | null> {
- const parent_map = new Map<string, string | null>()
-
- source_armature.traverse((child: Object3D) => {
- if (child.type === 'Bone') {
- const bone = child
- const parent_name = (bone.parent != null && bone.parent.type === 'Bone') ? bone.parent.name : null
- parent_map.set(bone.name, parent_name)
- }
- })
-
- return parent_map
- }
-
- /**
- * Extract parent relationships for target skeleton (uploaded mesh)
- * @param target_skeleton_data - Target skeleton data
- * @returns Map of bone name -> parent bone name (or null if root)
- */
- private static extract_target_bone_parent_map (target_skeleton_data: Scene): Map<string, string | null> {
- const parent_map = new Map<string, string | null>()
-
- // Traverse all SkinnedMesh objects in the target skeleton data
- target_skeleton_data.traverse((child: Object3D) => {
- if (child.type === 'SkinnedMesh') {
- const skinned_mesh = child as SkinnedMesh
-
- // For each bone in the skeleton, record parent relationship
- for (const bone of skinned_mesh.skeleton.bones) {
- const parent_name = (bone.parent != null && bone.parent.type === 'Bone') ? bone.parent.name : null
- parent_map.set(bone.name, parent_name)
- }
- }
- })
-
- return parent_map
- }
-
/**
* Create metadata objects for all bones from a parent map
* @param parent_map - Map of bone name -> parent bone name
* @returns Array of BoneMetadata for all bones
*/
- private static create_all_bone_metadata (parent_map: Map<string, string | null>): BoneMetadata[] {
+ private static create_all_bone_metadata (armature: Group | Scene, is_source_skeleton: boolean): BoneMetadata[] {
const metadata_list: BoneMetadata[] = []
+ const bones: Bone[] = []
+
+ // the source M2M skeleton is a Group that contains a lot of bones...but no Skinned Meshes,
+ // so just traverse the tree and build the bone list directly
+ if (is_source_skeleton) {
+ armature.traverse((child: Object3D) => {
+ if (child.type === 'Bone') {
+ bones.push(child as Bone)
+ }
+ })
+ } else {
+ // if we find multiple skinned meshes, we will log a warning. Probably won't be an issue, but just putting
+ // this in there for now
+ let skinned_mesh_found: boolean = false
+ let skinned_mesh: SkinnedMesh | null = null
+ armature.traverse((child: Object3D) => {
+ if (child.type === 'SkinnedMesh') {
+ if (skinned_mesh_found) {
+ console.log('create_all_bone_metadata(): Multiple SkinnedMesh objects found in armature. Only processing the first one.')
+ return
+ }
+ skinned_mesh_found = true
+ skinned_mesh = child as SkinnedMesh
+ }
+ })
+
+ if (skinned_mesh !== null) {
+ bones.push(...(skinned_mesh as SkinnedMesh).skeleton.bones)
+ }
+ }
- for (const [bone_name, parent_name] of parent_map.entries()) {
+ // create metadata for each bone
+ for (const bone of bones) {
+ const bone_name: string = bone.name
+ const parent_name: string | null = (bone.parent !== null) ? bone.parent.name : null
const normalized_name: string = this.normalize_bone_name(bone_name)
const side: BoneSide = this.detect_bone_side(bone_name)
const category: BoneCategory = this.detect_bone_category(normalized_name)
-
const metadata: BoneMetadata = {
name: bone_name,
normalized_name,
@@ -209,7 +201,6 @@ export class BoneAutoMapper {
category,
parent_name
}
-
metadata_list.push(metadata)
}
@@ -263,19 +254,23 @@ export class BoneAutoMapper {
* Detect anatomical category based on normalized bone name and parent relationships
*/
private static detect_bone_category (normalized_name: string): BoneCategory {
- if (normalized_name.includes('spine') || normalized_name.includes('chest') || normalized_name.includes('neck') || normalized_name.includes('head') || normalized_name.includes('hips')) {
+ if (normalized_name.includes('spine') || normalized_name.includes('chest') || normalized_name.includes('neck') ||
+ normalized_name.includes('head') || normalized_name.includes('hips')) {
return BoneCategory.Torso
}
- if (normalized_name.includes('shoulder') || normalized_name.includes('upperarm') || normalized_name.includes('forearm') || normalized_name.includes('hand') || normalized_name.includes('wrist')) {
+ if (normalized_name.includes('shoulder') || normalized_name.includes('upperarm') ||
+ normalized_name.includes('forearm') || normalized_name.includes('hand') || normalized_name.includes('wrist')) {
return BoneCategory.Arms
}
- if (normalized_name.includes('thumb') || normalized_name.includes('index') || normalized_name.includes('middle') || normalized_name.includes('ring') || normalized_name.includes('pinky')) {
+ if (normalized_name.includes('thumb') || normalized_name.includes('index') || normalized_name.includes('middle') ||
+ normalized_name.includes('ring') || normalized_name.includes('pinky')) {
return BoneCategory.Hands
}
- if (normalized_name.includes('thigh') || normalized_name.includes('shin') || normalized_name.includes('knee') || normalized_name.includes('foot') || normalized_name.includes('toe') || normalized_name.includes('calf')) {
+ if (normalized_name.includes('thigh') || normalized_name.includes('shin') || normalized_name.includes('knee') ||
+ normalized_name.includes('foot') || normalized_name.includes('toe') || normalized_name.includes('calf')) {
return BoneCategory.Legs
}
diff --git a/src/retarget/steps/StepBoneMapping.ts b/src/retarget/steps/StepBoneMapping.ts
index 4e5e592..d352845 100644
--- a/src/retarget/steps/StepBoneMapping.ts
+++ b/src/retarget/steps/StepBoneMapping.ts
@@ -118,10 +118,11 @@ export class StepBoneMapping extends EventTarget {
// Extract bone names from target skeleton (uploaded mesh)
public get_target_bone_names (): string[] {
- const skinned_meshes: SkinnedMesh[] = AnimationRetargetService.getInstance().get_target_skinned_meshes()
+ const target_armature: SkinnedMesh[] = AnimationRetargetService.getInstance().get_target_skinned_meshes()
- if (skinned_meshes.length === 0) {
- console.warn('get_target_bone_names(): No skinned meshes found in target armature.')
+ // when we first load the page, the target skinned mesh will be empty.
+ // this is expeted, so just return an empty list
+ if (target_armature.length === 0) {
return []
}
@@ -132,7 +133,7 @@ export class StepBoneMapping extends EventTarget {
// Target skeleton data contains SkinnedMesh objects with skeleton property
// Use Set to avoid duplicates when multiple SkinnedMesh share the same skeleton
- skinned_meshes.forEach((skinned_mesh) => {
+ target_armature.forEach((skinned_mesh) => {
skinned_mesh.skeleton.bones.forEach((bone) => {
bone_names_set.add(bone.name)
})