Com.jme.scene.TriMesh cannot be cast to com.jme.scene.Node

Hi, I'm trying to export a model from Blender to use with JME. However, when I try to load it with TestObljWrite I get the fallowing error:

com.jme.scene.TriMesh cannot be cast to com.jme.scene.Node

for the line:

Node r=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));

I'm getting the feeling that I'm exporting to Obj wrong with Blender. Can somebody tell me how I can fix this?

Thanks, Ryan.

P.S. here's the code:







import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.net.URL;

import java.util.logging.Level;



import com.jme.app.AbstractGame;

import com.jme.app.SimpleGame;

import com.jme.bounding.BoundingSphere;

import com.jme.scene.Node;

import com.jme.util.LoggingSystem;

import com.jme.util.export.binary.BinaryImporter;

import com.jmex.model.XMLparser.Converters.FormatConverter;

import com.jmex.model.XMLparser.Converters.ObjToJme;



/**

  • Started Date: Jul 22, 2004<br><br>

    *
  • Demonstrates loading formats.

    *
  • @author Jack Lindamood

    */

    public class HelloModelLoading extends SimpleGame {

        public static void main(String[] args) {

            HelloModelLoading app = new HelloModelLoading();

            app.setDialogBehaviour(AbstractGame.ALWAYS_SHOW_PROPS_DIALOG);

            // Turn the logger off so we can see the XML later on

            LoggingSystem.getLogger().setLevel(Level.OFF);

            app.start();

        }



        protected void simpleInitGame() {

            // Point to a URL of my model

            URL model=HelloModelLoading.class.getClassLoader().getResource("jmetest/data/model/crazy.obj");



            // Create something to convert .obj format to .jme

            FormatConverter converter=new ObjToJme();

            // Point the converter to where it will find the .mtl file from

            converter.setProperty("mtllib",model);



            // This byte array will hold my .jme file

            ByteArrayOutputStream BO=new ByteArrayOutputStream();

            try {

                // Use the format converter to convert .obj to .jme

                converter.convert(model.openStream(), BO);

                Node Avatar=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));

                // shrink this baby down some

                Avatar.setLocalScale(.1f);

                Avatar.setModelBound(new BoundingSphere());

                Avatar.updateModelBound();

                // Put her on the scene graph

                rootNode.attachChild(Avatar);

            } catch (IOException e) {  // Just in case anything happens

                System.out.println("Damn exceptions!" + e);

                e.printStackTrace();

                System.exit(0);

            }

        }

    }









    …I'm thinking I'm giving it a model in  a format it's not expecting, please help!

I also had the exact problem… but I used forum search button first and found a solution. In short when you load a model that is made of multiple objects Node will be returned. If you have a simple model that is not subdivided then TriMesh will be returned. My solution to this was to create a new Node for every loaded model and put whatever comes out (Node or TriMesh > can be cast to Spatial) into that Node.

I probably read the same post( I did search ), but it didn't make any sense to me. How do you cast to spatial?

TriMesh is a Spatial and Node is a Spatial, how can you not?

If you're just new to Java, casting is done like this: (WantedClass)object … so that would be (Spatial)loaded_model

so when you cast to Spatial you can add it to a Node …err wait a minute, think you can add it anyway as both are Spatial :slight_smile: oh… gotta do some code fixing now…



edit: oh no, loaded_model is Savable that you can cast to Spatial, so here's the code I use. Remember I'm noob also and if anyone has better way to do it I'm all ears:


node.attachChild((Spatial)BinaryImporter.getInstance().load(new ByteArrayInputStream(bo.toByteArray())));

Thanks for the help, I'll try that out!



Update: I tried it and it works, thanks!!