Problem in import model

hi,







my code's wrong  this part:


formatConverter.convert(modelURL.openStream(), BO);




entire code:

public static Node loadModel (String modelFile)
       {
      Node         loadedModel   = null;
      FormatConverter      formatConverter = null;
      ByteArrayOutputStream    BO       = new ByteArrayOutputStream();
      String         modelFormat    = modelFile.substring(modelFile.lastIndexOf(".") + 1, modelFile.length());
      String         modelBinary   = modelFile.substring(0, modelFile.lastIndexOf(".") + 1) + "jbin";
      URL         modelURL   = ModelLoader.class.getClassLoader().getResource(modelBinary);

      //verify the presence of the jbin model
      if (modelURL == null)
                {
          modelURL= ModelLoader.class.getClassLoader().getResource(modelFile);
       //evaluate the format
         if (modelFormat.equals("3ds"))
                           formatConverter = new MaxToJme();
         else
                        {
                            if (modelFormat.equals("obj"))
                               formatConverter = new ObjToJme();
                            else
                            {
                                if (modelFormat.equals("x3d"))
                                {
                                    try
                                    {
                                       formatConverter = new X3dToJme();
                                    }
                                     catch (Exception ex)
                                    {
                                        Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                 }
                              }
                         }

         formatConverter.setProperty("mtllib", modelURL);

         try
                        {
            formatConverter.convert(modelURL.openStream(), BO);
            loadedModel = (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));

            //save the jbin format
            BinaryExporter.getInstance().save((Savable)loadedModel, new File(modelBinary));
         } catch (IOException e) {
            e.printStackTrace();
            return null;
         }
      }else{
         try {
            //load the jbin format
            loadedModel = (Node) BinaryImporter.getInstance().load(modelURL.openStream());
         } catch (IOException e) {
            return null;
         }
      }

      return loadedModel;
       }




the error that appears:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at visao3d_construtor.MyClass.loadModel(MyClass.java:79)




please help me ... I dont understand why this error ...

I suppose you want to load a file from the filesystem and not from the classpath, but with this

ModelLoader.class.getClassLoader().getResource(modelBinary);

you get an URL from a classpath. Try

URL url=new File(modelBinary).toURI().toURL();

instead.

did not work … : /



the problem continues in the same part … the error is here:




formatConverter.convert(modelURL.openStream(), BO);



the error appear:

02/05/2010 01:15:09 com.jme.system.lwjgl.LWJGLDisplaySystem <init>
INFO: LWJGL Display System created.
java.lang.NullPointerException
        at com.jmex.model.converters.maxutils.MaterialBlock.initializeVariables(MaterialBlock.java:80)
        at com.jmex.model.converters.maxutils.MaterialBlock.<init>(MaterialBlock.java:75)
        at com.jmex.model.converters.maxutils.EditableObjectChunk.processChildChunk(EditableObjectChunk.java:94)
        at com.jmex.model.converters.maxutils.ChunkerClass.chunk(ChunkerClass.java:94)
        at com.jmex.model.converters.maxutils.EditableObjectChunk.<init>(EditableObjectChunk.java:80)
        at com.jmex.model.converters.maxutils.TDSFile.processChildChunk(TDSFile.java:102)
        at com.jmex.model.converters.maxutils.ChunkerClass.chunk(ChunkerClass.java:94)
        at com.jmex.model.converters.maxutils.TDSFile.chunk(TDSFile.java:70)
        at com.jmex.model.converters.maxutils.TDSFile.<init>(TDSFile.java:92)
        at com.jmex.model.converters.MaxToJme.convert(MaxToJme.java:76)
        at visao3d_construtor.ModeloBinario.loadModel(ModeloBinario.java:101)




I changed some things in my code ... but it did not work:

