Adding an .obj file

Hi!

I made an .obj file in blender that I would like to animate via Processing.js but I keep getting the following error:

" at sketch_3d_obj_interlaced_loops.objAbstractShape(sketch_3d_obj_interlaced_loops.java:37)
at sketch_3d_obj_interlaced_loops.draw(sketch_3d_obj_interlaced_loops.java:32)
at processing.core.PApplet.handleDraw(PApplet.java:2094)
at processing.opengl.PSurfaceJOGL$DrawListener.display(PSurfaceJOGL.java:840)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:692)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:674)
at jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:443)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1293)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1147)
at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:782)
at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81)
at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:453)
at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178)
at java.base/java.util.TimerThread.mainLoop(Timer.java:566)
at java.base/java.util.TimerThread.run(Timer.java:516)
The file “dalle_2.obj” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
java.lang.NullPointerException: Cannot invoke “java.io.BufferedReader.readLine()” because “reader” is null
at processing.core.PShapeOBJ.parseOBJ(PShapeOBJ.java:168)
at processing.core.PShapeOBJ.(PShapeOBJ.java:40)
at processing.core.PShapeOBJ.(PShapeOBJ.java:27)
at processing.opengl.PGraphics3D.loadShapeImpl(PGraphics3D.java:128)
at processing.opengl.PGraphicsOpenGL.loadShape(PGraphicsOpenGL.java:3383)
at processing.core.PApplet.loadShape(PApplet.java:10899)
at
"

Any way to see the source code which generates this error?

Could have something to do with this.

1 Like

Hi @svan !

Here is the source code and the file

PShape obj; // , obj2
PImage txtr;
float theta;

PGraphics canvas;

void setup(){
  size(1000,1000,P3D);
  txtr = loadImage("grid3.jpg");
}

void draw() {
  background(txtr);
  
  objAbstractShape();

void objAbstractShape(){
  obj = loadShape("dalle_2.obj");
  obj.setFill(color(255)); // Brigthens the texture
  obj.setTexture(txtr);
  
  pushMatrix();
  translate(width/2, height/2);
  shininess(5.0); 
  lights();
  rotateY(theta/2); //mouseY or (radians(frameCount));
  rotateX(theta/2); //mouseX or (radians(frameCount));
  rotateZ(theta/2);
  scale(70);
  shape(obj);
  popMatrix();
  
  theta+=.1; //.01
}
  
}

I’m now receiving the following message:

" The file “/Users/***************r/Desktop/3D Paintings/sketch_3d_obj_/data/dalle_.mtl” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

I have both file types in the folder though…

Hello @asymmetric,

Your function needs to be outside of draw:

PShape obj;
float theta;

PGraphics canvas;

void setup()  
  {
  size(500, 500,P3D);
  
  // load and scale obj once in setup:
  obj = loadShape("untitled.obj");
  obj.setFill(color(255)); // Brigthens the texture 
  obj.scale(70);
  }

void draw() 
  {
  objAbstractShape();
  }

void objAbstractShape()
  {
  // Your code  
  }

The above worked in Processing 4.3 with an OBJ I created with Blender:

Thank you, @glv but I don’t understand what you changed? My function needs to be in setup?

This tutorial will explain where to write the function definition:

I encourage you to watch the entire video.

Consider drawing a line between blocks of code (includes functions) to easily see these:

void setup()  
  {
  }
  
//**************************  

void draw() 
  {
  objAbstractShape();
  }
  
//**************************    

void objAbstractShape()
  {
  // Your code  
  }

I use my preferred indentation style to easily see blocks of code:
Indentation style - Wikipedia

:)

Try loading both the shape and image from setup() with everything else in draw(). For example:

void setup() {
  size(1000, 1000, P3D);
  obj = loadShape("dalle_2.obj");
  txtr = loadImage("grid3.jpg");
}

Personally, I would do away with the objAbstractShape() function and I’m not sure you really need pushMatrix() and popMatrix() unless there is more to your project than is being shown.

1 Like