Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Add DOM manipulation to populate or hide UI for variation for rig

Scott Petrovicf5af379a1e4f
diff --git a/src/lib/processes/animations-listing/ModelVariationSwitcher.ts b/src/lib/processes/animations-listing/ModelVariationSwitcher.ts
new file mode 100644
index 0000000..1834c20
--- /dev/null
+++ b/src/lib/processes/animations-listing/ModelVariationSwitcher.ts
@@ -0,0 +1,63 @@
+import { RigConfig, type RigConfigEntry } from '../../RigConfig'
+import { type SkeletonType } from '../../enums/SkeletonType'
+
+/**
+ * Manages the model variation dropdown on the Explore page.
+ * When a rig type is selected that has model_variations in its RigConfig,
+ * the dropdown is populated and shown. Otherwise it is hidden.
+ *
+ * Dispatches 'variation-changed' with the new model file path when the user
+ * picks a different variation.
+ */
+export class ModelVariationSwitcher extends EventTarget {
+ private readonly dom_switcher: HTMLElement | null = document.querySelector('#model-variation-switcher')
+ private readonly dom_select: HTMLSelectElement | null = document.querySelector('#model-variation-selection')
+ private current_rig: RigConfigEntry | undefined
+ private added_event_listeners = false
+
+ /**
+ * Call this whenever the active rig type changes.
+ * Populates the variation dropdown if the rig has variations, otherwise hides it.
+ */
+ public update_for_rig (skeleton_type: SkeletonType): void {
+ this.current_rig = RigConfig.by_skeleton_type(skeleton_type)
+ this.populate_select()
+ this.add_event_listeners()
+ }
+
+ private populate_select (): void {
+ const switcher = this.dom_switcher
+ const select = this.dom_select
+ if (switcher === null || select === null) return
+
+ const variations = this.current_rig?.model_variations
+ if (variations === undefined || variations.length === 0) {
+ switcher.style.display = 'none'
+ return
+ }
+
+ // clear any previous options and rebuild from RigConfig
+ select.innerHTML = ''
+ for (const variation of variations) {
+ const option = document.createElement('option')
+ option.value = variation.model_file
+ option.textContent = variation.display_name
+ select.appendChild(option)
+ }
+
+ switcher.style.display = '' // use default display style by removing inline style
+ }
+
+ private add_event_listeners (): void {
+ if (this.added_event_listeners) return
+ this.added_event_listeners = true
+
+ const select = this.dom_select
+ if (select === null) return
+
+ select.addEventListener('change', () => {
+ const model_file = select.value
+ this.dispatchEvent(new CustomEvent('variation-changed', { detail: { model_file } }))
+ })
+ }
+}
diff --git a/src/marketing/MarketingBootstrap.ts b/src/marketing/MarketingBootstrap.ts
index e960009..ea537e7 100644
--- a/src/marketing/MarketingBootstrap.ts
+++ b/src/marketing/MarketingBootstrap.ts
@@ -2,11 +2,13 @@ import { Vector3 } from 'three'
import { ProcessStep } from '../lib/enums/ProcessStep'
import { SkeletonType } from '../lib/enums/SkeletonType'
import { Mesh2MotionEngine } from '../Mesh2MotionEngine'
+import { ModelVariationSwitcher } from '../lib/processes/animations-listing/ModelVariationSwitcher'
import { RigConfig } from '../lib/RigConfig'
export class MarketingBootstrap {
private mesh2motion_engine: Mesh2MotionEngine
private skeleton_type: SkeletonType = SkeletonType.None
+ private model_variation_switcher: ModelVariationSwitcher = new ModelVariationSwitcher()
constructor () {
this.mesh2motion_engine = new Mesh2MotionEngine()
@@ -46,6 +48,7 @@ export class MarketingBootstrap {
this.mesh2motion_engine.load_model_step.load_model_file('../' + rig.model_file, 'glb')
this.skeleton_type = rig.skeleton_type
this.change_active_skeleton(model_button)
+ this.model_variation_switcher.update_for_rig(rig.skeleton_type)
})
model_section.appendChild(model_button)
@@ -60,6 +63,11 @@ export class MarketingBootstrap {
}
public add_event_listeners (): void {
+ // when the user picks a different model variation, reload the model
+ this.model_variation_switcher.addEventListener('variation-changed', ((event: CustomEvent) => {
+ console.log('TODO: model variation change to. load and swap out target rig reference', event)
+ }) as EventListener)
+
// event after the DOM is fully loaded for HTML elements
document.addEventListener('DOMContentLoaded', () => {
this.setup_model_buttons() // automatically trigger the human once we begin for the default
diff --git a/src/marketing/marketing.css b/src/marketing/marketing.css
index 699fad9..c921d97 100644
--- a/src/marketing/marketing.css
+++ b/src/marketing/marketing.css
@@ -36,7 +36,7 @@ button.active-button {
}
#animations-items {
- max-height: calc(100vh - 14rem);
+ max-height: calc(100vh - 17rem);
}
#animation-listing-count {