Save and load scenes

Hi,

I’ve sketched a scene and saved it in a babylon file using the BABYLON.SceneSerializer.Serialize(scene) and JSON.stringify(serializedScene) functions.

Now i try to load it again with the BABYLON.SceneLoader.Load function.

Here’s the playground https://playground.babylonjs.com/#67VZ89#1

I really don’t understand why the babylon file loses the architecture of the meshes, the camera, the gui objects and some more.

Are there any parameters i’ve forgotten when saving or loading the .babylon file ?

Thanks for your answers,

Boris

It is really hard to tell without the original scene you are trying to save.

In general it should work and keep your scene as is. @carolhmj could have a look if you provide the original scene serialization as a repro

Thanks @carolhmj :slight_smile:

1 Like

Ok, here’s the babylon file i want to load.

Is this what you want ?

Thanks,

Boris

Nope sorry for the confusion we need the scene you are serializing as I guess the error is some parts are not serialized :slight_smile:

Might be related to the same error we had in a different topic (that the models had the same node names and material names)?

1 Like

@sebavan Yes, but the scene is very complex and with multiple classes.

I will try to reproduce something easier in a playground

2 Likes

Ok, here are my playgrounds

playground to sketch and save the scene https://playground.babylonjs.com/#ZHMU3C#12

playground to load the scene : https://playground.babylonjs.com/#67VZ89#2

the gui objects are not found nor the arcRotate camera and if you look at the inspector, there is no node anymore, only the camera created before loading the scene.

Thanks for your help

3 Likes

Hello!

About the GUI elements, currently they aren’t exported in the .babylon format.

As for the camera, I’ve checked the generated .babylon file and it is there :smiley: What was happening is that when you use SceneLoader.Load, this function creates an entirely new scene, which wasn’t the same one being returned in the createScene function. You would use this Load even before creating your scene, like in this example: Blender to Babylon.js exporter | Babylon.js Documentation.

In the case you want to add the elements of a .babylon file to an preexisting scene, like the one created on the playground, you want to use the SceneLoader.Append function, like I’m doing here in this updated version of your playground: https://playground.babylonjs.com/#67VZ89#3. You can see that I pass the existing scene as an argument to the Append function, and also a callback to run once it’s ready.

5 Likes

Ok, thanks for your answer.

I will try your solution but there are two other problems.

1: how could i export my gui elements ?

2: my meshes have custom properties and i think that they can’t be exported because nothing is written in the documentation about that.

Is there any solution or another file format to export all ?
Can i add some other things to the json export string?

1 Like

For your GUI elements you can save to the snippet server or save to JSON separately from the Scene. The GUI editor can be used to modify and save changes to your gui.

HTH

3 Likes

For the mesh’s custom properties, mesh.metadata is serialized and parsed, so you can put things you want included there, like below for example. :slightly_smiling_face:

mesh.metadata = {
    customProperty1: value1,
    customProperty2: value2
};
2 Likes

Ok @Blake, i didn’t find in the docs that the metadata of the mesh was parsed, maybe it has been forgotten.

1 Like

For my Gui elements, i won’t save them but redesign them with the values after the loading because there are dimensions

1 Like