How do I load a model from an external file?

I literally have no idea how could I do this. jME seems to disallow loading assets from an external file. I tried this:



[java]

Mesh grass = new Mesh();

try

{

BinaryImporter bi = new BinaryImporter();

bi.load(new File(“models/grass.obj”));

grass.read(bi);

}

catch (IOException ex)

{}

Geometry grassGeometry = new Geometry(“grass”, grass);

[/java]



But it obviously didn’t work (surprisingly, I get out of memory error, while the file is only 5KB).



Is there any way to accomplish the task?

You should probably have a look at the tutorials and documentation (look in the upper right corner of the site).

Here is the tutorial on loading assets: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_asset

1 Like
@Tumaini said:
You should probably have a look at the tutorials and documentation (look in the upper right corner of the site).
Here is the tutorial on loading assets: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_asset

Well, maybe I was a little bit unclear -- I don't want to load it from zipped/jar file as jME suggests.

By external file, do you mean user created content or similar that you for some reason cannot have in your assets folder?

If so, use a Locator, as mentioned in the tutorial (it uses ZipLocator, but there is also FileLocator, UrlLocator etc.).

1 Like
@lucaseasedup said:
Well, maybe I was a little bit unclear -- I don't want to load it from zipped/jar file as jME suggests.

It doesn't need to be in a .zip or .jar, but it does need to be in the project's /assets/Model directory. As @Tumaini said, if you need to place the files elsewhere, check out the Locator classes as per the tutorial.

Pass the .obj file as the parameter…



[java]

public void loadExternalModel(File file) throws IOException

{

String path = file.getParent();

assetManager.registerLocator(path,FileLocator.class.getName());



String filename = file.getName();

Spatial model = assetManager.loadModel(filename);



Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

model.setMaterial(mat1);

model.setLocalTranslation(Vector3f.ZERO);



rootNode.attachChild(model);

}

[/java]