Importing 3D models into Processing
This was a fairly convoluted journey…. but I think I’ve managed to uncover a relatively simple method for importing 3D models into Processing… with help from Marius Watz of the excellent Code & Form: Computational Aesthetics blog via the “rapid prototyping” STL format.
Marius has provided a really useful selection of libraries, code snippets and workshop example sketches at his website and at the Code and Form code repository and has now also released his own unlekkerLib library – “a collection of utility classes that I use frequently, and which I’ve attempted to clean up enough for other people to use. Currently, the most significant features are the STL class for exporting 3D geometry for rapid prototyping and the TileSaver class for outputting high resolution stills from OpenGL applications.”
There’s not much documentation with the library – but there are a few examples – as well as additional notes Marius has made in the basic Javadocs reference that led me to the excellent open source mesh viewer MeshLab – and which turns out to be the way to convert from .3ds to .STL.
There’s an example sketch – unlekkerSTLReadWrite.pde – which once it’s written an .STL reads it back in. So I converted my .3ds model from the SketchUP 3D Warehouse to .STL using MeshLab – adapted the Processing sketch to load and display this file instead – and voila – it works – and with the minimum of code.
Apart from it using the
draw(this); method in the
void draw() function – and any
fill(); instructions in this loop override the whole sketch – it seems to work fine. I should possibly try using
redraw() – since I don’t actually need to see the models all the time?
I did learn some useful things in getting to this point though which I reckon are worth noting:
I googled for free 3D models and while I found some examples none of these collections were particularly substantial.
I use SketchUp Pro occasionally – it’s great for quick 3D mock-ups – and the 3D warehouse is a useful resource of models… so wondered if I could convert from these to other formats Processing could load. Having googled the article Converting SketchUp models to 3DS I downloaded the Mac version of FBXConverter as advised – but actually found that SketchUP Pro 7.1 exports directly to 3DS anyway – though FBXConverter came in useful later on when I was struggling to import OBJ files into Processing.
I tested a couple of the Import/Export libraries to load 3D models listed on the Processing site – both by Victor Martins at www.pixelnerve.com – and while I got both MRI3DS – “Load 3d studio .3ds files into your processing sketches.” & ObjImport – “This library imports .obj files and supports groups, material references, vertex-, normal-, and texture-coordinates.” to work with their own examples (MRI3DS required the NVIDIA Cg Toolkit – which installed easily enough) – and even my own .3ds files though I always got errors trying to load my own OBJ formatted files – they just seemed generally too complex and code heavy for my purposes. Also I tried to add functionality to these example sketches using PeasyCam – a mouse driven camera-control library for 3D sketches – and the older Arcball – a virtual 3D trackball interface – found in the Processsing:hacks section – but without success.
I then looked at Jonathan Feinberg’s Patchy v0.30 ( with its clone github social coding site) – an easy-to-use bicubic patch for 3D graphics in the Processing programming environment… which looks really interesting for controlling meshes… and one example displays a 3D model of a teapot – which turns out (I had to look inside the .jar file to find it) to be in the .bpt or Bézier Patch File Format – a simple structured text format for 3D models. But although I found some additional .bpt models I couldn’t see how to load an external file into Processing using Patchy… and with precious few google references and no tools to view that I could find .bpt seems to be a fairly marginal format – although I did find ways to convert .bpt to .OFF.
This format seemed a little more accessible with viewers such as Ryan Holmes’ RoffView and his fuller featured MeshMan 3D Mesh Manipulator – but with these either requiring compiling or being PC based they’re not really useful for me.
So I decided importing 3D models was probably too complex for my needs… but since I’m familiar with importing SVG into Processing I wondered whether this vector based format could include z-axis data. I even found a couple of 3D SVG examples – Sphere_wireframe.svg & Water molecule 3D.svg at Wikimedia Commons but I quickly realised these actually weren’t 3D – just 2D representations of 3D shapes – shame. I also followed some old links to SVG Importer by Michael Chang – hoping I might find some way to extend Processing’s currently limited core SVG handling – only to discover that Michael Chang subsequently worked on Candy which in turn became Processing’s core SVG handling… ho hum.
I decided to look at creating simple 3D shapes within Processing – after all I only need these to represent cameras and lights for my virtual studio rig – and so started to search for Processing beginShape() examples… which led me to the Code & Form: Computational Aesthetics…