Import a 3D Model and automatically assign and export animations
TypeScript
Move target armature storage to the retarget service
Scott Petrovic16ed788e9b60
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.tsindex eab9fb5..54ca592 100644--- a/src/retarget/AnimationRetargetService.ts+++ b/src/retarget/AnimationRetargetService.ts@@ -1,6 +1,6 @@import {AnimationClip, Euler, type Object3D, Quaternion, QuaternionKeyframeTrack, Vector3,- VectorKeyframeTrack, type Scene, type Group, type SkinnedMesh+ VectorKeyframeTrack, Scene, Group, type SkinnedMesh} from 'three'import { RetargetUtils } from './RetargetUtils.ts'import { TargetBoneMappingType } from './steps/StepBoneMapping.ts'@@ -23,16 +23,27 @@ export class AnimationRetargetService {* Get/set for the skeleton type. This will be the source of truth* for other classes to grab this data*/- private source_armature: Group | null = null- public set_source_armature (armature: Group | null): void {+ private source_armature: Group = new Group()+ private skeleton_type: SkeletonType = SkeletonType.None+ private target_armature: Scene = new Scene()+ private target_mapping_type: TargetBoneMappingType = TargetBoneMappingType.None++ public set_source_armature (armature: Group): void {this.source_armature = armature}- public get_source_armature (): Group | null {+ public get_source_armature (): Group {return this.source_armature}- private skeleton_type: SkeletonType = SkeletonType.None+ public set_target_armature (armature: Scene): void {+ this.target_armature = armature+ }++ public get_target_armature (): Scene {+ return this.target_armature+ }+public set_skeleton_type (type: SkeletonType): void {this.skeleton_type = type}@@ -41,6 +52,14 @@ export class AnimationRetargetService {return this.skeleton_type}+ public set_target_mapping_type (type: TargetBoneMappingType): void {+ this.target_mapping_type = type+ }++ public get_target_mapping_type (): TargetBoneMappingType {+ return this.target_mapping_type+ }+// #endregionprivate constructor () {}@@ -51,31 +70,18 @@ export class AnimationRetargetService {}return AnimationRetargetService.instance}+/*** Retarget an animation clip using bone mappings* @param source_clip - The original animation clip from the source skeleton* @param bone_mappings - Map of target bone name -> source bone name- * @param target_mapping_type - The type of target skeleton mapping (for applying corrections)- * @param source_armature - The source skeleton armature (for bone rotation corrections)- * @param target_skeleton_data - The target skeleton data (for bone rotation corrections)+ * @param target_armature - The target skeleton data (for bone rotation corrections)* @param target_skinned_meshes - The target skinned meshes (for finding bones)* @returns A new animation clip retargeted for the target skeleton*/-- private target_mapping_type: TargetBoneMappingType = TargetBoneMappingType.None-- public set_target_mapping_type (type: TargetBoneMappingType): void {- this.target_mapping_type = type- }-- public get_target_mapping_type (): TargetBoneMappingType {- return this.target_mapping_type- }-public retarget_animation_clip (source_clip: AnimationClip,bone_mappings: Map<string, string>,- target_skeleton_data: Scene | null = null,target_skinned_meshes: SkinnedMesh[] = []): AnimationClip {const new_tracks: any[] = [] // store new retargeted tracks@@ -120,7 +126,6 @@ export class AnimationRetargetService {this.apply_bone_rotation_correction(retargeted_clip,bone_mappings,- target_skeleton_data,target_skinned_meshes)}@@ -152,14 +157,12 @@ export class AnimationRetargetService {private apply_bone_rotation_correction (animation_clip: AnimationClip,bone_mappings: Map<string, string>,- target_skeleton_data: Scene | null,target_skinned_meshes: SkinnedMesh[]): void {bone_mappings.forEach((source_bone_name, target_bone_name) => {const delta = this.calculate_bone_rotation_delta(source_bone_name,target_bone_name,- target_skeleton_data,target_skinned_meshes)@@ -181,24 +184,23 @@ export class AnimationRetargetService {* @param source_bone_name - Name of the bone in the source skeleton* @param target_bone_name - Name of the bone in the target skeleton* @param source_armature - The source skeleton armature- * @param target_skeleton_data - The target skeleton data+ * @param target_armature - The target skeleton data* @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 calculate_bone_rotation_delta (source_bone_name: string,target_bone_name: string,- target_skeleton_data: Scene | null,target_skinned_meshes: SkinnedMesh[]): Quaternion | null {- if (this.source_armature === null || target_skeleton_data === null) {+ if (this.source_armature === null || this.target_armature === null) {console.warn('Cannot calculate rotation delta: missing skeleton data')return null}// Find source bone and target bone with normalized matchingconst source_bone = this.find_bone_by_name(this.source_armature, [], source_bone_name)- const target_bone = this.find_bone_by_name(target_skeleton_data, target_skinned_meshes, target_bone_name)+ const target_bone = this.find_bone_by_name(this.target_armature, target_skinned_meshes, target_bone_name)if (source_bone === null || target_bone === null) {const source_label = source_bone === null ? 'null' : source_bone.namediff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.tsindex 17d0703..00ccb7b 100644--- a/src/retarget/RetargetAnimationListing.ts+++ b/src/retarget/RetargetAnimationListing.ts@@ -2,7 +2,6 @@ import { AnimationPlayer } from '../lib/processes/animations-listing/AnimationPlimport { AnimationSearch } from '../lib/processes/animations-listing/AnimationSearch.ts'import { AnimationLoader } from '../lib/processes/animations-listing/AnimationLoader.ts'import { type AnimationClip, AnimationMixer, type SkinnedMesh, Object3D, type Scene, type AnimationAction } from 'three'-import type { SkeletonType } from '../lib/enums/SkeletonType.ts'import type { ThemeManager } from '../lib/ThemeManager.ts'import { type TransformedAnimationClipPair } from '../lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts'import { AnimationRetargetService } from './AnimationRetargetService.ts'@@ -209,8 +208,6 @@ export class RetargetAnimationListing extends EventTarget {const retargeted_clip: AnimationClip = AnimationRetargetService.getInstance().retarget_animation_clip(display_clip,bone_mappings,- this.step_bone_mapping.get_target_mapping_template(),- this.step_bone_mapping.get_target_skeleton_data(),this.skinned_meshes_to_animate)@@ -248,7 +245,6 @@ export class RetargetAnimationListing extends EventTarget {this.target_rig_scene,this.skinned_meshes_to_animate,this.step_bone_mapping.get_bone_mapping(),- this.step_bone_mapping.get_target_mapping_template(),this.step_bone_mapping.get_target_skeleton_data())this.step_export_retargeted_animations.export('retargeted_animations')diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.tsindex abd725b..2bfedd5 100644--- a/src/retarget/RetargetAnimationPreview.ts+++ b/src/retarget/RetargetAnimationPreview.ts@@ -185,7 +185,6 @@ export class RetargetAnimationPreview extends EventTarget {this.retargeted_animation_clip = AnimationRetargetService.getInstance().retarget_animation_clip(this.current_animation_clip,bone_mappings,- this.step_bone_mapping.get_target_skeleton_data(),this.target_skinned_meshes)diff --git a/src/retarget/bone-automap/BoneAutoMapper.ts b/src/retarget/bone-automap/BoneAutoMapper.tsindex 8c60dea..1f90de7 100644--- a/src/retarget/bone-automap/BoneAutoMapper.ts+++ b/src/retarget/bone-automap/BoneAutoMapper.ts@@ -48,11 +48,11 @@ export class BoneAutoMapper {/*** Attempts to automatically map source bones (Mesh2Motion) to target bones (uploaded mesh)* @param source_armature - Source skeleton armature (Mesh2Motion skeleton)- * @param target_skeleton_data - Target skeleton data (uploaded mesh)+ * @param target_armature - Target skeleton armature (uploaded mesh)* @returns Map of target bone name -> source bone name*/public static auto_map_bones (- target_skeleton_data: Scene,+ target_armature: Scene,target_bone_mapping_type: TargetBoneMappingType): Map<string, string> {// mappings: final output mapping of target bone name to source bone name@@ -69,7 +69,7 @@ export class BoneAutoMapper {// this also contains the parent bone relationship// which will help us later when doing auto-mapping calculationsconst source_parent_map: Map<string, string | null> = BoneAutoMapper.extract_source_bone_parent_map(source_armature)- const target_parent_map: Map<string, string | null> = BoneAutoMapper.extract_target_bone_parent_map(target_skeleton_data)+ const target_parent_map: Map<string, string | null> = BoneAutoMapper.extract_target_bone_parent_map(target_armature)// Create metadata for both source and target bonesconst source_bones_meta: BoneMetadata[] = BoneAutoMapper.create_all_bone_metadata(source_parent_map)diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.tsindex cf14779..9f09984 100644--- a/src/retarget/retarget.ts+++ b/src/retarget/retarget.ts@@ -1,11 +1,12 @@import { Mesh2MotionEngine } from '../Mesh2MotionEngine.ts'-import { type Group, type Object3DEventMap, Scene, Vector3 } from 'three'+import { type Group, type Scene, Vector3 } from 'three'import { StepLoadSourceSkeleton } from './steps/StepLoadSourceSkeleton.ts'import { StepLoadTargetModel } from './steps/StepLoadTargetModel.ts'import { StepBoneMapping } from './steps/StepBoneMapping.ts'import { RetargetAnimationPreview } from './RetargetAnimationPreview.ts'import { RetargetAnimationListing } from './RetargetAnimationListing.ts'import { AnimationRetargetService } from './AnimationRetargetService'+import { type SkeletonType } from '../lib/enums/SkeletonType.ts'class RetargetModule {private readonly mesh2motion_engine: Mesh2MotionEngine@@ -82,20 +83,27 @@ class RetargetModule {// Listen for source skeleton (Mesh2Motion) loadedthis.step_load_source_skeleton.addEventListener('skeleton-loaded', () => {- const source_armature = this.step_load_source_skeleton.get_loaded_source_armature() as import('three').Group- const skeleton_type = this.step_load_source_skeleton.get_skeleton_type()- const anim_service = AnimationRetargetService.getInstance()- anim_service.set_skeleton_type(skeleton_type)- anim_service.set_source_armature(source_armature)- this.step_bone_mapping.set_source_skeleton_data()+ // the load step stores the scene and skeleton type internally. grab the data here+ const source_armature: Group = this.step_load_source_skeleton.get_loaded_source_armature() as Group+ const skeleton_type: SkeletonType = this.step_load_source_skeleton.get_skeleton_type()++ // animation service keeps track of shared data across classes+ AnimationRetargetService.getInstance().set_source_armature(source_armature)+ AnimationRetargetService.getInstance().set_skeleton_type(skeleton_type)+ this.step_bone_mapping.source_armature_updated()})// Listen for target model (user-uploaded) loadedthis.step_load_target_model.addEventListener('target-model-loaded', (_event: Event) => {- const retargetable_meshes: Scene | null = this.step_load_target_model.get_retargetable_meshes()+ const retargetable_armature: Scene | null = this.step_load_target_model.get_retargetable_meshes()+ if (retargetable_armature == null) {+ console.error('No retargetable meshes found in the uploaded model.')+ return+ }+ AnimationRetargetService.getInstance().set_target_armature(retargetable_armature)// Set target skeleton data in bone mapping (uploaded mesh)- this.step_bone_mapping.set_target_skeleton_data(retargetable_meshes)+ this.step_bone_mapping.target_armature_updated()this.start_live_preview()// Show "Continue" button to proceed to animation listingdiff --git a/src/retarget/steps/StepBoneMapping.ts b/src/retarget/steps/StepBoneMapping.tsindex 1ea4154..ddb0d59 100644--- a/src/retarget/steps/StepBoneMapping.ts+++ b/src/retarget/steps/StepBoneMapping.ts@@ -1,5 +1,4 @@-import { Group, type Object3D, type Object3DEventMap, type Scene, type SkinnedMesh } from 'three'-import { SkeletonType } from '../../lib/enums/SkeletonType.ts'+import { type Scene, type SkinnedMesh } from 'three'import { BoneAutoMapper } from '../bone-automap/BoneAutoMapper.ts'import { MixamoMapper } from '../bone-automap/MixamoMapper.ts'import { AnimationRetargetService } from '../AnimationRetargetService.ts'@@ -15,8 +14,6 @@ export enum TargetBoneMappingType {export class StepBoneMapping extends EventTarget {private readonly _main_scene: Scene- private target_skeleton_data: Scene | null = null- private target_mapping_template: TargetBoneMappingType = TargetBoneMappingType.None// DOM referencesprivate source_bones_list: HTMLDivElement | null = null@@ -73,14 +70,12 @@ export class StepBoneMapping extends EventTarget {}}- public set_source_skeleton_data (): void {+ public source_armature_updated (): void {this.update_source_bones_list()this.update_auto_map_button_visibility()}- public set_target_skeleton_data (skeleton_data: Scene | null): void {- this.target_skeleton_data = skeleton_data- console.log('Target skeleton data set in bone mapping:', this.target_skeleton_data)+ public target_armature_updated (): void {this.update_target_bones_list()this.update_auto_map_button_visibility()}@@ -90,7 +85,7 @@ export class StepBoneMapping extends EventTarget {}public has_target_skeleton (): boolean {- return this.target_skeleton_data !== null+ return AnimationRetargetService.getInstance().get_target_armature() !== null}public has_both_skeletons (): boolean {@@ -99,11 +94,7 @@ export class StepBoneMapping extends EventTarget {// Getterspublic get_target_skeleton_data (): Scene | null {- return this.target_skeleton_data- }-- public get_target_mapping_template (): TargetBoneMappingType {- return this.target_mapping_template+ return AnimationRetargetService.getInstance().get_target_armature()}// Extract bone names from source skeleton (Mesh2Motion skeleton)@@ -124,7 +115,8 @@ export class StepBoneMapping extends EventTarget {// Extract bone names from target skeleton (uploaded mesh)public get_target_bone_names (): string[] {- if (this.target_skeleton_data === null) {+ const target_armature = AnimationRetargetService.getInstance().get_target_armature()+ if (target_armature === null) {return []}@@ -135,7 +127,7 @@ export class StepBoneMapping extends EventTarget {// Target skeleton data contains SkinnedMesh objects with skeleton property// Use Set to avoid duplicates when multiple SkinnedMesh share the same skeleton- this.target_skeleton_data.traverse((child) => {+ target_armature.traverse((child) => {if (child.type === 'SkinnedMesh') {const skinned_mesh = child as SkinnedMeshconst skeleton = skinned_mesh.skeleton@@ -362,20 +354,15 @@ export class StepBoneMapping extends EventTarget {// see if target bones follow a known templateif (MixamoMapper.is_target_valid_skeleton(this.get_target_bone_names())) {- this.target_mapping_template = TargetBoneMappingType.Mixamo+ AnimationRetargetService.getInstance().set_target_mapping_type(TargetBoneMappingType.Mixamo)} else {- this.target_mapping_template = TargetBoneMappingType.Custom- }-- if (this.target_skeleton_data === null) {- console.error('Target skeleton data is null during auto-mapping')- return+ AnimationRetargetService.getInstance().set_target_mapping_type(TargetBoneMappingType.Custom)}// Use BoneAutoMapper to generate mappingsconst auto_mappings = BoneAutoMapper.auto_map_bones(- this.target_skeleton_data,- this.target_mapping_template+ AnimationRetargetService.getInstance().get_target_armature(),+ AnimationRetargetService.getInstance().get_target_mapping_type())// Apply the auto-generated mappingsdiff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.tsindex 95d042e..4438d94 100644--- a/src/retarget/steps/StepExportRetargetedAnimations.ts+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts@@ -1,7 +1,6 @@-import { type Scene, type SkinnedMesh, type AnimationClip, type Group } from 'three'+import { type Scene, type SkinnedMesh, type AnimationClip } from 'three'import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter'import { AnimationRetargetService } from '../AnimationRetargetService'-import { TargetBoneMappingType } from './StepBoneMapping'export class StepExportRetargetedAnimations extends EventTarget {public animation_clips_to_export: AnimationClip[] = []@@ -47,7 +46,6 @@ export class StepExportRetargetedAnimations extends EventTarget {AnimationRetargetService.getInstance().retarget_animation_clip(clip,this.bone_mapping,- this.target_skeleton_data,this.target_skinned_meshes // use the meshes being exported as the target))