PDF of P3D Processing sketch

I’m finding it difficult to create a PDF from the PDF library when my sketch specifies P2D or P3D. What am I doing wrong?

import processing.pdf.*;

boolean record;

void setup(){
  size(1500,800,P3D);
  frameRate(1);
  background(#FF7605);
  
  stroke(2);
  strokeWeight(1);
  smooth(8);
  fill(#081EFF);
  float xstart = random(10);
  float ynoise = random(10);
  translate (width/2, height/2, 0);
  for (float y =-(height/8); y <= (height/8); y+=3){
    ynoise += 0.02;
  float xnoise = xstart;
  for (float x = -(width/8); x <= (width/8); x+=3){
    xnoise += 0.02;
    drawPoint(x,y,noise(xnoise,ynoise));
    }
  }
}

void draw(){
  if (record){
    beginRaw(PDF, "output.pdf");
  }

void drawPoint(float x, float y, float noiseFactor){
  pushMatrix();
  translate (x * noiseFactor * 6, y * noiseFactor * 6, y);
  float edgeSize = noiseFactor *26;
  ellipse(0,0, edgeSize, edgeSize);
  popMatrix();
  
  if (record){
    endRaw();
    record = false;
    }
    
    // hit 'p' to record a single frame
    void keyPressed(){
      if(key == 'p'){
        record = true;
      }
  }

}

PDF Files from 3D Geometry (With Screen Display)

To create vectors from 3D data, use the beginRaw() and endRaw() commands. These commands will grab the shape data just before it is rendered to the screen. At this stage, your entire scene is nothing but a long list of lines and triangles. This means that a shape created with sphere() method will be made up of hundreds of triangles, rather than a single object.

When using beginRaw() and endRaw(), it’s possible to write to either a 2D or 3D renderer. For instance, beginRaw() with the PDF library will write the geometry as flattened triangles and lines.

Hello @asymmetric ,

As per the examples in the library reference you need to do all the drawing between beginRaw() and endRaw().

In your code you are drawing in setup() before beginRaw().

You have a function inside the draw() loop; this should be outside of draw() loop and called within the draw() loop.

You will need to also correct your opening and closing brackets in your code; there are a few errors for you to correct.

Here is a simple working example:

import processing.pdf.*;

boolean record;

int seed;

void setup() 
  {
  size(400, 400, P3D);
  noLoop();
  }

//******************************************

void draw() 
  {
  if (record) 
    {
    beginRaw(PDF, "output.pdf");
    }

  randomSeed(seed);

  // Do all your drawing here This could be a function call!
  background(204);
  
  stroke(0);
  fill(0, 255, 0);
  
  for(int i=0; i<200; i++)
    {
    push();
    translate(random(width), random(height), -100);
    circle(0, 0, 30);
    pop();
    }
  
  if (record) 
    {
    endRaw();
    record = false;
    }
  }

//******************************************

// Hit 'r' to record a single frame
void keyPressed()
  {
  if (key == 'r') 
    {
    record = true;
    redraw();
    }
    
  if (key == 's') 
    {
    seed = int(random(0, 2000));
    redraw();
    }    
  }

I generated a randomSeed() so I could capture what was generated in the current sketch window.

References:

:)

1 Like

I was just about to try your solution!

I’m having a hard time putting the above conditional statement in the correct place… does it belong in the drawing block of code as well?

The answer is in the references:

1 Like

Aha! My code compiled but I don’t see the PDF anywhere in my sketch file?

Even when I press ‘r’ :frowning:

@glv IT WORKS!! BUT… The background is all of sudden white not black? How do I change it back to black?

@glv what am I doing wrong?

Hi @glv, I am still having issues with the PDF exporting P3D processing files… the PDF files have a white background instead of black, but I can see an off-white rendering of the image wireframe… how do I change this back to black?

I am not an expert on this library.

Searching the forum I found this:

You could also go here and do a search you will get more results:
https://processing.org/

If you provide a MCVE the community can run your code and may be able to help. An MCVE is mentioned in Guidelines - Asking Questions.

You can still get some nice effects without using P2D or P3D and get your background.

This is from the PDF:

Remove anything that has a z component from code:

translate (x * noiseFactor * 6, y * noiseFactor * 6);

You may lose some perspective but it seems you are modifying size of ellipse already.

:)

Is there another way to save higher resolution images of P3D Processing sketches?

Hi @glv, so I figured out an alternative way to go about putting the background back after converting my P3D Processing sketch to a PDF file. That alternative way is to add a background layer in Photoshop because the image turns into a vector image with a transparent background when you export your P3D file to a PDF. This way I can get the high resolution vector lines!

1 Like