Export A 3D Object From Processing To Blender

TL;DR

This sketch code exports a 3D object from Processing that you can import into Blender


import nervoussystem.obj.*;

void setup() {
  size(200,200, P3D);
  background(0);
  lights();
  beginRecord("nervoussystem.obj.OBJExport", "cubes.obj");
  for(int x=0; x<50; x++) {
    pushMatrix();
    translate(random(10, 190), random(10, 190), random(-100, 0));
    noStroke();
    box(10);
    popMatrix();
  }
  endRecord();
}

void draw() {}

(You'll need the OBJExport library installed to run the process)

Importing Into Blender

The example above exports a file called cubes.obj. The steps to import it into Blender are:

  1. Open a new project in Blender
  2. Click "File" in the main menu bar
  3. Click "Import"
  4. Select "Wavefront (.obj)"
  5. Navigate to the "cubes.obj" file
  6. Click "Import OBJ"
  7. Zoom way out to find the object

When I import the object it's pretty huge and way off center. Hence the last step to zoom way out. The center point of the object is the world origin. So, another option is to scale down and eventually you'll see it there.

If you figure out a way to keep it centered, let me know.

In the mean time, have fun using the apps together.

~a

Notes

  • These are notes from my setup using Processing 4.0.1 and Blender 3.2.0 on macOS 12.2.1. Give it a shot if you have something different and see what happens

  • Credit to funprogramming.org for the videos that got me started. Their example uses an image for the primary source to make the cubes. It's very cool but adds extra steps. I switched to the random cubes in an effort to simplify, but you should totally go check theirs out too for the image. I use a modified approach of it for some of my pieces

  • OBJExport hasn't been updated in a decade, but it's working great for me

  • You have to save the sketch file before running the sketch code so Processing knows where it is to output the file in the same directory. (You could also use a full path)

  • Two other Processing 3D libraries to investigate are "HE_Mesh", and "toxiclibscore". Probably there are other libraries as well, but those two were mentioned on funprogramming.org

  • The beginRecord() and endRecord() are in the setup() function because that only runs one time as opposed to the draw() function which runs continously. If you put it in draw() you have to setup something to trigger the capture. See the example on the home page)

  • Only a single object is imported in to Blender. You'll have to split things out if you want to work with them individually. You can do that by switching to Edit Mode, using l to select a single cube, pressing p to bring up the Separate menu, then clicking Selection

References