Import a 3D Model and automatically assign and export animations
TypeScript
Refactor animation import logic and update tooltips
google-labs-jules[bot]4696142a6a00
diff --git a/src/create.html b/src/create.htmlindex fd48013..9ff290e 100644--- a/src/create.html+++ b/src/create.html@@ -371,7 +371,7 @@</label></div>- <button class="secondary-button" id="import-animations-button" data-tippy-content="Import GLB animations" aria-label="Import GLB animations">+ <button class="secondary-button" id="import-animations-button" data-tippy-content="Import GLB animations. Only Mesh2Motion rigs are supported at this time." aria-label="Import GLB animations. Only Mesh2Motion rigs are supported at this time."><span class="material-symbols-outlined">upload</span></button><input id="import-animations-input" type="file" accept=".glb" multiple style="display:none" />diff --git a/src/index.html b/src/index.htmlindex 18b8451..8bafa46 100644--- a/src/index.html+++ b/src/index.html@@ -153,7 +153,7 @@</label></div>- <button class="secondary-button" id="import-animations-button" data-tippy-content="Import GLB animations" aria-label="Import GLB animations">+ <button class="secondary-button" id="import-animations-button" data-tippy-content="Import GLB animations. Only Mesh2Motion rigs are supported at this time." aria-label="Import GLB animations. Only Mesh2Motion rigs are supported at this time."><span class="material-symbols-outlined">upload</span></button><input id="import-animations-input" type="file" accept=".glb" multiple style="display:none" />diff --git a/src/lib/processes/animations-listing/CustomAnimationImporter.ts b/src/lib/processes/animations-listing/CustomAnimationImporter.tsnew file mode 100644index 0000000..9d76028--- /dev/null+++ b/src/lib/processes/animations-listing/CustomAnimationImporter.ts@@ -0,0 +1,123 @@+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 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 {+ private readonly ui: UI+ private 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++ constructor (+ animation_loader: AnimationLoader,+ get_skinned_meshes: () => SkinnedMesh[],+ get_skeleton_scale: () => number,+ is_loading_default_animations: () => boolean,+ on_import_success: (new_clips: TransformedAnimationClipPair[]) => void+ ) {+ 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+ }++ 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()) {+ 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+ }++ 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+ }++ 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+ }+ }+ })+ }++ 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.on_import_success(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(+ 'success',+ `${animation_count} ${animation_word} imported successfully`+ ).show()+ }++ return { success: true, clipCount: new_animation_clips.length }+ } catch (error) {+ console.error('Failed to import animations:', error)++ if (error instanceof NoAnimationsError) {+ new ModalDialog('import error', 'no animations found in that glb file').show()+ return { success: false, clipCount: 0 }+ }++ if (error instanceof IncompatibleSkeletonError) {+ const error_message = error.message === 'bone_count_mismatch'+ ? 'bone count mismatch'+ : 'bone names don\'t match'+ new ModalDialog('import error', error_message).show()+ return { success: false, clipCount: 0 }+ }++ if (error instanceof LoadError) {+ new ModalDialog('import error', 'failed to load the animation file').show()+ return { success: false, clipCount: 0 }+ }++ // Unknown error+ new ModalDialog('import error', 'failed to import animations from the glb file').show()+ return { success: false, clipCount: 0 }+ }+ }+}diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.tsindex 89d9e4d..e95b3f1 100644--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts@@ -7,7 +7,8 @@ import {} from 'three'import { AnimationUtility } from './AnimationUtility.ts'-import { AnimationLoader, type AnimationLoadProgress, NoAnimationsError, IncompatibleSkeletonError, LoadError } from './AnimationLoader.ts'+import { AnimationLoader, type AnimationLoadProgress } from './AnimationLoader.ts'+import { CustomAnimationImporter } from './CustomAnimationImporter.ts'import { SkeletonType } from '../../enums/SkeletonType.ts'import { Utility } from '../../Utilities.ts'@@ -34,6 +35,8 @@ export class StepAnimationsListing extends EventTarget {// we will use this to scale all position animation keyframes (uniform scale)private skeleton_scale: number = 1.0+ private readonly custom_animation_importer: CustomAnimationImporter+private _added_event_listeners: boolean = falseprivate is_loading_default_animations: boolean = false@@ -59,6 +62,17 @@ export class StepAnimationsListing extends EventTarget {this.ui = UI.getInstance()this.animation_player = new AnimationPlayer()this.theme_manager = theme_manager++ 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()+ }+ )}public begin (skeleton_type: SkeletonType, skeleton_scale: number): void {@@ -367,45 +381,7 @@ export class StepAnimationsListing extends EventTarget {this.play_animation(this.current_playing_index)})- this.ui.dom_import_animations_button?.addEventListener('click', () => {- if (this.ui.dom_import_animations_button?.disabled === true || this.is_loading_default_animations) {- 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- }-- 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- }-- 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.custom_animation_importer.add_event_listeners()// helps ensure we don't add event listeners multiple timesthis.has_added_event_listeners = true@@ -438,53 +414,4 @@ export class StepAnimationsListing extends EventTarget {}return this.animation_search.get_selected_animation_indices()}-- 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.skinned_meshes_to_animate,- this.skeleton_scale- )-- this.animation_clips_loaded.push(...new_animation_clips)- this.onAllAnimationsLoaded()-- // 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(- 'success',- `${animation_count} ${animation_word} imported successfully`- ).show()- }-- return { success: true, clipCount: new_animation_clips.length }- } catch (error) {- console.error('Failed to import animations:', error)-- if (error instanceof NoAnimationsError) {- new ModalDialog('import error', 'no animations found in that glb file').show()- return { success: false, clipCount: 0 }- }-- if (error instanceof IncompatibleSkeletonError) {- const error_message = error.message === 'bone_count_mismatch'- ? 'bone count mismatch'- : 'bone names don\'t match'- new ModalDialog('import error', error_message).show()- return { success: false, clipCount: 0 }- }-- if (error instanceof LoadError) {- new ModalDialog('import error', 'failed to load the animation file').show()- return { success: false, clipCount: 0 }- }-- // Unknown error- new ModalDialog('import error', 'failed to import animations from the glb file').show()- return { success: false, clipCount: 0 }- }- }}