Import a 3D Model and automatically assign and export animations
TypeScript
Use the scene directly when retargeting to prevent errors
Scott Petrovic27287c59768c
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.tsindex f49a0f3..6d4bf6d 100644--- a/src/retarget/RetargetAnimationListing.ts+++ b/src/retarget/RetargetAnimationListing.ts@@ -1,7 +1,7 @@import { AnimationPlayer } from '../lib/processes/animations-listing/AnimationPlayer.ts'import { AnimationSearch } from '../lib/processes/animations-listing/AnimationSearch.ts'import { AnimationLoader } from '../lib/processes/animations-listing/AnimationLoader.ts'-import { type AnimationClip, AnimationMixer, type SkinnedMesh, Object3D, type Object3DEventMap, type Group, Scene } from 'three'+import { type AnimationClip, AnimationMixer, type SkinnedMesh, Object3D, type Scene, type AnimationAction } from 'three'import { SkeletonType } from '../lib/enums/SkeletonType.ts'import type { ThemeManager } from '../lib/ThemeManager.ts'import { type TransformedAnimationClipPair } from '../lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts'@@ -21,7 +21,9 @@ export class RetargetAnimationListing extends EventTarget {private readonly step_export_retargeted_animations: StepExportRetargetedAnimations = new StepExportRetargetedAnimations()private animation_clips_loaded: TransformedAnimationClipPair[] = []private animation_mixer: AnimationMixer = new AnimationMixer(new Object3D())+private skinned_meshes_to_animate: SkinnedMesh[] = []+private skeleton_type: SkeletonType = SkeletonType.Humanprivate _added_event_listeners: boolean = false@@ -30,6 +32,7 @@ export class RetargetAnimationListing extends EventTarget {private is_animations_active: boolean = false+ private target_rig_scene: Scene | null = nullprivate export_button: HTMLButtonElement | null = nullconstructor (theme_manager: ThemeManager, step_bone_mapping: StepBoneMapping) {@@ -77,6 +80,9 @@ export class RetargetAnimationListing extends EventTarget {}public load_and_apply_default_animation_to_skinned_mesh (retarget_meshes: Scene): void {+ // we will need this later when exporting+ this.target_rig_scene = retarget_meshes+// load the Group skinned mesh and convert to normal SkinnedMesh array// the skinned meshes might be buried deep in the hierarchy, so traverse the sceneconst skinned_meshes: SkinnedMesh[] = []@@ -202,7 +208,7 @@ export class RetargetAnimationListing extends EventTarget {}// Retarget the animation using the shared service- const retargeted_clip = AnimationRetargetService.retarget_animation_clip(+ const retargeted_clip: AnimationClip = AnimationRetargetService.retarget_animation_clip(display_clip,bone_mappings,this.step_bone_mapping.get_target_mapping_template(),@@ -212,7 +218,7 @@ export class RetargetAnimationListing extends EventTarget {)// Create new actions for each skinned mesh with the retargeted animation- const actions = this.skinned_meshes_to_animate.map((mesh) => {+ const actions: AnimationAction[] = this.skinned_meshes_to_animate.map((mesh) => {const action = this.animation_mixer.clipAction(retargeted_clip, mesh)action.reset()action.play()@@ -241,6 +247,7 @@ export class RetargetAnimationListing extends EventTarget {// configure the export out step with retargeting infothis.step_export_retargeted_animations.setup_retargeting(+ this.target_rig_scene,this.skinned_meshes_to_animate,this.step_bone_mapping.get_bone_mapping(),this.step_bone_mapping.get_target_mapping_template(),diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.tsindex ac2a698..4472521 100644--- a/src/retarget/steps/StepExportRetargetedAnimations.ts+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts@@ -12,14 +12,16 @@ export class StepExportRetargetedAnimations extends EventTarget {private source_armature: Group | null = nullprivate target_skeleton_data: Scene | null = nullprivate target_skinned_meshes: SkinnedMesh[] = []+ private target_rig_scene: Scene | null = null- public setup_retargeting (meshes: SkinnedMesh[], bone_mapping: Map<string, string>,+ public setup_retargeting (target_rig_scene: Scene, meshes: SkinnedMesh[], bone_mapping: Map<string, string>,mapping_type: TargetBoneMappingType, armature: Group, skeleton_data: Scene | null): void {this.bone_mapping = bone_mappingthis.target_mapping_type = mapping_typethis.source_armature = armaturethis.target_skeleton_data = skeleton_datathis.target_skinned_meshes = meshes+ this.target_rig_scene = target_rig_scene}public set_animation_clips_to_export (all_animations_clips: AnimationClip[], animation_checkboxes: number[]): void {@@ -37,6 +39,11 @@ export class StepExportRetargetedAnimations extends EventTarget {return}+ if (this.target_rig_scene == null) {+ console.log('ERROR: target rig scene is null, cannot export')+ return+ }+// Retarget all animation clips before exportlet retargeted_clips: AnimationClip[] = []retargeted_clips = this.animation_clips_to_export.map((clip) =>@@ -51,35 +58,9 @@ export class StepExportRetargetedAnimations extends EventTarget {)console.log('Retargeted animation clips:', retargeted_clips)-- const export_scene = new Scene()- // When exporting to a file, we need to temporarily move the skinned mesh to a new scene- // skinned meshes can only be part of one scene at a time, so we must move it back to- // its original parent after exporting- const original_parents = new Map<SkinnedMesh, Object3D | null>()-- this.target_skinned_meshes.forEach((final_skinned_mesh) => {- // Save the original parent- original_parents.set(final_skinned_mesh, final_skinned_mesh.parent)- export_scene.add(final_skinned_mesh)- })-- console.log('Retarget: trying to export the following scene as a GLB ', this.target_skinned_meshes)- console.log('Retarget: animations to export', retargeted_clips)-- this.export_glb(export_scene, retargeted_clips, filename)+ this.export_glb(this.target_rig_scene, retargeted_clips, filename).then(() => {- // Move the skinned meshes back to their original parents- this.target_skinned_meshes.forEach((final_skinned_mesh) => {- const original_par = original_parents.get(final_skinned_mesh)- if (original_par != null) {- original_par.add(final_skinned_mesh)- } else {- // If there was no original parent, remove it from the scene- export_scene.remove(final_skinned_mesh)- console.log('ERROR: No original parent found for skinned mesh when exporting and re-parenting to original scene')- }- })+ console.log('Exported GLB successfully')}).catch((error) => { console.log('Error exporting GLB:', error) })}