Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Start adding a BVH loader for animations

Scott Petrovic794d7be75961
diff --git a/src/index.html b/src/index.html
index 1926fcf..a9e005a 100644
--- a/src/index.html
+++ b/src/index.html
@@ -176,7 +176,7 @@
<span>Import</span>
</span>
</label>
- <input id="import-animations-button" type="file" name="animation-upload" accept=".glb, .gltf, .fbx" />
+ <input id="import-animations-button" type="file" name="animation-upload" accept=".glb, .gltf, .fbx, .bvh" />
<button id="export-button">
<span class="button-icon-group">
diff --git a/src/lib/mapping/BVHToSimple.ts b/src/lib/mapping/BVHToSimple.ts
new file mode 100644
index 0000000..1e28fe7
--- /dev/null
+++ b/src/lib/mapping/BVHToSimple.ts
@@ -0,0 +1,24 @@
+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/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.ts
index b1a04f4..5c90211 100644
--- a/src/lib/processes/StepAnimationsListing.ts
+++ b/src/lib/processes/StepAnimationsListing.ts
@@ -2,11 +2,13 @@ import { UI } from '../UI.ts'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js'
+import { BVHLoader } from 'three/examples/jsm/loaders/BVHLoader.js'
import { AnimationClip, AnimationMixer, Quaternion, Vector3, type SkinnedMesh, type QuaternionKeyframeTrack, type KeyframeTrack, type AnimationAction } from 'three'
import { SkeletonType } from '../enums/SkeletonType.ts'
import { MixamoToSimpleMapping } from '../mapping/MixamoToSimple.js'
+import { BVHToSimpleMapping } from '../mapping/BVHToSimple.ts'
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepAnimationsListing extends EventTarget {
@@ -201,6 +203,8 @@ export class StepAnimationsListing extends EventTarget {
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
@@ -239,6 +243,60 @@ export class StepAnimationsListing extends EventTarget {
})
}
+ 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 => x.name.includes('quaternion'))
+ cloned_track.tracks = rotation_tracks
+
+ // keep the root bone position track
+ const root_position_track = animation_clip.tracks.filter(x => x.name === 'root.position')[0]
+ 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) => {
+ 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 tracks
animation_clips.forEach((animation_clip, index) => {
@@ -313,6 +371,7 @@ export class StepAnimationsListing extends EventTarget {
// Don't use this for now. Maybe delete this function later
const animation_clips = this.deep_clone_animation_clips(animation_clip_list)
+
// const filtered_tracks = animation_clips.map((animation_clip) => {
// const filteredTracks = animation_clip.tracks.filter((track) => {
// return track.name.indexOf('quaternion') > 0
diff --git a/static/blender/character.blend1 b/static/blender/character.blend1
new file mode 100644
index 0000000..849c0f9
Binary files /dev/null and b/static/blender/character.blend1 differ