Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Fix some state issues with switching models.

Scott Petrovic9a60dc370c4f
diff --git a/src/lib/RigConfig.ts b/src/lib/RigConfig.ts
index cf66236..ac0549a 100644
--- a/src/lib/RigConfig.ts
+++ b/src/lib/RigConfig.ts
@@ -65,6 +65,18 @@ export class RigConfig {
has_head_weight_correction: false,
has_arm_extension: false,
position_tracking_bone_name: 'hips',
+ model_variations: [
+ {
+ model_file: 'models-variation/fox-base.glb',
+ display_name: 'Fox',
+ attribution: 'None'
+ } satisfies ModelVariation,
+ {
+ model_file: 'models-variation/fox-horse.glb',
+ display_name: 'Horse',
+ attribution: 'None'
+ } satisfies ModelVariation
+ ]
} satisfies RigConfigEntry,
{
skeleton_type: SkeletonType.Bird,
diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.ts
index cff7b7d..bb83def 100644
--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts
+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts
@@ -25,7 +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 variation_model_root: Group | null = null // stored auto-rig model that comes from GLB
private current_playing_index: number = 0
private skeleton_type: SkeletonType = SkeletonType.Human
@@ -166,10 +166,10 @@ export class StepAnimationsListing extends EventTarget {
* 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 {
+ const had_variation_model = this.variation_model_root !== null
+ this.clear_variation_model_from_scene()
+
+ if (!had_variation_model) {
// remove the original skinned meshes that were added individually
for (const mesh of this.skinned_meshes_to_animate) {
Utility.remove_object_with_children(mesh)
@@ -187,6 +187,17 @@ export class StepAnimationsListing extends EventTarget {
this.animation_player.play()
}
+ /**
+ * Clears any variation model root that was injected during model variation swapping.
+ * 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
+ }
+ }
+
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 0062314..96af7ed 100644
--- a/src/marketing/MarketingBootstrap.ts
+++ b/src/marketing/MarketingBootstrap.ts
@@ -44,6 +44,8 @@ export class MarketingBootstrap {
model_button.textContent = rig.rig_display_name
model_button.addEventListener('click', () => {
+ // if we previously swapped a variation model into scene, clear it first
+ this.mesh2motion_engine.animations_listing_step.clear_variation_model_from_scene()
this.mesh2motion_engine.load_model_step.clear_loaded_model_data()
this.mesh2motion_engine.load_model_step.load_model_file('../' + rig.model_file, 'glb')
this.skeleton_type = rig.skeleton_type
@@ -70,6 +72,9 @@ export class MarketingBootstrap {
this.mesh2motion_engine.animations_listing_step.swap_skinned_meshes(
this.mesh2motion_engine.scene, skinned_meshes, model_root
)
+
+ this.rebuild_skeleton_helper(skinned_meshes)
+
}) as EventListener)
// event after the DOM is fully loaded for HTML elements
@@ -94,7 +99,24 @@ export class MarketingBootstrap {
this.mesh2motion_engine.process_step_changed(ProcessStep.BindPose)
})
}
+
+ private rebuild_skeleton_helper (skinned_meshes: SkinnedMesh[]): void {
+ // rebuild helper to target the new skeleton for this variation
+ if (skinned_meshes.length > 0) {
+ this.mesh2motion_engine.regenerate_skeleton_helper(skinned_meshes[0].skeleton)
+ this.mesh2motion_engine.sync_skeleton_helper_joint_visibility()
+
+ // keep current helper visibility aligned with the toggle state
+ if (this.mesh2motion_engine.skeleton_helper !== undefined) {
+ const show_skeleton = this.mesh2motion_engine.ui.dom_show_skeleton_checkbox?.checked ?? false
+ this.mesh2motion_engine.skeleton_helper.visible = show_skeleton
+ }
+ }
+ }
+
}
+
+
// instantiate the class to setup event listeners
const app = new MarketingBootstrap()
diff --git a/static/models-variation/fox-base.glb b/static/models-variation/fox-base.glb
new file mode 100644
index 0000000..9540a9c
Binary files /dev/null and b/static/models-variation/fox-base.glb differ
diff --git a/static/models-variation/fox-horse.glb b/static/models-variation/fox-horse.glb
new file mode 100644
index 0000000..c001921
Binary files /dev/null and b/static/models-variation/fox-horse.glb differ
diff --git a/static/models-variation/human-zombie.glb b/static/models-variation/human-zombie.glb
index 55e9045..14ea7e8 100644
Binary files a/static/models-variation/human-zombie.glb and b/static/models-variation/human-zombie.glb differ