[SOLVED]Loading a model, editing it and Overwriting the old file doesn't work [jme3.0]

Hi,

I’m trying to load a tree of nodes from a .j3o file. After the model was edited it shall be saved at the original filepath. For some reason this doesn’t work as expected. I wrote a simple testcase to demonstrate the problem:

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Node;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends SimpleApplication {

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        Node parentNode = new Node("parent");
        rootNode.attachChild(parentNode);
        System.out.println("Before first saving: " + ((Node) rootNode.getChild("parent")).getChild("child"));
        
        save(System.getProperty("user.home") + "\\Models\\someRandomName.j3o");
        load(System.getProperty("user.home") + "\\Models\\", "someRandomName.j3o");
        
        Node childNode = new Node("child");
        ((Node) rootNode.getChild("parent")).attachChild(childNode);
        System.out.println("Before second saving: " + ((Node) rootNode.getChild("parent")).getChild("child"));
        
        try {
            Thread.sleep(5000);
        }
        catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        save(System.getProperty("user.home") + "\\Models\\someRandomName.j3o");
        load(System.getProperty("user.home") + "\\Models\\", "someRandomName.j3o");
        System.out.println("After second loading: " + ((Node) rootNode.getChild("parent")).getChild("child"));
    }

    @Override
    public void simpleUpdate(float tpf) {
    }

    @Override
    public void simpleRender(RenderManager rm) {
    }

    public boolean save(String filePath) {
        Node parentNode = (Node) rootNode.getChild("parent");
        BinaryExporter exporter = BinaryExporter.getInstance();
        File file = new File(filePath); 
        file.delete();  //Doesn't fix the problem
        try {
            exporter.save(parentNode, file);
        }
        catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Error: Failed to save game!", ex);
            return false;
        }    
        return true;
    }

    public boolean load(String directoryPath, String fileName) {
        assetManager.registerLocator(directoryPath, FileLocator.class);
        rootNode.detachAllChildren();
        Node parentNode = (Node) assetManager.loadModel(fileName);
        rootNode.attachChild(parentNode);
        return true;
    }
}

If the Model is saved at the original filepath “childNode” is not saved together with the scene, although it is a child of “parentNode” in the scenegraph. However, right clicking on the file->properties shows, that the file is modified five seconds after it has been created.

Saving the edited scene to a different filepath “fixes” this problem. I have no idea why.

I’m sitting at this Problem for hours. Could anyone help me out with this?

Theres a cache in the AssetManager clear it before loading the same model if it changed.

1 Like

Thats it? Thank you very much @normen. You saved me some more hours of my life. I didn’t know, that you can clear the cache manually. The Problem was on the saving part though.

For anyone, who might stumble across this thread in the future:
You have to add:

((DesktopAssetManager)assetManager).clearCache();

Before you save the game. Adding this line before loading it won’t hurt either I guess :laughing:.
Or you could be a little bit more selective:

public boolean save(String directoryPath, String fileName) {
        assetManager.registerLocator(directoryPath, FileLocator.class);
        ((DesktopAssetManager)assetManager).deleteFromCache(new AssetKey(fileName));
[...]