Import a 3D Model and automatically assign and export animations
TypeScript
Process all the exported animations to do the retargeting with them
Scott Petrovic1794d7c7db14
diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.tsindex 56d2b88..92b1d0d 100644--- a/src/lib/processes/export-to-file/StepExportToFile.ts+++ b/src/lib/processes/export-to-file/StepExportToFile.ts@@ -35,6 +35,9 @@ export class StepExportToFile extends EventTarget {export_scene.add(final_skinned_mesh)})+ console.log('SKINNED MESH DATA TO EXPORT:', skinned_meshes)+ console.log('animations to export', this.animation_clips_to_export)+this.export_glb(export_scene, this.animation_clips_to_export, filename).then(() => {// Move the skinned meshes back to their original parentsdiff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.tsindex af3730e..d402541 100644--- a/src/retarget/RetargetAnimationListing.ts+++ b/src/retarget/RetargetAnimationListing.ts@@ -228,11 +228,21 @@ export class RetargetAnimationListing extends EventTarget {// configure export animations buttonthis.export_button = document.getElementById('export-retargeting-button') as HTMLButtonElementthis.export_button?.addEventListener('click', () => {+ // send in all the selected animation clips for exportthis.step_export_retargeted_animations.set_animation_clips_to_export(this.animation_clips_loaded.map(clip => clip.display_animation_clip),this.get_selected_animation_indices())- this.step_export_retargeted_animations.export(this.skinned_meshes_to_animate, 'retargeted_animations')++ // configure the export out step with retargeting info+ this.step_export_retargeted_animations.setup_retargeting(+ 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_source_armature(),+ this.step_bone_mapping.get_target_skeleton_data()+ )+ this.step_export_retargeted_animations.export('retargeted_animations')})}diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.tsindex edfe04f..ac2a698 100644--- a/src/retarget/steps/StepExportRetargetedAnimations.ts+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts@@ -1,9 +1,27 @@-import { Object3D, Scene, SkinnedMesh, type AnimationClip } from "three"-import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter"+import { type Object3D, 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'export class StepExportRetargetedAnimations extends EventTarget {public animation_clips_to_export: AnimationClip[] = []+ // Retargeting-related properties+ private bone_mapping = new Map<string, string>()+ private target_mapping_type: TargetBoneMappingType = TargetBoneMappingType.None+ private source_armature: Group | null = null+ private target_skeleton_data: Scene | null = null+ private target_skinned_meshes: SkinnedMesh[] = []++ public setup_retargeting (meshes: SkinnedMesh[], bone_mapping: Map<string, string>,+ mapping_type: TargetBoneMappingType, armature: Group, skeleton_data: Scene | null): void {+ this.bone_mapping = bone_mapping+ this.target_mapping_type = mapping_type+ this.source_armature = armature+ this.target_skeleton_data = skeleton_data+ this.target_skinned_meshes = meshes+ }+public set_animation_clips_to_export (all_animations_clips: AnimationClip[], animation_checkboxes: number[]): void {this.animation_clips_to_export = []animation_checkboxes.forEach((indx) => {@@ -13,37 +31,46 @@ export class StepExportRetargetedAnimations extends EventTarget {})}- public export (skinned_meshes: SkinnedMesh[], filename = 'exported_model'): void {+ public export (filename = 'exported_model'): void {if (this.animation_clips_to_export.length === 0) {console.log('ERROR: No animation clips added to export')return}- console.log('Starting the logic to do the exporting')- console.log(skinned_meshes)- console.log(this.animation_clips_to_export)- console.log('--------------------')+ // Retarget all animation clips before export+ let retargeted_clips: AnimationClip[] = []+ retargeted_clips = this.animation_clips_to_export.map((clip) =>+ AnimationRetargetService.retarget_animation_clip(+ clip,+ this.bone_mapping,+ this.target_mapping_type,+ this.source_armature,+ this.target_skeleton_data,+ this.target_skinned_meshes // use the meshes being exported as the target+ )+ )+ console.log('Retargeted animation clips:', retargeted_clips)- const export_scene = new Scene()+ const export_scene = new Scene()// When exporting to a file, we need to temporarily move the skinned mesh to a new scene// skinned meshes can only be part of one scene at a time, so we must move it back to// its original parent after exportingconst original_parents = new Map<SkinnedMesh, Object3D | null>()- skinned_meshes.forEach((final_skinned_mesh) => {- // Save the original parent+ this.target_skinned_meshes.forEach((final_skinned_mesh) => {+ // Save the original parentoriginal_parents.set(final_skinned_mesh, final_skinned_mesh.parent)export_scene.add(final_skinned_mesh)})- // TODO: Figure out why the export_scene has no children that contain the skinned mesh- console.log('trying to export the following scene as a GLB ', export_scene)+ console.log('Retarget: trying to export the following scene as a GLB ', this.target_skinned_meshes)+ console.log('Retarget: animations to export', retargeted_clips)- this.export_glb(export_scene, this.animation_clips_to_export, filename)+ this.export_glb(export_scene, retargeted_clips, filename).then(() => {// Move the skinned meshes back to their original parents- skinned_meshes.forEach((final_skinned_mesh) => {+ this.target_skinned_meshes.forEach((final_skinned_mesh) => {const original_par = original_parents.get(final_skinned_mesh)if (original_par != null) {original_par.add(final_skinned_mesh)