Import a 3D Model and automatically assign and export animations
TypeScript
Start to move importing custom animations to a more event driven architecture
Scott Petrovicffddcddf1e8c
diff --git a/src/lib/processes/animations-listing/CustomAnimationImporter.ts b/src/lib/processes/animations-listing/CustomAnimationImporter.tsindex 460a49f..3e64391 100644--- a/src/lib/processes/animations-listing/CustomAnimationImporter.ts+++ b/src/lib/processes/animations-listing/CustomAnimationImporter.ts@@ -1,97 +1,106 @@import { UI } from '../../UI.ts'import { ModalDialog } from '../../ModalDialog.ts'import { type SkinnedMesh } from 'three'-import { AnimationLoader, NoAnimationsError, IncompatibleSkeletonError, LoadError } from './AnimationLoader.ts'+import { type AnimationLoader, NoAnimationsError, IncompatibleSkeletonError, LoadError } from './AnimationLoader.ts'import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'/*** Handles the importing of custom animations from GLB files.* This class encapsulates the UI and logic for the import process.*/-export class CustomAnimationImporter {+export class CustomAnimationImporter extends EventTarget {private readonly ui: UIprivate readonly animation_loader: AnimationLoader- private readonly get_skinned_meshes: () => SkinnedMesh[]- private readonly get_skeleton_scale: () => number- private readonly is_loading_default_animations: () => boolean- private readonly on_import_success: (new_clips: TransformedAnimationClipPair[]) => void+ private skinned_meshes_to_animate: SkinnedMesh[]+ private skeleton_scale: number+ private enabled: boolean = trueconstructor (animation_loader: AnimationLoader,- get_skinned_meshes: () => SkinnedMesh[],- get_skeleton_scale: () => number,- is_loading_default_animations: () => boolean,- on_import_success: (new_clips: TransformedAnimationClipPair[]) => void+ skinned_meshes_to_animate: SkinnedMesh[],+ skeleton_scale: number) {+ super()this.ui = UI.getInstance()this.animation_loader = animation_loader- this.get_skinned_meshes = get_skinned_meshes- this.get_skeleton_scale = get_skeleton_scale- this.is_loading_default_animations = is_loading_default_animations- this.on_import_success = on_import_success+ this.skinned_meshes_to_animate = skinned_meshes_to_animate+ this.skeleton_scale = skeleton_scale+ }++ public set_import_context (skinned_meshes_to_animate: SkinnedMesh[], skeleton_scale: number): void {+ this.skinned_meshes_to_animate = skinned_meshes_to_animate+ this.skeleton_scale = skeleton_scale+ }++ public set_enabled (enabled: boolean): void {+ this.enabled = enabled+ if (this.ui.dom_import_animations_button != null) {+ this.ui.dom_import_animations_button.disabled = !enabled+ }+ }++ public is_enabled (): boolean {+ return this.enabled}public add_event_listeners (): void {this.ui.dom_import_animations_button?.addEventListener('click', () => {- if (this.ui.dom_import_animations_button?.disabled === true || this.is_loading_default_animations()) {+ if (!this.is_enabled()) {return}this.ui.dom_import_animations_input?.click()})- this.ui.dom_import_animations_input?.addEventListener('change', async (event) => {- if (this.ui.dom_import_animations_button?.disabled === true || this.is_loading_default_animations()) {- return- }- const input = event.target as HTMLInputElement- const files = input.files- if (files === null || files.length === 0) {- return- }+ this.ui.dom_import_animations_input?.addEventListener('change', (event) => {+ void this.handle_import_animations_input_change(event)+ })+ }- const import_button = this.ui.dom_import_animations_button- const previous_disabled_state: boolean | undefined = import_button?.disabled- if (import_button != null) {- import_button.disabled = true- }+ private async handle_import_animations_input_change (event: Event): Promise<void> {+ if (!this.is_enabled()) {+ return+ }+ const input = event.target as HTMLInputElement+ const files = input.files+ if (files === null || files.length === 0) {+ return+ }- try {- for (const file of Array.from(files)) {- const file_name = file.name.toLowerCase()- if (!file_name.endsWith('.glb')) {- new ModalDialog('Unsupported file type. Please select a GLB file.', 'Error').show()- continue- }- await this.import_animation_glb(file)- }- } finally {- input.value = ''- if (import_button != null && previous_disabled_state !== undefined) {- import_button.disabled = previous_disabled_state+ this.set_enabled(false)++ try {+ for (const file of Array.from(files)) {+ const file_name = file.name.toLowerCase()+ if (!file_name.endsWith('.glb')) {+ new ModalDialog('Unsupported file type. Please select a GLB file.', 'Error').show()+ continue}+ await this.import_animation_glb(file)}- })+ } finally {+ input.value = ''+ this.set_enabled(true)+ }}private async import_animation_glb (file: File): Promise<{ success: boolean, clipCount: number }> {try {const new_animation_clips = await this.animation_loader.load_animations_from_file(file,- this.get_skinned_meshes(),- this.get_skeleton_scale()+ this.skinned_meshes_to_animate,+ this.skeleton_scale)- this.on_import_success(new_animation_clips)+ this.dispatchEvent(new CustomEvent<TransformedAnimationClipPair[]>('import-success', {+ detail: new_animation_clips+ }))- // Show success message only for user imports (not default animations)- if (!this.is_loading_default_animations()) {- const animation_count = new_animation_clips.length- const animation_word = animation_count === 1 ? 'animation' : 'animations'- new ModalDialog(- 'Import Success',- `${animation_count} ${animation_word} Imported successfully`- ).show()- }+ const animation_count = new_animation_clips.length+ const animation_word = animation_count === 1 ? 'animation' : 'animations'+ new ModalDialog(+ 'Import Success',+ `${animation_count} ${animation_word} Imported successfully`+ ).show()return { success: true, clipCount: new_animation_clips.length }} catch (error) {diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.tsindex e95b3f1..5ff36bc 100644--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts@@ -1,6 +1,5 @@import { UI } from '../../UI.ts'import { AnimationPlayer } from './AnimationPlayer.ts'-import { ModalDialog } from '../../ModalDialog.ts'import {type AnimationClip, AnimationMixer, type SkinnedMesh, type AnimationAction, Object3D@@ -65,14 +64,15 @@ export class StepAnimationsListing extends EventTarget {this.custom_animation_importer = new CustomAnimationImporter(this.animation_loader,- () => this.skinned_meshes_to_animate,- () => this.skeleton_scale,- () => this.is_loading_default_animations,- (new_clips: TransformedAnimationClipPair[]) => {- this.animation_clips_loaded.push(...new_clips)- this.onAllAnimationsLoaded()- }+ this.skinned_meshes_to_animate,+ this.skeleton_scale)++ this.custom_animation_importer.addEventListener('import-success', (event: Event) => {+ const new_clips = (event as CustomEvent<TransformedAnimationClipPair[]>).detail+ this.animation_clips_loaded.push(...new_clips)+ this.onAllAnimationsLoaded()+ })}public begin (skeleton_type: SkeletonType, skeleton_scale: number): void {@@ -95,6 +95,8 @@ export class StepAnimationsListing extends EventTarget {}this.reset_step_data()+ this.custom_animation_importer.set_import_context(this.skinned_meshes_to_animate, this.skeleton_scale)+ this.custom_animation_importer.set_enabled(!this.is_loading_default_animations)this.skeleton_type = skeleton_type@@ -115,9 +117,9 @@ export class StepAnimationsListing extends EventTarget {this.animation_mixer = new AnimationMixer(new Object3D())this.current_playing_index = 0this.animation_player.clear_animation()+ this.custom_animation_importer.set_import_context(this.skinned_meshes_to_animate, this.skeleton_scale)}-public mixer (): AnimationMixer {return this.animation_mixer}@@ -138,14 +140,13 @@ export class StepAnimationsListing extends EventTarget {public load_and_apply_default_animation_to_skinned_mesh (final_skinned_meshes: SkinnedMesh[]): void {this.skinned_meshes_to_animate = final_skinned_meshes+ this.custom_animation_importer.set_import_context(this.skinned_meshes_to_animate, this.skeleton_scale)// Set the animations file path on the loaderthis.animation_loader.set_animations_file_path(this.animations_file_path)this.is_loading_default_animations = true- if (this.ui.dom_import_animations_button != null) {- this.ui.dom_import_animations_button.disabled = true- }+ this.custom_animation_importer.set_enabled(false)// Reset the animation clips loadedthis.animation_clips_loaded = []@@ -161,18 +162,14 @@ export class StepAnimationsListing extends EventTarget {.catch((error: Error) => {console.error('Failed to load animations:', error)this.is_loading_default_animations = false- if (this.ui.dom_import_animations_button != null) {- this.ui.dom_import_animations_button.disabled = false- }+ this.custom_animation_importer.set_enabled(true)// You could emit an error event here or show a user-friendly message})}private onAllAnimationsLoaded (): void {this.is_loading_default_animations = false- if (this.ui.dom_import_animations_button != null) {- this.ui.dom_import_animations_button.disabled = false- }+ this.custom_animation_importer.set_enabled(true)// sort all animation names alphabeticallythis.animation_clips_loaded.sort((a: TransformedAnimationClipPair, b: TransformedAnimationClipPair) => {if (a.display_animation_clip.name < b.display_animation_clip.name) { return -1 }