Standard TriMesh loading when mesh made of several objects?

Hi all,



I'm creating some objects under blender that are the aggregation of other blend files, and in the end, I select all the parts of my scene, and export them as .obj (that's what I'm using all the time). This is the first time I tried to export a whole scene. Then in my code, I run as usual something like



ObjToJme converter=new ObjToJme();      
      
        try {
            URL objFile=RoomsManager.class.getClassLoader().getResource(path);
            converter.setProperty("mtllib",objFile);
            converter.setProperty("texdir",objFile);
            ByteArrayOutputStream BO=new ByteArrayOutputStream();
            InsideMap.logger.info("Starting to convert .obj to .jme");
            converter.convert(objFile.openStream(),BO);
           
            //jbr.setProperty("texurl",new File(".").toURL());
            InsideMap.logger.info("Done converting, now watch how fast it loads!");
            long time=System.currentTimeMillis();
            TriMesh r=(TriMesh)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            InsideMap.logger.info("Finished loading time is "+(System.currentTimeMillis()-time));
            r.setModelBound(new BoundingBox());
            r.updateModelBound();
            return r;
        } catch (IOException e) {
           InsideMap.logger.logp(Level.SEVERE, this.getClass().toString(),"simpleInitGame()", "Exception", e);
        }



but then! I receive an error message saying that at the line

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

'cannot cast Node to TriMesh'

first I tried to modifiy quickly the code

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

but then I only managed to import part of the scene (some objects but not all)

finally I just did this to get around the problem : I broke my blender scene, imported each obejct and merged them in jme ....


is there a way I could manage to import a whole blender scene ?

(btw after exporting the whole scene through blender, I tried to read the .obj with blender, and it was fine, the whole scene was there)

any idea ?

Thanks,
Adrien

Hi Shagros,



Sorry for a delay in response.



I took a look at it…it doesn't appear that you're losing geometry when importing it into jme - which is good. I imported it on my end in jme, and in another model tool I use, and both matched the blender file with 3 candles, the couch, and the table.



I went through the obj importer code some too, when it constructs the trimeshes from the vertex/tex/vertexNormal lists, it does so by the materials in the file. So the couch was one trimesh, the table another, and the 3 candles were lumped into one trimesh - which explains the 3 attached to the node that the importer returned. Geometry wise, nothing's wrong.



Kinda sucks in my opinion, but that's why I stopped using the obj importer in the first place heh.



Guess I should have noticed that from your first partial obj posting heh, though I haven't been through the obj importer code in a very long time.

I'm not an expert but I do use blender with obj exports. You could cast the import object to a Spatial as both a Node and a TriMesh extends Spatial.

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


As for missing parts Im not to sure whats going on there. Are your sure blender exports to just one obj file? And are you applying modifiers when exporting?
Petah said:

I'm not an expert but I do use blender with obj exports. You could cast the import object to a Spatial as both a Node and a TriMesh extends Spatial.

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


As for missing parts Im not to sure whats going on there. Are your sure blender exports to just one obj file? And are you applying modifiers when exporting?


If I remember right, if you have more than one object, all your meshes get attached to a node and that's returned. It's usually good practice to cast it to a spatial from what you get from the importers anyways for this reason.

Are you getting any errors while the obj importer runs by any chance? I've always had some issues with it in the past, so it's certainly not blender and the obj file, but how the importer reads it (or perhaps how blender's own obj exporter/importer algorithms run). Importers/exporters aren't always consistent...

EDIT: you evoke using 'apply modifiers' before exporting, I never use that, what is it supposed to be ?



That's what I tried to do through casting to Node, I don't mind telling myself the code when I'm importing a Node or a simple TriMesh, but the problem really is even through debugger (eclipse) I found the resulting Node contained 3 children when it should contain 4. What I did to ensure blender was exporting correctly, and that my output .obj file was correct, is that I tried opening a new blender window and importing the .obj file, and it contained the whole scene (the 4 objects).



here's  my condensed .obj file if you mind taking a look : (the "…" is a set of the same first line)

you tell me if it helps you


Blender3D v248 OBJ File: couch+table.blend

www.blender3d.org

mtllib couch+table.mtl

v 0.771444 0.242781 0.440316



vt 0.492000 0.006212



vn 0.000000 0.000000 -1.000000



usemtl Material_candel_Cube._candel_Cube.tga

s off

f 17/1/1 1/2/1 2/3/1



v 0.778017 0.590675 0.111980



vt 0.250892 0.666030



vn 0.999993 0.001440 -0.003451



usemtl Material_gothic_couch_gothic_couch_Cube.tga

s off

f 19/21/18 40/22/18 23/23/18 20/24/18



v -0.292018 -0.746820 0.347653



vt 0.492000 0.006212



vn -0.246445 -0.102079 0.963766



usemtl Material_candel_Cube._candel_Cube.tga

s off

f 261/235/1 245/236/1 246/237/1



v -0.497633 -0.645067 0.347653



vt 0.492000 0.006212



vn -0.102079 0.246444 0.963766



usemtl Material_candel_Cube._candel_Cube.tga

s off

f 279/255/1 263/256/1 264/257/1



v -0.804830 -1.041403 0.299972



vt 0.918320 0.425725



vn -1.000000 0.000001 0.000010



usemtl Material_low_table_Cu_low_table_Cube.tga

s off

f 285/275/46 288/276/46 287/277/46 286/278/46

Mind posting with the files attached? Or you could PM me if you prefer. I can take a look.

ok so that's a partial good news (at least I have correct .obj files), ok so I'll try again doing it this way again, and hope this time it works better,



what do you use for importing model ? is there something much better than .obj ??



thanks a lot for helping me on this,



Adrien

more specifically, I'm wondering how to import a set of Nodes, that I could each name inside blender, so that in jme I have only one node containing specifically named nodes …

Well I don't use blender (use DeleD3D) and I built my own importer for that.



I don't know about having nodes, you'd have to be able to put objects into a hiearchy (think you can do that in blender), but also specify what are internal nodes in the model format. I don't think that's something readily available from the obj format nor the jme obj importer.



Of course, you can always develop your own art pipeline that could account for that…otherwise you'd have to manually organize the meshes you imported and add specifically named internal nodes to create the hierarchy.