Problem with .obj file (scale shape)

Hello,
Returning to Processing, which I haven’t used for several years, I have a problem importing a 3D shape made in Blender and saved as an .obj file.
the image I get is in 2D.
if I test the file with viewer Windows it is indeed in 3D and I can make it run on its own
What can be the problem?
thanks in advance

Hi @Philippe1 ,

Would you please show your code, otherwise we only could guessing what’s wrong ?

Cheers
— mnse

PShape ball;

void setup(){
size (500,500,P3D);
ball = loadShape(“Ball.obj”);
}

void draw(){
background(20);
pushMatrix();
translate(width/2, height/2);
shape(ball, 200, 200, 300,300);
popMatrix();
}

Try adding lights, texture, and rotation.

1 Like

Hello @Philippe1 ,

This will only resize the width (x) and height (y).

You need to scale the x, y and z:

ball.scale(100, 100, 100); //scale the x, y and z

Example using the default cube in Blender:

void setup() 
  {
  size (500, 500, P3D);
  ball = loadShape("untitled.obj");
  ball.scale(100, 100, 100);  //scale the x, y and z of the shape
  }

void draw() 
  {
  background(20);
  lights();
  translate(width/2, height/2);
  rotateY(frameCount*(TAU/360));
  shape(ball, 0, 0);
  }

Reference:

Another example:

:)

Thanks for the “scale” indication, I didn’t understand how to use it in 3D. It works perfectly.
Now, I would like to make my object grow over time.
In the setup I put “x,y,z” variables and I thought I would increment them like for my rotation variables.
The scale being in the setup this is not possible.
So I put the scale in the draw it doesn’t work. How to do it, I can’t find the solution.
Here is my code.

PShape ball;
float a=0;
float b=0;
float x=8;
float y=8;
float z=8;
float tx=2;
float tz=2;

void setup(){
size (500,500,P3D);
ball = loadShape(“Ball.obj”);
ball.scale(x,y,z);
}

void draw(){
background(20);
lights();
translate(width/tx, height/tz);
a +=0.001;
b +=0.002;
tx -=0.0008;
tz -=0.0005;
rotateY(a);
rotateZ(b);
shape(ball, 0, 0);
}

This belongs into draw

Then say x+=0.01; and for the other two as well

y and z

precisely if I put “ball.scale(x,y,z)”, my object is not displayed. I only have the black background.

Maybe it’s getting bigger too fast

Try smaller initial values and smaller adding

(Edit: ok, ball.scale(x,y,z); changes the ball matrix, so my idea x+=0.01; in draw() is wrong; instead define float x=0.01; before setup() to make for a slow growing - or see better answer below)

Hello @Philippe1,

This can be challenging so providing a working example:

PShape cube;
float x = 1;

void setup() 
  {
  size (500, 500, P3D);
  cube = loadShape("cube.obj");
  println(cube.getWidth(), cube.getHeight(), cube.getDepth()); // 2x2x2 for cube
  delay(1000);
  }

void draw() 
  {
  background(0);
  lights();
  translate(width/2, height/2, -100);
  rotateX(PI/4);
  rotateY(PI/4);
  cube.resetMatrix(); // cube is now back to original size
  if (x>100) x = 1;
  println(x);
  cube.scale(x);
  x=x+0.25;
  println(cube.getWidth(), cube.getHeight(), cube.getDepth());
  cube.setFill(color(255, 255, 0));
  shape(cube);
  }

cube.obj:

# Blender v3.4.1 OBJ File: ''
# www.blender.org
# mtllib untitled.mtl
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
vt 0.625000 0.500000
vt 0.875000 0.500000
vt 0.875000 0.750000
vt 0.625000 0.750000
vt 0.375000 0.750000
vt 0.625000 1.000000
vt 0.375000 1.000000
vt 0.375000 0.000000
vt 0.625000 0.000000
vt 0.625000 0.250000
vt 0.375000 0.250000
vt 0.125000 0.500000
vt 0.375000 0.500000
vt 0.125000 0.750000
vn 0.0000 1.0000 0.0000
vn 0.0000 0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 -1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
usemtl Material
s off
f 1/1/1 5/2/1 7/3/1 3/4/1
f 4/5/2 3/4/2 7/6/2 8/7/2
f 8/8/3 7/9/3 5/10/3 6/11/3
f 6/12/4 2/13/4 4/5/4 8/14/4
f 2/13/5 1/1/5 3/4/5 4/5/5
f 6/11/6 5/10/6 1/1/6 2/13/6

I am scaling equally for each axis so scale(x, y, x) can be scale(s) where s=x=y=z.

The size of the shape with each cycle of draw will keep scaling (multiplying) the last size and will increase exponentially.

cube.resetMatrix() restores original size of shape and gives you some control over scaling with each cycle of draw().

Try removing/commenting cube.resetMatrix() and see what happens!

Have fun!

:)

1 Like

Thank you glv
indeed it works, just need two clarifications.
why the “delay” in the setep and why in the draw the line “if (x>100) x = 1”
On the other hand, how can I map my object with a jpg image?
than you

1 Like

Hello @Philippe1,

Experiment with by commenting out code to see what happens.
You should also look up the references to understand what it does.

Explore the resources and experiment away!

PShape tutorial:

References:

:)