Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

try to make a few things a bit more strongly typed

Scott Petrovic3d2f638675cd
diff --git a/src/lib/components/CustomTransformControls.ts b/src/lib/components/CustomTransformControls.ts
index f18b08e..92ca551 100644
--- a/src/lib/components/CustomTransformControls.ts
+++ b/src/lib/components/CustomTransformControls.ts
@@ -81,7 +81,7 @@ const _objectChangeEvent = { type: 'objectChange' }
// ─── Main class ───────────────────────────────────────────────────────────────
-class CustomTransformControls extends Controls<Record<string, unknown>> {
+class CustomTransformControls extends Controls<Record<string, Record<string, unknown>>> {
declare camera: Camera
// object is declared as Object3D in base Controls but is set to undefined via defineProperty
@@ -157,7 +157,11 @@ class CustomTransformControls extends Controls<Record<string, unknown>> {
this._plane = plane
root.add(plane)
- const scope = this as any
+ // scope can be used to access the instance of CustomTransformControls within the defineProperty function
+ // & is used to assert that scope has the same properties as CustomTransformControls
+ const scope = this as CustomTransformControls & Record<string, unknown>
+ const plane_scope = plane as CustomTransformControlsPlane & Record<string, unknown>
+ const gizmo_scope = gizmo as CustomTransformControlsGizmo & Record<string, unknown>
function defineProperty (propName: string, defaultValue: unknown): void {
@@ -170,8 +174,8 @@ class CustomTransformControls extends Controls<Record<string, unknown>> {
set: function (value: unknown) {
if (propValue !== value) {
propValue = value
- ;(plane as any)[propName] = value
- ;(gizmo as any)[propName] = value
+ plane_scope[propName] = value
+ gizmo_scope[propName] = value
scope.dispatchEvent({ type: propName + '-changed', value })
scope.dispatchEvent(_changeEvent)
}
@@ -179,8 +183,8 @@ class CustomTransformControls extends Controls<Record<string, unknown>> {
})
scope[propName] = defaultValue
- ;(plane as any)[propName] = defaultValue
- ;(gizmo as any)[propName] = defaultValue
+ plane_scope[propName] = defaultValue
+ gizmo_scope[propName] = defaultValue
}
diff --git a/src/retarget/human-retargeting/Rig.ts b/src/retarget/human-retargeting/Rig.ts
index dc20a77..7603b7f 100644
--- a/src/retarget/human-retargeting/Rig.ts
+++ b/src/retarget/human-retargeting/Rig.ts
@@ -27,9 +27,8 @@ export class Rig {
// this.tpose.debug();
}
- fromConfig (cfg: Record<string, string[]> = {}): this {
- for (const [k, v] of Object.entries(cfg)) {
- const chain_names: string[] = Array.isArray(v) ? v : (v as any).names ?? []
+ fromConfig (cfg: Record<string, string[]>): this {
+ for (const [k, chain_names] of Object.entries(cfg)) {
if (chain_names.length === 0) {
console.log('Error - Rig.fromConfig : No joint names for chain ', k)
continue
diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.ts
index 82746ab..5bff991 100644
--- a/src/retarget/retarget.ts
+++ b/src/retarget/retarget.ts
@@ -98,7 +98,7 @@ class RetargetModule {
})
// Listen for target model (user-uploaded) loaded
- this.step_load_target_model.addEventListener('target-model-loaded', (_event: Event) => {
+ this.step_load_target_model.addEventListener('target-model-loaded', () => {
const temp_target_armature: Scene | null = this.step_load_target_model.get_retargetable_meshes()
if (temp_target_armature == null) {
diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.ts
index 7406a44..c08dbfd 100644
--- a/src/retarget/steps/StepExportRetargetedAnimations.ts
+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts
@@ -130,9 +130,9 @@ export class StepExportRetargetedAnimations extends EventTarget {
gltf_exporter.parse(
exported_scene,
- (result: ArrayBuffer) => {
+ (result: ArrayBuffer | { [key: string]: unknown }) => {
// Handle the result of the export
- if (result !== null) {
+ if (result instanceof ArrayBuffer) {
this.save_array_buffer(result, `${file_name}.glb`)
resolve() // Resolve the promise when the export is complete
} else {
@@ -140,7 +140,7 @@ export class StepExportRetargetedAnimations extends EventTarget {
reject(new Error('Export result is not an ArrayBuffer'))
}
},
- (error: any) => {
+ (error: unknown) => {
console.log('An error happened during parsing', error)
reject(error) // Reject the promise if an error occurs
},
diff --git a/src/retarget/steps/StepLoadTargetModel.ts b/src/retarget/steps/StepLoadTargetModel.ts
index 81f31e0..125f8a1 100644
--- a/src/retarget/steps/StepLoadTargetModel.ts
+++ b/src/retarget/steps/StepLoadTargetModel.ts
@@ -79,8 +79,10 @@ export class StepLoadTargetModel extends EventTarget {
console.log('File selected:', file.name, 'Size:', file.size, 'Type:', file.type)
// Get file extension
- const file_name = file.name.toLowerCase()
- let file_extension = ''
+ const file_name: string = file.name.toLowerCase()
+
+ // Determine the file extension and validate it
+ let file_extension: string
if (file_name.endsWith('.glb')) {
file_extension = 'glb'
} else if (file_name.endsWith('.fbx')) {