Import a 3D Model and automatically assign and export animations
TypeScript
Fix export to work with new variation files. Add Sophia model variation
Scott Petrovic9f9fd6b6ebde
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.tsindex 5e9f3c8..3f35729 100644--- a/src/lib/EventListeners.ts+++ b/src/lib/EventListeners.ts@@ -198,7 +198,7 @@ export class EventListeners {const animations_to_export: number[] = this.bootstrap.animations_listing_step.get_animation_indices_to_export()this.bootstrap.file_export_step.set_animation_clips_to_export(all_clips, animations_to_export)- this.bootstrap.file_export_step.export(this.bootstrap.weight_skin_step.final_skinned_meshes(), 'exported-model')+ this.bootstrap.file_export_step.export(this.bootstrap.animations_listing_step.active_skinned_meshes(), 'exported-model')})// going back to edit skeleton step after skinningdiff --git a/src/lib/RigConfig.ts b/src/lib/RigConfig.tsindex ac0549a..e5ef753 100644--- a/src/lib/RigConfig.ts+++ b/src/lib/RigConfig.ts@@ -51,6 +51,11 @@ export class RigConfig {model_file: 'models-variation/human-zombie.glb',display_name: 'Zombie',attribution: 'Model by Kenney.nl, CC0-licensed'+ } satisfies ModelVariation,+ {+ model_file: 'models-variation/human-sophia.glb',+ display_name: 'Sophia',+ attribution: 'Art by Tysan Tan, CC-SA 4.0'} satisfies ModelVariation]} satisfies RigConfigEntry,diff --git a/src/lib/processes/animations-listing/ModelVariationSwitcher.ts b/src/lib/processes/animations-listing/ModelVariationSwitcher.tsindex 4103ca5..ef7f6e6 100644--- a/src/lib/processes/animations-listing/ModelVariationSwitcher.ts+++ b/src/lib/processes/animations-listing/ModelVariationSwitcher.ts@@ -1,4 +1,4 @@-import { SkinnedMesh, type Group } from 'three'+import { Skeleton, SkinnedMesh } from 'three'import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'import { RigConfig, type RigConfigEntry } from '../../RigConfig'import { type SkeletonType } from '../../enums/SkeletonType'@@ -17,6 +17,15 @@ export class ModelVariationSwitcher extends EventTarget {private readonly loader: GLTFLoader = new GLTFLoader()private current_rig: RigConfigEntry | undefinedprivate added_event_listeners = false+ private _is_variation_active: boolean = false++ public get is_variation_active (): boolean {+ return this._is_variation_active+ }++ public set is_variation_active (value: boolean) {+ this._is_variation_active = value+ }/*** Call this whenever the active rig type changes.@@ -62,8 +71,10 @@ export class ModelVariationSwitcher extends EventTarget {}})+ this.normalize_skinned_meshes(skinned_meshes)+this.dispatchEvent(new CustomEvent('variation-changed', {- detail: { model_file, skinned_meshes, model_root: gltf.scene }+ detail: { model_file, skinned_meshes }}))},undefined,@@ -73,6 +84,25 @@ export class ModelVariationSwitcher extends EventTarget {)}+ /**+ * Re-parents each skeleton's root bone to be a child of its skinned mesh.+ * This matches the structure created by StepWeightSkin in the normal pipeline,+ * so the export path can treat both cases identically.+ */+ private normalize_skinned_meshes (skinned_meshes: SkinnedMesh[]): void {+ const processed_skeletons = new Set<Skeleton>()++ for (const mesh of skinned_meshes) {+ if (processed_skeletons.has(mesh.skeleton)) continue+ processed_skeletons.add(mesh.skeleton)++ const root_bone = mesh.skeleton.bones[0]+ if (root_bone !== undefined) {+ mesh.add(root_bone)+ }+ }+ }+private add_event_listeners (): void {if (this.added_event_listeners) returnthis.added_event_listeners = truediff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.tsindex bb83def..048a5d7 100644--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts@@ -2,12 +2,13 @@ import { UI } from '../../UI.ts'import { AnimationPlayer } from './AnimationPlayer.ts'import {- type AnimationClip, AnimationMixer, type SkinnedMesh, type AnimationAction, Object3D, type Scene, type Group+ type AnimationClip, AnimationMixer, type SkinnedMesh, type AnimationAction, Object3D, type Scene} from 'three'import { AnimationUtility } from './AnimationUtility.ts'import { AnimationLoader, type AnimationLoadProgress } from './AnimationLoader.ts'import { CustomAnimationImporter } from './CustomAnimationImporter.ts'+import { type ModelVariationSwitcher } from './ModelVariationSwitcher.ts'import { SkeletonType } from '../../enums/SkeletonType.ts'import { Utility } from '../../Utilities.ts'@@ -25,7 +26,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 // stored auto-rig model that comes from GLB+ private model_variation_switcher: ModelVariationSwitcher | null = nullprivate current_playing_index: number = 0private skeleton_type: SkeletonType = SkeletonType.Human@@ -161,24 +162,40 @@ export class StepAnimationsListing extends EventTarget {return this.get_animation_metadata(index)?.source_type === 'custom-import'}+ /**+ * Returns the skinned meshes currently used for animation playback.+ * This is used during the download/export process to retrieved final skinned mesh+ */+ public active_skinned_meshes (): SkinnedMesh[] {+ return this.skinned_meshes_to_animate+ }++ public set_model_variation_switcher (switcher: ModelVariationSwitcher): void {+ this.model_variation_switcher = switcher+ }+/*** 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 {- const had_variation_model = this.variation_model_root !== null+ public swap_skinned_meshes (scene: Scene, new_skinned_meshes: SkinnedMesh[]): void {+ const is_variation_active = this.model_variation_switcher?.is_variation_active ?? falsethis.clear_variation_model_from_scene()- if (!had_variation_model) {+ if (!is_variation_active) {// remove the original skinned meshes that were added individuallyfor (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)+ // add individual skinned meshes to scene (bones are children of the mesh after normalization)+ if (this.model_variation_switcher !== null) {+ this.model_variation_switcher.is_variation_active = true+ }+ for (const mesh of new_skinned_meshes) {+ scene.add(mesh)+ }this.skinned_meshes_to_animate = new_skinned_meshes@@ -192,9 +209,14 @@ export class StepAnimationsListing extends EventTarget {* This should be called before doing a full model load flow.*/public clear_variation_model_from_scene (): void {- if (this.variation_model_root !== null) {- Utility.remove_object_with_children(this.variation_model_root)- this.variation_model_root = null+ const is_variation_active = this.model_variation_switcher?.is_variation_active ?? false+ if (is_variation_active) {+ for (const mesh of this.skinned_meshes_to_animate) {+ Utility.remove_object_with_children(mesh)+ }+ if (this.model_variation_switcher !== null) {+ this.model_variation_switcher.is_variation_active = false+ }}}diff --git a/src/marketing/MarketingBootstrap.ts b/src/marketing/MarketingBootstrap.tsindex 96af7ed..b178cb6 100644--- a/src/marketing/MarketingBootstrap.ts+++ b/src/marketing/MarketingBootstrap.ts@@ -1,4 +1,4 @@-import { Vector3, type SkinnedMesh, type Group } from 'three'+import { Vector3, type SkinnedMesh } from 'three'import { ProcessStep } from '../lib/enums/ProcessStep'import { SkeletonType } from '../lib/enums/SkeletonType'import { Mesh2MotionEngine } from '../Mesh2MotionEngine'@@ -12,6 +12,7 @@ export class MarketingBootstrap {constructor () {this.mesh2motion_engine = new Mesh2MotionEngine()+ this.mesh2motion_engine.animations_listing_step.set_model_variation_switcher(this.model_variation_switcher)this.add_event_listeners()// default: X:0 (centered), Y:1.7 (eye-level), Z:5 (front view)@@ -68,9 +69,8 @@ export class MarketingBootstrap {// when the user picks a different model variation, swap the skinned mesh in the scenethis.model_variation_switcher.addEventListener('variation-changed', ((event: CustomEvent) => {const skinned_meshes = event.detail.skinned_meshes as SkinnedMesh[]- const model_root = event.detail.model_root as Groupthis.mesh2motion_engine.animations_listing_step.swap_skinned_meshes(- this.mesh2motion_engine.scene, skinned_meshes, model_root+ this.mesh2motion_engine.scene, skinned_meshes)this.rebuild_skeleton_helper(skinned_meshes)diff --git a/static/models-variation/human-sophia.glb b/static/models-variation/human-sophia.glbnew file mode 100644index 0000000..289cf5dBinary files /dev/null and b/static/models-variation/human-sophia.glb differ