Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

improve event binding when updating custom animation import

Scott Petrovic973cef1565a9
diff --git a/src/lib/processes/animations-listing/AnimationValidation.ts b/src/lib/processes/animations-listing/AnimationValidation.ts
deleted file mode 100644
index b61d2b7..0000000
--- a/src/lib/processes/animations-listing/AnimationValidation.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import { type AnimationClip, type SkinnedMesh } from 'three'
-import { IncompatibleSkeletonError, NoAnimationsError } from './AnimationImportErrors.ts'
-
-/**
- * Validates that animation bones are compatible with target skeleton
- * @throws {IncompatibleSkeletonError} If bones don't match
- * @throws {NoAnimationsError} If no recognizable bone tracks found
- */
-export function validate_animation_bones_match (
- animation_clips: AnimationClip[],
- skinned_meshes: SkinnedMesh[]
-): void {
- if (skinned_meshes.length === 0) {
- throw new Error('No skinned meshes available to validate animations.')
- }
-
- // Get target bone names from skeleton
- const target_bone_names = new Set<string>()
- skinned_meshes.forEach((skinned_mesh) => {
- skinned_mesh.skeleton.bones.forEach((bone) => {
- target_bone_names.add(bone.name.toLowerCase())
- })
- })
-
- const animation_bone_names = get_animation_bone_names(animation_clips)
-
- if (animation_bone_names.size === 0) {
- throw new NoAnimationsError('Imported animations do not contain recognizable bone tracks.')
- }
-
- // Check bone count mismatch
- if (animation_bone_names.size !== target_bone_names.size) {
- throw new IncompatibleSkeletonError('bone count mismatch')
- }
-
- // Check bone name mismatch (animation has bones not in target skeleton)
- const missing_bones = Array.from(animation_bone_names).filter(bone => !target_bone_names.has(bone))
- if (missing_bones.length > 0) {
- throw new IncompatibleSkeletonError('bone names don\'t match')
- }
-}
-
-/**
- * Gets all bone names referenced in animation clips
- */
-function get_animation_bone_names (animation_clips: AnimationClip[]): Set<string> {
- const bone_names = new Set<string>()
- animation_clips.forEach((clip) => {
- clip.tracks.forEach((track) => {
- const bone_name = extract_bone_name_from_track(track.name)
- if (bone_name !== null) {
- bone_names.add(bone_name)
- }
- })
- })
- return bone_names
-}
-
-/**
- * Extracts bone name from animation track name
- */
-function extract_bone_name_from_track (track_name: string): string | null {
- const bones_match = track_name.match(/\.bones\[([^\]]+)\]\.(?:quaternion|position|scale)$/)
- if (bones_match !== null) {
- return bones_match[1].toLowerCase()
- }
-
- const simple_match = track_name.match(/^([^.]+)\.(?:quaternion|position|scale)$/)
- if (simple_match !== null) {
- return simple_match[1].toLowerCase()
- }
-
- return null
-}
diff --git a/src/lib/processes/animations-listing/CustomAnimationImporter.ts b/src/lib/processes/animations-listing/CustomAnimationImporter.ts
index fa8a2d0..12c4645 100644
--- a/src/lib/processes/animations-listing/CustomAnimationImporter.ts
+++ b/src/lib/processes/animations-listing/CustomAnimationImporter.ts
@@ -3,7 +3,7 @@ import { ModalDialog } from '../../ModalDialog.ts'
import { type AnimationClip, type SkinnedMesh } from 'three'
import { type AnimationLoader } from './AnimationLoader.ts'
import { NoAnimationsError, IncompatibleSkeletonError, LoadError } from './AnimationImportErrors.ts'
-import { validate_animation_bones_match } from './AnimationValidation.ts'
+import CustomAnimationValidation from './CustomAnimationValidation.ts'
import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'
/**
@@ -15,7 +15,9 @@ export class CustomAnimationImporter extends EventTarget {
private readonly animation_loader: AnimationLoader
private skinned_meshes_to_animate: SkinnedMesh[]
private skeleton_scale: number
+ private import_context_provider: (() => { skinned_meshes_to_animate: SkinnedMesh[], skeleton_scale: number }) | null = null
private enabled: boolean = true
+ private has_added_event_listeners: boolean = false
constructor (
animation_loader: AnimationLoader,
@@ -27,6 +29,7 @@ export class CustomAnimationImporter extends EventTarget {
this.animation_loader = animation_loader
this.skinned_meshes_to_animate = skinned_meshes_to_animate
this.skeleton_scale = skeleton_scale
+ this.add_event_listeners()
}
public set_import_context (skinned_meshes_to_animate: SkinnedMesh[], skeleton_scale: number): void {
@@ -34,6 +37,10 @@ export class CustomAnimationImporter extends EventTarget {
this.skeleton_scale = skeleton_scale
}
+ public set_import_context_provider (provider: () => { skinned_meshes_to_animate: SkinnedMesh[], skeleton_scale: number }): void {
+ this.import_context_provider = provider
+ }
+
public set_enabled (enabled: boolean): void {
this.enabled = enabled
if (this.ui.dom_import_animations_button != null) {
@@ -45,17 +52,24 @@ export class CustomAnimationImporter extends EventTarget {
return this.enabled
}
- public add_event_listeners (): void {
+ private add_event_listeners (): void {
+ if (this.has_added_event_listeners) {
+ return
+ }
+
this.ui.dom_import_animations_button?.addEventListener('click', () => {
if (!this.is_enabled()) {
return
}
+ this.sync_import_context_from_provider()
this.ui.dom_import_animations_input?.click()
})
this.ui.dom_import_animations_input?.addEventListener('change', (event) => {
void this.handle_import_animations_input_change(event)
})
+
+ this.has_added_event_listeners = true
}
private async handle_import_animations_input_change (event: Event): Promise<void> {
@@ -63,6 +77,8 @@ export class CustomAnimationImporter extends EventTarget {
return
}
+ this.sync_import_context_from_provider()
+
// potentially allow multiple files
const input = event.target as HTMLInputElement
const files = input.files
@@ -87,6 +103,15 @@ export class CustomAnimationImporter extends EventTarget {
}
}
+ private sync_import_context_from_provider (): void {
+ if (this.import_context_provider === null) {
+ return
+ }
+
+ const { skinned_meshes_to_animate, skeleton_scale } = this.import_context_provider()
+ this.set_import_context(skinned_meshes_to_animate, skeleton_scale)
+ }
+
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(
@@ -94,7 +119,9 @@ export class CustomAnimationImporter extends EventTarget {
this.skeleton_scale
)
- this.validate_loaded_animation_clips(new_animation_clips)
+ // Validate custom animations against our target skeleton
+ const clips_to_validate: AnimationClip[] = new_animation_clips.map((clip_pair) => clip_pair.display_animation_clip)
+ CustomAnimationValidation.validate_animation_bones_match(clips_to_validate, this.skinned_meshes_to_animate)
this.dispatchEvent(new CustomEvent<TransformedAnimationClipPair[]>('import-success', {
detail: new_animation_clips
@@ -146,7 +173,6 @@ export class CustomAnimationImporter extends EventTarget {
}
private validate_loaded_animation_clips (new_animation_clips: TransformedAnimationClipPair[]): void {
- const clips_to_validate: AnimationClip[] = new_animation_clips.map((clip_pair) => clip_pair.display_animation_clip)
- validate_animation_bones_match(clips_to_validate, this.skinned_meshes_to_animate)
+
}
}
diff --git a/src/lib/processes/animations-listing/CustomAnimationValidation.ts b/src/lib/processes/animations-listing/CustomAnimationValidation.ts
new file mode 100644
index 0000000..eb3d487
--- /dev/null
+++ b/src/lib/processes/animations-listing/CustomAnimationValidation.ts
@@ -0,0 +1,76 @@
+import { type AnimationClip, type SkinnedMesh } from 'three'
+import { IncompatibleSkeletonError, NoAnimationsError } from './AnimationImportErrors.ts'
+
+export default class CustomAnimationValidation {
+ /**
+ * Validates that animation bones are compatible with target skeleton
+ * @throws {IncompatibleSkeletonError} If bones don't match
+ * @throws {NoAnimationsError} If no recognizable bone tracks found
+ */
+ static validate_animation_bones_match (
+ animation_clips: AnimationClip[],
+ skinned_meshes: SkinnedMesh[]
+ ): void {
+ if (skinned_meshes.length === 0) {
+ throw new Error('No skinned meshes available to validate animations.')
+ }
+
+ // Get target bone names from skeleton
+ const target_bone_names = new Set<string>()
+ skinned_meshes.forEach((skinned_mesh) => {
+ skinned_mesh.skeleton.bones.forEach((bone) => {
+ target_bone_names.add(bone.name.toLowerCase())
+ })
+ })
+
+ const animation_bone_names = CustomAnimationValidation.get_animation_bone_names(animation_clips)
+
+ if (animation_bone_names.size === 0) {
+ throw new NoAnimationsError('Imported animations do not contain recognizable bone tracks.')
+ }
+
+ // Check bone count mismatch
+ if (animation_bone_names.size !== target_bone_names.size) {
+ throw new IncompatibleSkeletonError('bone count mismatch')
+ }
+
+ // Check bone name mismatch (animation has bones not in target skeleton)
+ const missing_bones = Array.from(animation_bone_names).filter(bone => !target_bone_names.has(bone))
+ if (missing_bones.length > 0) {
+ throw new IncompatibleSkeletonError('bone names don\'t match')
+ }
+ }
+
+ /**
+ * Gets all bone names referenced in animation clips
+ */
+ static get_animation_bone_names (animation_clips: AnimationClip[]): Set<string> {
+ const bone_names = new Set<string>()
+ animation_clips.forEach((clip) => {
+ clip.tracks.forEach((track) => {
+ const bone_name = CustomAnimationValidation.extract_bone_name_from_track(track.name)
+ if (bone_name !== null) {
+ bone_names.add(bone_name)
+ }
+ })
+ })
+ return bone_names
+ }
+
+ /**
+ * Extracts bone name from animation track name
+ */
+ static extract_bone_name_from_track (track_name: string): string | null {
+ const bones_match = track_name.match(/\.bones\[([^\]]+)\]\.(?:quaternion|position|scale)$/)
+ if (bones_match !== null) {
+ return bones_match[1].toLowerCase()
+ }
+
+ const simple_match = track_name.match(/^([^.]+)\.(?:quaternion|position|scale)$/)
+ if (simple_match !== null) {
+ return simple_match[1].toLowerCase()
+ }
+
+ return null
+ }
+}
diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.ts
index 5ff36bc..42f0bd1 100644
--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts
+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts
@@ -68,6 +68,16 @@ export class StepAnimationsListing extends EventTarget {
this.skeleton_scale
)
+ // fancy way to bind the import context by implementing the function
+ // from the CustomAnimationImporter and passing in the current context values. this allows
+ // the CustomAnimationImporter to be decoupled from the StepAnimationsListing
+ this.custom_animation_importer.set_import_context_provider(() => {
+ return {
+ skinned_meshes_to_animate: this.skinned_meshes_to_animate,
+ skeleton_scale: 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)
@@ -95,7 +105,6 @@ 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
@@ -117,7 +126,6 @@ export class StepAnimationsListing extends EventTarget {
this.animation_mixer = new AnimationMixer(new Object3D())
this.current_playing_index = 0
this.animation_player.clear_animation()
- this.custom_animation_importer.set_import_context(this.skinned_meshes_to_animate, this.skeleton_scale)
}
public mixer (): AnimationMixer {
@@ -140,7 +148,6 @@ 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 loader
this.animation_loader.set_animations_file_path(this.animations_file_path)
@@ -378,8 +385,6 @@ export class StepAnimationsListing extends EventTarget {
this.play_animation(this.current_playing_index)
})
- this.custom_animation_importer.add_event_listeners()
-
// helps ensure we don't add event listeners multiple times
this.has_added_event_listeners = true
}