Exporting
The command
saveFrame("file_name-####.png); if put inside the
draw() method will save an image each frame.
The four hashmark symbols, ####, show you where the numbers will appear in the file name.You can also specify a subfolder to save the images into, which is helpful when working with many image frames:
saveFrame("frames/file_name-####.png);
Processing will write an image based on the file extension that you use (
.png,
.jpg, or
.tif are all built in, and some platforms may support others).
If your output is vector graphics, you can write the output to
PDF files for higher resolution. The
PDF Export library makes it possible to write PDF files directly from a sketch. These vector graphics files can be scaled to any size without losing resolution, which makes them ideal for print output.
Exporting 3D Forms
To export your sketches as STL files, you need the
modelbuilder library and you need to use the
beginRaw() and
endRaw() functions.
If you include
#### in the filename, separate numbered stl files will be made each time that frame is rendered:
beginRaw("unlekker.data.STL","shape-####.stl");
import unlekker.util.*;
import unlekker.data.*;
import unlekker.modelbuilder.*;
import ec.util.*;
int i=0;
void setup() {
size(100,100, P3D);
}
void draw() {
background(0);
lights();
noStroke();
pushMatrix();
translate(width/2, height/2,0);
rotateY(map(mouseX, 0, width, -PI,PI));
rotateX(map(mouseY, 0, height, PI,-PI));
box(20);
popMatrix();
}
void keyPressed() {
if (key == 'e') {
i++;
// Initialize STL output
beginRaw("unlekker.data.STL","shape"+i+".stl");
//or
// beginRaw("unlekker.data.STL","shape-####.stl");
noStroke();
pushMatrix();
translate(width/2, height/2,0);
rotateY(0);
rotateX(0);
box(20);
popMatrix();
endRaw();
}
}
In this next example:
Press 1 for a cone, 2 for a cylinder, 3 for a pyramid, and e to export as STL:
import unlekker.util.*;
import unlekker.data.*;
import unlekker.modelbuilder.*;
import ec.util.*;
void setup() {
size(400,400, P3D);
}
int i=0;
int drawShape=1;
boolean imgmode = true;
boolean rot =false;
void draw() {
background(0);
lights();
translate(width / 2, height / 2);
float rotY=map(mouseX, 0, width, -PI, PI);
noStroke();
fill(255, 255, 255);
// Draw a mix between a cylinder and a cone
switch(drawShape) {
case 1:
drawCylinder(10, 30, 30, 36);
break;
case 2:
drawCylinder(10, 10, 30, 64); // Draw a cylinder
break;
case 3:
drawCylinder(0, 30, 30, 4); // Draw a pyramid
break;
}
}
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
// If it is not a cone, draw the circular top cap
if (topRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, 0, 0);
for (int i = 0; i <sides + 1; i++) {
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
// If it is not a cone, draw the circular bottom cap
if (bottomRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, tall, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
}
void keyPressed() {
if(key=='1') {
drawShape=1;
}
else if(key=='2') {
drawShape=2;
}
else if (key=='3'){
drawShape=3;
}
else if (key == 'e') {
i++;
beginRaw("unlekker.data.STL","cylinder"+i+".stl");
rotateY(-PI/2);
rotateZ(PI/2);
switch(drawShape) {
case 1:
drawCylinder(10, 30, 30, 36);
break;
case 2:
drawCylinder(10, 10, 30, 64); // Draw a cylinder
break;
case 3:
drawCylinder(0, 30, 30, 4); // Draw a pyramid
break;
}
endRaw();
}
}