Import a 3D Model and automatically assign and export animations
TypeScript
Get basic undo system setup
Scott Petrovica4bed85f694b
diff --git a/src/index.html b/src/index.htmlindex 1ff135b..381d99c 100644--- a/src/index.html+++ b/src/index.html@@ -121,6 +121,18 @@<span id="skeleton-step-actions">+ <!-- undo/redo system-->+ <fieldset style="position: fixed; right: 240px; top: 20px;">+ <div style="display: flex; gap: 2px;">+ <button id="undo-button" class="secondary-button" title="Undo (Ctrl+Z)" disabled>+ <img src="images/icons/undo.png" alt="Undo" width="16" height="16" />+ </button>+ <button id="redo-button" class="secondary-button" title="Redo (Ctrl+Y)" disabled>+ <img src="images/icons/redo.png" alt="Redo" width="16" height="16" />+ </button>+ </div>+ </fieldset>+<!-- <p class="step-instructions">Move bones into position.</p> --><p id="bone-selection-section">@@ -159,8 +171,6 @@</label></div></fieldset>--</div>diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.tsindex 6156c52..187f624 100644--- a/src/lib/EventListeners.ts+++ b/src/lib/EventListeners.ts@@ -36,6 +36,11 @@ export class EventListeners {this.bootstrap.is_transform_controls_dragging = event.valuethis.bootstrap.controls.enabled = !event.value+ // Store undo state when we start dragging (event.value = true)+ if (event.value && this.bootstrap.process_step === ProcessStep.EditSkeleton) {+ this.bootstrap.edit_skeleton_step.store_bone_state_for_undo()+ }+// if we stopped dragging, that means a mouse up.// if we are editing skeleton and viewing weight painted mesh, refresh the weight paintingif (this.bootstrap.process_step === ProcessStep.EditSkeleton &&@@ -139,5 +144,52 @@ export class EventListeners {console.warn(`Unknown mesh preview type selected: ${radio_button_selected}`)}})++ // Keyboard shortcuts for undo/redo+ document.addEventListener('keydown', (event: KeyboardEvent) => {+ // Only handle keyboard shortcuts when in EditSkeleton step+ if (this.bootstrap.process_step !== ProcessStep.EditSkeleton) {+ return+ }++ // Prevent default if we're handling the event+ const is_ctrl_or_cmd = event.ctrlKey || event.metaKey++ if (is_ctrl_or_cmd && event.key === 'z' && !event.shiftKey) {+ // Ctrl+Z or Cmd+Z for undo+ event.preventDefault()+ const success = this.bootstrap.edit_skeleton_step.undo_bone_transformation()+ if (success) {+ console.log('Undo successful')++ // Refresh weight painting if in weight painted mode+ if (this.bootstrap.mesh_preview_display_type === ModelPreviewDisplay.WeightPainted) {+ this.bootstrap.regenerate_weight_painted_preview_mesh()+ }++ // Update skeleton helper if it exists+ if (this.bootstrap.skeleton_helper !== undefined) {+ this.bootstrap.regenerate_skeleton_helper(this.bootstrap.edit_skeleton_step.skeleton(), 'Skeleton Helper')+ }+ }+ } else if (is_ctrl_or_cmd && (event.key === 'y' || (event.key === 'z' && event.shiftKey))) {+ // Ctrl+Y, Cmd+Y, Ctrl+Shift+Z, or Cmd+Shift+Z for redo+ event.preventDefault()+ const success = this.bootstrap.edit_skeleton_step.redo_bone_transformation()+ if (success) {+ console.log('Redo successful')+ // Update skeleton helper if it exists+ if (this.bootstrap.skeleton_helper !== undefined) {+ this.bootstrap.regenerate_skeleton_helper(this.bootstrap.edit_skeleton_step.skeleton(), 'Skeleton Helper')+ }+ // Refresh weight painting if in weight painted mode+ if (this.bootstrap.mesh_preview_display_type === ModelPreviewDisplay.WeightPainted) {+ this.bootstrap.regenerate_weight_painted_preview_mesh()+ }+ } else {+ console.log('No redo states available')+ }+ }+ })}}diff --git a/src/lib/UI.ts b/src/lib/UI.tsindex 38d8b26..e2dae65 100644--- a/src/lib/UI.ts+++ b/src/lib/UI.ts@@ -20,6 +20,8 @@ export class UI {dom_skeleton_drop_type: HTMLSelectElement | null = nulldom_mirror_skeleton_checkbox: HTMLElement | null = nulldom_scale_skeleton_button: HTMLButtonElement | null = null+ dom_undo_button: HTMLButtonElement | null = null+ dom_redo_button: HTMLButtonElement | null = nulldom_bind_pose_button: HTMLButtonElement | null = nulldom_scale_skeleton_input_box: HTMLElement | null = nulldom_move_to_origin_button: HTMLButtonElement | null = null@@ -86,6 +88,8 @@ export class UI {this.dom_skeleton_drop_type = document.querySelector('#skeleton-selection')this.dom_mirror_skeleton_checkbox = document.querySelector('#mirror-skeleton')this.dom_scale_skeleton_button = document.querySelector('#scale-skeleton-button')+ this.dom_undo_button = document.querySelector('#undo-button')+ this.dom_redo_button = document.querySelector('#redo-button')this.dom_selected_bone_label = document.querySelector('#edit-selected-bone-label')diff --git a/src/lib/UndoRedoSystem.ts b/src/lib/UndoRedoSystem.tsnew file mode 100644index 0000000..fd5aa3a--- /dev/null+++ b/src/lib/UndoRedoSystem.ts@@ -0,0 +1,230 @@+import { type Skeleton } from 'three'+import { Utility } from './Utilities.ts'+import type BoneTransformState from './models/BoneTransformState.ts'++/**+ * UndoRedoSystem+ * Manages undo/redo functionality for skeleton bone transformations+ * Stores states and allows reverting to previous states+ */+export class UndoRedoSystem extends EventTarget {+ private undo_stack: BoneTransformState[][] = []+ private redo_stack: BoneTransformState[][] = []+ private readonly max_history_size: number = 50+ private skeleton_ref: Skeleton | null = null++ constructor (max_history_size: number = 50) {+ super()+ this.max_history_size = max_history_size+ }++ /**+ * Set the skeleton reference that this undo/redo system will operate on+ */+ public set_skeleton (skeleton: Skeleton): void {+ this.skeleton_ref = skeleton+ this.clear_history()+ }++ /**+ * Store the current state of all bones in the skeleton+ * This should be called before making any changes to bones+ */+ public store_current_state (): void {+ if (this.skeleton_ref === null) {+ console.warn('Cannot store undo state: skeleton reference is null')+ return+ }++ const current_state = Utility.store_bone_transforms(this.skeleton_ref)+ this.undo_stack.push(current_state)++ // If the stack exceeds the maximum size, remove the oldest state+ // so it is a rolling history+ if (this.undo_stack.length > this.max_history_size) {+ this.undo_stack.shift() // Remove the oldest state+ }++ // Clear redo stack when a new action is performed+ this.redo_stack = []++ this.dispatch_state_changed_event()+ }++ /**+ * Restore the previous state (undo)+ * Returns true if undo was successful, false if no undo available+ */+ public undo (): boolean {+ if (this.skeleton_ref === null) {+ console.warn('Cannot undo: skeleton reference is null')+ return false+ }++ if (this.undo_stack.length === 0) {+ console.log('No undo states available')+ return false+ }++ // Store current state in redo stack before undoing+ const current_state = Utility.store_bone_transforms(this.skeleton_ref)+ this.redo_stack.push(current_state)++ // Get and apply the previous state+ const previous_state = this.undo_stack.pop()+ if (previous_state === undefined) {+ console.warn('Failed to get previous state from undo stack')+ return false+ }+ Utility.restore_bone_transforms(this.skeleton_ref, previous_state)++ // Update world matrices for all bones+ this.skeleton_ref.bones.forEach((bone: any) => {+ bone.updateWorldMatrix(true, true)+ })++ this.dispatch_state_changed_event()+ return true+ }++ /**+ * Restore the next state (redo)+ * Returns true if redo was successful, false if no redo available+ */+ public redo (): boolean {+ if (this.skeleton_ref === null) {+ console.warn('Cannot redo: skeleton reference is null')+ return false+ }++ if (this.redo_stack.length === 0) {+ console.log('No redo states available')+ return false+ }++ // Store current state in undo stack before redoing+ const current_state = Utility.store_bone_transforms(this.skeleton_ref)+ this.undo_stack.push(current_state)++ // Get and apply the next state+ const next_state = this.redo_stack.pop()+ if (next_state === undefined) {+ console.warn('Failed to get next state from redo stack')+ return false+ }+ Utility.restore_bone_transforms(this.skeleton_ref, next_state)++ // Update world matrices for all bones+ this.skeleton_ref.bones.forEach((bone: any) => {+ bone.updateWorldMatrix(true, true)+ })++ this.dispatch_state_changed_event()+ return true+ }++ /**+ * Check if undo is available+ */+ public can_undo (): boolean {+ return this.undo_stack.length > 0+ }++ /**+ * Check if redo is available+ */+ public can_redo (): boolean {+ return this.redo_stack.length > 0+ }++ /**+ * Get the number of available undo states+ */+ public get_undo_count (): number {+ return this.undo_stack.length+ }++ /**+ * Get the number of available redo states+ */+ public get_redo_count (): number {+ return this.redo_stack.length+ }++ /**+ * Clear all undo/redo history+ */+ public clear_history (): void {+ this.undo_stack = []+ this.redo_stack = []+ this.dispatch_state_changed_event()+ }++ /**+ * Get a snapshot of the current bone transforms without storing it+ * Useful for comparison or external storage+ */+ public get_current_state_snapshot (): BoneTransformState[] | null {+ if (this.skeleton_ref === null) {+ return null+ }+ return Utility.store_bone_transforms(this.skeleton_ref)+ }++ /**+ * Restore a specific state snapshot+ * This adds the current state to undo history before applying the snapshot+ */+ public restore_state_snapshot (state_snapshot: BoneTransformState[]): void {+ if (this.skeleton_ref === null) {+ console.warn('Cannot restore state snapshot: skeleton reference is null')+ return+ }++ // Store current state before restoring snapshot+ this.store_current_state()++ // Apply the snapshot+ Utility.restore_bone_transforms(this.skeleton_ref, state_snapshot)++ // Update world matrices for all bones+ this.skeleton_ref.bones.forEach((bone: any) => {+ bone.updateWorldMatrix(true, true)+ })++ this.dispatch_state_changed_event()+ }++ /**+ * Store initial state - this should be called once when the skeleton is first loaded+ * This provides a baseline state to return to+ */+ public store_initial_state (): void {+ if (this.skeleton_ref === null) {+ console.warn('Cannot store initial state: skeleton reference is null')+ return+ }++ // Clear any existing history+ this.clear_history()++ // Store the initial state+ this.store_current_state()+ }++ /**+ * Dispatch a custom event when the undo/redo state changes+ * This allows UI elements to update their enabled/disabled state+ */+ private dispatch_state_changed_event (): void {+ const event = new CustomEvent('undoRedoStateChanged', {+ detail: {+ canUndo: this.can_undo(),+ canRedo: this.can_redo(),+ undoCount: this.get_undo_count(),+ redoCount: this.get_redo_count()+ }+ })+ this.dispatchEvent(event)+ }+}diff --git a/src/lib/processes/StepEditSkeleton.ts b/src/lib/processes/StepEditSkeleton.tsindex 9da4060..23c16f8 100644--- a/src/lib/processes/StepEditSkeleton.ts+++ b/src/lib/processes/StepEditSkeleton.ts@@ -1,6 +1,7 @@import { UI } from '../UI.ts'import { Generators } from '../Generators.ts'import { Utility } from '../Utilities.ts'+import { UndoRedoSystem } from '../UndoRedoSystem.ts'import { Vector3, Euler, Object3D, Skeleton, type Scene, type Bone, BufferGeometry,PointsMaterial, Points, Float32BufferAttribute, TextureLoader,Raycaster, type Intersection, type Camera, SphereGeometry, MeshBasicMaterial, Mesh } from 'three'@@ -16,6 +17,7 @@ import { SkinningFormula } from '../enums/SkinningFormula.ts'*/export class StepEditSkeleton extends EventTarget {private readonly ui: UI+ private readonly undo_redo_system: UndoRedoSystem// Original armature data from the model data. A Skeleton type object is not// part of the original model data that is loadedprivate edited_armature: Object3D = new Object3D()@@ -33,10 +35,32 @@ export class StepEditSkeleton extends EventTarget {private readonly joint_texture = new TextureLoader().load('images/skeleton-joint-point.png')-constructor () {super()this.ui = new UI()+ this.undo_redo_system = new UndoRedoSystem(50) // Store up to 50 undo states+ }++ /**+ * Store the current bone state before making changes+ * Call this before any bone transformations+ */+ public store_bone_state_for_undo (): void {+ this.undo_redo_system.store_current_state()+ }++ /**+ * Undo the last bone transformation+ */+ public undo_bone_transformation (): boolean {+ return this.undo_redo_system.undo()+ }++ /**+ * Redo the last undone bone transformation+ */+ public redo_bone_transformation (): boolean {+ return this.undo_redo_system.redo()}public setup_scene (main_scene: Scene): void {@@ -72,6 +96,12 @@ export class StepEditSkeleton extends EventTarget {this.add_event_listeners()++ // Initialize undo/redo button states+ this.update_undo_redo_button_states(+ this.undo_redo_system.can_undo(),+ this.undo_redo_system.can_redo()+ )}private update_bind_button_text (): void {@@ -149,6 +179,32 @@ export class StepEditSkeleton extends EventTarget {this.show_debug = event.target.checkedthis.update_bind_button_text()})++ // Add undo/redo button event listeners+ this.ui.dom_undo_button?.addEventListener('click', () => {+ this.undo_bone_transformation()+ })++ this.ui.dom_redo_button?.addEventListener('click', () => {+ this.redo_bone_transformation()+ })++ // Listen for undo/redo state changes to update button states+ this.undo_redo_system.addEventListener('undoRedoStateChanged', (event: any) => {+ this.update_undo_redo_button_states(event.detail.canUndo, event.detail.canRedo)+ })+ }++ /**+ * Update the enabled/disabled state of undo/redo buttons+ */+ private update_undo_redo_button_states (can_undo: boolean, can_redo: boolean): void {+ if (this.ui.dom_undo_button !== null) {+ this.ui.dom_undo_button.disabled = !can_undo+ }+ if (this.ui.dom_redo_button !== null) {+ this.ui.dom_redo_button.disabled = !can_redo+ }}private convert_skinning_algorithm_to_enum (value: string): SkinningFormula {@@ -182,6 +238,14 @@ export class StepEditSkeleton extends EventTarget {if (this.ui.dom_enable_skin_debugging !== null) {this.ui.dom_enable_skin_debugging.removeEventListener('change', () => {})}++ if (this.ui.dom_undo_button !== null) {+ this.ui.dom_undo_button.removeEventListener('click', () => {})+ }++ if (this.ui.dom_redo_button !== null) {+ this.ui.dom_redo_button.removeEventListener('click', () => {})+ }}public cleanup_on_exit_step (): void {@@ -196,6 +260,11 @@ export class StepEditSkeleton extends EventTarget {public load_original_armature_from_model (armature: Object3D): void {this.edited_armature = armature.clone()this.create_threejs_skeleton_object()++ // Initialize the undo/redo system with the skeleton+ this.undo_redo_system.set_skeleton(this.threejs_skeleton)+ // Store the initial state as the baseline+ this.undo_redo_system.store_initial_state()}private create_threejs_skeleton_object (): Skeleton {diff --git a/static/images/icons/redo.png b/static/images/icons/redo.pngnew file mode 100644index 0000000..7265fd9Binary files /dev/null and b/static/images/icons/redo.png differdiff --git a/static/images/icons/undo.png b/static/images/icons/undo.pngnew file mode 100644index 0000000..b20bb1aBinary files /dev/null and b/static/images/icons/undo.png differ