Problem loading textured model (probably solved)

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?

Did that exception have a stack trace? It’s 99% of the useful information of an exception.

1 Like

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).

1 Like

Looks like Issue 1174.

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:

…which is making a TextureKey.

OP, what version of JME are you using?

1 Like

I’m using JME v3
Edit: looks like v3.2

1 Like

Even still, looks like this error shouldn’t happen.

…but it’s in the block for missing textures. So maybe if you figure out how to avoid the missing textures then you can work around the issue.

1 Like

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.

1 Like

Apparently, obj isn’t that reliable with jme. :thinking:
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.

1 Like

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 cant update blender, here are the scripts,

1 Like

It has its uses but they are limited.

1 Like

Is there any other way to export from Blender or convert from [types listed below] to end up with j3o or GITF?

To use gltf with 2.79, read this,

1 Like

You can install an ogre scripts into 2.79,

This is also a legacy format that will go away one day and it also does not work with blender 2.8.

1 Like

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.

1 Like

@codex you can also use an online obj → gltf converter

1 Like

That might be best for me.
Are there any that your recommend?

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.

1 Like

I can do fbx from Blender.
Thanks! :slightly_smiling_face:

1 Like

You are welcome.

Note, just took a look and it does already support OBJ.