Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Small refactoring

Scott Petrovicce8439e153a7
diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.ts
index 90bb63f..7987014 100644
--- a/src/retarget/RetargetAnimationPreview.ts
+++ b/src/retarget/RetargetAnimationPreview.ts
@@ -1,9 +1,9 @@
-import { AnimationClip, AnimationMixer, type Scene, type SkinnedMesh, VectorKeyframeTrack, QuaternionKeyframeTrack, type AnimationAction, Quaternion, Euler, type Object3D, Vector3 } from 'three'
+import { AnimationClip, AnimationMixer, Object3D, type Scene, type SkinnedMesh, VectorKeyframeTrack, QuaternionKeyframeTrack, type AnimationAction, Quaternion, Euler, Vector3 } from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
-import { SkeletonType } from '../lib/enums/SkeletonType.ts'
import { AnimationUtility } from '../lib/processes/animations-listing/AnimationUtility.ts'
import type { StepBoneMapping } from './StepBoneMapping.ts'
import { TargetBoneMappingType } from './StepBoneMapping.ts'
+import { RetargetUtils } from './RetargetUtils.ts'
/**
* RetargetAnimationPreview - Provides live preview of bone retargeting by automatically
@@ -20,7 +20,7 @@ export class RetargetAnimationPreview extends EventTarget {
private target_skinned_meshes: SkinnedMesh[] = []
private is_preview_active: boolean = false
- private _added_event_listeners: boolean = false
+ private has_added_event_listeners: boolean = false
constructor (main_scene: Scene, step_bone_mapping: StepBoneMapping) {
super()
@@ -29,9 +29,9 @@ export class RetargetAnimationPreview extends EventTarget {
}
public begin (): void {
- if (!this._added_event_listeners) {
+ if (!this.has_added_event_listeners) {
this.add_event_listeners()
- this._added_event_listeners = true
+ this.has_added_event_listeners = true
}
}
@@ -72,7 +72,9 @@ export class RetargetAnimationPreview extends EventTarget {
}
// Create animation mixer for the target skeleton
- this.animation_mixer = new AnimationMixer(this.target_skinned_meshes[0])
+ // Use a dummy Object3D as root since we apply animations directly to skinned meshes
+ // there are often more than one skinned mesh we want to animate at a time
+ this.animation_mixer = new AnimationMixer(new Object3D())
// Load the first animation based on skeleton type
await this.load_first_animation()
@@ -100,8 +102,9 @@ export class RetargetAnimationPreview extends EventTarget {
* Load the first animation from the appropriate animation file
*/
private async load_first_animation (): Promise<void> {
+ // get location of animation file to load that has preview animations
const source_skeleton_type = this.step_bone_mapping.get_source_skeleton_type()
- const animation_file_path = this.get_animation_file_path(source_skeleton_type)
+ const animation_file_path = RetargetUtils.get_animation_file_path(source_skeleton_type)
if (animation_file_path === null) {
console.log('No animation file found for skeleton type:', source_skeleton_type)
@@ -142,24 +145,6 @@ export class RetargetAnimationPreview extends EventTarget {
}
}
- /**
- * Get the animation file path based on skeleton type
- */
- private get_animation_file_path (skeleton_type: SkeletonType): string | null {
- switch (skeleton_type) {
- case SkeletonType.Human:
- return '/animations/human-base-animations.glb'
- case SkeletonType.Quadraped:
- return '/animations/quad-creature-animations.glb'
- case SkeletonType.Bird:
- return '/animations/bird-animations.glb'
- case SkeletonType.Dragon:
- return '/animations/dragon-animations.glb'
- default:
- return null
- }
- }
-
/**
* Update the preview animation by applying current bone mappings
*/
@@ -237,7 +222,7 @@ export class RetargetAnimationPreview extends EventTarget {
// Create a track for each target bone this source bone maps to
target_bone_names.forEach((target_bone_name) => {
- const new_track_name = this.create_track_name(target_bone_name, property)
+ const new_track_name = RetargetUtils.create_track_name(target_bone_name, property)
// Clone the track with the new name
if (property === 'quaternion') {
@@ -345,7 +330,7 @@ export class RetargetAnimationPreview extends EventTarget {
* @param target_bone_name - Name of the bone in the target skeleton
* @returns The rotation difference as a quaternion (Y-axis only), or null if bones not found
*/
- private calculate_bone_rotation_delta (source_bone_name: string, target_bone_name: string): Quaternion | null {
+ private calculate_bone_rotation_delta (source_bone_name: string, target_bone_name: string): Quaternion | null {
// Get both skeleton roots
const source_armature = this.step_bone_mapping.get_source_armature()
const target_skeleton_data = this.step_bone_mapping.get_target_skeleton_data()
@@ -429,35 +414,24 @@ export class RetargetAnimationPreview extends EventTarget {
return null
}
- /**
- * Create a track name in the format expected by Three.js
- * For named bones, use: BoneName.property
- */
- private create_track_name (bone_name: string, property: string): string {
- return `${bone_name}.${property}`
- }
+
/**
* Update animation mixer on each frame
*/
public update (delta_time: number): void {
- if (this.animation_mixer !== null && this.is_preview_active) {
- this.animation_mixer.update(delta_time)
-
- // CRITICAL: Update the skeleton and skinned meshes after animation changes the bones
- // Why do I need this when I don't need it in the main Mesh2Motion engine?
- this.target_skinned_meshes.forEach((skinned_mesh) => {
- skinned_mesh.skeleton.bones.forEach(bone => {
- bone.updateMatrixWorld(true)
- })
- })
+ if (this.animation_mixer === null || !this.is_preview_active) {
+ return
}
- }
- /**
- * Check if preview is currently active
- */
- public is_active (): boolean {
- return this.is_preview_active
+ this.animation_mixer.update(delta_time)
+
+ // CRITICAL: Update the skeleton and skinned meshes after animation changes the bones
+ // Why do I need this when I don't need it in the main Mesh2Motion engine?
+ this.target_skinned_meshes.forEach((skinned_mesh) => {
+ skinned_mesh.skeleton.bones.forEach(bone => {
+ bone.updateMatrixWorld(true)
+ })
+ })
}
}
diff --git a/src/retarget/RetargetUtils.ts b/src/retarget/RetargetUtils.ts
index 964a69a..e9f706f 100644
--- a/src/retarget/RetargetUtils.ts
+++ b/src/retarget/RetargetUtils.ts
@@ -1,5 +1,6 @@
import { type Group, type Object3DEventMap, type Skeleton, type SkinnedMesh } from 'three'
import { ModalDialog } from '../lib/ModalDialog.ts'
+import { SkeletonType } from '../lib/enums/SkeletonType.ts'
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class RetargetUtils {
@@ -40,4 +41,30 @@ export class RetargetUtils {
console.log('skinned meshes found. ready to start retargeting process:', skinned_meshes)
return true
}
+
+ /**
+ * Get the animation file path based on skeleton type
+ */
+ static get_animation_file_path (skeleton_type: SkeletonType): string | null {
+ switch (skeleton_type) {
+ case SkeletonType.Human:
+ return '/animations/human-base-animations.glb'
+ case SkeletonType.Quadraped:
+ return '/animations/quad-creature-animations.glb'
+ case SkeletonType.Bird:
+ return '/animations/bird-animations.glb'
+ case SkeletonType.Dragon:
+ return '/animations/dragon-animations.glb'
+ default:
+ return null
+ }
+ }
+
+ /**
+ * Create a track name in the format expected by Three.js
+ * For named bones, use: BoneName.property
+ */
+ static create_track_name (bone_name: string, property: string): string {
+ return `${bone_name}.${property}`
+ }
}
diff --git a/src/retarget/StepBoneMapping.ts b/src/retarget/StepBoneMapping.ts
index 198c826..da606bf 100644
--- a/src/retarget/StepBoneMapping.ts
+++ b/src/retarget/StepBoneMapping.ts
@@ -14,7 +14,7 @@ export enum TargetBoneMappingType {
export class StepBoneMapping extends EventTarget {
private readonly _main_scene: Scene
- private target_skeleton_data: Group<Object3DEventMap> | null = null
+ private target_skeleton_data: Scene | null = null
private target_mapping_template: TargetBoneMappingType = TargetBoneMappingType.None
private source_armature: Object3D | null = null
@@ -29,10 +29,10 @@ export class StepBoneMapping extends EventTarget {
private target_bone_count: HTMLSpanElement | null = null
// Bone mapping: target bone name (uploaded mesh) -> source bone name (Mesh2Motion skeleton)
- private bone_mapping: Map<string, string> = new Map()
+ private bone_mapping = new Map<string, string>()
// Track if event listeners have been added
- private added_event_listeners: boolean = false
+ private has_added_event_listeners: boolean = false
constructor (main_scene: Scene) {
super()
@@ -50,16 +50,7 @@ export class StepBoneMapping extends EventTarget {
this.source_bone_count = document.getElementById('source-bone-count') as HTMLSpanElement
this.target_bone_count = document.getElementById('target-bone-count') as HTMLSpanElement
- // Add event listener for clear mappings button
- this.clear_mappings_button?.addEventListener('click', () => {
- this.clear_all_bone_mappings()
- console.log('All bone mappings cleared')
- })
-
- // Add event listener for auto-map button
- this.auto_map_button?.addEventListener('click', () => {
- this.auto_map_bones()
- })
+ this.add_event_listeners()
// Populate the lists
this.update_bone_lists()
@@ -67,15 +58,35 @@ export class StepBoneMapping extends EventTarget {
this.update_auto_map_button_visibility()
}
+ private add_event_listeners (): void {
+ if (!this.has_added_event_listeners) {
+ // Add event listener for clear mappings button
+ this.clear_mappings_button?.addEventListener('click', () => {
+ this.clear_all_bone_mappings()
+ console.log('All bone mappings cleared')
+ })
+
+ // Add event listener for auto-map button
+ this.auto_map_button?.addEventListener('click', () => {
+ this.auto_map_bones()
+ })
+
+ this.has_added_event_listeners = true
+ }
+ }
+
public set_source_skeleton_data (armature: Object3D, skeleton_type: SkeletonType): void {
this.source_armature = armature
+
+ console.log('setting the source armature in bone mapping:', this.source_armature)
+
this.source_skeleton_type = skeleton_type
console.log('Source skeleton data set in bone mapping:', this.source_armature, 'Type:', this.source_skeleton_type)
this.update_source_bones_list()
this.update_auto_map_button_visibility()
}
- public set_target_skeleton_data (skeleton_data: Group<Object3DEventMap>): void {
+ public set_target_skeleton_data (skeleton_data: Scene): void {
this.target_skeleton_data = skeleton_data
console.log('Target skeleton data set in bone mapping:', this.target_skeleton_data)
this.update_target_bones_list()
@@ -99,7 +110,7 @@ export class StepBoneMapping extends EventTarget {
return this.source_armature
}
- public get_target_skeleton_data (): Group<Object3DEventMap> | null {
+ public get_target_skeleton_data (): Scene | null {
return this.target_skeleton_data
}
@@ -162,20 +173,20 @@ export class StepBoneMapping extends EventTarget {
private update_source_bones_list (): void {
if (this.source_bones_list === null) return
- const bone_names = this.get_source_bone_names()
+ const source_bone_names = this.get_source_bone_names()
// Update bone count display
if (this.source_bone_count !== null) {
- this.source_bone_count.textContent = `(${bone_names.length})`
+ this.source_bone_count.textContent = `(${source_bone_names.length})`
}
- if (bone_names.length === 0) {
+ if (source_bone_names.length === 0) {
this.source_bones_list.innerHTML = '<em>No source skeleton loaded</em>'
return
}
this.source_bones_list.innerHTML = ''
- bone_names.forEach((name) => {
+ source_bone_names.forEach((name) => {
const bone_item = document.createElement('div')
bone_item.textContent = name
bone_item.className = 'bone-item bone-item-source'
@@ -195,20 +206,20 @@ export class StepBoneMapping extends EventTarget {
private update_target_bones_list (): void {
if (this.target_bones_list === null) return
- const bone_names = this.get_target_bone_names()
+ const target_bone_names = this.get_target_bone_names()
// Update bone count display
if (this.target_bone_count !== null) {
- this.target_bone_count.textContent = `(${bone_names.length})`
+ this.target_bone_count.textContent = `(${target_bone_names.length})`
}
- if (bone_names.length === 0) {
+ if (target_bone_names.length === 0) {
this.target_bones_list.innerHTML = '<em>No target skeleton loaded</em>'
return
}
this.target_bones_list.innerHTML = ''
- bone_names.forEach((name) => {
+ target_bone_names.forEach((name) => {
const bone_item = document.createElement('div')
bone_item.className = 'bone-item bone-item-target'
bone_item.dataset.targetBoneName = name