Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

turn arm extension control into a shared class and add to retargeting page

Scott Petrovic7a7c5c79394c
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index 4c15036..c14c257 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -16,6 +16,7 @@ import { StepLoadSkeleton } from './lib/processes/load-skeleton/StepLoadSkeleton
import { StepEditSkeleton } from './lib/processes/edit-skeleton/StepEditSkeleton.ts'
import { MeshDragBonePlacement } from './lib/processes/edit-skeleton/MeshDragBonePlacement.ts'
import { StepAnimationsListing } from './lib/processes/animations-listing/StepAnimationsListing.ts'
+import { ArmExtensionControl } from './lib/processes/animations-listing/ArmExtensionControl.ts'
import { DownloadSettings } from './lib/processes/export-to-file/DownloadSettings.ts'
import { StepExportToFile } from './lib/processes/export-to-file/StepExportToFile.ts'
import { StepWeightSkin } from './lib/processes/weight-skin/StepWeightSkin.ts'
@@ -243,13 +244,9 @@ export class Mesh2MotionEngine {
}
public update_a_pose_options_visibility (): void {
- if (this.ui.dom_arm_extension_options != null) {
- if (this.load_skeleton_step.skeleton_type() === SkeletonType.Human) {
- this.ui.dom_arm_extension_options.style.display = 'block'
- } else {
- this.ui.dom_arm_extension_options.style.display = 'none'
- }
- }
+ ArmExtensionControl.getInstance().set_visible(
+ this.load_skeleton_step.skeleton_type() === SkeletonType.Human
+ )
}
public handle_transform_controls_moving (): void {
@@ -711,6 +708,11 @@ export class Mesh2MotionEngine {
DOMUtilities.populate_animation_player(animation_player_mount)
}
+ const arm_extension_mount = document.querySelector('#arm-extension-mount')
+ if (arm_extension_mount instanceof HTMLElement) {
+ DOMUtilities.populate_arm_extension_controls(arm_extension_mount)
+ }
+
const download_control_mount = document.querySelector('#download-control-mount')
if (download_control_mount instanceof HTMLElement) {
DOMUtilities.populate_download_control(download_control_mount)
diff --git a/src/create.html b/src/create.html
index ac2ce9c..0403c29 100644
--- a/src/create.html
+++ b/src/create.html
@@ -362,64 +362,8 @@
that we get from the animations file(s) -->
<div id="animations-items" class="create-page"></div>
- <!-- slider to control arm extension (hide this feature until the base functionality is more stable) -->
- <div id="arm-extension-options">
- <div
- style="
- display: flex;
- flex-direction: row;
- align-items: center;
- gap: 1rem;
- justify-content: center;
- "
- >
- <label style="display: inline-flex">Expand / Contract Arms</label>
-
-
- <img src="../images/icons/help.svg" alt="Help" width="20" height="20"
- data-tippy-content="Fine-tune arm spread across all animations for heavier or skinnier characters." />
-
- <button class="secondary-button" id="reset-arm-extension-button">
- <img
- src="images/icons/reset.svg"
- alt="Reset Arm Extension"
- width="20"
- height="20"
- />
- </button>
- </div>
-
- <div
- style="
- display: flex;
- flex-direction: row;
- gap: 1rem;
- justify-content: flex-start;
- align-items: center;
- "
- >
- <input
- type="number"
- id="extend-arm-numeric-input"
- name="arm-extend-input"
- value="0"
- min="-70"
- max="100"
- step="1"
- />
- <span class="suffix-unit">%</span>
- <input
- id="extend-arm-range-input"
- style="flex-grow: 1"
- type="range"
- min="-70"
- max="100"
- value="0"
- />
- </div>
-
- <hr />
- </div>
+ <!-- slider to control arm extension. shared with the retarget workflow -->
+ <div id="arm-extension-mount"></div>
<div class="download-actions-row">
<button class="no-style-button" id="action_back_to_edit_skeleton">
diff --git a/src/lib/DOMUtilities.ts b/src/lib/DOMUtilities.ts
index 697da83..9455f82 100644
--- a/src/lib/DOMUtilities.ts
+++ b/src/lib/DOMUtilities.ts
@@ -147,6 +147,39 @@ export class DOMUtilities {
`
}
+ /**
+ * Render the shared "Expand / Contract Arms" controls into the provided mount element.
+ * Shared by the create workflow and the retarget workflow, so icon paths are
+ * root-absolute to work from both /create.html and /retarget/index.html.
+ */
+ static populate_arm_extension_controls (mount: HTMLElement): void {
+ mount.innerHTML = `
+ <div id="arm-extension-options">
+ <div style="display: flex; flex-direction: row; align-items: center; gap: 1rem; justify-content: center;">
+ <label style="display: inline-flex">Expand / Contract Arms</label>
+
+ <img src="/images/icons/help.svg" alt="Help" width="20" height="20"
+ data-tippy-content="Fine-tune arm spread across all animations for heavier or skinnier characters." />
+
+ <button class="secondary-button" id="reset-arm-extension-button"
+ aria-label="Reset arm extension" data-tippy-content="Reset">
+ <img src="/images/icons/reset.svg" alt="Reset Arm Extension" width="20" height="20" />
+ </button>
+ </div>
+
+ <div style="display: flex; flex-direction: row; gap: 1rem; justify-content: flex-start; align-items: center;">
+ <input type="number" id="extend-arm-numeric-input" name="arm-extend-input"
+ value="0" min="-70" max="100" step="1" />
+ <span class="suffix-unit">%</span>
+ <input id="extend-arm-range-input" style="flex-grow: 1" type="range"
+ min="-70" max="100" value="0" />
+ </div>
+
+ <hr />
+ </div>
+ `
+ }
+
/**
* Render the shared export/download button, settings split toggle, and hidden link.
* Page-specific button IDs and icon paths are resolved from the current route.
diff --git a/src/lib/processes/animations-listing/ArmExtensionControl.ts b/src/lib/processes/animations-listing/ArmExtensionControl.ts
new file mode 100644
index 0000000..dd9ed27
--- /dev/null
+++ b/src/lib/processes/animations-listing/ArmExtensionControl.ts
@@ -0,0 +1,117 @@
+import { UI } from '../../UI.ts'
+import { Utility } from '../../Utilities.ts'
+
+/**
+ * ArmExtensionControl - shared controller for the "Expand / Contract Arms" UI.
+ *
+ * The markup is injected by DOMUtilities.populate_arm_extension_controls(), and both
+ * the create workflow (StepAnimationsListing) and the retarget workflow
+ * (RetargetAnimationListing) drive the same instance.
+ *
+ * This is a singleton on purpose. The controls live in static page markup, but the
+ * retarget listing step is re-created every time the user clicks "Continue". Binding
+ * the listeners once here and only swapping the change callback avoids stacking up
+ * duplicate listeners on those round trips.
+ */
+export class ArmExtensionControl {
+ private static instance: ArmExtensionControl | null = null
+
+ private readonly ui: UI
+
+ /** The amount to raise the arms, as a percentage. */
+ private arm_extension_amount: number = 0.0
+
+ private on_change: ((value: number) => void) | null = null
+
+ private has_added_event_listeners: boolean = false
+
+ private constructor () {
+ this.ui = UI.getInstance()
+ }
+
+ public static getInstance (): ArmExtensionControl {
+ if (ArmExtensionControl.instance === null) {
+ ArmExtensionControl.instance = new ArmExtensionControl()
+ }
+ return ArmExtensionControl.instance
+ }
+
+ /**
+ * Point the control at the workflow step that should react to changes.
+ * Safe to call multiple times: listeners are only added once.
+ */
+ public initialize (on_change: (value: number) => void): void {
+ this.on_change = on_change
+
+ if (!this.has_added_event_listeners) {
+ this.add_event_listeners()
+ this.has_added_event_listeners = true
+ }
+ }
+
+ public value (): number {
+ return this.arm_extension_amount
+ }
+
+ /**
+ * Return the value and both inputs to zero without notifying the current listener.
+ * Needed because the markup is static and survives leaving/re-entering a step.
+ */
+ public reset (): void {
+ this.arm_extension_amount = 0.0
+ this.update_input_elements(0.0)
+ }
+
+ public set_visible (visible: boolean): void {
+ if (this.ui.dom_arm_extension_options != null) {
+ this.ui.dom_arm_extension_options.style.display = visible ? 'block' : 'none'
+ }
+
+ // let the animations listing reserve vertical space only when the controls are shown
+ const animations_listing = document.getElementById('animations-listing')
+ if (animations_listing !== null) {
+ animations_listing.classList.toggle('has-arm-extension', visible)
+ }
+ }
+
+ private update_input_elements (new_value: number): void {
+ if (this.ui.dom_extend_arm_numeric_input !== null) {
+ this.ui.dom_extend_arm_numeric_input.value = new_value.toString()
+ }
+ if (this.ui.dom_extend_arm_range_input !== null) {
+ this.ui.dom_extend_arm_range_input.value = new_value.toString()
+ }
+ }
+
+ // three different things might update this value: numeric input, range input, or reset button
+ private update_value (new_value: number): void {
+ this.arm_extension_amount = new_value
+ this.on_change?.(new_value)
+ }
+
+ private add_event_listeners (): void {
+ // reset A-Pose arm extension button
+ this.ui.dom_reset_arm_extension_button?.addEventListener('click', () => {
+ const extend_arm_value: number = 0 // reset to zero
+ this.update_input_elements(extend_arm_value)
+ this.update_value(extend_arm_value)
+ })
+
+ // A-Pose arm extension event listener
+ this.ui.dom_extend_arm_numeric_input?.addEventListener('input', () => {
+ const extend_arm_value: number = Utility.parse_input_number(this.ui.dom_extend_arm_numeric_input?.value)
+ if (this.ui.dom_extend_arm_range_input !== null) {
+ this.ui.dom_extend_arm_range_input.value = extend_arm_value.toString()
+ }
+ this.update_value(extend_arm_value)
+ })
+
+ this.ui.dom_extend_arm_range_input?.addEventListener('input', () => {
+ const extend_arm_value: number = Utility.parse_input_number(this.ui.dom_extend_arm_range_input?.value)
+ if (this.ui.dom_extend_arm_numeric_input !== null) {
+ this.ui.dom_extend_arm_numeric_input.value = extend_arm_value.toString()
+ }
+ this.update_value(extend_arm_value)
+ })
+ }
+}
diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.ts
index 6adfebe..6eac770 100644
--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts
+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts
@@ -6,6 +6,7 @@ import {
} from 'three'
import { AnimationUtility } from './AnimationUtility.ts'
+import { ArmExtensionControl } from './ArmExtensionControl.ts'
import { AnimationLoader, type AnimationLoadProgress } from './AnimationLoader.ts'
import { CustomAnimationImporter } from './CustomAnimationImporter.ts'
import { type ModelVariationSwitcher } from './ModelVariationSwitcher.ts'
@@ -52,10 +53,8 @@ export class StepAnimationsListing extends EventTarget {
this.animations_file_path = path
}
- /**
- * The amount to raise the arms.
- */
- private warp_arm_amount: number = 0.0
+ // shared "Expand / Contract Arms" controls. owns the current arm extension amount
+ private readonly arm_extension_control: ArmExtensionControl = ArmExtensionControl.getInstance()
private has_added_event_listeners: boolean = false
@@ -354,7 +353,7 @@ export class StepAnimationsListing extends EventTarget {
}
/// Apply the arm extension warp:
- AnimationUtility.apply_arm_extension_warp(this.animation_clips_loaded, this.warp_arm_amount)
+ AnimationUtility.apply_arm_extension_warp(this.animation_clips_loaded, this.arm_extension_control.value())
}
/**
@@ -445,37 +444,13 @@ export class StepAnimationsListing extends EventTarget {
})
}
- // reset A-Pose arm extension button
- this.ui.dom_reset_arm_extension_button?.addEventListener('click', (event) => {
- const extend_arm_value: number = 0 // reset to zero
- if (this.ui.dom_extend_arm_numeric_input !== null) {
- this.ui.dom_extend_arm_numeric_input.value = extend_arm_value.toString()
- }
- if (this.ui.dom_extend_arm_range_input !== null) {
- this.ui.dom_extend_arm_range_input.value = extend_arm_value.toString()
- }
- this.update_a_pose_value(extend_arm_value)
- })
-
- // A-Pose arm extension event listener
- this.ui.dom_extend_arm_numeric_input?.addEventListener('input', (event) => {
- const extend_arm_value: number = Utility.parse_input_number(this.ui.dom_extend_arm_numeric_input?.value)
- if (this.ui.dom_extend_arm_range_input !== null) {
- this.ui.dom_extend_arm_range_input.value = extend_arm_value.toString()
- }
- this.update_a_pose_value(extend_arm_value)
- })
-
- this.ui.dom_extend_arm_range_input?.addEventListener('input', (event) => {
- const extend_arm_value: number = Utility.parse_input_number(this.ui.dom_extend_arm_range_input?.value)
- if (this.ui.dom_extend_arm_numeric_input !== null) {
- this.ui.dom_extend_arm_numeric_input.value = extend_arm_value.toString()
- }
- this.update_a_pose_value(extend_arm_value)
+ // shared A-Pose arm extension controls (reset button, numeric input, range input)
+ this.arm_extension_control.initialize(() => {
+ this.update_a_pose_value()
})
// check for changes to mirror animations checkbox
- this.ui.dom_mirror_animations_checkbox?.addEventListener('change', (event) => {
+ this.ui.dom_mirror_animations_checkbox?.addEventListener('change', () => {
const is_checked: boolean = this.ui.dom_mirror_animations_checkbox?.checked ?? false
this.mirror_animations_enabled = is_checked
// Rebuild animations with or without mirroring
@@ -504,9 +479,8 @@ export class StepAnimationsListing extends EventTarget {
this.has_added_event_listeners = true
}
- // three different things might update this value: numeric input, range input, or reset button
- private update_a_pose_value (new_value: number): void {
- this.warp_arm_amount = new_value
+ // called by ArmExtensionControl whenever the arm extension amount changes
+ private update_a_pose_value (): void {
this.rebuild_warped_animations()
this.play_animation(this.current_playing_index)
}
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.ts
index 72da041..2549f55 100644
--- a/src/retarget/RetargetAnimationListing.ts
+++ b/src/retarget/RetargetAnimationListing.ts
@@ -1,12 +1,15 @@
import { AnimationPlayer } from '../lib/processes/animations-listing/AnimationPlayer.ts'
import { AnimationSearch } from '../lib/processes/animations-listing/AnimationSearch.ts'
import { AnimationLoader } from '../lib/processes/animations-listing/AnimationLoader.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 { ThemeManager } from '../lib/ThemeManager.ts'
import { type TransformedAnimationClipPair } from '../lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts'
import { AnimationRetargetService } from './AnimationRetargetService.ts'
import { StepExportRetargetedAnimations } from './steps/StepExportRetargetedAnimations.ts'
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'
@@ -24,6 +27,13 @@ export class RetargetAnimationListing extends EventTarget {
private animation_mixer: AnimationMixer = new AnimationMixer(new Object3D())
private readonly ui: UI = UI.getInstance()
+ // shared "Expand / Contract Arms" controls. owns the current arm extension amount
+ private readonly arm_extension_control: ArmExtensionControl = ArmExtensionControl.getInstance()
+
+ // -1 means nothing has been played yet, so there is nothing to re-play when the
+ // arm extension changes. unlike the create workflow, this step does not auto-play
+ private current_playing_index: number = -1
+
private _added_event_listeners: boolean = false
public animation_search: AnimationSearch | null = null
@@ -47,6 +57,16 @@ export class RetargetAnimationListing extends EventTarget {
this._added_event_listeners = true
}
+ // the arm extension markup is static, so it survives leaving and re-entering this
+ // step. re-point it at this instance and clear whatever the last visit left behind
+ this.arm_extension_control.initialize(() => {
+ this.update_arm_extension_value()
+ })
+ this.arm_extension_control.reset()
+ this.arm_extension_control.set_visible(
+ AnimationRetargetService.getInstance().get_skeleton_type() === SkeletonType.Human
+ )
+
this.show_bone_toggle_button(true)
}
@@ -64,6 +84,7 @@ export class RetargetAnimationListing extends EventTarget {
public reset_step_data (): void {
this.animation_clips_loaded = []
this.animation_mixer = new AnimationMixer(new Object3D())
+ this.current_playing_index = -1
this.animation_player.clear_animation()
}
@@ -134,6 +155,9 @@ export class RetargetAnimationListing extends EventTarget {
}
private on_all_animations_loaded (): void {
+ // the arm slider can be moved while the animation files are still downloading
+ this.rebuild_warped_animations()
+
// Sort alphabetically
this.animation_clips_loaded.sort((a: TransformedAnimationClipPair, b: TransformedAnimationClipPair) => {
if (a.display_animation_clip.name < b.display_animation_clip.name) return -1
@@ -190,17 +214,47 @@ export class RetargetAnimationListing extends EventTarget {
}
}
+ /**
+ * Rebuilds all of the warped animations by applying the specified warps.
+ * The warp is applied to the source-space display clip, so both the preview
+ * and the export (which each retarget the display clip) pick it up.
+ */
+ private rebuild_warped_animations (): void {
+ // Reset all of the warped clips to the corresponding original clip.
+ this.animation_clips_loaded.forEach((warped_clip: TransformedAnimationClipPair) => {
+ warped_clip.display_animation_clip = AnimationUtility.deep_clone_animation_clip(warped_clip.original_animation_clip)
+ })
+
+ /// Apply the arm extension warp:
+ AnimationUtility.apply_arm_extension_warp(this.animation_clips_loaded, this.arm_extension_control.value())
+ }
+
+ // called by ArmExtensionControl whenever the arm extension amount changes
+ private update_arm_extension_value (): void {
+ this.rebuild_warped_animations()
+
+ // nothing auto-plays in this step, so only re-play if the user picked an animation
+ if (this.current_playing_index >= 0) {
+ this.play_animation(this.current_playing_index)
+ }
+ }
+
private play_animation (index: number): void {
if (index < 0 || index >= this.animation_clips_loaded.length) {
console.warn('Invalid animation index:', index)
return
}
+ this.current_playing_index = index
+
const animation_pair = this.animation_clips_loaded[index]
const display_clip = animation_pair.display_animation_clip
- // Stop all current actions
+ // the mixer keeps an internal cache of every clip it has been handed. dragging the
+ // arm extension slider fires an input event per pixel, and each one produces a new
+ // retargeted clip, so replace the mixer instead of letting dead clips pile up
this.animation_mixer.stopAllAction()
+ this.animation_mixer = new AnimationMixer(new Object3D())
// Retarget the animation using the shared service
const retargeted_clip: AnimationClip = AnimationRetargetService.getInstance().retarget_animation_clip(
diff --git a/src/retarget/index.html b/src/retarget/index.html
index d076db8..769c278 100644
--- a/src/retarget/index.html
+++ b/src/retarget/index.html
@@ -189,9 +189,10 @@
<!-- javascript will populate this item with animations
that we get from the animations file(s) -->
- <div id="animations-items"> </div>
-
+ <div id="animations-items"> </div>
+ <!-- slider to control arm extension. shared with the create workflow -->
+ <div id="arm-extension-mount"></div>
<div style="display: flex; gap: 0.5rem;">
<button class="no-style-button" id="back_to_bone_map_button" >&#x2039; Back</button>
diff --git a/src/retarget/retarget.css b/src/retarget/retarget.css
index 80d6dac..c48b2f1 100644
--- a/src/retarget/retarget.css
+++ b/src/retarget/retarget.css
@@ -118,6 +118,11 @@ h1 {
max-height: calc(100vh - 17rem);
}
+#animations-listing.has-arm-extension #animations-items {
+ /* reserve room for the arm extension controls so the download row stays on screen */
+ max-height: calc(100vh - 25rem);
+}
+
#auto-bone-map-match {
font-size: 0.9rem;
border: 3px solid var(--bg-tertiary);