Hi,
I’m trying to create a full copy of an existing Babylon.js scene in order to modify it and export it to GLTF without affecting the original scene.
Goal
-
Clone the entire scene (nodes, meshes, hierarchy, lights, cameras)
-
Keep materials and textures shared or reused (no deep copy needed)
-
Preserve complex hierarchy (including nested nodes and InstancedMesh)
-
Ensure textures are correctly exported into GLTF (
imagessection is not empty)
Problems I encountered
1. Manual deep clone
I tried implementing my own recursive clone:
-
cloning
TransformNode,Mesh,InstancedMesh, etc. -
restoring parent hierarchy
But:
-
very slow with large scenes (~48k nodes)
-
breaks hierarchy in some cases (especially cabinets with nested nodes)
-
InstancedMeshdepends on cloning order -
hard to maintain correctness
2. SceneSerializer + SceneLoader
I also tried:
const serialized = BABYLON.SceneSerializer.Serialize(scene);
const json = JSON.stringify(serialized);
const url = "data:application/json," + encodeURIComponent(json);
const newScene = await BABYLON.SceneLoader.LoadAsync("", url, engine);
This works much better for hierarchy, but I had issues where:
-
textures were not always present in GLTF export (
imagesarray empty) -
likely because export was triggered before textures finished loading
Questions
-
What is the recommended way to fully clone a scene in Babylon.js for export purposes?
- Is
SceneSerializer + SceneLoaderthe correct approach?
- Is
-
Is there any built-in or recommended pattern to:
-
duplicate a scene without modifying the original
-
while keeping performance reasonable?
-
-
What is the correct way to ensure that:
- all textures are fully loaded before calling
GLTF2Export.GLTFAsync(...)?
- all textures are fully loaded before calling
-
Are there any known pitfalls when cloning scenes with:
-
InstancedMesh -
complex transform hierarchies
-
PBR materials with textures?
-
Current approach
Right now I:
-
clone scene via
SceneSerializer + LoadAsync -
await
scene.whenReadyAsync() -
wait for all textures via
texture.onLoadObservable -
then process scene and export
But I want to confirm if this is the correct and recommended approach.
Thanks in advance!