Scene Saving and Loading

    Hello there.

    How can I save my scene and then load it? Seen something about JMEExporter and JMEImporter interfaces but dunno how to use them.

    Thanks in advance :slight_smile:

I'm not sure about JMEExporter and JMEImporter, but it would probably be best you write your own saving/loading code to suit your needs.  Saving and loading can vary greatly from project to project.

Look into BinaryExporter.getInstance().save(model, path); to export, and BinaryImporter.getInstance().load(path); to bring back in.

nymon said:

Look into BinaryExporter.getInstance().save(model, path); to export, and BinaryImporter.getInstance().load(path); to bring back in.



Can't seem to make it work. I have a Node (objectNode) attached to the rootNode and all of my models are attached from my objectNode. What I would like is to manage to serialize them into a file so that I can later restore my scene.
If I save my objectNode, will it save its children?
nautilus_me said:

If I save my objectNode, will it save its children?

Yes.

Have a look at jmetest.util.TestExporter. Thats should get you going.

nymon said:

Have a look at jmetest.util.TestExporter. Thats should get you going.

It did get me going, but I see I've got a bit of a problem. The objects attached to my objectNode are of type Object3d, which is derived from Node and implements Savable. Each object has a couple of fields, like it's name. When I save it, it saves only it's geometry. Any ideas why or how to fix it?
I'm still not sure that I'm saving/loading properly.

Saving method :

try {
            BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(filename));
            ArrayList<Spatial> objects = objectNode.getChildren();
            for(ListIterator<Spatial> i = objects.listIterator(); i.hasNext();) {
                //((Object3d)i.next()).detachChildNamed("selectionBox");
                Object3d temp = (Object3d)i.next();
                temp.detachChildNamed("selectionBox");
                temp.detachChildAt(1);
            }

            BinaryExporter.getInstance().save(objectNode, bos);
            writer.write(bos.toByteArray());
            writer.flush();
            writer.close();
        } catch (FileNotFoundException e){
            System.err.println("file not found. ");
        } catch (IOException e) {
            logger.log(Level.SEVERE, "BinaryExporter failed to save file", e);
        }



Loading :

try {
            //DataInputStream dis = new DataInputStream(new FileInputStream(filename));
            //dis.rea
            //ByteArrayInputStream bis = new ByteArrayInputStream();
            objectNode = (Node)BinaryImporter.getInstance().load(new File(filename));
           
            ArrayList<Spatial> objects = objectNode.getChildren();
            for(ListIterator<Spatial> i = objects.listIterator(); i.hasNext();) {
                //((Object3d)i.next()).detachChildNamed("selectionBox");
                Object3d temp = (Object3d)i.next();
                System.out.println("temp's name : " + temp.getName());
                temp.attachChild(new TextLabel2d(temp.getModel().getName()).getBillboard(1f));
                System.out.println("object3d child count : " + temp.getChildren().size());
                temp.getChild(1).setLocalTranslation(0f, temp.getHeight()/10 - 2f, 0f);
            }
           
            rootNode.attachChild(objectNode);
            rootNode.updateGeometricState(0f, true);
            rootNode.updateRenderState();
            mousePick.setPickedObject(new Object3d());
            logger.log(Level.INFO, "Finished loading export!");
        } catch (IOException e) {
            logger.log(Level.SEVERE, "BinaryImporter failed to load file", e);
        }



I just want to be able to save my scene, my objects and then be able to restore them as they were.

So your having problems saving and loading fields that you define in your ObjectNode and Object3D classes? If so, it is because you need override the save and load functions, to also save and load your fields. Take a look at the ones in Node to get an idea.

Thank you nymon. Using your suggestion I finally managed to properly save my scene. So far no problems :P. Here is the code, maybe somebody will need something like this  :):



public void write(JMEExporter e) throws IOException {
        super.write(e);
        e.getCapsule(this).write(this.name, "object name", "object");
        e.getCapsule(this).write(this.length, "object length", 10f);
        e.getCapsule(this).write(this.height, "object height", 10f);
        e.getCapsule(this).write(this.width, "object width", 10f);
        e.getCapsule(this).write(this.selectable, "object selectable", true);
        //e.getCapsule(this).write(thi)
        e.getCapsule(this).writeSavableArrayList(children, "children", null);
    }

    @SuppressWarnings("unchecked")
    public void read(JMEImporter e) throws IOException {
        super.read(e);
        children = e.getCapsule(this).readSavableArrayList("children", null);
        this.name = e.getCapsule(this).readString("object name", "object3D");
        this.length = e.getCapsule(this).readFloat("object length", 10f);
        this.height = e.getCapsule(this).readFloat("object height", 10f);
        this.width = e.getCapsule(this).readFloat("object width", 10f);
        this.selectable = e.getCapsule(this).readBoolean("object selectable", true);
        // go through children and set parent to this node
        if (children != null) {
            for (int x = 0, cSize = children.size(); x < cSize; x++) {
                Spatial child = children.get(x);
                this.attachChild(child);
            }
        }
    }



I was so wrong. Dunno why, but I can't move my objects after I load my scene.