Loading 3ds model with textures

Hello everyone! I just don't get my textures loaded with the model.

I wrote the following method to load the model:



   public Node getModel3ds(File file) {
      
      FormatConverter converter = new MaxToJme();   
      Node model;
      ByteArrayOutputStream bo = new ByteArrayOutputStream();
      File input = new File("cache/"+file.getName() + ".jme");
      
      // check if a converted version of the model already exists
      if(input.isFile()) {
         
         try {
            model = (Node)BinaryImporter.getInstance().load(input);
            return model;
         } catch(IOException e) {
            e.printStackTrace();
         }         
         
      }
      
      // if not create one
      try {                              
         
         converter.setProperty("texurl", file.getParentFile().toURL());
         converter.convert(new FileInputStream(file), bo);
         model = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(bo.toByteArray()));
      
         // save model to jme format
         bo.writeTo(new FileOutputStream("cache/"+file.getName()+".jme"));
      
      } catch(IOException e) {
         e.printStackTrace();
         model = new Node();
      }
      
      return model;
   }



The textures are in the same directory as the 3ds file, but they aren't loaded.

I've been searching the forum for at least an hour but could not find a (working) solution.
As you can see i try to set the path with converter.setProperty, but it has no effect, as it seems.
Also i read about using TextureKey.setOverrideLocation, but this method does not exist any more
in my jME version. So I'm pretty much stumped.

Can anyone help me please?

Thank you very much for your help in advance.

Do you get an exception during loading? If you don't, it's possible that the model does not have the textures assigned in the 3ds file.

See http://www.jmonkeyengine.com/jmeforum/index.php?topic=5707.0

Thanks for your answers! :smiley:

The ResourceLocatorTool helped me out. :slight_smile:

Now everything works perfectly.



For other beginners i'll post the code of my 3ds model loading method, maybe it's useful for someone.



This method tries to load a model specified by "file". If it could be loaded the file will be saved in jme format

into the directory "cache" (relative to the project directory). Next time the method is called a saved model

will be loaded from that "cache" right away for speeding things up.


   public Node getModel3ds(File file) {
      
      FormatConverter converter = new MaxToJme();   
      Node model = null;
      ByteArrayOutputStream bo = new ByteArrayOutputStream();
      File input = new File("cache/"+file.getName() + ".jme");
      
      // check if there is a cached jme format model for the requested file
      if(input.isFile()) {
         
         try {
            
            ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE,
                  new SimpleResourceLocator(file.getParentFile().toURI()));
            
            model = (Node)BinaryImporter.getInstance().load(input);
            
            ResourceLocatorTool.removeResourceLocator(ResourceLocatorTool.TYPE_TEXTURE,
                  new SimpleResourceLocator(file.getParentFile().toURI()));
            
            return model;
         } catch(IOException e) {
            e.printStackTrace();
         }         
         
      }
      
      // no cached model found -> convert the 3ds file and cache it
      try {                              
         
         ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE,
            new SimpleResourceLocator(file.getParentFile().toURI()));
         
         converter.convert(new FileInputStream(file), bo);
         model = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(bo.toByteArray()));

         bo.writeTo(new FileOutputStream("cache/"+file.getName()+".jme"));
         
         ResourceLocatorTool.removeResourceLocator(ResourceLocatorTool.TYPE_TEXTURE,
            new SimpleResourceLocator(file.getParentFile().toURI()));
      
      } catch(IOException e) {
         e.printStackTrace();
      }
      
      return model;
   }



Thanks again for your help. :)

Wow, I can't believe it took so long for me to find your post. I've been searching all over the place to try and figure out why my textures weren't showing up. Thanks for sharing that simple code, it is exactly what I needed (though I doubt you're still watching this post after a year).