ObjToJme and ResourceLocatorTool

I'm having a problem with the following code block in my ResourceLoader.



    public static Spatial loadShip() {
        URL modelDir = null;
        URL objFile = null;
        try {
            modelDir = new File("/C:/WingCommander1/").toURI().toURL();
            objFile = new File("/C:/WingCommander1/wing1.obj").toURI().toURL();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        FormatConverter converter = new ObjToJme();
        converter.setProperty("mtllib", modelDir);
        converter.setProperty("texdir", modelDir);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Spatial s = null;
        try {
            converter.convert(objFile.openStream(), output);
            byte[] ba = output.toByteArray();
            ByteArrayInputStream bais = new ByteArrayInputStream(ba);
            s = (Spatial) BinaryImporter.getInstance().load(bais);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        return s;
    }



The object is converted and the mtl is read but the ResourceLocatorTool results in the following warning.

WARNING: Unable to locate: /C:/WingCommander1/wing1.png

This file is present.

I found that this is happening in locateResource.  It appears that both sections result in null.  Can anyone shed some light on this for me?

File paths for windows look like this: new File("c:\path\file.obj");

normen said:

File paths for windows look like this: new File("c:\path\file.obj");


I'm using toURI().toURL(); so it should be converting it to a URL regardless but just in case I tried it like that and got the same response.


            modelDir = new File("C:\WingCommander1\").toURI().toURL();
            objFile = new File("C:\WingCommander1\wing1.obj").toURI().toURL();



still results in

Mar 19, 2010 12:47:02 PM com.jme.util.resource.ResourceLocatorTool locateResource
WARNING: Unable to locate: /C:/WingCommander1/wing1.png

I have somewhat the same problem.

  URL objFile=TestObjJmeWrite.class.getClassLoader().getResource("jmetest/data/model/VIKING1.obj");
            converter.setProperty("mtllib",objFile);
            converter.setProperty("texdir",objFile);

            ByteArrayOutputStream BO=new ByteArrayOutputStream();
            //logger.info("Starting to convert .obj to .jme");
            converter.convert(objFile.openStream(),BO);
            
          
            //logger.info("Done converting, now watch how fast it loads!");
            long time=System.currentTimeMillis();
            
            Spatial r = (Spatial)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            //logger.info("Finished loading time is "+(System.currentTimeMillis()-time));
            r.setLocalScale(150f);
            //r.setLocalScale(new Vector3f(0, 150, 400));
          
            r.setLocalTranslation(new Vector3f(-500,-190,1400));
      
          //rotate about the Y-Axis by approximately 1 pi
            Vector3f axis = Vector3f.UNIT_Y; // this equals (0, 1, 0) and does not require to create a new object
            float angle = 1.6f;
            r.getLocalRotation().fromAngleAxis(angle, axis);
            
            Vector3f axisZ = Vector3f.UNIT_Z; // this equals (0, 1, 0) and does not require to create a new object
            float angleZ = 0.1f;
           // r.getLocalRotation().fromAngleAxis(angleZ, axisZ);
            
                    
            objects.attachChild(r);



Apparently there is an mtl file which has to location to the image.
But when i start the programm i see the default image of jme and i get the following messagge:


20-mrt-2010 15:43:53 com.jme.util.resource.ResourceLocatorTool locateResource
WARNING: Unable to locate: /Q:/EclipseWorkSpace/jmonkeyengine/bin/jmetest/data/model/Viking.tif
20-mrt-2010 15:43:53 com.jme.util.TextureManager loadImage
WARNING: loadImage(String fileExt, InputStream stream, boolean flipped): no imageData found.  defaultTexture used.

How can i solve this?


Thanks in advance.

R

The MTL file is the materials file that contains information about how the texture is mapped to the object.  It indicates the file name for the texture as well. 



I've tried both tga and png formats for the texture.



Here is my current MTL data.


Blender3D MTL File: wing1.blend

Material Count: 1

newmtl Material_wing1.png

Ns 96.078431

Ka 0.000000 0.000000 0.000000

Kd 0.640000 0.640000 0.640000

Ks 0.500000 0.500000 0.500000

Ni 1.000000

d 1.000000

illum 2

map_Kd wing1.png

The obj file itself contains the vertex data, any normal data, and the faces and some other info as well as the mtl file name.  This is all in plain text.

My problem is that the file is present but i can't be found by jme.

Has anyone encountered the same problem?

you need to set up a resourcelocator, so that the textures / models can be found.



since your Viking.tif seems to be in jmetest/data/model, tell the resourcelocator to search for textures also in the model folder



            ResourceLocatorTool.addResourceLocator(
                    ResourceLocatorTool.TYPE_TEXTURE,
                    new SimpleResourceLocator(getClass().getResource(
                            "/jmetest/data/model/")));





@angellus00
pretty sure your textures need to be inside the classpath, if you want to load them automatically when importing the model.

edit: you probably could with AbsoluteResourceLocator

@ CoreDump



I did what you suggested and now i get this error:

21-mrt-2010 17:48:00 com.jme.util.TextureManager loadTexture
WARNING: Could not load image.  Specified URL is null.



I mean how difficult could it be to load a texture  :oops:

Maybe you know the cause is?

Thanks

getClass().getResource("/jmetest/data/model/") probably returned null ?

Is it the correct folder? is the jmetest package inside the classpath of your app ?

It is the correct folder 

Q:EclipseWorkSpacejmonkeyenginesrcjmetestdatamodel


there are the textures i need.

How can i check whether  jmetest package is  inside the classpath?

I think it is

My ship has a texture but the defualt is now used.



This is what i do:

         URL ship1 = TestObjJmeWrite.class.getClassLoader().getResource(
               "jmetest/data/model/VIKING1.obj");
         converter.setProperty("mtllib", ship1);
         converter.setProperty("texdir", ship1);
   ByteArrayOutputStream BO = new ByteArrayOutputStream();
      converter.convert(ship1.openStream(), BO);

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



I assume that the Mtl file is automatically recognized. But now i only see a ship without texture.


The textures are a different kind of resource, you will have to create a ResourceLocator yourself that finds the texture by the string given and returns the URL, then register it as a texture locator. Write this from my phone so I have no example right now, but I posted one not so long ago on this forum…

I've tried putting them just about everywhere.  For instance.



public class ResourceLoader {

    public static Spatial loadShip() {
        URL modelDir = null;
        URL objFile = null;
        try {
            modelDir = new File(".").toURI().toURL();
            objFile = new File("./wing2.obj").toURI().toURL();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        FormatConverter converter = new ObjToJme();
        converter.setProperty("mtllib", modelDir);
        converter.setProperty("texdir", modelDir);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Spatial s = null;
        try {
            converter.convert(objFile.openStream(), output);
            byte[] ba = output.toByteArray();
            ByteArrayInputStream bais = new ByteArrayInputStream(ba);
            s = (Spatial) BinaryImporter.getInstance().load(bais);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        return s;
    }



The object loads fine, I can see it on screen.  I know it reads the mtl file because it gives me a warning with the URL of the location of the texture.

Mar 22, 2010 2:40:49 AM com.jme.util.resource.ResourceLocatorTool locateResource
WARNING: Unable to locate: /C:/Users/angellus00/Documents/NetBeansProjects/Tyrian3d/wing1_color2.tga

This file is there.  I've even tried sending it texurl with


public class ResourceLoader {

    public static Spatial loadShip() {
        URL modelDir = null;
        URL objFile = null;
        URL texURL = null;
        try {
            modelDir = new File(".").toURI().toURL();
            objFile = new File("./wing2.obj").toURI().toURL();
            texURL = new File("./wing1_color2.tga").toURI().toURL();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        FormatConverter converter = new ObjToJme();
        converter.setProperty("mtllib", modelDir);
        converter.setProperty("texdir", modelDir);
        converter.setProperty("texurl", texURL);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Spatial s = null;
        try {
            converter.convert(objFile.openStream(), output);
            byte[] ba = output.toByteArray();
            ByteArrayInputStream bais = new ByteArrayInputStream(ba);
            s = (Spatial) BinaryImporter.getInstance().load(bais);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        return s;
    }
}



i dont see where you set up the resource locator …