Search code examples
blenderjava-3d

JAVA3D load .obj file exported from blender


I'm trying to load .obj file from blender to Java3D but error always occur:

com.sun.j3d.utils.image.ImageException: javax.imageio.IIOException: Can't get input stream from URL!

The weirdest thing of it all is that when i load model from web everything is ok but when i import same file to blender and then export it (without any change) it doesn't work.

    import java.applet.*;
    import java.awt.*;

    import javax.media.j3d.*;
    import javax.vecmath.*;

    import com.sun.j3d.utils.applet.MainFrame;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    import com.sun.j3d.utils.universe.PlatformGeometry;
    import com.sun.j3d.utils.behaviors.keyboard.*;

    import com.sun.j3d.loaders.Scene;

    import com.sun.j3d.loaders.objectfile.ObjectFile;

    import java.io.*;

    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;

    public class Mykeynavbeh extends Applet implements KeyListener {

     private SimpleUniverse universe = null;
     private Canvas3D canvas = null;
     private TransformGroup viewtrans = null;

     private TransformGroup tg = null;
     private Transform3D t3d = null;
     private Transform3D t3dstep = new Transform3D();
     private Matrix4d matrix = new Matrix4d();

     public Mykeynavbeh() {
      setLayout(new BorderLayout());
      GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();

      canvas = new Canvas3D(config);
      add("Center", canvas);
      universe = new SimpleUniverse(canvas);

      BranchGroup scene = createSceneGraph();
      universe.getViewingPlatform().setNominalViewingTransform();

      universe.getViewer().getView().setBackClipDistance(100.0);

      canvas.addKeyListener(this);

      universe.addBranchGraph(scene);
     }

     private BranchGroup createSceneGraph() {
      BranchGroup objRoot = new BranchGroup();

      BoundingSphere bounds = new BoundingSphere(new Point3d(), 10000.0);

      viewtrans = universe.getViewingPlatform().getViewPlatformTransform();

      KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(viewtrans);
      keyNavBeh.setSchedulingBounds(bounds);
      PlatformGeometry platformGeom = new PlatformGeometry();
      platformGeom.addChild(keyNavBeh);
      universe.getViewingPlatform().setPlatformGeometry(platformGeom);

      objRoot.addChild(createLadybird());

      Background background = new Background();
      background.setColor(0.75f, 0.69f, 0.680f);
      background.setApplicationBounds(bounds);
      objRoot.addChild(background);

      return objRoot;
     }

     private BranchGroup createLadybird() {

      BranchGroup objRoot = new BranchGroup();
      tg = new TransformGroup();
      t3d = new Transform3D();

      tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

      t3d.setTranslation(new Vector3d(-0.15, -0.3, -5.0));
      t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f));
      t3d.setScale(1.0);

      tg.setTransform(t3d);

      ObjectFile loader = new ObjectFile(ObjectFile.RESIZE);
      Scene s = null;

      File file = new java.io.File("model/female2.obj");

      try {
       s = loader.load(file.toURI().toURL());
      } catch (Exception e) {
       System.err.println(e);
       System.exit(1);
      }

      tg.addChild(s.getSceneGroup());

      objRoot.addChild(tg);
      objRoot.addChild(createLight());

      objRoot.compile();

      return objRoot;

     }

     private Light createLight() {
      DirectionalLight light = new DirectionalLight(true, new Color3f(1.0f,
        1.0f, 1.0f), new Vector3f(-0.3f, 0.2f, -1.0f));

      light.setInfluencingBounds(new BoundingSphere(new Point3d(), 10000.0));

      return light;
     }

     public static void main(String[] args) {
      Mykeynavbeh applet = new Mykeynavbeh();
      Frame frame = new MainFrame(applet, 800, 600);
     }

     public void keyTyped(KeyEvent e) {
      char key = e.getKeyChar();

      if (key == 'd') {
       t3dstep.set(new Vector3d(0.0, 0.0, -0.1));
       tg.getTransform(t3d);
       t3d.mul(t3dstep);
       tg.setTransform(t3d);
      }

      if (key == 's') {

       t3dstep.rotY(Math.PI / 32);
       tg.getTransform(t3d);
       t3d.get(matrix);
       t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
       t3d.mul(t3dstep);
       t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23));
       tg.setTransform(t3d);

      }

      if (key == 'f') {

       t3dstep.rotY(-Math.PI / 32);
       tg.getTransform(t3d);
       t3d.get(matrix);
       t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
       t3d.mul(t3dstep);
       t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23));
       tg.setTransform(t3d);

      }

      if (key == 'r') {

       t3dstep.rotX(Math.PI / 32);
       tg.getTransform(t3d);
       t3d.get(matrix);
       t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
       t3d.mul(t3dstep);
       t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23));
       tg.setTransform(t3d);

      }

      if (key == 'v') {

       t3dstep.rotX(-Math.PI / 32);
       tg.getTransform(t3d);
       t3d.get(matrix);
       t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
       t3d.mul(t3dstep);
       t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23));
       tg.setTransform(t3d);

      }

      if (key == 'e') {
       t3dstep.set(new Vector3d(0.0, 0.1, 0.0));
       tg.getTransform(t3d);
       t3d.mul(t3dstep);
       tg.setTransform(t3d);
      }

      if (key == 'c') {
       t3dstep.set(new Vector3d(0.0, -0.1, 0.0));
       tg.getTransform(t3d);
       t3d.mul(t3dstep);
       tg.setTransform(t3d);
      }
     }

     public void keyReleased(KeyEvent e) {
     }

     public void keyPressed(KeyEvent e) {
     }
    }

Solution

  • Please try the following steps:

    [1] Open the .mtl file, and make sure all texture paths are relative like this:

    map_Kd female02_1.jpg

    Not like this:

    map_Kd C:\Users\Username\MyJava3D\Female02\model\female02_1.jpg (absolute path)

    On my PCs, if texture paths are not correct, that error occurs in Java3D 1.5.1. In Java3D 1.6.0, no error occurs, but the textures do not show up.

    Are you talking about female02.obj in Sweet Home 3D? (I'm just guessing from the file name "female2.obj.") If so, [2] and [3] are needed.

    [2] For multiple textures, check "Objects as OBJ Groups" and "Material Groups" (on the lower left corner) before exporting the model. Otherwise, the textures won't show up.

    [3] For the .obj file, the textures may not show up properly. If so, flip all normals outside.

    Blender 2.77:

    For each part (hair, face, dress, ....)

    1) In the Object mode, right-click the part.

    2) Press the Tab key to enter the Edit mode.

    3) In the 3D View window, press Ctrl-n several times.

    4) Press the Tab key to enter the Object mode.

    5) Repeat steps 1) to 4) above.

    [4] Also, comment out all the lines beginning with 'o' in the exported .obj file. Otherwise, the following error occurs: com.sun.j3d.loaders.ParsingErrorException: Unrecognized token, line~