Importing and exporting a scene

Hello!

I am trying to export my entire scene and save it too firebase. Then reimport it. It is an essential function for the application I am developing.

I am looking through the exporters and imports and I have yet to be able to get a round trip to work. I have been able to successfully export the scene with SceneSerilizer, but when I try to reimport that scene with SceneLoader it throws an error.

I am also willing to use gltf export/import.

I am using typescript.

Is there a guide out there already for this situation?
(I have been unsuccessful in finding one that helps me to understand what I need to do or what I have done wrong).

Hello and welcome to our community!
Can you share a repro in the Playground?

pinging @drigax for review once we will get the repro :wink:

I will work on reproducing with firebase.

In the meantime, this is the error I am seeing:

Unable to find a plugin to load .5,“overlaycolor”:[1,0,0],“applyfog”:true}],“particlesystems”:[]} files. Trying to use .babylon default plugin. To load from a specific filetype (eg. gltf) see

My methods

private exportScene(scene: Scene): string {
return JSON.stringify(SceneSerializer.Serialize(scene))
}

private importScene(gameState: GameState): void {
SceneLoader.loggingLevel = 1
SceneLoader.LoadAsync(’’, ‘Data:’ + gameState.jsonScene, this.engine)
}

Well I guess loadAsync does not expect json data but base64 string or an actual url

Hmmmm, Load() gives me the same error. Is Append() the only method that supports json?

@Drigax

https://www.babylonjs-playground.com/#KC5W6L

It does not present an error here but it does fail silently.

It does generate errors:


You should use “data:” and not “Data:” and the json scene should be stringified:

https://www.babylonjs-playground.com/#KC5W6L#2

It still does not display anything but there’s no more errors in the console, so hopefully it’s a step in the right direction.

@ncarr I don’t think that there is anything wrong with the SceneLoader, I traced through the sceneloader as it serialized your JSON scene successfully. I believe that the problem is what you’re doing with the scene after its serialized.

Check my modified playground: https://www.babylonjs-playground.com/#KC5W6L#3

In this, we create that serialized json string, but append it to the current scene. we can see that our object and material serialize and are populated in the active scene as expected.

The problem with the previous playgrounds are that we serialized the scene, but the engine was already told to use our empty scene with camera we created. we have 2 scenes in memory in the callback function for BABYLON.SceneLoader.Load(), but we’re only using the first one we made and returned via createScene.

The loaded actual scene sits in memory, but we don’t use it. @sebavan, do we have any samples for how to “change” the current active scene? It seems that appending is the “preferred” way to handle scene management, but I’m not sure if we have too many examples for how to properly mange multiple scenes at a time.

In the playground you should not change the current scene or you would need to render 2 of them per frame with engine.runRenderLoop()

So let me make sure I understand.

I need to Append and not Load?

This will merge the scene I am trying to load from firebase into the current scene.
Logically if the current scene is nearly blank (just a camera). Then it will show the scene I am loading.

The loaded actual scene sits in memory, but we don’t use it. @sebavan, do we have any samples for how to “change” the current active scene? It seems that appending is the “preferred” way to handle scene management, but I’m not sure if we have too many examples for how to properly mange multiple scenes at a time.

Part of my confusion here is because I have worked with THREEjs in the past and I was disposing of the default scene and loading the one from DB.

In the playground (as it only deals with one scene from the createScene function), it is preferable to append, and in your own app, as you control the render loop, it is entirely up to you :slight_smile:

2 Likes

Thank you all for the assistance, I think I have a better understanding of what I need to do.

1 Like