Import a 3D Model and automatically assign and export animations
TypeScript
moved skinned mesh logic into shared retarget service
Scott Petrovic03aa4b671056
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.tsindex 54ca592..fc65c96 100644--- a/src/retarget/AnimationRetargetService.ts+++ b/src/retarget/AnimationRetargetService.ts@@ -18,7 +18,7 @@ export interface TrackNameParts {export class AnimationRetargetService {private static instance: AnimationRetargetService | null = null- // #region Central Data Properties+ // #region GETTER/SETTER/*** Get/set for the skeleton type. This will be the source of truth* for other classes to grab this data@@ -26,6 +26,7 @@ export class AnimationRetargetService {private source_armature: Group = new Group()private skeleton_type: SkeletonType = SkeletonType.Noneprivate target_armature: Scene = new Scene()+ private target_skinned_meshes: SkinnedMesh[] = []private target_mapping_type: TargetBoneMappingType = TargetBoneMappingType.Nonepublic set_source_armature (armature: Group): void {@@ -36,8 +37,20 @@ export class AnimationRetargetService {return this.source_armature}- public set_target_armature (armature: Scene): void {- this.target_armature = armature+ public set_target_armature (new_armature: Scene): void {+ this.target_armature = new_armature++ // re-calculate skinned meshes from target armature scene+ this.target_skinned_meshes = []+ this.target_armature.traverse((child) => {+ if (child.type === 'SkinnedMesh') {+ this.target_skinned_meshes.push(child as SkinnedMesh)+ }+ })+ }++ public get_target_skinned_meshes (): SkinnedMesh[] {+ return this.target_skinned_meshes}public get_target_armature (): Scene {@@ -75,14 +88,11 @@ export class AnimationRetargetService {* Retarget an animation clip using bone mappings* @param source_clip - The original animation clip from the source skeleton* @param bone_mappings - Map of target bone name -> source bone name- * @param target_armature - The target skeleton data (for bone rotation corrections)- * @param target_skinned_meshes - The target skinned meshes (for finding bones)* @returns A new animation clip retargeted for the target skeleton*/public retarget_animation_clip (source_clip: AnimationClip,- bone_mappings: Map<string, string>,- target_skinned_meshes: SkinnedMesh[] = []+ bone_mappings: Map<string, string>): AnimationClip {const new_tracks: any[] = [] // store new retargeted tracks@@ -125,8 +135,7 @@ export class AnimationRetargetService {if (this.target_mapping_type === TargetBoneMappingType.Mixamo) {this.apply_bone_rotation_correction(retargeted_clip,- bone_mappings,- target_skinned_meshes+ bone_mappings)}@@ -156,14 +165,12 @@ export class AnimationRetargetService {*/private apply_bone_rotation_correction (animation_clip: AnimationClip,- bone_mappings: Map<string, string>,- target_skinned_meshes: SkinnedMesh[]+ bone_mappings: Map<string, string>): void {bone_mappings.forEach((source_bone_name, target_bone_name) => {const delta = this.calculate_bone_rotation_delta(source_bone_name,- target_bone_name,- target_skinned_meshes+ target_bone_name)if (delta !== null) {@@ -190,8 +197,7 @@ export class AnimationRetargetService {*/private calculate_bone_rotation_delta (source_bone_name: string,- target_bone_name: string,- target_skinned_meshes: SkinnedMesh[]+ target_bone_name: string): Quaternion | null {if (this.source_armature === null || this.target_armature === null) {console.warn('Cannot calculate rotation delta: missing skeleton data')@@ -200,7 +206,7 @@ export class AnimationRetargetService {// Find source bone and target bone with normalized matchingconst source_bone = this.find_bone_by_name(this.source_armature, [], source_bone_name)- const target_bone = this.find_bone_by_name(this.target_armature, target_skinned_meshes, target_bone_name)+ const target_bone = this.find_bone_by_name(this.target_armature, this.target_skinned_meshes, target_bone_name)if (source_bone === null || target_bone === null) {const source_label = source_bone === null ? 'null' : source_bone.namediff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.tsindex 3b9c39f..f937727 100644--- a/src/retarget/RetargetAnimationListing.ts+++ b/src/retarget/RetargetAnimationListing.ts@@ -21,8 +21,6 @@ export class RetargetAnimationListing extends EventTarget {private animation_clips_loaded: TransformedAnimationClipPair[] = []private animation_mixer: AnimationMixer = new AnimationMixer(new Object3D())- private skinned_meshes_to_animate: SkinnedMesh[] = []-private _added_event_listeners: boolean = falsepublic animation_search: AnimationSearch | null = null@@ -49,7 +47,6 @@ export class RetargetAnimationListing extends EventTarget {public reset_step_data (): void {this.animation_clips_loaded = []- this.skinned_meshes_to_animate = []this.animation_mixer = new AnimationMixer(new Object3D())this.animation_player.clear_animation()}@@ -74,18 +71,6 @@ export class RetargetAnimationListing extends EventTarget {}public load_and_apply_default_animation_to_skinned_mesh (): void {- // load the Group skinned mesh and convert to normal SkinnedMesh array- // the skinned meshes might be buried deep in the hierarchy, so traverse the scene- const skinned_meshes: SkinnedMesh[] = []- AnimationRetargetService.getInstance().get_target_armature().traverse((child: Object3D) => {- if ((child as SkinnedMesh).isSkinnedMesh) {- skinned_meshes.push(child as SkinnedMesh)- }- })- this.skinned_meshes_to_animate = skinned_meshes-- console.log(`Preparing to load animations for ${this.skinned_meshes_to_animate.length} skinned meshes`)-this.animation_loader.set_animations_file_path('../../animations/')this.animation_clips_loaded = []this.animation_mixer = new AnimationMixer(new Object3D())@@ -203,12 +188,12 @@ export class RetargetAnimationListing extends EventTarget {// Retarget the animation using the shared serviceconst retargeted_clip: AnimationClip = AnimationRetargetService.getInstance().retarget_animation_clip(display_clip,- bone_mappings,- this.skinned_meshes_to_animate+ bone_mappings)// Create new actions for each skinned mesh with the retargeted animation- const actions: AnimationAction[] = this.skinned_meshes_to_animate.map((mesh) => {+ const skinned_meshes_to_animate: SkinnedMesh[] = AnimationRetargetService.getInstance().get_target_skinned_meshes()+ const actions: AnimationAction[] = skinned_meshes_to_animate.map((mesh) => {const action = this.animation_mixer.clipAction(retargeted_clip, mesh)action.reset()action.play()@@ -237,9 +222,7 @@ export class RetargetAnimationListing extends EventTarget {// configure the export out step with retargeting infothis.step_export_retargeted_animations.setup_retargeting(- this.skinned_meshes_to_animate,- this.step_bone_mapping.get_bone_mapping(),- this.step_bone_mapping.get_target_skeleton_data()+ this.step_bone_mapping.get_bone_mapping())this.step_export_retargeted_animations.export('retargeted_animations')})@@ -277,11 +260,11 @@ export class RetargetAnimationListing extends EventTarget {this.animation_player.update(delta_time)// CRITICAL: Update the skeleton and skinned meshes after animation changes the bones- // Why do I need this when I don't need it in the main Mesh2Motion engine?- this.skinned_meshes_to_animate.forEach((skinned_mesh) => {- skinned_mesh.skeleton.bones.forEach(bone => {- bone.updateMatrixWorld(true)- })- })+ // const skinned_meshes: SkinnedMesh[] = AnimationRetargetService.getInstance().get_target_skinned_meshes()+ // skinned_meshes.forEach((skinned_mesh) => {+ // skinned_mesh.skeleton.bones.forEach(bone => {+ // bone.updateMatrixWorld(true)+ // })+ // })}}diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.tsindex 2bfedd5..0653ca3 100644--- a/src/retarget/RetargetAnimationPreview.ts+++ b/src/retarget/RetargetAnimationPreview.ts@@ -17,7 +17,6 @@ export class RetargetAnimationPreview extends EventTarget {private animation_mixer: AnimationMixer | null = nullprivate current_animation_clip: AnimationClip | null = nullprivate retargeted_animation_clip: AnimationClip | null = null- private target_skinned_meshes: SkinnedMesh[] = []private is_preview_active: boolean = falseprivate has_added_event_listeners: boolean = false@@ -63,24 +62,25 @@ export class RetargetAnimationPreview extends EventTarget {return}- const target_skeleton_data = this.step_bone_mapping.get_target_skeleton_data()- if (target_skeleton_data === null) {- console.log('Cannot start preview: target skeleton data is null')- return- }-- // Extract skinned meshes from target skeleton data- this.target_skinned_meshes = []- target_skeleton_data.traverse((child) => {- if (child.type === 'SkinnedMesh') {- this.target_skinned_meshes.push(child as SkinnedMesh)- }- })-- if (this.target_skinned_meshes.length === 0) {- console.log('Cannot start preview: no skinned meshes found in target')- return- }+ // bone mappings might have chnaged+ // const target_skeleton_data = this.step_bone_mapping.get_target_skeleton_data()+ // if (target_skeleton_data === null) {+ // console.log('Cannot start preview: target skeleton data is null')+ // return+ // }++ // // Extract skinned meshes from target skeleton data+ // this.target_skinned_meshes = []+ // target_skeleton_data.traverse((child) => {+ // if (child.type === 'SkinnedMesh') {+ // this.target_skinned_meshes.push(child as SkinnedMesh)+ // }+ // })++ // if (this.target_skinned_meshes.length === 0) {+ // console.log('Cannot start preview: no skinned meshes found in target')+ // return+ // }// Create animation mixer for the target skeleton// Use a dummy Object3D as root since we apply animations directly to skinned meshes@@ -184,8 +184,7 @@ export class RetargetAnimationPreview extends EventTarget {// Create retargeted animation clip using shared servicethis.retargeted_animation_clip = AnimationRetargetService.getInstance().retarget_animation_clip(this.current_animation_clip,- bone_mappings,- this.target_skinned_meshes+ bone_mappings)this.play_default_animation()@@ -203,7 +202,8 @@ export class RetargetAnimationPreview extends EventTarget {}// Apply the retargeted animation to all target skinned meshes- this.target_skinned_meshes.forEach((skinned_mesh) => {+ const skinned_meshes: SkinnedMesh[] = AnimationRetargetService.getInstance().get_target_skinned_meshes()+ skinned_meshes.forEach((skinned_mesh) => {const action: AnimationAction = this.animation_mixer.clipAction(this.retargeted_animation_clip, skinned_mesh)action.reset()action.play() // should loop automatically@@ -234,7 +234,8 @@ export class RetargetAnimationPreview extends EventTarget {// CRITICAL: Update the skeleton and skinned meshes after animation changes the bones// Why do I need this when I don't need it in the main Mesh2Motion engine?- this.target_skinned_meshes.forEach((skinned_mesh) => {+ const skinned_meshes: SkinnedMesh[] = AnimationRetargetService.getInstance().get_target_skinned_meshes()+ skinned_meshes.forEach((skinned_mesh) => {skinned_mesh.skeleton.bones.forEach(bone => {bone.updateMatrixWorld(true)})diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.tsindex 7cff5b4..a717ef0 100644--- a/src/retarget/steps/StepExportRetargetedAnimations.ts+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts@@ -1,4 +1,4 @@-import { type Scene, type SkinnedMesh, type AnimationClip } from 'three'+import { type Scene, type AnimationClip } from 'three'import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter'import { AnimationRetargetService } from '../AnimationRetargetService'@@ -7,15 +7,9 @@ export class StepExportRetargetedAnimations extends EventTarget {// Retargeting-related propertiesprivate bone_mapping = new Map<string, string>()- // target_mapping_type is now managed by AnimationRetargetService singleton- private target_skeleton_data: Scene | null = null- private target_skinned_meshes: SkinnedMesh[] = []- public setup_retargeting (meshes: SkinnedMesh[], bone_mapping: Map<string, string>,- skeleton_data: Scene | null): void {+ public setup_retargeting (bone_mapping: Map<string, string>): void {this.bone_mapping = bone_mapping- this.target_skeleton_data = skeleton_data- this.target_skinned_meshes = meshes}public set_animation_clips_to_export (all_animations_clips: AnimationClip[], animation_checkboxes: number[]): void {@@ -38,8 +32,7 @@ export class StepExportRetargetedAnimations extends EventTarget {retargeted_clips = this.animation_clips_to_export.map((clip) =>AnimationRetargetService.getInstance().retarget_animation_clip(clip,- this.bone_mapping,- this.target_skinned_meshes // use the meshes being exported as the target+ this.bone_mapping))console.log('Retargeted animation clips:', retargeted_clips)