I’ve used Blender v2.79 to make an model and export it as an .obj. It worked just fine until I attempted to texture my model in Blender and got this runtime error:
ClassCastException: com.jme3.asset.ModelKey cannot be cast to com.jme3.asset.TextureKey
Is there a different model format that JME will handle and will handle textures? Or is there a different problem here?
Stack trace: java.lang.ClassCastException: com.jme3.asset.AssetKey cannot be cast to com.jme3.asset.TextureKey
at com.jme3.texture.Texture.setKey(Texture.java:427)
at com.jme3.scene.plugins.MTLLoader.loadTexture(MTLLoader.java:186)
at com.jme3.scene.plugins.MTLLoader.readLine(MTLLoader.java:232)
at com.jme3.scene.plugins.MTLLoader.load(MTLLoader.java:304)
at com.jme3.asset.DesktopAssetManager.loadLocatedAsset(DesktopAssetManager.java:259)
at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:373)
at com.jme3.scene.plugins.OBJLoader.loadMtlLib(OBJLoader.java:336)
at com.jme3.scene.plugins.OBJLoader.readLine(OBJLoader.java:389)
at com.jme3.scene.plugins.OBJLoader.load(OBJLoader.java:578)
at com.jme3.asset.DesktopAssetManager.loadLocatedAsset(DesktopAssetManager.java:259)
at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:373)
at com.jme3.asset.DesktopAssetManager.loadModel(DesktopAssetManager.java:416)
at com.jme3.asset.DesktopAssetManager.loadModel(DesktopAssetManager.java:420)
at mygame.Main.initCar(Main.java:76)
at mygame.Main.simpleInitApp(Main.java:60)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:220)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:211)
at java.lang.Thread.run(Thread.java:748)
Code:
private void initCar(float x, float y, float z) {
Spatial car = assetManager.loadModel("Models/vehicle.obj");
Material mat = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
player = new Car(x, y, z, car, mat);
rootNode.attachChild(player.createVehicle());
}
Where I make my spatial is where the error occurs (line 76).
Looks different to me. No “MaterialList” involved in this case:
Seems like Texture is expecting all of its asset keys to be TextureKey… which I don’t think is necessarily a good assumption… but I guess it’s a requirement of Textures since forever.
Also looks like it’s only a problem with older JME versions. Master has this code for MTLLoader.loadTexture:
Have you tried loading the model in .j3o format? I was unaware the engine could even load .obj models at run-time and am unsure how reliable it is.
But I would suggest exporting your model from Blender in GLTF format, and then use JME to convert from gltf → j3o; that will be a much more reliable format in a JME app.
Apparently, obj isn’t that reliable with jme.
How do you export from Blender in GLTF? The only export formats I can find are dae, abc, 3ds, fbx, ply, obj, x3d, and stl. I cannot get any of those (except obj, kinda) to work.
Update your blender version to 2.8+ and the scripts is included. Using gltf with 2.79 requires you install separate scripts and they are loaded with bugs.
If you use sdk 3.2, ogre still works with that just fine, sdk 3.3 uses the new animation system by default and you will run into more trouble as it is not in the wiki yet. The info is available by forum searches though.
Edit:
I added another link for gltf documentation above. It goes into depth on blender and gltf and 2.79.
Edit:
You can try fbx but you may or may not hit problems there. I have not used it much.
Have tried it only for FBX to GLTF (it only export as GLB which is the GLTF binary format) but should work for OBJ as well.
Also, you can find more by a google search.