|
| 1 | +import { Node, SceneSerializer } from "babylonjs"; |
| 2 | + |
| 3 | +import { saveSingleFileDialog } from "../../../tools/dialog"; |
| 4 | +import { writeJSON } from "fs-extra"; |
| 5 | +import { toast } from "sonner"; |
| 6 | + |
| 7 | +import { Editor } from "../../main"; |
| 8 | + |
| 9 | +const JSON_CONFIG = { |
| 10 | + spaces: 4, |
| 11 | + encoding: "utf8", |
| 12 | +}; |
| 13 | +/** |
| 14 | + * Exports the entire scene to a .babylon file. |
| 15 | + * @param editor defines the reference to the editor used to get the scene. |
| 16 | + */ |
| 17 | +export async function exportScene(editor: Editor): Promise<void> { |
| 18 | + const filePath = saveSingleFileDialog({ |
| 19 | + title: "Export Scene", |
| 20 | + filters: [{ name: "Babylon Scene Files", extensions: ["babylon"] }], |
| 21 | + defaultPath: "scene.babylon", |
| 22 | + }); |
| 23 | + |
| 24 | + if (!filePath) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const scene = editor.layout.preview.scene; |
| 30 | + const data = await SceneSerializer.SerializeAsync(scene); |
| 31 | + |
| 32 | + await writeJSON(filePath, data, JSON_CONFIG); |
| 33 | + |
| 34 | + editor.layout.console.log(`Scene exported successfully to ${filePath}`); |
| 35 | + toast.success(`Scene exported successfully to ${filePath}`); |
| 36 | + } catch (e) { |
| 37 | + if (e instanceof Error) { |
| 38 | + editor.layout.console.error(`Error exporting scene: ${e.message}`); |
| 39 | + toast.error("Error exporting scene"); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Exports a specific node and all its children to a .babylon file. |
| 46 | + * @param editor defines the reference to the editor used to get the scene. |
| 47 | + * @param node defines the node to export along with its descendants. |
| 48 | + */ |
| 49 | +export async function exportNode(editor: Editor, node: Node): Promise<void> { |
| 50 | + const filePath = saveSingleFileDialog({ |
| 51 | + title: "Export Node", |
| 52 | + filters: [{ name: "Babylon Scene Files", extensions: ["babylon"] }], |
| 53 | + defaultPath: `${node.name}.babylon`, |
| 54 | + }); |
| 55 | + |
| 56 | + if (!filePath) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + const scene = editor.layout.preview.scene; |
| 62 | + |
| 63 | + // Get all nodes that should be included (selected node + descendants) |
| 64 | + const nodesToInclude = new Set<Node>(); |
| 65 | + nodesToInclude.add(node); |
| 66 | + |
| 67 | + // Add all descendants |
| 68 | + const descendants = node.getDescendants(false); |
| 69 | + descendants.forEach((descendant) => nodesToInclude.add(descendant)); |
| 70 | + |
| 71 | + // Store original doNotSerialize values |
| 72 | + const originalDoNotSerialize = new Map<Node, boolean>(); |
| 73 | + |
| 74 | + // Temporarily set doNotSerialize = true for all nodes except the ones we want to include |
| 75 | + scene.meshes.forEach((mesh) => { |
| 76 | + originalDoNotSerialize.set(mesh, mesh.doNotSerialize); |
| 77 | + mesh.doNotSerialize = !nodesToInclude.has(mesh); |
| 78 | + }); |
| 79 | + |
| 80 | + scene.lights.forEach((light) => { |
| 81 | + originalDoNotSerialize.set(light, light.doNotSerialize); |
| 82 | + light.doNotSerialize = !nodesToInclude.has(light); |
| 83 | + }); |
| 84 | + |
| 85 | + scene.cameras.forEach((camera) => { |
| 86 | + originalDoNotSerialize.set(camera, camera.doNotSerialize); |
| 87 | + camera.doNotSerialize = !nodesToInclude.has(camera); |
| 88 | + }); |
| 89 | + |
| 90 | + scene.transformNodes.forEach((transformNode) => { |
| 91 | + originalDoNotSerialize.set(transformNode, transformNode.doNotSerialize); |
| 92 | + transformNode.doNotSerialize = !nodesToInclude.has(transformNode); |
| 93 | + }); |
| 94 | + |
| 95 | + // Serialize the filtered scene |
| 96 | + const data = await SceneSerializer.SerializeAsync(scene); |
| 97 | + |
| 98 | + // Restore original doNotSerialize values |
| 99 | + originalDoNotSerialize.forEach((value, node) => { |
| 100 | + node.doNotSerialize = value; |
| 101 | + }); |
| 102 | + |
| 103 | + await writeJSON(filePath, data, JSON_CONFIG); |
| 104 | + |
| 105 | + editor.layout.console.log(`Node exported successfully to ${filePath}`); |
| 106 | + toast.success(`Node exported successfully to ${filePath}`); |
| 107 | + } catch (e) { |
| 108 | + if (e instanceof Error) { |
| 109 | + editor.layout.console.error(`Error exporting node: ${e.message}`); |
| 110 | + toast.error("Error exporting node"); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments