Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Fixed issue with stacking event listeners with skeleton loaded

Scott Petrovice4775aa02d4a
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index 4ee85cf..c82ca55 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -254,22 +254,10 @@ export class Mesh2MotionEngine {
*********/
if (this.process_step === ProcessStep.LoadModel) {
// reset the state in the case of coming back to this step
- if (this.load_model_step.model_meshes() !== undefined) {
- const imported_model = this.scene.getObjectByName('Imported Model')
- if (imported_model !== null) {
- this.scene.remove(imported_model)
- }
- }
-
+ this.remove_imported_model()
this.load_model_step.begin()
}
else if (this.process_step === ProcessStep.LoadSkeleton) {
- // add event listener. TODO: put this in the load skeleton process step
- this.load_skeleton_step.addEventListener('skeletonLoaded', () => {
- this.edit_skeleton_step.load_original_armature_from_model(this.load_skeleton_step.armature())
- this.process_step = this.process_step_changed(ProcessStep.EditSkeleton)
- })
-
// if skeleton helper existed because we are going back to this
if (this.skeleton_helper !== undefined) {
this.scene.remove(this.skeleton_helper)
@@ -308,7 +296,10 @@ export class Mesh2MotionEngine {
else if (this.process_step === ProcessStep.BindPose) {
this.transform_controls.enabled = false // shouldn't be editing bones
this.calculate_skin_weighting_for_models()
+
+ this.remove_skinned_meshes_from_scene() // clean up in case we had skinned meshes in scene previously
this.scene.add(...this.weight_skin_step.final_skinned_meshes()) // add final skinned mesh to scene
+
this.weight_skin_step.weight_painted_mesh_group().visible = false // hide weight painted mesh
this.process_step_changed(ProcessStep.AnimationsListing)
}
@@ -342,6 +333,7 @@ export class Mesh2MotionEngine {
return this.process_step
} // end process_step_changed()
+
private animate (): void {
requestAnimationFrame(this.animate)
const delta_time: number = this.clock.getDelta()
@@ -440,12 +432,30 @@ export class Mesh2MotionEngine {
}
public remove_skinned_meshes_from_scene (): void {
- const existing_skinned_meshes = this.scene.children.filter((child: THREE.Object3D) => child.name === 'Skinned Mesh')
+ const existing_skinned_meshes = this.scene.children.filter((child: THREE.Object3D) => child.name.includes('Skinned Mesh'))
existing_skinned_meshes.forEach((existing_skinned_mesh: THREE.Object3D) => {
Utility.remove_object_with_children(existing_skinned_mesh)
})
}
+ public remove_imported_model (): void {
+ if (this.load_model_step.model_meshes() !== undefined) {
+ const imported_model = this.scene.getObjectByName('Imported Model')
+ if (imported_model !== null) {
+ this.scene.remove(imported_model)
+ }
+ }
+ }
+
+ public remove_weight_painted_mesh_preview (): void {
+ if (this.load_model_step.model_meshes() !== undefined) {
+ const weight_painted_mesh = this.scene.getObjectByName('Weight Painted Mesh Preview')
+ if (weight_painted_mesh !== null) {
+ this.scene.remove(weight_painted_mesh)
+ }
+ }
+ }
+
public regenerate_weight_painted_preview_mesh (): void {
// needed for skinning process
this.calculate_skin_weighting_for_models()
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 55d12d7..10025e3 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -1,10 +1,10 @@
-import { type Bootstrap } from '../script'
+import { type Mesh2MotionEngine } from '../Mesh2MotionEngine'
import { ModelPreviewDisplay } from './enums/ModelPreviewDisplay'
import { ProcessStep } from './enums/ProcessStep'
import { Utility } from './Utilities'
export class EventListeners {
- constructor (private readonly bootstrap: Bootstrap) {}
+ constructor (private readonly bootstrap: Mesh2MotionEngine) {}
public addEventListeners (): void {
// monitor theme changes
@@ -12,6 +12,11 @@ export class EventListeners {
this.bootstrap.regenerate_floor_grid()
})
+ this.bootstrap.load_skeleton_step.addEventListener('skeletonLoaded', () => {
+ this.bootstrap.edit_skeleton_step.load_original_armature_from_model(this.bootstrap.load_skeleton_step.armature())
+ this.bootstrap.process_step = this.bootstrap.process_step_changed(ProcessStep.EditSkeleton)
+ })
+
// Listen for skeleton transformation events to update UI and visuals
// this can happen with undo/redo system
this.bootstrap.edit_skeleton_step.addEventListener('skeletonTransformed', () => {
@@ -32,7 +37,6 @@ export class EventListeners {
this.bootstrap.show_contributors_dialog()
})
-
// listen for view helper changes
document.getElementById('view-control-hitbox')?.addEventListener('pointerdown', (event: PointerEvent) => {
if (this.bootstrap.view_helper.handleClick(event)) {
diff --git a/src/lib/processes/weight-skin/StepWeightSkin.ts b/src/lib/processes/weight-skin/StepWeightSkin.ts
index df8d42c..a926df2 100644
--- a/src/lib/processes/weight-skin/StepWeightSkin.ts
+++ b/src/lib/processes/weight-skin/StepWeightSkin.ts
@@ -135,7 +135,7 @@ export class StepWeightSkin extends EventTarget {
this.all_mesh_materials.push(material)
}
- public create_skinned_mesh (geometry: BufferGeometry, material: Material): SkinnedMesh {
+ public create_skinned_mesh (geometry: BufferGeometry, material: Material, idx: number): SkinnedMesh {
if (this.binding_skeleton === undefined) {
console.warn('Tried to create_skinned_mesh() but binding_skeleton is undefined!')
return
@@ -143,7 +143,7 @@ export class StepWeightSkin extends EventTarget {
// create skinned mesh
const skinned_mesh: SkinnedMesh = new SkinnedMesh(geometry, material)
- skinned_mesh.name = 'Skinned Mesh'
+ skinned_mesh.name = 'Skinned Mesh ' + idx.toString()
skinned_mesh.castShadow = true // skinned mesh won't update right if this is false
// do the binding for the mesh to the skelleton
@@ -205,7 +205,7 @@ export class StepWeightSkin extends EventTarget {
const associated_material: Material = this.all_mesh_materials[idx]
// create skined mesh from the geometry and material
- const temp_skinned_mesh: SkinnedMesh = this.create_skinned_mesh(geometry_data, associated_material)
+ const temp_skinned_mesh: SkinnedMesh = this.create_skinned_mesh(geometry_data, associated_material, idx)
this.skinned_meshes.push(temp_skinned_mesh) // add to skinned meshes references
// re-generate the weight painted mesh display if needed
diff --git a/src/marketing/MarketingBootstrap.ts b/src/marketing/MarketingBootstrap.ts
index d9875bc..9d3b87b 100644
--- a/src/marketing/MarketingBootstrap.ts
+++ b/src/marketing/MarketingBootstrap.ts
@@ -11,31 +11,6 @@ export class MarketingBootstrap {
this.add_event_listeners()
}
- public reset_to_beginning (): void {
- // remove existing canvas element since we are starting over
- const existing_canvas = document.querySelector('canvas')
- if (existing_canvas != null) {
- existing_canvas.remove()
- }
-
- // reset data points and our current step of the process so we can continue
- this.mesh2motion_engine = new Mesh2MotionEngine()
-
- // we are re-creating the engine, so need to manually add the event listeners again
- this.mesh2motion_engine.load_model_step.addEventListener('modelLoaded', () => {
- // this (this.skeleton_type) value contains the filename for the skeleton rig
- this.mesh2motion_engine.load_skeleton_step.load_skeleton_file('../' + this.skeleton_type)
- this.mesh2motion_engine.load_skeleton_step.set_skeleton_type(this.skeleton_type)
-
- // need to automatically finish the edit skeleton step and move onto the next step
- // this needs to be added after the load skeleton is done...since it expects a valid skeleton
- this.mesh2motion_engine.load_skeleton_step.addEventListener('skeletonLoaded', () => {
- this.mesh2motion_engine.animations_listing_step.set_animations_file_path('../animations/')
- this.mesh2motion_engine.process_step_changed(ProcessStep.BindPose)
- })
- })
- }
-
public setup_model_buttons (): void {
// add click events for each button type
const human_button = document.getElementById('load-human-model-button')
@@ -44,25 +19,21 @@ export class MarketingBootstrap {
const dragon_button = document.getElementById('load-dragon-model-button')
human_button?.addEventListener('click', () => {
- this.reset_to_beginning()
this.mesh2motion_engine.load_model_step.load_model_file('../models/model-human.glb', 'glb')
this.skeleton_type = SkeletonType.Human
})
fox_button?.addEventListener('click', () => {
- this.reset_to_beginning()
this.mesh2motion_engine.load_model_step.load_model_file('../models/model-fox.glb', 'glb')
this.skeleton_type = SkeletonType.Quadraped
})
bird_button?.addEventListener('click', () => {
- this.reset_to_beginning()
this.mesh2motion_engine.load_model_step.load_model_file('../models/model-bird.glb', 'glb')
this.skeleton_type = SkeletonType.Bird
})
dragon_button?.addEventListener('click', () => {
- this.reset_to_beginning()
this.mesh2motion_engine.load_model_step.load_model_file('../models/model-dragon.glb', 'glb')
this.skeleton_type = SkeletonType.Dragon
})
@@ -75,6 +46,20 @@ export class MarketingBootstrap {
document.addEventListener('DOMContentLoaded', () => {
this.setup_model_buttons() // automatically trigger the human once we begin for the default
}) // end the DOMContentLoaded function
+
+ // we are re-creating the engine, so need to manually add the event listeners again
+ this.mesh2motion_engine.load_model_step.addEventListener('modelLoaded', () => {
+ // this (this.skeleton_type) value contains the filename for the skeleton rig
+ this.mesh2motion_engine.process_step_changed(ProcessStep.LoadSkeleton)
+ this.mesh2motion_engine.load_skeleton_step.load_skeleton_file('../' + this.skeleton_type)
+ this.mesh2motion_engine.load_skeleton_step.set_skeleton_type(this.skeleton_type)
+ })
+
+ // need to automatically finish the edit skeleton step and move onto the next step
+ this.mesh2motion_engine.load_skeleton_step.addEventListener('skeletonLoaded', () => {
+ this.mesh2motion_engine.animations_listing_step.set_animations_file_path('../animations/')
+ this.mesh2motion_engine.process_step_changed(ProcessStep.BindPose)
+ })
}
}
diff --git a/src/preview-generator/preview-generator.ts b/src/preview-generator/preview-generator.ts
index 2cdf0a5..06d5eaf 100644
--- a/src/preview-generator/preview-generator.ts
+++ b/src/preview-generator/preview-generator.ts
@@ -163,7 +163,7 @@ class PreviewGenerator {
// zip up all the video results and download everything
await this.generate_zip()
- console.log('finishe successfully')
+ console.log('finished successfully')
}
private async process_animation_clip (): Promise<void> {