Import a 3D Model and automatically assign and export animations
TypeScript
Convert export step to typescript
Scott Petrovica1a2af2871fc
diff --git a/src/lib/geometry/BoneEnvelopeGeometry.js b/src/lib/geometry/BoneEnvelopeGeometry.jsindex 8f5b66a..f174982 100644--- a/src/lib/geometry/BoneEnvelopeGeometry.js+++ b/src/lib/geometry/BoneEnvelopeGeometry.js@@ -1,9 +1,11 @@import { BufferGeometry } from 'three/src/core/BufferGeometry.js'import { BufferAttribute } from 'three/src/core/BufferAttribute.js'-import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'++// eslint-disable-next-line @typescript-eslint/naming-convention+import * as BufferGeometryUtils from 'three/examples/jsm/utils/BufferGeometryUtils.js'class BoneEnvelopeGeometry {- constructor ()+ constructor (){let geometry = new BufferGeometry()geometry.type = 'BoneEnvelopeGeometry'@@ -118,7 +120,7 @@ class BoneEnvelopeGeometryUtils// X-Positive Bottom Left indexed vertexgeometry.attributes.position.array[9] += xgeometry.attributes.position.array[10] += y- geometry.attributes.position.array[11] += z+ geometry.attributes.position.array[11] += z}static move_x_pos_bottom_right(geometry, x, y, z)@@ -126,7 +128,7 @@ class BoneEnvelopeGeometryUtils// X-Positive Bottom Right indexed vertexgeometry.attributes.position.array[12] += xgeometry.attributes.position.array[13] += y- geometry.attributes.position.array[14] += z+ geometry.attributes.position.array[14] += z}static move_x_pos_top_right(geometry, x, y, z)@@ -134,7 +136,7 @@ class BoneEnvelopeGeometryUtils// X-Positive Top Right indexed vertexgeometry.attributes.position.array[21] += xgeometry.attributes.position.array[22] += y- geometry.attributes.position.array[23] += z+ geometry.attributes.position.array[23] += z}}diff --git a/src/lib/processes/StepExportToFile.js b/src/lib/processes/StepExportToFile.jsdeleted file mode 100644index f4e897c..0000000--- a/src/lib/processes/StepExportToFile.js+++ /dev/null@@ -1,94 +0,0 @@-import { UI } from '../UI.ts'-import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js'-import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js'--// import scene from three.js-import { Scene } from 'three'--// Note: EventTarget is a built-ininterface and do not need to import it-export class StepExportToFile extends EventTarget-{- ui = null- animation_clips_to_export = []-- constructor()- {- super()- this.ui = new UI()- }-- set_animation_clips_to_export(all_animations_clips)- {- this.animation_clips_to_export = []- const animation_checkboxes = this.ui.get_animated_selected_elements()- animation_checkboxes.forEach((checkbox) => {- if(checkbox.checked)- {- const animation_index = checkbox.getAttribute('value')- let originalClip = all_animations_clips[animation_index]- let clonedClip = originalClip.clone()- this.animation_clips_to_export.push(clonedClip)- }- })- }-- export(skinned_meshes, filename = 'exported_model')- {- if(this.animation_clips_to_export.length == 0)- {- console.log('ERROR: No animation clips added to export')- return- }-- const export_scene = new Scene()-- skinned_meshes.forEach((final_skinned_mesh) => {- export_scene.add(SkeletonUtils.clone(final_skinned_mesh))- })-- this.export_GLB(export_scene, this.animation_clips_to_export, filename)- }-- export_GLB(exported_scene, animations_to_export, file_name)- {- const gltf_exporter = new GLTFExporter()-- let export_options =- {- binary: true,- onlyVisible: false,- embedImages: true,- animations: animations_to_export,- }-- gltf_exporter.parse(exported_scene, (result) =>- {- if (result instanceof ArrayBuffer)- {- this._save_array_buffer(result, `${file_name}.glb`)- }- else- {- console.log('ERROR: result is not an instance of ArrayBuffer')- }- },- (error) =>- {- console.log('An error happened during parsing', error)- },- export_options- )- };-- _save_file(blob, filename)- {- this.ui.dom_export_button_hidden_link.href = URL.createObjectURL(blob)- this.ui.dom_export_button_hidden_link.download = filename- this.ui.dom_export_button_hidden_link.click()- }-- // used for GLB to turn content into a byte array for saving- _save_array_buffer(buffer, filename) {- this._save_file(new Blob([buffer], { type: 'application/octet-stream' }), filename)- }-}diff --git a/src/lib/processes/StepExportToFile.ts b/src/lib/processes/StepExportToFile.tsnew file mode 100644index 0000000..2d4cfea--- /dev/null+++ b/src/lib/processes/StepExportToFile.ts@@ -0,0 +1,84 @@+import { UI } from '../UI.ts'+import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js'++// eslint-disable-next-line @typescript-eslint/naming-convention+import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils.js'++// import scene from three.js+import { type AnimationClip, Scene, type SkinnedMesh } from 'three'++// Note: EventTarget is a built-ininterface and do not need to import it+export class StepExportToFile extends EventTarget {+ private readonly ui: UI = new UI()+ private animation_clips_to_export: AnimationClip[] = []++ public set_animation_clips_to_export(all_animations_clips: AnimationClip[]): void {+ this.animation_clips_to_export = []+ const animation_checkboxes = this.ui.get_animated_selected_elements()+ animation_checkboxes.forEach((checkbox) => {+ if (checkbox.checked === true) {+ const animation_index = checkbox.getAttribute('value')+ const original_clip: AnimationClip = all_animations_clips[animation_index]+ const cloned_clip: AnimationClip = original_clip.clone()+ this.animation_clips_to_export.push(cloned_clip)+ }+ })+ }++ public export (skinned_meshes: SkinnedMesh[], filename = 'exported_model'): void {+ if (this.animation_clips_to_export.length === 0) {+ console.log('ERROR: No animation clips added to export')+ return+ }++ const export_scene = new Scene()++ skinned_meshes.forEach((final_skinned_mesh) => {+ export_scene.add(SkeletonUtils.clone(final_skinned_mesh))+ })++ this.export_glb(export_scene, this.animation_clips_to_export, filename)+ }++ public export_glb(exported_scene: Scene, animations_to_export: AnimationClip[], file_name: string): void {+ const gltf_exporter = new GLTFExporter()++ const export_options =+ {+ binary: true,+ // eslint-disable-next-line @typescript-eslint/naming-convention+ onlyVisible: false,+ // eslint-disable-next-line @typescript-eslint/naming-convention+ embedImages: true,+ animations: animations_to_export+ }++ gltf_exporter.parse(exported_scene, (result) => {+ if (result instanceof ArrayBuffer) {+ this.save_array_buffer(result, `${file_name}.glb`)+ } else {+ console.log('ERROR: result is not an instance of ArrayBuffer')+ }+ },+ (error) => {+ console.log('An error happened during parsing', error)+ },+ export_options+ )+ };++ private save_file(blob: Blob, filename: string): void {+ if (this.ui.dom_export_button_hidden_link != null) {+ this.ui.dom_export_button_hidden_link.href = URL.createObjectURL(blob)+ this.ui.dom_export_button_hidden_link.download = filename+ this.ui.dom_export_button_hidden_link.click()+ } else {+ console.log('ERROR: dom_export_button_hidden_link is null')+ }+ }++ // used for GLB to turn content into a byte array for saving+ private save_array_buffer(buffer: ArrayBuffer, filename: string): void {+ this.save_file(new Blob([buffer], { type: 'application/octet-stream' }), filename)+ }+}diff --git a/src/script.js b/src/script.jsindex 2a90ea2..3eb8b78 100644--- a/src/script.js+++ b/src/script.js@@ -12,7 +12,7 @@ import { StepLoadModel } from './lib/processes/StepLoadModel.js'import { StepLoadSkeleton } from './lib/processes/StepLoadSkeleton.js'import { StepEditSkeleton } from './lib/processes/StepEditSkeleton.ts'import { StepAnimationsListing } from './lib/processes/StepAnimationsListing.ts'-import { StepExportToFile } from './lib/processes/StepExportToFile.js'+import { StepExportToFile } from './lib/processes/StepExportToFile.ts'import { StepWeightSkin } from './lib/processes/StepWeightSkin.js'import { ProcessStep } from './lib/enums/ProcessStep.js'