Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

FIX: update glb import UI tooltips and refine skeleton validation

Lino Emmeneggera32bf54ec3de
diff --git a/src/create.html b/src/create.html
index 3342201..fd48013 100644
--- a/src/create.html
+++ b/src/create.html
@@ -371,7 +371,7 @@
</label>
</div>
- <button class="secondary-button" id="import-animations-button" title="Import GLB animations" aria-label="Import GLB animations">
+ <button class="secondary-button" id="import-animations-button" data-tippy-content="Import GLB animations" aria-label="Import GLB animations">
<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.html
index cf06116..18b8451 100644
--- a/src/index.html
+++ b/src/index.html
@@ -153,7 +153,7 @@
</label>
</div>
- <button class="secondary-button" id="import-animations-button" title="Import GLB animations" aria-label="Import GLB animations">
+ <button class="secondary-button" id="import-animations-button" data-tippy-content="Import GLB animations" aria-label="Import GLB animations">
<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/AnimationLoader.ts b/src/lib/processes/animations-listing/AnimationLoader.ts
index ee6b5e8..73cd639 100644
--- a/src/lib/processes/animations-listing/AnimationLoader.ts
+++ b/src/lib/processes/animations-listing/AnimationLoader.ts
@@ -212,7 +212,7 @@ export class AnimationLoader extends EventTarget {
/**
* Processes raw animation clips from GLTF file
*/
- private process_loaded_animations (
+ public process_loaded_animations (
raw_animations: AnimationClip[],
skeleton_scale: number
): TransformedAnimationClipPair[] {
diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.ts
index fce9051..0d90d71 100644
--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts
+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts
@@ -229,6 +229,71 @@ export class StepAnimationsListing extends EventTarget {
}
}
+ private get_target_bone_names (): Set<string> {
+ const bone_names = new Set<string>()
+ this.skinned_meshes_to_animate.forEach((skinned_mesh: SkinnedMesh) => {
+ skinned_mesh.skeleton.bones.forEach((bone) => {
+ bone_names.add(bone.name.toLowerCase())
+ })
+ })
+ return bone_names
+ }
+
+ private 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
+ }
+
+ private 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 = this.extract_bone_name_from_track(track.name)
+ if (bone_name !== null) {
+ bone_names.add(bone_name)
+ }
+ })
+ })
+ return bone_names
+ }
+
+ private validate_animation_bones_match (animation_clips: AnimationClip[]): boolean {
+ if (this.skinned_meshes_to_animate.length === 0) {
+ new ModalDialog('No skinned meshes available to validate animations.', 'Error').show()
+ return false
+ }
+
+ const target_bone_names = this.get_target_bone_names()
+ const animation_bone_names = this.get_animation_bone_names(animation_clips)
+
+ if (animation_bone_names.size === 0) {
+ new ModalDialog('Imported animations do not contain recognizable bone tracks.', 'Error').show()
+ return false
+ }
+
+ const missing_bones = Array.from(animation_bone_names).filter(bone => !target_bone_names.has(bone))
+ if (missing_bones.length > 0) {
+ const preview = missing_bones.slice(0, 5).join(', ')
+ const suffix = missing_bones.length > 5 ? '...' : ''
+ new ModalDialog(
+ `Imported animations reference bones not in the current skeleton: ${preview}${suffix}`,
+ 'Error'
+ ).show()
+ return false
+ }
+
+ return true
+ }
+
/**
* Rebuilds all of the warped animations by applying the specified warps.
*/
@@ -442,9 +507,10 @@ export class StepAnimationsListing extends EventTarget {
/**
* Load custom animations from imported skeleton storage
*/
- private load_custom_animations (animations: AnimationClip[]): void {
- this.animation_clips_loaded = []
- this.animation_mixer = new AnimationMixer(new Object3D())
+ public load_custom_animations (animations: AnimationClip[]): void {
+ if (!this.validate_animation_bones_match(animations)) {
+ return
+ }
// Process the custom animations through the same centralized pipeline
const processed_animations = this.animation_loader.process_loaded_animations(
@@ -452,10 +518,12 @@ export class StepAnimationsListing extends EventTarget {
this.skeleton_scale
)
+ this.animation_clips_loaded = []
+ this.animation_mixer = new AnimationMixer(new Object3D())
+
this.animation_clips_loaded.push(...processed_animations)
// Ensure custom animations go through the same post-processing steps
- this.apply_rest_pose_rotation_corrections()
this.rebuild_warped_animations()
this.onAllAnimationsLoaded()
this.play_animation(0)
@@ -469,6 +537,10 @@ export class StepAnimationsListing extends EventTarget {
return
}
+ if (!this.validate_animation_bones_match(new_animation_clips.map(clip => clip.display_animation_clip))) {
+ return
+ }
+
this.animation_clips_loaded.push(...new_animation_clips)
this.onAllAnimationsLoaded()