Can I serialize scene out of the box?

Hi there. I’ve found pc.SceneParser, but how can I make json of scene for this parser? Do you have any integrated function?

As I know, it uses by loadSceneHierarchy to parse scene from json file. You probably can debug this function, make brakepoint somewhere inside and get your data from json and just save it.

But its cheating, I guess.

I’ve tried to do this with no luck :frowning: This is tricky to find such feature in sources. I need to save my entities dynamically in script, not initial state from editor

I’ll do it in a hour, okay.

So you can get your scene data by this way:

var myScene;

editor.call('loadScene', yourSceneId, function (err, scene) {
     myScene = scene;
}, false);

Then you can save your scene in json or parse it:

var jsonScene = JSON.stringify(myScene);
var parser = new pc.SceneParser(pc.app);
parser.parse(myScene);

But this is a Editor-level feature, you have to avoid using this.

1 Like

I need this feature without editor) So, if I will try to stringify scene, I get circular structure error

1 Like

Obtaining scene hierarchy by this way is a Editor feature. And it’s cheating, I guess.

Try to get rid of it. My scenes had no errors.

By the way, Three.js has this feature

Nice to know, PlayCanvas has too, but it’s little complicated, as you can see.

Just to be clear, are you looking for a way to export a current scene to JSON so you can load it again via SceneParser?

Exactly! :slight_smile: I want to save my scene to json and load it via parser again.
Now I’m making my own serializer function, but may be I’m wasting time and this function already exists?

1 Like

Well, it seems, I have to write this by myself… I’m not lazy, just if you have decode function, it should be code function too :frowning:

Okay, if you don’t trust me, then trust my code.

https://playcanvas.com/project/536664/overview/test-scene-export

Take a look at SceneChange script in assets.

You can press 3 on keyboard to load scene from text asset

this script is not attached to any node))
And why targetScene is typed as string?

Camera. It’s attached to camera. Just press 3 and see what’s happening.

To parse this scene.

nope, it isn’t

It is. Scene main 2

Even if I use your code in console

@mikefinch I think @Disorder is looking for a way to get the scene JSON at runtime in an app. For example, like he is making a level editor.

Scene structure is pretty simple, I don’t think he could faced with problems.