Import a 3D Model and automatically assign and export animations
TypeScript
Only add origin points to imported model meshes. Remove duplicate initialization
Scott Petrovic2634febac574
diff --git a/src/lib/processes/load-skeleton/OriginMarkerManager.ts b/src/lib/processes/load-skeleton/OriginMarkerManager.tsindex 323a8b9..f6d2b03 100644--- a/src/lib/processes/load-skeleton/OriginMarkerManager.ts+++ b/src/lib/processes/load-skeleton/OriginMarkerManager.ts@@ -1,39 +1,47 @@-import { type Object3D, Mesh, SphereGeometry, MeshBasicMaterial, type Scene } from 'three'+import { type Object3D, Mesh, SphereGeometry, MeshBasicMaterial, type Scene, Group, Vector3 } from 'three'++const ORIGIN_MARKER_GROUP_NAME: string = 'origin_marker_group'export function add_origin_markers (root: Scene): void {remove_origin_markers(root)+ // Create a group to hold all markers+ let markerGroup = root.getObjectByName(ORIGIN_MARKER_GROUP_NAME) as Group+ if (!markerGroup) {+ markerGroup = new Group()+ markerGroup.name = ORIGIN_MARKER_GROUP_NAME+ root.add(markerGroup)+ }++ // with the root scene, there is a group called "Imported Model" that contains+ // the meshes we want to put markers on+ const imported_meshes: Mesh[] = root.children.find(x => x.name === 'Imported Model')+const meshes: Mesh[] = []- root.traverse((child: Object3D) => {- if ((child as Mesh).isMesh) {- meshes.push(child as Mesh)+ imported_meshes.children.forEach(element => {+ if (element.type === 'Mesh') {+ meshes.push(element)}})for (const mesh of meshes) {+ // Get mesh world position+ const worldPos = new Vector3()+ mesh.getWorldPosition(worldPos)+const marker = new Mesh(new SphereGeometry(0.02, 8, 8),new MeshBasicMaterial({ color: 0xff4c00 }) // orange)- marker.name = 'origin_marker'- marker.position.set(0, 0, 0)- mesh.add(marker)+ marker.name = 'origin_marker_' + mesh.name+ marker.position.copy(worldPos)+ markerGroup.add(marker)}}-export function remove_origin_markers (root: Object3D): void {- const markers: Object3D[] = []- root.traverse((child: Object3D) => {- for (const c of child.children) {- if (c.name === 'origin_marker') {- markers.push(c)- }- }- })-- for (const marker of markers) {- if (marker.parent) {- marker.parent.remove(marker)- }+export function remove_origin_markers(root: Object3D): void {+ const markerGroup = root.getObjectByName(ORIGIN_MARKER_GROUP_NAME)+ if (markerGroup && markerGroup.parent) {+ markerGroup.parent.remove(markerGroup)}}diff --git a/src/script.ts b/src/script.tsindex 28f6e67..2e07bc9 100644--- a/src/script.ts+++ b/src/script.ts@@ -229,10 +229,6 @@ export class Bootstrap {this.load_model_step.begin()breakcase ProcessStep.LoadSkeleton:-- // Resetting state for the load skeleton step- this.load_skeleton_step.begin()-// add event listener. TODO: put this in the load skeleton process stepthis.load_skeleton_step.addEventListener('skeletonLoaded', () => {this.edit_skeleton_step.load_original_armature_from_model(this.load_skeleton_step.armature())@@ -251,6 +247,9 @@ export class Bootstrap {// initializing all the load skeleton step stuffthis.scene.add(this.load_model_step.model_meshes())process_step = ProcessStep.LoadSkeleton++ // finish initialization and add origin markers+ // this needs to happen at the end since it is expecting the mesh datathis.load_skeleton_step.begin()breakcase ProcessStep.EditSkeleton: