Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

try to add props to use your rigged model

Scott Petrovicb151ba26ecba
diff --git a/src/lib/processes/animations-listing/props/HandBoneResolver.ts b/src/lib/processes/animations-listing/props/HandBoneResolver.ts
index d30cfba..91a8f5b 100644
--- a/src/lib/processes/animations-listing/props/HandBoneResolver.ts
+++ b/src/lib/processes/animations-listing/props/HandBoneResolver.ts
@@ -12,7 +12,11 @@ export class HandBoneResolver {
]
public static resolve (skeleton: Skeleton, side: PropSide): Bone | null {
- const bones = skeleton.bones.filter((bone) => bone !== undefined && bone !== null)
+ return this.resolve_from_bones(skeleton.bones, side)
+ }
+
+ public static resolve_from_bones (all_bones: Bone[], side: PropSide): Bone | null {
+ const bones = all_bones.filter((bone) => bone !== undefined && bone !== null)
for (const exact_name of this.exact_names[side]) {
const exact_match = bones.find((bone) => bone.name === exact_name)
diff --git a/src/lib/processes/animations-listing/props/PropCatalog.ts b/src/lib/processes/animations-listing/props/PropCatalog.ts
index f8f9c1b..8308869 100644
--- a/src/lib/processes/animations-listing/props/PropCatalog.ts
+++ b/src/lib/processes/animations-listing/props/PropCatalog.ts
@@ -129,8 +129,19 @@ export class PropCatalog {
private static readonly loader: GLTFLoader = new GLTFLoader()
private static readonly model_cache = new Map<string, Promise<Group>>()
+ // prefix that points from the current page to the static root, e.g. '../' for pages in a sub-folder
+ private static static_root_path: string = ''
+
private static readonly definitions: PropDefinition[] = create_definitions()
+ public static set_static_root_path (path: string): void {
+ this.static_root_path = path
+ }
+
+ public static url_for (static_relative_path: string): string {
+ return `${this.static_root_path}${static_relative_path}`
+ }
+
public static all (): PropDefinition[] {
return this.definitions
}
@@ -167,7 +178,7 @@ export class PropCatalog {
private static async load_model (asset_path: string): Promise<Group> {
let model_promise = this.model_cache.get(asset_path)
if (model_promise === undefined) {
- model_promise = this.loader.loadAsync(asset_path)
+ model_promise = this.loader.loadAsync(this.url_for(asset_path))
.then((gltf) => gltf.scene)
.catch((error: unknown) => {
this.model_cache.delete(asset_path)
diff --git a/src/lib/processes/animations-listing/props/PropPicker.ts b/src/lib/processes/animations-listing/props/PropPicker.ts
index 26700fb..45a201c 100644
--- a/src/lib/processes/animations-listing/props/PropPicker.ts
+++ b/src/lib/processes/animations-listing/props/PropPicker.ts
@@ -210,7 +210,7 @@ export class PropPicker extends EventTarget {
return '<span class="prop-picker-empty" aria-hidden="true"></span>'
}
- return `<img src="${definition.preview_path}" alt="" loading="lazy">`
+ return `<img src="${PropCatalog.url_for(definition.preview_path)}" alt="" loading="lazy">`
}
private option_html (value: PropId, name: string, definition?: PropDefinition): string {
diff --git a/src/lib/processes/animations-listing/props/PropsManager.ts b/src/lib/processes/animations-listing/props/PropsManager.ts
index b70fc0f..9403c0b 100644
--- a/src/lib/processes/animations-listing/props/PropsManager.ts
+++ b/src/lib/processes/animations-listing/props/PropsManager.ts
@@ -8,6 +8,10 @@ import { PropsPanel } from './PropsPanel.ts'
import { HandBoneResolver } from './HandBoneResolver.ts'
import { PROP_USER_DATA_KEY } from './PropsExportFilter.ts'
+export type HandBoneLookup = (skeleton: Skeleton, side: PropSide) => Bone | null
+
+const default_hand_bone_lookup: HandBoneLookup = (skeleton, side) => HandBoneResolver.resolve(skeleton, side)
+
interface HandGripFrame {
local_down: Vector3
local_forward: Vector3
@@ -39,6 +43,7 @@ export class PropsManager extends EventTarget {
private readonly grip_frames = new Map<Bone, HandGripFrame>()
private added_event_listeners: boolean = false
private attachment_generation: number = 0
+ private resolve_hand_bone: HandBoneLookup = default_hand_bone_lookup
public begin (skeleton_type: SkeletonType, skeleton_scale: number): void {
this.panel.initialize()
@@ -64,8 +69,9 @@ export class PropsManager extends EventTarget {
}
// meshes must still be in their rest pose here, since the grip frames are captured from it
- public attach_to_skinned_meshes (skinned_meshes: SkinnedMesh[]): void {
+ public attach_to_skinned_meshes (skinned_meshes: SkinnedMesh[], resolve_hand_bone: HandBoneLookup = default_hand_bone_lookup): void {
this.target_skinned_meshes = skinned_meshes
+ this.resolve_hand_bone = resolve_hand_bone
this.capture_grip_frames()
this.refresh_attached_props()
}
@@ -81,12 +87,17 @@ export class PropsManager extends EventTarget {
this.target_skinned_meshes = []
}
+ public end (): void {
+ this.detach_all()
+ this.panel.set_visible(false)
+ }
+
private capture_grip_frames (): void {
this.grip_frames.clear()
this.unique_skeletons().forEach((skeleton) => {
Object.values(PropSide).forEach((side) => {
- const hand_bone = HandBoneResolver.resolve(skeleton, side)
+ const hand_bone = this.resolve_hand_bone(skeleton, side)
if (hand_bone !== null) {
this.grip_frames.set(hand_bone, this.compute_grip_frame(hand_bone))
}
@@ -119,7 +130,7 @@ export class PropsManager extends EventTarget {
let found_hand_bone = false
skeletons.forEach((skeleton) => {
- const hand_bone = HandBoneResolver.resolve(skeleton, side)
+ const hand_bone = this.resolve_hand_bone(skeleton, side)
if (hand_bone === null) {
return
}
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.ts
index eb95dff..2256dcc 100644
--- a/src/retarget/RetargetAnimationListing.ts
+++ b/src/retarget/RetargetAnimationListing.ts
@@ -6,7 +6,7 @@ import { type AnimationLibraryLoadProgress } from '../lib/processes/animations-l
import { AnimationLibraryProgressBar } from '../lib/processes/animations-listing/AnimationLibraryProgressBar.ts'
import { AnimationUtility } from '../lib/processes/animations-listing/AnimationUtility.ts'
import { ArmExtensionControl } from '../lib/processes/animations-listing/ArmExtensionControl.ts'
-import { type AnimationClip, AnimationMixer, type SkinnedMesh, Object3D, type AnimationAction } from 'three'
+import { type AnimationClip, AnimationMixer, type SkinnedMesh, Object3D, type AnimationAction, Bone, Box3, type Skeleton, Vector3 } from 'three'
import type { ThemeManager } from '../lib/ThemeManager.ts'
import { type TransformedAnimationClipPair } from '../lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts'
import { type AnimationClipExportSelection } from '../lib/processes/animations-listing/interfaces/AnimationExportSelection.ts'
@@ -16,6 +16,10 @@ import { UI } from '../lib/UI.ts'
import { SkeletonType } from '../lib/enums/SkeletonType.ts'
import { DownloadSettings } from '../lib/processes/export-to-file/DownloadSettings.ts'
import { ModalDialog } from '../lib/ModalDialog.ts'
+import { type PropsManager } from '../lib/processes/animations-listing/props/PropsManager.ts'
+import { HandBoneResolver } from '../lib/processes/animations-listing/props/HandBoneResolver.ts'
+import { type PropSide } from '../lib/processes/animations-listing/props/PropSide.ts'
+import { RetargetUtils } from './RetargetUtils.ts'
/**
* RetargetAnimationListing - Handles animation listing and playback specifically for retargeting workflow
@@ -27,6 +31,7 @@ export class RetargetAnimationListing extends EventTarget {
private readonly animation_loader: AnimationLoader = new AnimationLoader()
private readonly step_export_retargeted_animations: StepExportRetargetedAnimations = new StepExportRetargetedAnimations()
private readonly download_settings: DownloadSettings
+ private readonly props_manager: PropsManager
private readonly animation_library: AnimationLibrary = new AnimationLibrary(this.animation_loader)
private animation_mixer: AnimationMixer = new AnimationMixer(new Object3D())
private readonly ui: UI = UI.getInstance()
@@ -46,10 +51,12 @@ export class RetargetAnimationListing extends EventTarget {
private export_button: HTMLButtonElement | null = null
- constructor (theme_manager: ThemeManager, download_settings: DownloadSettings) {
+ // the props manager is shared across listing instances since its panel binds to static markup
+ constructor (theme_manager: ThemeManager, download_settings: DownloadSettings, props_manager: PropsManager) {
super()
this.theme_manager = theme_manager
this.download_settings = download_settings
+ this.props_manager = props_manager
this.animation_player = new AnimationPlayer()
// The warp is applied to the source-space display clip, so both the preview
@@ -78,13 +85,83 @@ export class RetargetAnimationListing extends EventTarget {
)
this.show_bone_toggle_button(true)
+ this.attach_props()
}
public end (): void {
+ this.props_manager.end()
this.show_bone_toggle_button(false) // cannot toggle bone display in configuration area
this.stop_preview() // stop any playing animations in the listing
}
+ private attach_props (): void {
+ const retarget_service = AnimationRetargetService.getInstance()
+
+ // the live preview leaves the target mid-animation, and grip frames are captured from the current pose
+ RetargetUtils.reset_skinned_mesh_to_rest_pose(retarget_service.get_target_armature())
+
+ this.props_manager.begin(retarget_service.get_skeleton_type(), this.target_to_source_height_ratio())
+ this.props_manager.attach_to_skinned_meshes(
+ retarget_service.get_target_skinned_meshes(),
+ (skeleton, side) => this.resolve_target_hand_bone(skeleton, side)
+ )
+ }
+
+ // custom rigs may not use a recognizable hand bone name, so prefer whatever the user mapped to the Mesh2Motion hand
+ private resolve_target_hand_bone (skeleton: Skeleton, side: PropSide): Bone | null {
+ const retarget_service = AnimationRetargetService.getInstance()
+ const source_hand_bone = HandBoneResolver.resolve_from_bones(
+ this.collect_bones(retarget_service.get_source_armature()),
+ side
+ )
+
+ if (source_hand_bone !== null) {
+ for (const [target_bone_name, source_bone_name] of retarget_service.get_bone_mappings()) {
+ const mapped_bone = source_bone_name === source_hand_bone.name
+ ? skeleton.bones.find((bone) => bone.name === target_bone_name)
+ : undefined
+ if (mapped_bone !== undefined) {
+ return mapped_bone
+ }
+ }
+ }
+
+ return HandBoneResolver.resolve(skeleton, side)
+ }
+
+ // props are sized for the Mesh2Motion rig, so scale them by how much taller the target rig is
+ private target_to_source_height_ratio (): number {
+ const retarget_service = AnimationRetargetService.getInstance()
+ const source_height = this.bone_height(retarget_service.get_source_armature())
+ const target_height = this.bone_height(retarget_service.get_target_armature())
+
+ if (source_height <= 0 || target_height <= 0) {
+ return 1.0
+ }
+
+ return target_height / source_height
+ }
+
+ private bone_height (root: Object3D): number {
+ root.updateMatrixWorld(true)
+ const bounds = new Box3()
+ this.collect_bones(root).forEach((bone) => {
+ bounds.expandByPoint(bone.getWorldPosition(new Vector3()))
+ })
+
+ return bounds.isEmpty() ? 0 : bounds.max.y - bounds.min.y
+ }
+
+ private collect_bones (root: Object3D): Bone[] {
+ const bones: Bone[] = []
+ root.traverse((child) => {
+ if (child instanceof Bone) {
+ bones.push(child)
+ }
+ })
+ return bones
+ }
+
private show_bone_toggle_button (show: boolean): void {
if (this.ui.dom_show_skeleton_container != null) {
this.ui.dom_show_skeleton_container.style.display = show ? 'inline-flex' : 'none'
diff --git a/src/retarget/index.html b/src/retarget/index.html
index 31fdcc0..71bd8c5 100644
--- a/src/retarget/index.html
+++ b/src/retarget/index.html
@@ -12,6 +12,7 @@
<link rel="stylesheet" href="../animation-player.css" crossorigin="anonymous" />
<link rel="stylesheet" href="../settings-dropdown.css" crossorigin="anonymous" />
<link rel="stylesheet" href="./retarget.css" crossorigin="anonymous" />
+ <link rel="stylesheet" href="../lib/processes/animations-listing/props/props.css" crossorigin="anonymous" />
<link rel="stylesheet" href="../dialog.css" crossorigin="anonymous" />
<link rel="stylesheet" href="../offline.css" crossorigin="anonymous" />
@@ -193,6 +194,22 @@
</div>
+ <!-- floating props toggle and panel docked to the left of the tool panel -->
+ <div id="props-dock" style="display: none">
+ <button class="secondary-button" id="props-toggle-button" aria-expanded="false" aria-controls="props-panel" data-tippy-content="Attach props to the hands" aria-label="Attach props to the hands">
+ <img src="../images/icons/props.svg" alt="Props" width="32" height="32" />
+ Props
+ </button>
+ <div id="props-panel" class="props-panel" role="region" aria-label="Props" hidden>
+ <div id="props-left-hand" class="prop-picker"></div>
+ <div id="props-right-hand" class="prop-picker"></div>
+ <button class="secondary-button" id="props-close-button" data-tippy-content="Close props" aria-label="Close props">
+ <img src="../images/icons/props.svg" alt="" width="32" height="32" />
+ Props
+ </button>
+ </div>
+ </div>
+
<div id="animation-player-mount"></div>
diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.ts
index 6dab65b..b5882c0 100644
--- a/src/retarget/retarget.ts
+++ b/src/retarget/retarget.ts
@@ -8,6 +8,8 @@ import { RetargetAnimationListing } from './RetargetAnimationListing.ts'
import { AnimationRetargetService } from './AnimationRetargetService'
import { type SkeletonType } from '../lib/enums/SkeletonType.ts'
import { RetargetUtils } from './RetargetUtils.ts'
+import { PropsManager } from '../lib/processes/animations-listing/props/PropsManager.ts'
+import { PropCatalog } from '../lib/processes/animations-listing/props/PropCatalog.ts'
class RetargetModule {
private readonly mesh2motion_engine: Mesh2MotionEngine
@@ -16,6 +18,7 @@ class RetargetModule {
private readonly step_bone_mapping: StepBoneMapping
private readonly retarget_animation_preview: RetargetAnimationPreview
private animation_listing_step: RetargetAnimationListing | null = null
+ private readonly props_manager: PropsManager = new PropsManager()
private back_to_bone_map_button: HTMLButtonElement | null = null
private continue_to_listing_button: HTMLButtonElement | null = null
@@ -46,6 +49,7 @@ class RetargetModule {
}
public init (): void {
+ PropCatalog.set_static_root_path('../')
this.add_event_listeners()
this.step_load_source_skeleton.begin()
this.step_load_target_model.begin()
@@ -143,7 +147,8 @@ class RetargetModule {
// load the animation listing step and start it
this.animation_listing_step = new RetargetAnimationListing(
this.mesh2motion_engine.get_theme_manager(),
- this.mesh2motion_engine.download_settings
+ this.mesh2motion_engine.download_settings,
+ this.props_manager
)
this.animation_listing_step.begin()
this.mesh2motion_engine.download_settings.update_download_settings_ui_visibility(
diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.ts
index 5c99774..094a623 100644
--- a/src/retarget/steps/StepExportRetargetedAnimations.ts
+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts
@@ -10,6 +10,7 @@ import { ExportHierarchyService } from '../../lib/processes/export-to-file/Expor
import { ExportAnimationCleanupService } from '../../lib/processes/export-to-file/ExportAnimationCleanupService'
import { GlbSkinCleanupService } from '../../lib/processes/export-to-file/GlbSkinCleanupService'
import { FbxTextureCompatibilityService } from '../../lib/processes/export-to-file/FbxTextureCompatibilityService'
+import { PropsExportFilter } from '../../lib/processes/animations-listing/props/PropsExportFilter'
export class StepExportRetargetedAnimations extends EventTarget {
public animation_clips_to_export: AnimationClip[] = []
@@ -70,6 +71,7 @@ export class StepExportRetargetedAnimations extends EventTarget {
const skeleton_only = download_settings.export_contents() === ExportContents.Skeleton
const objects_to_export: Object3D[] = ExportHierarchyService.collect_objects_to_export(skinned_meshes, skeleton_only)
+ const restore_props = skeleton_only ? PropsExportFilter.detach_props(objects_to_export) : () => {}
// The base animation data can contain tracks for joints the target skeleton does not
// have, and keyframe times can contain float32 duplicates that glTF validators reject.
@@ -101,6 +103,7 @@ export class StepExportRetargetedAnimations extends EventTarget {
}
})
+ restore_props()
restore_bone_names()
}
}