Obj import to Jme2

Hello, I think this is the one houndred and one thread for importing models to JME2. But I’m very new to the engine and I have not a clue. I fellowed the wiki: http://www.jmonkeyengine.com/wiki/doku.php?id=howto_jme2_and_blender2.48 but I can’t load the materials. Loading the model works but it is without any materials. I have downloaded a nice model of a MacOS Computer from http://www.katorlegaz.com/3d_models/electronics_computers/0065/index.php. In blender I changed the coordinates to right hand side and export it to the Wavefront Obj format.



This is my code to load the model:

   try {

       URL url = new File(filename).toURI().toURL();
       FormatConverter converter = new ObjToJme();
       converter.setProperty("mtllib", new File(
          "resources/models/obj/apple-mac-plus/").toURI().toURL());

       ByteArrayOutputStream ostream = new ByteArrayOutputStream();

       converter.convert(url.openStream(), ostream);
       Spatial spatial = (Spatial) BinaryImporter.getInstance().load(
          new ByteArrayInputStream(ostream.toByteArray()));
       spatial.setLocalScale(2f);
       spatial.setModelBound(new BoundingSphere());
       spatial.updateModelBound();

       return spatial;
   } catch (IOException e) {
       throw new ModelsLoadException(e);
   }



It's from the wiki site with the exception that my ordner where to find the materials is not in the jar file as a resource. Is there anything wrong either with the format of the material file or with my code?

I just tested it with the maggie.obg from jmetest. It works. So I think the error is in my exported file.

does the ResourceLocatorTool help at all?

basixs said:

does the ResourceLocatorTool help at all?


I have no idea how to use this tool. I think it maybe locates resources somehow :|
XXX: Needs more documentation

The model and materials are probably loading correctly. The problem I had when trying it appears to be that the model needs to have its normals flipped as its only showing the proper colours on the inside.

As you are using blender, you can just follow basixs instructions in this post to flip them the other way.



http://www.jmonkeyengine.com/jmeforum/index.php?topic=10027.0





Maybe the importer could do with a flag to flip the normals at convert time? Shouldn’t be tooo hard to add.

Hmm… as a test I changed the converter to invert the normals and it didnt look right - guess theres more to it than that :slight_smile: (presuming I got it right)



Flipping the normals in blender did work tho . . .  not sure what the difference is.


Cause I got the same problem with ms3d Files, I can just agree with the RessourceLocatorTool. All you need to know about it, stands in this thread:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=10401.0



Frankly speaking it did not work with a certain .obj Model, but definitly with the Milkshape format



greetings