Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Turn animation retargeting service into singleton so we can start storing data in it

Scott Petrovic8cfe6799db2c
diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md
index 74ee858..44cc28d 100644
--- a/.github/copilot-instructions.md
+++ b/.github/copilot-instructions.md
@@ -107,6 +107,7 @@ export class StepLoadModel extends EventTarget {
- camelCase for private methods
- Getter pattern: `get_property_name()` returns data
- Setter pattern: `set_property_name(value)`
+- Function definitions ALWAYS have spaces after function name parentheses: `function_name (args)`
### Classes
- PascalCase: `StepLoadModel`, `CustomFBXLoader`, `ModalDialog`
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.ts
index 586b1d6..c750875 100644
--- a/src/retarget/AnimationRetargetService.ts
+++ b/src/retarget/AnimationRetargetService.ts
@@ -10,12 +10,19 @@ export interface TrackNameParts {
property: string
}
-/**
- * AnimationRetargetService - Shared service for retargeting animations from one skeleton to another
- * Used by both RetargetAnimationPreview and RetargetAnimationListing
- */
-// eslint-disable-next-line @typescript-eslint/no-extraneous-class
+// AnimationRetargetService - Shared service for retargeting animations from one skeleton to another
+// Used by both RetargetAnimationPreview and RetargetAnimationListing
export class AnimationRetargetService {
+ private static instance: AnimationRetargetService | null = null
+
+ private constructor () {}
+
+ public static getInstance (): AnimationRetargetService {
+ if (AnimationRetargetService.instance === null) {
+ AnimationRetargetService.instance = new AnimationRetargetService()
+ }
+ return AnimationRetargetService.instance
+ }
/**
* Retarget an animation clip using bone mappings
* @param source_clip - The original animation clip from the source skeleton
@@ -26,7 +33,8 @@ export class AnimationRetargetService {
* @param target_skinned_meshes - The target skinned meshes (for finding bones)
* @returns A new animation clip retargeted for the target skeleton
*/
- static retarget_animation_clip (
+
+ public retarget_animation_clip (
source_clip: AnimationClip,
bone_mappings: Map<string, string>,
target_mapping_type: TargetBoneMappingType,
@@ -89,7 +97,7 @@ export class AnimationRetargetService {
/**
* Create a reverse mapping: source bone name -> array of target bone names
*/
- private static reverse_bone_mapping (bone_mappings: Map<string, string>): Map<string, string[]> {
+ private reverse_bone_mapping (bone_mappings: Map<string, string>): Map<string, string[]> {
const reverse_mappings = new Map<string, string[]>()
bone_mappings.forEach((source_bone_name, target_bone_name) => {
if (!reverse_mappings.has(source_bone_name)) {
@@ -106,7 +114,7 @@ export class AnimationRetargetService {
/**
* Apply bone rotation correction for fixing bone roll delta between target and source skeleton
*/
- private static apply_bone_rotation_correction (
+ private apply_bone_rotation_correction (
animation_clip: AnimationClip,
bone_mappings: Map<string, string>,
source_armature: Group | null,
@@ -125,7 +133,7 @@ export class AnimationRetargetService {
if (delta !== null) {
const delta_euler = new Euler().setFromQuaternion(delta)
- this.rotate_bone_for_retargeting(animation_clip, [target_bone_name], delta_euler)
+ AnimationRetargetService.rotate_bone_for_retargeting(animation_clip, [target_bone_name], delta_euler)
} else {
console.log(`Warning: delta is NULL when fixing bone roll. Skipping correction for bone: ${target_bone_name}`)
}
@@ -144,7 +152,7 @@ export class AnimationRetargetService {
* @param target_skinned_meshes - The target skinned meshes
* @returns The rotation difference as a quaternion (Y-axis only), or null if bones not found
*/
- private static calculate_bone_rotation_delta (
+ private calculate_bone_rotation_delta (
source_bone_name: string,
target_bone_name: string,
source_armature: Group | null,
@@ -202,7 +210,7 @@ export class AnimationRetargetService {
const rotation_amount: Quaternion = new Quaternion().setFromEuler(rotate_obj)
for (const track of tracks_to_change) {
- const name_info = this.parse_track_name_for_metadata(track.name)
+ const name_info = AnimationRetargetService.getInstance().parse_track_name_for_metadata(track.name)
if (name_info === null) continue
const values = track.values
@@ -228,7 +236,7 @@ export class AnimationRetargetService {
/**
* Find a bone by name in the skeleton hierarchy
*/
- private static find_bone_by_name (
+ private find_bone_by_name (
root: Object3D,
skinned_meshes: SkinnedMesh[],
bone_name: string
@@ -259,7 +267,7 @@ export class AnimationRetargetService {
* Parse a track name to extract bone name and property (e.g., "quaternion", "position", "scale")
* Handles various formats like "boneName.property" or ".bones[boneName].property"
*/
- private static parse_track_name_for_metadata (track_name: string): TrackNameParts | null {
+ private parse_track_name_for_metadata (track_name: string): TrackNameParts | null {
// Try format: "boneName.property"
const simple_match = track_name.match(/^([^.]+)\.(.+)$/)
if (simple_match !== null) {
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.ts
index 6d4bf6d..939797b 100644
--- a/src/retarget/RetargetAnimationListing.ts
+++ b/src/retarget/RetargetAnimationListing.ts
@@ -208,7 +208,7 @@ export class RetargetAnimationListing extends EventTarget {
}
// Retarget the animation using the shared service
- const retargeted_clip: AnimationClip = AnimationRetargetService.retarget_animation_clip(
+ const retargeted_clip: AnimationClip = AnimationRetargetService.getInstance().retarget_animation_clip(
display_clip,
bone_mappings,
this.step_bone_mapping.get_target_mapping_template(),
diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.ts
index ed0d28f..fc7947d 100644
--- a/src/retarget/RetargetAnimationPreview.ts
+++ b/src/retarget/RetargetAnimationPreview.ts
@@ -181,7 +181,7 @@ export class RetargetAnimationPreview extends EventTarget {
}
// Create retargeted animation clip using shared service
- this.retargeted_animation_clip = AnimationRetargetService.retarget_animation_clip(
+ this.retargeted_animation_clip = AnimationRetargetService.getInstance().retarget_animation_clip(
this.current_animation_clip,
bone_mappings,
this.step_bone_mapping.get_target_mapping_template(),
diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.ts
index 4472521..99fb067 100644
--- a/src/retarget/steps/StepExportRetargetedAnimations.ts
+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts
@@ -1,4 +1,4 @@
-import { type Object3D, Scene, type SkinnedMesh, type AnimationClip, type Group } from 'three'
+import { type Scene, type SkinnedMesh, type AnimationClip, type Group } from 'three'
import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter'
import { AnimationRetargetService } from '../AnimationRetargetService'
import { TargetBoneMappingType } from './StepBoneMapping'
@@ -47,7 +47,7 @@ export class StepExportRetargetedAnimations extends EventTarget {
// Retarget all animation clips before export
let retargeted_clips: AnimationClip[] = []
retargeted_clips = this.animation_clips_to_export.map((clip) =>
- AnimationRetargetService.retarget_animation_clip(
+ AnimationRetargetService.getInstance().retarget_animation_clip(
clip,
this.bone_mapping,
this.target_mapping_type,