Import a 3D Model and automatically assign and export animations
TypeScript
Move source armature data to be stored in animation retargeting service
Scott Petrovic0ab529126d50
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.tsindex 131d41e..b4abe91 100644--- a/src/retarget/AnimationRetargetService.ts+++ b/src/retarget/AnimationRetargetService.ts@@ -18,11 +18,21 @@ export interface TrackNameParts {export class AnimationRetargetService {private static instance: AnimationRetargetService | null = null+ // #region Central Data Properties/*** Get/set for the skeleton type. This will be the source of truth* for other classes to grab this data*/private skeleton_type: SkeletonType = SkeletonType.None+ private source_armature: Group | null = null+ public set_source_armature (armature: Group | null): void {+ this.source_armature = armature+ }++ public get_source_armature (): Group | null {+ return this.source_armature+ }+public set_skeleton_type (type: SkeletonType): void {this.skeleton_type = type}@@ -31,6 +41,8 @@ export class AnimationRetargetService {return this.skeleton_type}+ // #endregion+private constructor () {}public static getInstance (): AnimationRetargetService {@@ -54,7 +66,6 @@ export class AnimationRetargetService {source_clip: AnimationClip,bone_mappings: Map<string, string>,target_mapping_type: TargetBoneMappingType,- source_armature: Group | null = null,target_skeleton_data: Scene | null = null,target_skinned_meshes: SkinnedMesh[] = []): AnimationClip {@@ -100,7 +111,6 @@ export class AnimationRetargetService {this.apply_bone_rotation_correction(retargeted_clip,bone_mappings,- source_armature,target_skeleton_data,target_skinned_meshes)@@ -133,7 +143,6 @@ export class AnimationRetargetService {private apply_bone_rotation_correction (animation_clip: AnimationClip,bone_mappings: Map<string, string>,- source_armature: Group | null,target_skeleton_data: Scene | null,target_skinned_meshes: SkinnedMesh[]): void {@@ -141,7 +150,6 @@ export class AnimationRetargetService {const delta = this.calculate_bone_rotation_delta(source_bone_name,target_bone_name,- source_armature,target_skeleton_data,target_skinned_meshes)@@ -171,17 +179,16 @@ export class AnimationRetargetService {private calculate_bone_rotation_delta (source_bone_name: string,target_bone_name: string,- source_armature: Group | null,target_skeleton_data: Scene | null,target_skinned_meshes: SkinnedMesh[]): Quaternion | null {- if (source_armature === null || target_skeleton_data === null) {+ if (this.source_armature === null || target_skeleton_data === null) {console.warn('Cannot calculate rotation delta: missing skeleton data')return null}// Find source bone and target bone with normalized matching- const source_bone = this.find_bone_by_name(source_armature, [], source_bone_name)+ const source_bone = this.find_bone_by_name(this.source_armature, [], source_bone_name)const target_bone = this.find_bone_by_name(target_skeleton_data, target_skinned_meshes, target_bone_name)if (source_bone === null || target_bone === null) {diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.tsindex ce68dde..17d0703 100644--- a/src/retarget/RetargetAnimationListing.ts+++ b/src/retarget/RetargetAnimationListing.ts@@ -210,7 +210,6 @@ export class RetargetAnimationListing extends EventTarget {display_clip,bone_mappings,this.step_bone_mapping.get_target_mapping_template(),- this.step_bone_mapping.get_source_armature(),this.step_bone_mapping.get_target_skeleton_data(),this.skinned_meshes_to_animate)@@ -250,7 +249,6 @@ export class RetargetAnimationListing extends EventTarget {this.skinned_meshes_to_animate,this.step_bone_mapping.get_bone_mapping(),this.step_bone_mapping.get_target_mapping_template(),- this.step_bone_mapping.get_source_armature(),this.step_bone_mapping.get_target_skeleton_data())this.step_export_retargeted_animations.export('retargeted_animations')diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.tsindex ba1f6d0..1480d9d 100644--- a/src/retarget/RetargetAnimationPreview.ts+++ b/src/retarget/RetargetAnimationPreview.ts@@ -186,7 +186,6 @@ export class RetargetAnimationPreview extends EventTarget {this.current_animation_clip,bone_mappings,this.step_bone_mapping.get_target_mapping_template(),- this.step_bone_mapping.get_source_armature(),this.step_bone_mapping.get_target_skeleton_data(),this.target_skinned_meshes)diff --git a/src/retarget/bone-automap/BoneAutoMapper.ts b/src/retarget/bone-automap/BoneAutoMapper.tsindex 0578969..8c60dea 100644--- a/src/retarget/bone-automap/BoneAutoMapper.ts+++ b/src/retarget/bone-automap/BoneAutoMapper.ts@@ -2,6 +2,7 @@ import { Bone, Group, Object3D, Scene, SkinnedMesh } from 'three'import { BoneCategoryMapper } from './BoneCategoryMapper'import { MixamoMapper } from './MixamoMapper'import { TargetBoneMappingType } from '../steps/StepBoneMapping'+import { AnimationRetargetService } from '../AnimationRetargetService'/*** Bone categories for grouping bones by anatomical area@@ -51,22 +52,28 @@ export class BoneAutoMapper {* @returns Map of target bone name -> source bone name*/public static auto_map_bones (- source_armature: Group,target_skeleton_data: Scene,target_bone_mapping_type: TargetBoneMappingType): Map<string, string> {// mappings: final output mapping of target bone name to source bone namelet mappings = new Map<string, string>()+ // Traverse source skeleton to build parent-child relationships+ const source_armature: Group | null = AnimationRetargetService.getInstance().get_source_armature()+ if (source_armature === null) {+ console.error('Source armature is null while extracting bone parent map.')+ 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> = this.extract_source_bone_parent_map(source_armature)- const target_parent_map: Map<string, string | null> = this.extract_target_bone_parent_map(target_skeleton_data)+ 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_skeleton_data)// Create metadata for both source and target bones- const source_bones_meta: BoneMetadata[] = this.create_all_bone_metadata(source_parent_map)- const target_bones_meta: BoneMetadata[] = this.create_all_bone_metadata(target_parent_map)+ 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)console.log('\n=== FINAL BONE METADATA ===')console.log('Source bones metadata:', source_bones_meta)@@ -147,7 +154,6 @@ export class BoneAutoMapper {private static extract_source_bone_parent_map (source_armature: Group): Map<string, string | null> {const parent_map = new Map<string, string | null>()- // Traverse source skeleton to build parent-child relationshipssource_armature.traverse((child: Object3D) => {if (child.type === 'Bone') {const bone = childdiff --git a/src/retarget/retarget.ts b/src/retarget/retarget.tsindex 577099b..cf14779 100644--- a/src/retarget/retarget.ts+++ b/src/retarget/retarget.ts@@ -82,10 +82,12 @@ class RetargetModule {// Listen for source skeleton (Mesh2Motion) loadedthis.step_load_source_skeleton.addEventListener('skeleton-loaded', () => {- const source_armature = this.step_load_source_skeleton.get_loaded_source_armature()+ const source_armature = this.step_load_source_skeleton.get_loaded_source_armature() as import('three').Groupconst skeleton_type = this.step_load_source_skeleton.get_skeleton_type()- AnimationRetargetService.getInstance().set_skeleton_type(skeleton_type)- this.step_bone_mapping.set_source_skeleton_data(source_armature)+ const anim_service = AnimationRetargetService.getInstance()+ anim_service.set_skeleton_type(skeleton_type)+ anim_service.set_source_armature(source_armature)+ this.step_bone_mapping.set_source_skeleton_data()})// Listen for target model (user-uploaded) loadeddiff --git a/src/retarget/steps/StepBoneMapping.ts b/src/retarget/steps/StepBoneMapping.tsindex 2ee1cc8..1ea4154 100644--- a/src/retarget/steps/StepBoneMapping.ts+++ b/src/retarget/steps/StepBoneMapping.ts@@ -2,6 +2,7 @@ import { Group, type Object3D, type Object3DEventMap, type Scene, type SkinnedMeimport { SkeletonType } from '../../lib/enums/SkeletonType.ts'import { BoneAutoMapper } from '../bone-automap/BoneAutoMapper.ts'import { MixamoMapper } from '../bone-automap/MixamoMapper.ts'+import { AnimationRetargetService } from '../AnimationRetargetService.ts'// when we are auto-mapping, keep track of what rig type we matched target againstexport enum TargetBoneMappingType {@@ -17,9 +18,6 @@ export class StepBoneMapping extends EventTarget {private target_skeleton_data: Scene | null = nullprivate target_mapping_template: TargetBoneMappingType = TargetBoneMappingType.None- private source_armature: Group = new Group()- private source_skeleton_type: SkeletonType = SkeletonType.None-// DOM referencesprivate source_bones_list: HTMLDivElement | null = nullprivate target_bones_list: HTMLDivElement | null = null@@ -75,10 +73,7 @@ export class StepBoneMapping extends EventTarget {}}- public set_source_skeleton_data (armature: Group, skeleton_type: SkeletonType): void {- this.source_armature = armature-- this.source_skeleton_type = skeleton_type+ public set_source_skeleton_data (): void {this.update_source_bones_list()this.update_auto_map_button_visibility()}@@ -91,7 +86,7 @@ export class StepBoneMapping extends EventTarget {}public has_source_skeleton (): boolean {- return this.source_armature !== null+ return AnimationRetargetService.getInstance().get_source_armature() !== null}public has_target_skeleton (): boolean {@@ -103,30 +98,22 @@ export class StepBoneMapping extends EventTarget {}// Getters- public get_source_armature (): Group {- return this.source_armature- }-public get_target_skeleton_data (): Scene | null {return this.target_skeleton_data}- public get_source_skeleton_type (): SkeletonType {- return this.source_skeleton_type- }-public get_target_mapping_template (): TargetBoneMappingType {return this.target_mapping_template}// Extract bone names from source skeleton (Mesh2Motion skeleton)public get_source_bone_names (): string[] {- if (this.source_armature === null) {+ if (AnimationRetargetService.getInstance().get_source_armature() === null) {return []}const bone_names: string[] = []- this.source_armature.traverse((child) => {+ AnimationRetargetService.getInstance().get_source_armature()?.traverse((child) => {if (child.type === 'Bone') {bone_names.push(child.name)}@@ -380,9 +367,13 @@ export class StepBoneMapping extends EventTarget {this.target_mapping_template = TargetBoneMappingType.Custom}+ if (this.target_skeleton_data === null) {+ console.error('Target skeleton data is null during auto-mapping')+ return+ }+// Use BoneAutoMapper to generate mappingsconst auto_mappings = BoneAutoMapper.auto_map_bones(- this.source_armature,this.target_skeleton_data,this.target_mapping_template)diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.tsindex 99fb067..2e4b176 100644--- a/src/retarget/steps/StepExportRetargetedAnimations.ts+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts@@ -9,16 +9,14 @@ export class StepExportRetargetedAnimations extends EventTarget {// Retargeting-related propertiesprivate bone_mapping = new Map<string, string>()private target_mapping_type: TargetBoneMappingType = TargetBoneMappingType.None- private source_armature: Group | null = nullprivate target_skeleton_data: Scene | null = nullprivate target_skinned_meshes: SkinnedMesh[] = []private target_rig_scene: Scene | null = nullpublic setup_retargeting (target_rig_scene: Scene, meshes: SkinnedMesh[], bone_mapping: Map<string, string>,- mapping_type: TargetBoneMappingType, armature: Group, skeleton_data: Scene | null): void {+ mapping_type: TargetBoneMappingType, skeleton_data: Scene | null): void {this.bone_mapping = bone_mappingthis.target_mapping_type = mapping_type- this.source_armature = armaturethis.target_skeleton_data = skeleton_datathis.target_skinned_meshes = meshesthis.target_rig_scene = target_rig_scene@@ -51,7 +49,6 @@ export class StepExportRetargetedAnimations extends EventTarget {clip,this.bone_mapping,this.target_mapping_type,- this.source_armature,this.target_skeleton_data,this.target_skinned_meshes // use the meshes being exported as the target)@@ -97,11 +94,6 @@ export class StepExportRetargetedAnimations extends EventTarget {})}- /**- *- * @param blob- * @param filename- */private save_file (blob: Blob, filename: string): void {const export_button_hidden_link: HTMLAnchorElement | null = document.querySelector('#download-hidden-link')if (export_button_hidden_link != null) {