public static Node loadModel (String modelFile)
       {
      Node         loadedModel   = null;
      FormatConverter      formatConverter = null;
      ByteArrayOutputStream    BO       = new ByteArrayOutputStream();
      String         modelFormat    = modelFile.substring(modelFile.lastIndexOf(".") + 1, modelFile.length());
      String         modelBinary   = modelFile.substring(0, modelFile.lastIndexOf(".") + 1) + "jbin";
      URL         modelURL=null;
                try
                {
                    modelURL = new File(modelBinary).toURI().toURL();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
JOptionPane.showMessageDialog(null, modelURL.getPath());
      //verify the presence of the jbin model
      if (!new File (modelBinary).exists())
                {
                     JOptionPane.showMessageDialog(null, 0);
          
       //evaluate the format
         if (modelFormat.equals("3ds"))
                           formatConverter = new MaxToJme();
         else
                        {
                            if (modelFormat.equals("obj"))
                               formatConverter = new ObjToJme();
                            else
                            {
                                if (modelFormat.equals("x3d"))
                                {
                                    try
                                    {
                                       formatConverter = new X3dToJme();
                                    }
                                     catch (Exception ex)
                                    {
                                        Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                 }
                              }
                         }

                   JOptionPane.showMessageDialog(null, 1);

         formatConverter.setProperty("mtllib", modelURL);

         try
                        {
                                modelURL= ModeloBinario.class.getClassLoader().getResource(modelFile);
            formatConverter.convert(modelURL.openStream(), BO);
                                JOptionPane.showMessageDialog(null, 2);
                               loadedModel = (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            //save the jbin format
            BinaryExporter.getInstance().save((Savable)loadedModel, new File(modelBinary));
         }
                        catch (Exception e)
                        {
            e.printStackTrace();
            return null;
         }
                       // JOptionPane.showMessageDialog(null,2);
      }
                else
                {
         try
                        {
            //load the jbin format
            loadedModel = (Node) BinaryImporter.getInstance().load(modelURL.openStream());
         } catch (IOException e)
                        {
            return null;
         }

                        JOptionPane.showMessageDialog(null, 3);
      }

      return loadedModel;
       }




you have some code ready for me to import a template?

or so please help me to solve the problem of my code ...

Seems to me as if the file can be loaded but the file might be corrupt. Did you

try another model? Where is the model from? Did you try to load your model with

ModelLoader (located in com.jmex.model.util package)?

I tried with the class ModelLoader, but still did not work:


import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.util.ModelLoader;
import java.io.File;
import javax.swing.JOptionPane;

public class ModeloBinario
{
    private String urlModelo = null;
    private String urlModeloBinario = null;
    Node Modelo = null;
 

    ModeloBinario(String urlModelo)
    {
       this.urlModelo = urlModelo;
    }   
 


      public void loadModel ()
      {
          if (ModelLoader.isValidModelFile(urlModelo))
          {

              try
              {
          
                  //the program hangs on this line:
                  Modelo = ModelLoader.loadModel(urlModelo);
      
              }
              catch (Exception e)
              {
                  JOptionPane.showMessageDialog(null, "error in the import");
                  e.printStackTrace();
              }
          }
          else
              JOptionPane.showMessageDialog(null, "invalid");
      }

        boolean export ()
        {
            urlModeloBinario = urlModelo.substring(0, urlModelo.lastIndexOf(".") + 1) + "jbin";;
            BinaryExporter exp = new BinaryExporter();
           try
           {
               if (exp.save(Modelo, new File(urlModeloBinario)))
                    return true;
              

               return false;
           }
           catch (Exception e)
            {
               JOptionPane.showMessageDialog(null, "error in export");
                e.printStackTrace();
                return false;
            }

        }

        Spatial getModelo()
        {
            return Modelo;
        }

        String getUrlModelo ()
        {
            return urlModeloBinario;
        }
}




I do not know what else to do ......  :|

the file that I'm doing the tests is that first car made in 3ds this link:

http://www.dynamicwork.net/download-3d-model-library.html

did you try to use the obj-file instead of the 3ds? I think the obj-importer is more reliable than the 3ds-importer

not resolved, the same thing happened …

Did you method worked one single time? Mabye with the bike.obj in jmetest/data/model?


dosen't work any way … someone has a class ready to pass me? I'm about to desist …

in jmetest there is a package with example for all loaders… (sry don't know the

exact packagename, but I'm sure you will find it…)

I do not understand … has no logic … I'm desperate … I can not make my class care at all …



always the problem here:


formatConverter.convert(rawIn, outStream);




my class:

import com.jme.bounding.BoundingSphere;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.model.converters.X3dToJme;
import com.jmex.model.util.ModelLoader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class ModeloBinario
{
    static Spatial criaModelo (String urlModelo)
    {
        FormatConverter formatConverter = null;
           String modelFormat    = urlModelo.substring(urlModelo.lastIndexOf(".") + 1, urlModelo.length());

          
           if (modelFormat.equals("3ds"))
                           formatConverter = new MaxToJme();
         else
                        {
                            if (modelFormat.equals("obj"))
                               formatConverter = new ObjToJme();
                            else
                            {
                                if (modelFormat.equals("x3d"))
                                {
                                    try
                                    {
                                       formatConverter = new X3dToJme();
                                    }
                                     catch (Exception ex)
                                    {
                                        Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                 }
                              }
                         }

          
           Spatial Modelo=null;
           ByteArrayOutputStream outStream = new ByteArrayOutputStream();
           FileInputStream rawIn = null;
            try {
                rawIn = new FileInputStream(urlModelo);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
            try
            {
                formatConverter.convert(rawIn, outStream);
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rawIn.close();
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
                ByteArrayInputStream convertedIn =   new ByteArrayInputStream(outStream.toByteArray());
            try {
                Modelo = (Spatial) BinaryImporter.getInstance().load(convertedIn);
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
              

             export(Modelo,urlModelo);


            return Modelo;
    }

        static void  export (Spatial s, String url)
        {
            BinaryExporter exp = new BinaryExporter();
            url = url.substring(0, url.lastIndexOf(".") + 1) + "jbin";
           try
           {
               if (exp.save(s, new File(url)))
                   JOptionPane.showMessageDialog(null,"yes!","file binary", JOptionPane.INFORMATION_MESSAGE);
           }
           catch (IOException ex) {
            Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
           }

        }
}






the error is always this:



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at visao3d_construtor.ModeloBinario.criaModelo(ModeloBinario.java:67)
        at visao3d_construtor.Form.exportaBinario(Form.java:1315)
        at visao3d_construtor.Form.addLOD(Form.java:1340)
        at visao3d_construtor.Form.access$500(Form.java:50)
        at visao3d_construtor.Form$6.mouseClicked(Form.java:408)
        at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
        at java.awt.Component.processMouseEvent(Component.java:6266)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



please help me .. I'm desperate, I need to do this very much .......!

Hi there,



I tried your last posted code and it works just fine.

I can load  my models and even "Model Toyota Century" from the site you mentioned.



About the Nullpointerexception, I can only guess, since I added the "package" statement, and have more lines.

However, It may be possible that your code does not create a formatConverter, since you compare the file extensions case sensitive.

Try : modelformat.equalsIgnoreCase("3ds")

Same with obj and x3d.



The Toyota just had the file extension ".3DS"

does not work!



I do not know if it's some configuration problem …



I'll post the test I'm doing and ask that anyone attempt to see what the possible problem:









package visao3d_construtor;

class Main
{

   public static void main(String[] args)
   {
       ModeloBinario.criaModelo("D:/century_refix29_3DS.3DS");
    }
}




package visao3d_construtor;


import com.jme.bounding.BoundingSphere;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.model.converters.X3dToJme;
import com.jmex.model.util.ModelLoader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class ModeloBinario
{
    static Spatial criaModelo (String urlModelo)
    {
        FormatConverter formatConverter = null;
           String modelFormat    = urlModelo.substring(urlModelo.lastIndexOf(".") + 1, urlModelo.length());

          
           if (modelFormat.equalsIgnoreCase("3DS"))
           {
               try
               {
                   formatConverter = new MaxToJme();
                  // JOptionPane.showMessageDialog(null,"iniciou","file binary", JOptionPane.INFORMATION_MESSAGE);
               }
               catch(Exception e)
               {
                   //JOptionPane.showMessageDialog(null,"yes!","erro ao iniciar", JOptionPane.INFORMATION_MESSAGE);
                   e.printStackTrace();
               }
           }
         else
                        {
                            if (modelFormat.equals("obj"))
                            {
                               formatConverter = new ObjToJme();
                            }
                            else
                            {
                                if (modelFormat.equals("x3d"))
                                {
                                    try
                                    {
                                       formatConverter = new X3dToJme();
                                    }
                                     catch (Exception ex)
                                    {
                                        Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                 }
                              }
                         }

          
           Spatial Modelo=null;
           ByteArrayOutputStream outStream = new ByteArrayOutputStream();
           FileInputStream rawIn = null;
            try {
                rawIn = new FileInputStream(urlModelo);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
            try
            {
                formatConverter.convert(rawIn, outStream); // the error is in this line
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rawIn.close();
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
                ByteArrayInputStream convertedIn =   new ByteArrayInputStream(outStream.toByteArray());
            try {
                Modelo = (Spatial) BinaryImporter.getInstance().load(convertedIn);
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
              

             export(Modelo,urlModelo);


            return Modelo;
    }

        static void  export (Spatial s, String url)
        {
            BinaryExporter exp = new BinaryExporter();
            url = url.substring(0, url.lastIndexOf(".") + 1) + "jbin";
           try
           {
               if (exp.save(s, new File(url)))
                   JOptionPane.showMessageDialog(null,"yes!","file binary", JOptionPane.INFORMATION_MESSAGE);
           }
           catch (IOException ex) {
            Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
           }

        }
}





if this code work with someone, it's because I have some configuration problem here .....

Ahaaa,



Well, as far as I can remember this simple main()-method sollution won't work.



I think these importers already acces basic monkey features, which then have to be set up.



Heres some modified code. (I didn't not verify the loaded model, however no Exception)



package visao3d_construtor;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import com.jme.app.SimpleGame;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import com.jme.util.Timer;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.model.converters.X3dToJme;

public class ModeloBinario extends SimpleGame {

    static Spatial criaModelo(String urlModelo) {
        FormatConverter formatConverter = null;
        String modelFormat = urlModelo.substring(
                urlModelo.lastIndexOf(".") + 1, urlModelo.length());

        if (modelFormat.equalsIgnoreCase("3DS")) {
            try {
                formatConverter = new MaxToJme();
                // JOptionPane.showMessageDialog(null,"iniciou","file binary",
                // JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                // JOptionPane.showMessageDialog(null,"yes!","erro ao iniciar",
                // JOptionPane.INFORMATION_MESSAGE);
                e.printStackTrace();
            }
        } else {
            if (modelFormat.equals("obj")) {
                formatConverter = new ObjToJme();
            } else {
                if (modelFormat.equals("x3d")) {
                    try {
                        formatConverter = new X3dToJme();
                    } catch (Exception ex) {
                        Logger.getLogger(ModeloBinario.class.getName()).log(
                                Level.SEVERE, null, ex);
                    }
                }
            }
        }

        Spatial Modelo = null;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        FileInputStream rawIn = null;
        try {
            rawIn = new FileInputStream(urlModelo);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        try {
            formatConverter.convert(rawIn, outStream); // the error is in this
            // line
        } catch (IOException ex) {
            Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        try {
            rawIn.close();
        } catch (IOException ex) {
            Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        ByteArrayInputStream convertedIn = new ByteArrayInputStream(outStream
                .toByteArray());
        try {
            Modelo = (Spatial) BinaryImporter.getInstance().load(convertedIn);
        } catch (IOException ex) {
            Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE,
                    null, ex);
        }

        export(Modelo, urlModelo);

        return Modelo;
    }

    static void export(Spatial s, String url) {
        BinaryExporter exp = new BinaryExporter();
        url = url.substring(0, url.lastIndexOf(".") + 1) + "jbin";
        try {
            if (exp.save(s, new File(url)))
                JOptionPane.showMessageDialog(null, "yes!", "file binary",
                        JOptionPane.INFORMATION_MESSAGE);
        } catch (IOException ex) {
            Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE,
                    null, ex);
        }

    }

    public static void main(String[] args) {
        ModeloBinario modeloBinario = new ModeloBinario();
        modeloBinario.simpleInitGame();
        ModeloBinario
                .criaModelo("/home/claireit/tmp/century_refix29_3DS.3DS/century_refix29_3DS.3ds");
    }

    /**
     * {@inheritDoc}
     *
     * @see com.jme.app.BaseSimpleGame#simpleInitGame()
     */
    @Override
    protected void simpleInitGame() {
        setConfigShowMode(ConfigShowMode.AlwaysShow);
        getAttributes();
        display = DisplaySystem.getDisplaySystem(settings.getRenderer());

        display.setMinDepthBits(depthBits);
        display.setMinStencilBits(stencilBits);
        display.setMinAlphaBits(alphaBits);
        display.setMinSamples(samples);

        display.createHeadlessWindow(1600, 1200, 24);

        /** Set a black background. */
        display.getRenderer().setBackgroundColor(ColorRGBA.black.clone());
        /** Get a high resolution timer for FPS updates. */
        timer = Timer.getTimer();

        /** Sets the title of our display. */
        String className = getClass().getName();
        if (className.lastIndexOf('.') > 0)
            className = className.substring(className.lastIndexOf('.') + 1);
        display.setTitle(className);
    }
}



I got it!



tested here and it worked!



thanks for the help and excuse me for my ignorance!





:smiley: