Import a 3D Model and automatically assign and export animations
TypeScript
Get the Carnegie Mellon bone mappings loading and playing when importing with bone mapping
Scott Petrovic48c14d94fb4a
diff --git a/src/index.html b/src/index.htmlindex 518050e..eec4f39 100644--- a/src/index.html+++ b/src/index.html@@ -180,11 +180,13 @@</span></button><div id="animation-import-options" class="dropdown-content">- <a href="#" data-value="mixamo-animation">Mixamo Animation</a>- <a href="#" data-value="carnegie-animation">Carnegie Mellon Animation</a>+ <a href="#" data-value="mixamo-skeleton">Mixamo Animation</a>+ <a href="#" data-value="carnegie-skeleton">Carnegie Mellon Animation</a></div></div>+ <input id="import-animations-upload" type="file" name="animation-upload" accept=".glb, .gltf, .fbx, .bvh" />+<!-- <div class="dropdown button"><span class="button-icon-group">diff --git a/src/lib/UI.ts b/src/lib/UI.tsindex 1162222..3c58939 100644--- a/src/lib/UI.ts+++ b/src/lib/UI.ts@@ -36,6 +36,7 @@ export class UI {dom_build_version: Element | null = nulldom_animation_import_options: Element | null = nulldom_import_animations_buton: Element | null = null+ dom_import_animation_upload: Element | null = nullconstructor () {this.initialize_dom_elements()@@ -81,13 +82,14 @@ export class UI {this.dom_animation_clip_list = document.querySelector('#animations-items')this.dom_export_button = document.querySelector('#export-button')this.dom_info_panel = document.querySelector('#info-messaging')- this.dom_import_animations_button = document.querySelector('#import-animations-button')this.dom_extend_arm_input = document.querySelector('#extend-arm-input')this.dom_extend_arm_button = document.querySelector('#extend-arm-button')this.dom_build_version = document.querySelector('#build-version')+ this.dom_import_animation_upload = document.querySelector('#import-animations-upload')+this.dom_import_animations_buton = document.querySelector('#import-animations-button')this.dom_animation_import_options = document.querySelector('#animation-import-options')diff --git a/src/lib/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.tsindex ca7ddd5..4b9c786 100644--- a/src/lib/processes/StepAnimationsListing.ts+++ b/src/lib/processes/StepAnimationsListing.ts@@ -9,6 +9,7 @@ import { AnimationClip, AnimationMixer, Quaternion, Vector3, type SkinnedMesh, timport { SkeletonType } from '../enums/SkeletonType.ts'import { MixamoToSimpleMapping } from '../mapping/MixamoToSimple.js'import { BVHToSimpleMapping } from '../mapping/BVHToSimple.ts'+import { CarnegieMellonUniversityToMixamoMapping } from '../mapping/CarnegieMellonUniversityToMixamo.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepAnimationsListing extends EventTarget {@@ -22,6 +23,8 @@ export class StepAnimationsListing extends EventTarget {private current_playing_index: number = 0private skeleton_type: string = SkeletonType.BipedalSimple+ private skeleton_type_trying_to_import: string = 'mixamo-skeleton' // manually importing animation file+private has_added_event_listeners: boolean = falseconstructor () {@@ -184,7 +187,7 @@ export class StepAnimationsListing extends EventTarget {})}- this.ui.dom_import_animations_button?.addEventListener('change', (event) => {+ this.ui.dom_import_animation_upload?.addEventListener('change', (event) => {const file = event.target?.files[0]const reader = new FileReader()reader.readAsDataURL(file)@@ -208,7 +211,7 @@ export class StepAnimationsListing extends EventTarget {}// clear out the file input field in case we want to test by loading same file again- this.ui.dom_import_animations_button.value = ''+ this.ui.dom_import_animation_upload.value = ''}})@@ -227,7 +230,8 @@ export class StepAnimationsListing extends EventTarget {// handle clicking an animation type to importthis.ui.dom_animation_import_options.addEventListener('click', (event) => {const animation_type = event.target.getAttribute('data-value')- console.log('clicked on an import animation option', animation_type)+ this.skeleton_type_trying_to_import = animation_type+ this.ui.dom_import_animation_upload.click() // initiate file upload})}@@ -242,13 +246,21 @@ export class StepAnimationsListing extends EventTarget {const animations_for_scene: AnimationClip[] = fbx.animations // we only need the animations// check to see if the animation is a mixamo skeleton. we will need this later for potential mapping- const is_mixamo_animation: boolean = animations_for_scene[0].name === 'mixamo.com'+ const is_mixamo_animation: boolean = animations_for_scene[0].name === 'mixamo.com' || this.skeleton_type_trying_to_import === 'mixamo'+ const is_carnegie_mellon_skeleton = this.skeleton_type_trying_to_import === 'carnegie-skeleton'if (is_mixamo_animation) {// mutates animations_for_scene contentsthis.process_mixamo_animation_clips(animations_for_scene, file_name)}+ if (is_carnegie_mellon_skeleton) {+ // all these functions mutate animation data+ this.process_carnegie_skeleton_clip(animations_for_scene, file_name)+ this.reduce_position_keyframes(animations_for_scene)+ console.log('animation clips after processing 2:', animations_for_scene)+ }+// add the animations to the animation_clips_loaded// update the UI with the new animation clipsthis.append_animation_clips(animations_for_scene)@@ -256,6 +268,39 @@ export class StepAnimationsListing extends EventTarget {})}+ private process_carnegie_skeleton_clip (animation_clips: AnimationClip[], file_name: string): void {+ // Do bone mappings for the carnegie mellon skeleton+ animation_clips.forEach((animation_clip, index) => {+ animation_clip.tracks.forEach((track) => {+ const original_bone_name: string = track.name.split('.')[0]+ const keyframe_type: string = track.name.split('.')[1]++ const bone_mapping_result: boolean = CarnegieMellonUniversityToMixamoMapping[original_bone_name]+ if (bone_mapping_result) {+ track.name = bone_mapping_result + '.' + keyframe_type+ }+ })+ })++ // use file name for animation clip name+ animation_clips.forEach((animation_clip, index) => {+ animation_clip.name = `${file_name} (${index.toString()})`+ })+ }++ private reduce_position_keyframes (animation_clips: AnimationClip[]): void {+ animation_clips.forEach((animation_clip, index) => {+ animation_clip.tracks.forEach((track) => {+ const keyframe_type: string = track.name.split('.')[1]+ if (keyframe_type === 'position') {+ (track.values as Float32Array).forEach((value, index) => {+ track.values[index] = value / 200+ })+ }+ })+ })+ }+private load_bvh_animation_clip (bvh_file: string): void {const bvh_loader = new BVHLoader()