Import a 3D Model and automatically assign and export animations
TypeScript
Swap source and target. The target is supposed to be the final skinned mesh
Scott Petrovic26a2b3cb2295
diff --git a/src/retarget/BoneAutoMapper.ts b/src/retarget/BoneAutoMapper.tsindex 1827f92..b1952ac 100644--- a/src/retarget/BoneAutoMapper.ts+++ b/src/retarget/BoneAutoMapper.ts@@ -33,13 +33,15 @@ export interface BoneMetadata {/*** BoneAutoMapper - Handles automatic bone mapping between source and target skeletons+ * Source = Mesh2Motion skeleton (draggable bones)+ * Target = Uploaded mesh skeleton (drop zones)* Uses string comparison and pattern matching to suggest bone mappings*/export class BoneAutoMapper {/**- * Attempts to automatically map source bones to target bones- * @param source_bone_names - Array of bone names from the source skeleton- * @param target_bone_names - Array of bone names from the target skeleton+ * Attempts to automatically map source bones (Mesh2Motion) to target bones (uploaded mesh)+ * @param source_bone_names - Array of bone names from the Mesh2Motion skeleton (source)+ * @param target_bone_names - Array of bone names from the uploaded mesh skeleton (target)* @returns Map of target bone name -> source bone name*/public static auto_map_bones (source_bone_names: string[], target_bone_names: string[]): Map<string, string> {@@ -232,9 +234,9 @@ export class BoneAutoMapper {}/**- * Find the best matching source bone for a given target bone- * @param target_bone_meta - Target bone metadata to match- * @param source_bones_meta - Array of source bone metadata to search+ * Find the best matching source bone (Mesh2Motion) for a given target bone (uploaded mesh)+ * @param target_bone_meta - Target bone metadata to match (uploaded mesh)+ * @param source_bones_meta - Array of source bone metadata to search (Mesh2Motion skeleton)* @param used_source_bones - Set of source bone names that have already been mapped* @returns Best matching source bone metadata, or null if no good match found*/diff --git a/src/retarget/StepBoneMapping.ts b/src/retarget/StepBoneMapping.tsindex 1bcb2dd..bacae20 100644--- a/src/retarget/StepBoneMapping.ts+++ b/src/retarget/StepBoneMapping.ts@@ -4,9 +4,9 @@ import { BoneAutoMapper } from './BoneAutoMapper.ts'export class StepBoneMapping extends EventTarget {private readonly _main_scene: Scene- private source_skeleton_data: Group<Object3DEventMap> | null = null- private target_armature: Object3D | null = null- private target_skeleton_type: SkeletonType = SkeletonType.None+ private target_skeleton_data: Group<Object3DEventMap> | null = null+ private source_armature: Object3D | null = null+ private source_skeleton_type: SkeletonType = SkeletonType.None// DOM referencesprivate source_bones_list: HTMLDivElement | null = null@@ -16,7 +16,7 @@ export class StepBoneMapping extends EventTarget {private source_bone_count: HTMLSpanElement | null = nullprivate target_bone_count: HTMLSpanElement | null = null- // Bone mapping: target bone name -> source bone name+ // Bone mapping: target bone name (uploaded mesh) -> source bone name (Mesh2Motion skeleton)private bone_mapping: Map<string, string> = new Map()// Track if event listeners have been added@@ -55,27 +55,27 @@ export class StepBoneMapping extends EventTarget {this.update_auto_map_button_visibility()}- public set_source_skeleton_data (skeleton_data: Group<Object3DEventMap>): void {- this.source_skeleton_data = skeleton_data- console.log('Source skeleton data set in bone mapping:', this.source_skeleton_data)+ public set_source_skeleton_data (armature: Object3D, skeleton_type: SkeletonType): void {+ this.source_armature = 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 (armature: Object3D, skeleton_type: SkeletonType): void {- this.target_armature = armature- this.target_skeleton_type = skeleton_type- console.log('Target skeleton data set in bone mapping:', this.target_armature, 'Type:', this.target_skeleton_type)+ public set_target_skeleton_data (skeleton_data: Group<Object3DEventMap>): 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()this.update_auto_map_button_visibility()}public has_source_skeleton (): boolean {- return this.source_skeleton_data !== null+ return this.source_armature !== null}public has_target_skeleton (): boolean {- return this.target_armature !== null+ return this.target_skeleton_data !== null}public has_both_skeletons (): boolean {@@ -83,49 +83,49 @@ export class StepBoneMapping extends EventTarget {}// Getters- public get_source_skeleton_data (): Group<Object3DEventMap> | null {- return this.source_skeleton_data+ public get_source_armature (): Object3D | null {+ return this.source_armature}- public get_target_armature (): Object3D | null {- return this.target_armature+ public get_target_skeleton_data (): Group<Object3DEventMap> | null {+ return this.target_skeleton_data}- public get_target_skeleton_type (): SkeletonType {- return this.target_skeleton_type+ public get_source_skeleton_type (): SkeletonType {+ return this.source_skeleton_type}- // Extract bone names from source skeleton+ // Extract bone names from source skeleton (Mesh2Motion skeleton)public get_source_bone_names (): string[] {- if (this.source_skeleton_data === null) {+ if (this.source_armature === null) {return []}const bone_names: string[] = []- // Source skeleton data contains SkinnedMesh objects with skeleton property- this.source_skeleton_data.traverse((child) => {- if (child.type === 'SkinnedMesh') {- const skinned_mesh = child as SkinnedMesh- const skeleton = skinned_mesh.skeleton- skeleton.bones.forEach((bone) => {- bone_names.push(bone.name)- })+ this.source_armature.traverse((child) => {+ if (child.type === 'Bone') {+ bone_names.push(child.name)}})return bone_names}- // Extract bone names from target skeleton+ // Extract bone names from target skeleton (uploaded mesh)public get_target_bone_names (): string[] {- if (this.target_armature === null) {+ if (this.target_skeleton_data === null) {return []}const bone_names: string[] = []- this.target_armature.traverse((child) => {- if (child.type === 'Bone') {- bone_names.push(child.name)+ // Target skeleton data contains SkinnedMesh objects with skeleton property+ this.target_skeleton_data.traverse((child) => {+ if (child.type === 'SkinnedMesh') {+ const skinned_mesh = child as SkinnedMesh+ const skeleton = skinned_mesh.skeleton+ skeleton.bones.forEach((bone) => {+ bone_names.push(bone.name)+ })}})@@ -159,7 +159,7 @@ export class StepBoneMapping extends EventTarget {bone_item.textContent = namebone_item.className = 'bone-item bone-item-source'- // Make the bone draggable+ // Make the bone draggable (source bones from Mesh2Motion skeleton)bone_item.draggable = truebone_item.dataset.boneName = name@@ -209,7 +209,7 @@ export class StepBoneMapping extends EventTarget {bone_item.textContent = name}- // Make the bone a drop target+ // Make the bone a drop target (target bones from uploaded mesh)bone_item.addEventListener('dragover', this.handle_drag_over.bind(this))bone_item.addEventListener('dragleave', this.handle_drag_leave.bind(this))bone_item.addEventListener('drop', this.handle_drop.bind(this))diff --git a/src/retarget/StepLoadTargetSkeleton.ts b/src/retarget/StepLoadSourceSkeleton.tssimilarity index 82%rename from src/retarget/StepLoadTargetSkeleton.tsrename to src/retarget/StepLoadSourceSkeleton.tsindex ea74a96..b7bea76 100644--- a/src/retarget/StepLoadTargetSkeleton.ts+++ b/src/retarget/StepLoadSourceSkeleton.ts@@ -4,10 +4,10 @@ import { SkeletonType } from '../lib/enums/SkeletonType.ts'import type GLTFResult from '../lib/processes/load-skeleton/interfaces/GLTFResult.ts'import { ModalDialog } from '../lib/ModalDialog.ts'-export class StepLoadTargetSkeleton extends EventTarget {+export class StepLoadSourceSkeleton extends EventTarget {private readonly loader: GLTFLoader = new GLTFLoader() // all skeletons are in GLB formatprivate readonly _main_scene: Scene- private loaded_target_armature: Object3D = new Object3D()+ private loaded_source_armature: Object3D = new Object3D()private skeleton_helper: SkeletonHelper | null = nullprivate _added_event_listeners: boolean = false@@ -111,14 +111,14 @@ export class StepLoadTargetSkeleton extends EventTarget {return}- this.loaded_target_armature = armature.clone()- this.loaded_target_armature.name = 'Target Armature'+ this.loaded_source_armature = armature.clone()+ this.loaded_source_armature.name = 'Source Armature (Mesh2Motion)'- // Offset position to the side so it's visible next to the source skeleton- this.loaded_target_armature.position.set(2.5, 0, 0)- this.loaded_target_armature.updateWorldMatrix(true, true)+ // Offset position to the side so it's visible next to the target skeleton+ this.loaded_source_armature.position.set(2.5, 0, 0)+ this.loaded_source_armature.updateWorldMatrix(true, true)- console.log('Target skeleton loaded successfully:', this.loaded_target_armature)+ console.log('Source skeleton (Mesh2Motion) loaded successfully:', this.loaded_source_armature)// Add to scene and create skeleton helperthis.add_skeleton_and_helper_to_scene()@@ -147,22 +147,22 @@ export class StepLoadTargetSkeleton extends EventTarget {}private add_skeleton_and_helper_to_scene (): void {- // Add the target skeleton to the scene- this._main_scene.add(this.loaded_target_armature)+ // Add the source skeleton to the scene+ this._main_scene.add(this.loaded_source_armature)// Create skeleton helper for visualization- this.skeleton_helper = new SkeletonHelper(this.loaded_target_armature)- this.skeleton_helper.name = 'Target Skeleton Helper'+ this.skeleton_helper = new SkeletonHelper(this.loaded_source_armature)+ this.skeleton_helper.name = 'Source Skeleton Helper (Mesh2Motion)'this._main_scene.add(this.skeleton_helper)- console.log('Target skeleton added to scene with helper')+ console.log('Source skeleton added to scene with helper')}private clear_previous_skeleton (): void {// Remove previous armature from scene- if (this.loaded_target_armature.parent !== null) {- this._main_scene.remove(this.loaded_target_armature)- console.log('Removed previous target armature from scene')+ if (this.loaded_source_armature.parent !== null) {+ this._main_scene.remove(this.loaded_source_armature)+ console.log('Removed previous source armature from scene')}// Remove previous skeleton helper from scene@@ -179,8 +179,8 @@ export class StepLoadTargetSkeleton extends EventTarget {}// Getters to be used by main retarget module- public get_loaded_target_armature (): Object3D {- return this.loaded_target_armature+ public get_loaded_source_armature (): Object3D {+ return this.loaded_source_armature}public get_skeleton_type (): SkeletonType {diff --git a/src/retarget/index.html b/src/retarget/index.htmlindex 8a23081..ace207a 100644--- a/src/retarget/index.html+++ b/src/retarget/index.html@@ -44,15 +44,21 @@<p>Instructions and information go here with what this page is for</p><div style="display: flex; flex-direction: row; gap: 1rem; margin-top: 1rem;">- <!-- imported 3d skinned mesh and bones list-->+ <!-- Mesh2Motion skeleton (source) - draggable bones --><div class="bone-map-section">- <p class="bone-map-heading">Your 3d rig model</p>+ <h2 class="bone-map-heading">Mesh2Motion Skeleton (Source)</h2><div class="bone-map-content">- <!-- there are some issues going on with scaling the skeleton for FBX and other formats so we will start simple for now with just GLB files -->- <input type="file" id="upload-file" accept=".glb" style="display: none;" />- <label for="upload-file" class="button" id="load-model-button">Upload GLB</label>+ <div id="skeleton-selection-container" >+ <select id="skeleton-type-select" class="button">+ <option value="">-- Select Skeleton --</option>+ <option value="human">Human</option>+ <option value="quadraped">Quadruped (Fox)</option>+ <option value="bird">Bird</option>+ <option value="dragon">Dragon</option>+ </select>+ </div>- <p>Bones List <span id="source-bone-count">(0)</span></p>+ <p>Bones List (Drag from here) <span id="source-bone-count">(0)</span></p><div id="source-bones-list"><p style="color: var(--text-secondary);">No source skeleton loaded</p></div>@@ -64,23 +70,16 @@⇨</div>- <!-- target skeleton selection and bones list-->+ <!-- imported 3d skinned mesh (target) - drop zone for bones --><div class="bone-map-section">- <h2 class="bone-map-heading">Mesh2Motion Skeleton</h2>+ <p class="bone-map-heading">Your 3D Rig Model (Target)</p><div class="bone-map-content">- <div id="skeleton-selection-container" >- <!-- <label for="skeleton-type-select">Choose Target Skeleton:</label> -->- <select id="skeleton-type-select" class="button">- <option value="">-- Select Skeleton --</option>- <option value="human">Human</option>- <option value="quadraped">Quadruped (Fox)</option>- <option value="bird">Bird</option>- <option value="dragon">Dragon</option>- </select>- </div>+ <!-- there are some issues going on with scaling the skeleton for FBX and other formats so we will start simple for now with just GLB files -->+ <input type="file" id="upload-file" accept=".glb" style="display: none;" />+ <label for="upload-file" class="button" id="load-model-button">Upload GLB</label>- <p>Bones List <span id="target-bone-count">(0)</span></p>- <div id="target-bones-list" >+ <p>Bones List (Drop here) <span id="target-bone-count">(0)</span></p>+ <div id="target-bones-list"><p style="color: var(--text-secondary);">No target skeleton loaded</p></div></div>diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.tsindex 79ed910..ff848e0 100644--- a/src/retarget/retarget.ts+++ b/src/retarget/retarget.ts@@ -1,7 +1,7 @@import { Mesh2MotionEngine } from '../Mesh2MotionEngine.ts'import { type Group, type Object3DEventMap, type SkinnedMesh, Vector3 } from 'three'import { ModalDialog } from '../lib/ModalDialog.ts'-import { StepLoadTargetSkeleton } from './StepLoadTargetSkeleton.ts'+import { StepLoadSourceSkeleton } from './StepLoadSourceSkeleton.ts'import { StepBoneMapping } from './StepBoneMapping.ts'import { RetargetUtils } from './RetargetUtils.ts'@@ -9,7 +9,7 @@ class RetargetModule {private readonly mesh2motion_engine: Mesh2MotionEngineprivate fileInput: HTMLInputElement | null = nullprivate load_model_button: HTMLLabelElement | null = null- private readonly step_load_target_skeleton: StepLoadTargetSkeleton+ private readonly step_load_source_skeleton: StepLoadSourceSkeletonprivate readonly step_bone_mapping: StepBoneMappingconstructor () {@@ -18,8 +18,8 @@ class RetargetModule {const camera_position = new Vector3().set(0, 1.7, 5)this.mesh2motion_engine.set_camera_position(camera_position)- // Initialize skeleton loading step- this.step_load_target_skeleton = new StepLoadTargetSkeleton(this.mesh2motion_engine.get_scene())+ // Initialize Mesh2Motion skeleton loading step (source)+ this.step_load_source_skeleton = new StepLoadSourceSkeleton(this.mesh2motion_engine.get_scene())// Initialize bone mapping stepthis.step_bone_mapping = new StepBoneMapping(this.mesh2motion_engine.get_scene())@@ -27,7 +27,7 @@ class RetargetModule {public init (): void {this.add_event_listeners()- this.step_load_target_skeleton.begin()+ this.step_load_source_skeleton.begin()this.step_bone_mapping.begin()}@@ -42,11 +42,11 @@ class RetargetModule {this.handleFileSelect(event)})- // Listen for target skeleton loaded- this.step_load_target_skeleton.addEventListener('skeleton-loaded', () => {- const target_armature = this.step_load_target_skeleton.get_loaded_target_armature()- const skeleton_type = this.step_load_target_skeleton.get_skeleton_type()- this.step_bone_mapping.set_target_skeleton_data(target_armature, skeleton_type)+ // Listen for source skeleton (Mesh2Motion) loaded+ this.step_load_source_skeleton.addEventListener('skeleton-loaded', () => {+ const source_armature = this.step_load_source_skeleton.get_loaded_source_armature()+ const skeleton_type = this.step_load_source_skeleton.get_skeleton_type()+ this.step_bone_mapping.set_source_skeleton_data(source_armature, skeleton_type)})}@@ -83,7 +83,7 @@ class RetargetModule {console.log('Model loaded for retargeting successfully.')URL.revokeObjectURL(file_url) // Revoke the object URL after loading is complete- // read in mesh2motion engine's retargetable model data+ // read in mesh2motion engine's retargetable model data (this is the target)const retargetable_meshes = this.mesh2motion_engine.load_model_step.get_final_retargetable_model_data()const is_valid_skinned_mesh = RetargetUtils.validate_skinned_mesh_has_bones(retargetable_meshes)if (is_valid_skinned_mesh) {@@ -94,8 +94,8 @@ class RetargetModule {// Add skeleton helperthis.add_skeleton_helper(retargetable_meshes)- // Set source skeleton data in bone mapping- this.step_bone_mapping.set_source_skeleton_data(retargetable_meshes)+ // Set target skeleton data in bone mapping (uploaded mesh)+ this.step_bone_mapping.set_target_skeleton_data(retargetable_meshes)}}, { once: true })} catch (error) {