Import a 3D Model and automatically assign and export animations
TypeScript
Removed some unused code to clean things up a bit
Scott Petrovicd5f5155f066c
diff --git a/src/lib/mapping/BVHToSimple.ts b/src/lib/mapping/BVHToSimple.tsdeleted file mode 100644index 1e28fe7..0000000--- a/src/lib/mapping/BVHToSimple.ts+++ /dev/null@@ -1,24 +0,0 @@-export const BVHToSimpleMapping = {- root: 'SpineBase',- lowerback: 'SpineLow',- upperback: 'SpineMid',- thorax: '',- neck: 'Neck',- head: 'Head',- lshoulderjoint: 'L_Shoulder',- lhumerus: 'L_Arm',- lradius: 'L_Forearm',- lhand: 'L_HandBase',- rshoulderjoint: 'R_Shoulder',- rhumerus: 'R_Arm',- rradius: 'R_Forearm',- rhand: 'R_HandBase',- rfemur: 'R_UpLeg',- rtibia: 'R_Leg',- rfoot: 'R_Foot',- rtoes: 'R_Toe',- lfemur: 'L_UpLeg',- ltibia: 'L_Leg',- lfoot: 'L_Foot',- ltoes: 'L_Toe'-}diff --git a/src/lib/mapping/CarnegieMellonUniversityToMixamo.ts b/src/lib/mapping/CarnegieMellonUniversityToMixamo.tsdeleted file mode 100644index f2c212b..0000000--- a/src/lib/mapping/CarnegieMellonUniversityToMixamo.ts+++ /dev/null@@ -1,45 +0,0 @@-export const CarnegieMellonUniversityToMixamoMapping = {- hip: 'mixamorig_Hips',- abdomen: 'mixamorig_Spine1',- chest: 'mixamorig_Spine2',- neck: 'mixamorig_Neck',- head: 'mixamorig_Head',- leftEye: '',- rightEye: '',- rCollar: '',- lCollar: '',- rShldr: 'mixamorig_RightShoulder',- lShldr: 'mixamorig_LeftShoulder',- rForeArm: 'mixamorig_RightForeArm',- lForeArm: 'mixamorig_LeftForeArm',-- rHand: 'mixamorig_RightHand',- lHand: 'mixamorig_LeftHand',- rThumb1: 'mixamorig_RightHandThumb1',- lThumb1: 'mixamorig_LeftHandThumb1',- rThumb2: 'mixamorig_RightHandThumb2',- lThumb2: 'mixamorig_LeftHandThumb2',- rIndex1: 'mixamorig_RightHandIndex1',- lIndex1: 'mixamorig_LeftHandIndex1',- rIndex2: '',- lIndex2: '',- rMid1: 'mixamorig_RightHandMiddle1',- lMid1: 'mixamorig_LeftHandMiddle1',- rMid2: 'mixamorig_RightHandMiddle2',- lMid2: ' mixamorig_LeftHandMiddle2',- rRing1: 'mixamorig_RightHandRing1',- lRing1: 'mixamorig_LeftHandRing1',- rRing2: 'mixamorig_RightHandRing2',- lRing2: 'mixamorig_LeftHandRing2',- rPinky1: 'mixamorig_RightHandPinky1',- lPinky1: 'mixamorig_LeftHandPinky1',-- rButtock: '',- lButtock: '',- rThigh: 'mixamorig_RightUpLeg',- lThigh: 'mixamorig_LeftUpLeg',- rShin: 'mixamorig_RightLeg',- lShin: 'mixamorig_LeftLeg',- rFoot: 'mixamorig_RightFoot',- lFoot: 'mixamorig_LeftFoot'-}diff --git a/src/lib/mapping/MixamoToSimple.ts b/src/lib/mapping/MixamoToSimple.tsdeleted file mode 100644index 65f3637..0000000--- a/src/lib/mapping/MixamoToSimple.ts+++ /dev/null@@ -1,54 +0,0 @@-export const MixamoToSimpleMapping = {- mixamorigHips: 'SpineBase',- mixamorigSpine: 'SpineLow',- mixamorigSpine1: 'SpineLow',- mixamorigSpine2: 'SpineMid',- mixamorigNeck: 'Neck',- mixamorigHead: 'Head',- mixamorigRightShoulder: 'R_Shoulder',- mixamorigRightArm: 'R_Arm',- mixamorigRightForeArm: 'R_Forearm',- mixamorigRightHand: 'R_HandBase',- mixamorigRightHandThumb1: 'R_ThumbBase',- mixamorigRightHandThumb2: 'R_ThumbMid',- mixamorigRightHandThumb3: 'R_ThumbTip',- mixamorigRightHandIndex1: 'R_PointerBase',- mixamorigRightHandIndex2: 'R_PointerMid',- mixamorigRightHandIndex3: 'R_PointerTip',- mixamorigRightHandMiddle1: 'R_FingersBase',- mixamorigRightHandMiddle2: 'R_FingersMid',- mixamorigRightHandMiddle3: 'R_FingersTip',- mixamorigRightHandRing1: '',- mixamorigRightHandRing2: '',- mixamorigRightHandRing3: '',- mixamorigRightHandPinky1: '',- mixamorigRightHandPinky2: '',- mixamorigRightHandPinky3: '',- mixamorigLeftShoulder: 'L_Shoulder',- mixamorigLeftArm: 'L_Arm',- mixamorigLeftForeArm: 'L_Forearm',- mixamorigLeftHand: 'L_HandBase',- mixamorigLeftHandThumb1: 'L_ThumbBase',- mixamorigLeftHandThumb2: 'L_ThumbMid',- mixamorigLeftHandThumb3: 'L_ThumbTip',- mixamorigLeftHandIndex1: 'L_PointerBase',- mixamorigLeftHandIndex2: 'L_PointerMid',- mixamorigLeftHandIndex3: 'L_PointerTip',- mixamorigLeftHandMiddle1: 'L_FingersBase',- mixamorigLeftHandMiddle2: 'L_FingersMid',- mixamorigLeftHandMiddle3: 'L_FingersTip',- mixamorigLeftHandRing1: '',- mixamorigLeftHandRing2: '',- mixamorigLeftHandRing3: '',- mixamorigLeftHandPinky1: '',- mixamorigLeftHandPinky2: '',- mixamorigLeftHandPinky3: '',- mixamorigRightUpLeg: 'R_UpLeg',- mixamorigRightLeg: 'R_Leg',- mixamorigRightFoot: 'R_Foot',- mixamorigRightToeBase: 'R_Toe',- mixamorigLeftUpLeg: 'L_UpLeg',- mixamorigLeftLeg: 'L_Leg',- mixamorigLeftFoot: 'L_Foot',- mixamorigLeftToeBase: 'L_Toe'-}diff --git a/src/lib/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.tsindex 55963a1..9d8e8c1 100644--- a/src/lib/processes/StepAnimationsListing.ts+++ b/src/lib/processes/StepAnimationsListing.ts@@ -8,9 +8,6 @@ import { AnimationClip, AnimationMixer, Quaternion, Vector3, type SkinnedMesh, ttype KeyframeTrack, type AnimationAction, type Object3D, Bone } from 'three'import { 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 {@@ -23,7 +20,6 @@ export class StepAnimationsListing extends EventTarget {private skinned_meshes_to_animate: SkinnedMesh[] = []private current_playing_index: number = 0private skeleton_type: string = SkeletonType.Human- private skeleton_type_trying_to_import: string = 'mixamo-skeleton' // manually importing animation file// -z will bring hip bone downprivate hip_bone_offset: Vector3 = new Vector3(0, 0, 0) // -z will bring hip bone down. Helps set new base hip position@@ -41,8 +37,7 @@ export class StepAnimationsListing extends EventTarget {// this should take the distance between the original and edited armature// and divide it by the original armature hip bone positionthis.hip_bone_scale_factor_z = edited_hip_bone.position.z / original_hip_bone.position.z- //console.log('hip bone scale factor:', this.hip_bone_scale_factor) // should be 1 if no change-+// small T-pose offset that somehow is getting lostthis.hip_bone_offset = this.hip_bone_offset.sub(new Vector3(0, 0, -0.04))}@@ -219,8 +214,6 @@ export class StepAnimationsListing extends EventTarget {let rotation_tracks: KeyframeTrack[] = []- //const name: String = "".toLowerCase()-if (preserve_root_position) {rotation_tracks = animation_clip.tracks.filter((x: KeyframeTrack) => x.name.includes('quaternion') || x.name.toLowerCase().includes('hips.position'))} else {@@ -228,7 +221,7 @@ export class StepAnimationsListing extends EventTarget {}animation_clip.tracks = rotation_tracks // update track data- //console.log(animation_clip.tracks)+ // console.log(animation_clip.tracks) // UNUSED DEBUG CODE})}@@ -375,14 +368,12 @@ export class StepAnimationsListing extends EventTarget {// GLB and FBX will come in as a data:application/octet-stream;base64// GLTF will come in as a JSON string// even the binary data comes in as string, so treat it all that way for now- const file_info: string = reader.result+ const file_info: string | ArrayBuffer | null = reader.resultif (file_extension === 'fbx') {this.load_fbx_animation_clips(file_info, file_name)} else if (file_extension === 'glb' || file_extension === 'gltf') {this.load_gltf_animation_clips(file_info)- } else if (file_extension === 'bvh') {- this.load_bvh_animation_clip(file_info)}// clear out the file input field in case we want to test by loading same file again@@ -405,8 +396,6 @@ 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')- this.skeleton_type_trying_to_import = animation_typethis.ui.dom_import_animation_upload.click() // initiate file upload})}@@ -422,21 +411,13 @@ 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' || this.skeleton_type_trying_to_import === 'mixamo'- const is_carnegie_mellon_skeleton = this.skeleton_type_trying_to_import === 'carnegie-skeleton'+ const is_mixamo_animation: boolean = animations_for_scene[0].name === 'mixamo.com' // || this.skeleton_type_trying_to_import === 'mixamo' // UNUSEDif (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)@@ -444,125 +425,15 @@ 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()})`- })- }-- /* this will mutate the animation clips passed in by effectively removing the position keyframes */- 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()-- bvh_loader.load(bvh_file, (result) => {- let clip: AnimationClip = result.clip-- clip = this.process_bvh_animation_clip(clip, 'bvh')-- // add the animations to the animation_clips_loaded- // update the UI with the new animation clips- this.append_animation_clips([clip])- this.ui.build_animation_clip_ui(this.animation_clips_loaded)- })- }-- private process_bvh_animation_clip (animation_clip: AnimationClip, file_name: string): AnimationClip {- // need to remove the position keyframes from the animation- // since we will be offsetting the root bone position- const cloned_track: AnimationClip = this.deep_clone_animation_clips([animation_clip])[0]-- // only keep the rotation tracks...and the root bone position track- const rotation_tracks = cloned_track.tracks.filter((x: KeyframeTrack) => x.name.includes('quaternion'))- cloned_track.tracks = rotation_tracks-- // keep the root bone position track- const root_position_track = animation_clip.tracks.filter((x: KeyframeTrack) => x.name === 'root.position')[0]-- if (root_position_track !== undefined) {- cloned_track.tracks.push(root_position_track)- }-- // need to do bone mapping- // loop through each animation clip to update the tracks- cloned_track.tracks.forEach((track) => {- if (track === undefined) {- throw new Error('Error processing BVH animation clip. Track input is undefined')- }-- const bvh_bone_name: string = track.name.split('.')[0]- const keyframe_type: string = track.name.split('.')[1]-- // selected skeleton is a simplified mixamo skeleton. We need to map bone names- if (this.skeleton_type === SkeletonType.BipedalSimple) {- const bone_from_mapping: boolean = BVHToSimpleMapping[bvh_bone_name]- if (bone_from_mapping) {- track.name = bone_from_mapping + '.' + keyframe_type- }- }-- // BVH seems to have something like 1 unit = 1cm. We need to scale position data to compensate for that- // the 200 value is arbitrary, but the results seem to look good, so I went with that. FBX seems to be the same- if (keyframe_type === 'position') {- (track.values as Float32Array).forEach((value, index) => {- track.values[index] = value / 200- })- }- })-- return cloned_track- }-private process_mixamo_animation_clips (animation_clips: AnimationClip[], file_name: string): void {// loop through each animation clip to update the tracksanimation_clips.forEach((animation_clip, index) => {animation_clip.name = `${file_name} (${index.toString()})` // mixamo just calles the clip names 'mixamo.com'// get the track name and replace it with our simplfied mapping- animation_clip.tracks.forEach((track) => {- const mixamo_bone_name: string = track.name.split('.')[0]+ animation_clip.tracks.forEach((track: any) => {const keyframe_type: string = track.name.split('.')[1]- // selected skeleton is a simplified mixamo skeleton. We need to map bone names- if (this.skeleton_type === SkeletonType.BipedalSimple) {- const is_mappping_found: boolean = MixamoToSimpleMapping[mixamo_bone_name]- if (is_mappping_found) {- track.name = MixamoToSimpleMapping[mixamo_bone_name] + '.' + keyframe_type- }- }-- // my mixamo skeleton has an underscore character. Maybe need to replace- // this later so the skeleton doesn't have to do this replacing when importing- if (this.skeleton_type === SkeletonType.BipedalFull) {- track.name = mixamo_bone_name.replace('mixamorig', 'mixamorig_') + '.' + keyframe_type- }-// Mixamo has 1 unit = 1cm. We need to scale position data to compensate for that// the 200 value is arbitrary, but the results seem to look good, so I went with that.if (keyframe_type === 'position') {@@ -571,12 +442,6 @@ export class StepAnimationsListing extends EventTarget {})}})-- // with the simple skeleton, there are many bones that are not used from the full mixamo animation- // remove unmapped bones since they won't be used- if (this.skeleton_type === SkeletonType.BipedalSimple) {- animation_clip.tracks = animation_clip.tracks.filter((x: KeyframeTrack) => !x.name.includes('mixamorig'))- }})}