Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Remove old skinned mesh and and replace with new one

Scott Petrovicb922652331d0
diff --git a/src/lib/processes/animations-listing/ModelVariationSwitcher.ts b/src/lib/processes/animations-listing/ModelVariationSwitcher.ts
index 1834c20..4103ca5 100644
--- a/src/lib/processes/animations-listing/ModelVariationSwitcher.ts
+++ b/src/lib/processes/animations-listing/ModelVariationSwitcher.ts
@@ -1,3 +1,5 @@
+import { SkinnedMesh, type Group } from 'three'
+import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { RigConfig, type RigConfigEntry } from '../../RigConfig'
import { type SkeletonType } from '../../enums/SkeletonType'
@@ -6,12 +8,13 @@ import { type SkeletonType } from '../../enums/SkeletonType'
* When a rig type is selected that has model_variations in its RigConfig,
* the dropdown is populated and shown. Otherwise it is hidden.
*
- * Dispatches 'variation-changed' with the new model file path when the user
- * picks a different variation.
+ * Loads the selected variation GLB and dispatches 'variation-changed'
+ * with the extracted SkinnedMesh[] so consumers can swap the model directly.
*/
export class ModelVariationSwitcher extends EventTarget {
private readonly dom_switcher: HTMLElement | null = document.querySelector('#model-variation-switcher')
private readonly dom_select: HTMLSelectElement | null = document.querySelector('#model-variation-selection')
+ private readonly loader: GLTFLoader = new GLTFLoader()
private current_rig: RigConfigEntry | undefined
private added_event_listeners = false
@@ -48,6 +51,28 @@ export class ModelVariationSwitcher extends EventTarget {
switcher.style.display = '' // use default display style by removing inline style
}
+ private load_variation_model (model_file: string): void {
+ this.loader.load(
+ '../' + model_file,
+ (gltf) => {
+ const skinned_meshes: SkinnedMesh[] = []
+ gltf.scene.traverse((child) => {
+ if (child instanceof SkinnedMesh) {
+ skinned_meshes.push(child)
+ }
+ })
+
+ this.dispatchEvent(new CustomEvent('variation-changed', {
+ detail: { model_file, skinned_meshes, model_root: gltf.scene }
+ }))
+ },
+ undefined,
+ (error) => {
+ console.error('Failed to load model variation:', model_file, error)
+ }
+ )
+ }
+
private add_event_listeners (): void {
if (this.added_event_listeners) return
this.added_event_listeners = true
@@ -56,8 +81,7 @@ export class ModelVariationSwitcher extends EventTarget {
if (select === null) return
select.addEventListener('change', () => {
- const model_file = select.value
- this.dispatchEvent(new CustomEvent('variation-changed', { detail: { model_file } }))
+ this.load_variation_model(select.value)
})
}
}
diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.ts
index 896122a..cff7b7d 100644
--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts
+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts
@@ -2,7 +2,7 @@ import { UI } from '../../UI.ts'
import { AnimationPlayer } from './AnimationPlayer.ts'
import {
- type AnimationClip, AnimationMixer, type SkinnedMesh, type AnimationAction, Object3D
+ type AnimationClip, AnimationMixer, type SkinnedMesh, type AnimationAction, Object3D, type Scene, type Group
} from 'three'
import { AnimationUtility } from './AnimationUtility.ts'
@@ -25,6 +25,7 @@ export class StepAnimationsListing extends EventTarget {
private animation_mixer: AnimationMixer = new AnimationMixer(new Object3D())
private skinned_meshes_to_animate: SkinnedMesh[] = []
+ private variation_model_root: Group | null = null
private current_playing_index: number = 0
private skeleton_type: SkeletonType = SkeletonType.Human
@@ -160,6 +161,32 @@ export class StepAnimationsListing extends EventTarget {
return this.get_animation_metadata(index)?.source_type === 'custom-import'
}
+ /**
+ * Removes the current skinned meshes from the scene, adds the new ones,
+ * and replays the current animation on them.
+ */
+ public swap_skinned_meshes (scene: Scene, new_skinned_meshes: SkinnedMesh[], model_root: Group): void {
+ // remove previous variation model root if one was added
+ if (this.variation_model_root !== null) {
+ Utility.remove_object_with_children(this.variation_model_root)
+ } else {
+ // remove the original skinned meshes that were added individually
+ for (const mesh of this.skinned_meshes_to_animate) {
+ Utility.remove_object_with_children(mesh)
+ }
+ }
+
+ // add the full model group (bones + meshes) so world matrices update
+ this.variation_model_root = model_root
+ scene.add(model_root)
+
+ this.skinned_meshes_to_animate = new_skinned_meshes
+
+ // replay current animation on the new meshes
+ this.play_animation(this.current_playing_index)
+ this.animation_player.play()
+ }
+
public load_and_apply_default_animation_to_skinned_mesh (final_skinned_meshes: SkinnedMesh[]): void {
this.skinned_meshes_to_animate = final_skinned_meshes
diff --git a/src/marketing/MarketingBootstrap.ts b/src/marketing/MarketingBootstrap.ts
index ea537e7..0062314 100644
--- a/src/marketing/MarketingBootstrap.ts
+++ b/src/marketing/MarketingBootstrap.ts
@@ -1,4 +1,4 @@
-import { Vector3 } from 'three'
+import { Vector3, type SkinnedMesh, type Group } from 'three'
import { ProcessStep } from '../lib/enums/ProcessStep'
import { SkeletonType } from '../lib/enums/SkeletonType'
import { Mesh2MotionEngine } from '../Mesh2MotionEngine'
@@ -63,9 +63,13 @@ export class MarketingBootstrap {
}
public add_event_listeners (): void {
- // when the user picks a different model variation, reload the model
+ // when the user picks a different model variation, swap the skinned mesh in the scene
this.model_variation_switcher.addEventListener('variation-changed', ((event: CustomEvent) => {
- console.log('TODO: model variation change to. load and swap out target rig reference', event)
+ const skinned_meshes = event.detail.skinned_meshes as SkinnedMesh[]
+ const model_root = event.detail.model_root as Group
+ this.mesh2motion_engine.animations_listing_step.swap_skinned_meshes(
+ this.mesh2motion_engine.scene, skinned_meshes, model_root
+ )
}) as EventListener)
// event after the DOM is fully loaded for HTML elements