Category: Code

After several requests I’ve taken the time to make unlekkerLib compatible with Processing 1.0. Apologies to anyone who’s been inconvenienced by the lack of a 1.0 version.

In particular, STL export should work again. However I haven’t had the chance to test the code very much, so please let me know if you find any bugs.

Download

No Comments »

Multiple JDIC browsers in Processing sketch

Multiple JDIC browsers integrated into Processing sketch

A while back I posted a simple hack to open a web browser from Processing by using JDesktop Integration Components (JDIC). A recent discussion on the Processing forums asked about how to use it to open multiple browsers inside the actual Processing sketch window.

My original hack used an instance of org.jdesktop.jdic.browser.WebBrowser integrated into a java.awt.Panel instance and laid out in a java.awt.Frame. That meant that the browser would open in an external window. The discussion on the Processing forum asked specifically about opening multiple browsers in the main sketch window, so I made the following quick hack.

Code - JDIC_multiple.pde

Read the rest of this entry »

4 Comments »

I’ve uploaded a new version of my unlekkerLib library. For some inexplicable reason version 0002 was missing some classes that were needed for certain examples to function. As a result the TileSaver class was broken, which was a serious omission.

You can download unlekkerLib-0003 from the Code & Form Google Code repository. I’ve added the missing classes, but otherwise the library is the same. If you still experience problems please report them here.

2 Comments »

Demo of Lee Byron\'s Mesh library

MeshLibDemo.pde - Demo of Lee Byron’s Mesh library

Lee Byron has written a neat little Processing library called Mesh which allows for easy calculation and display of Voronoi, Delaunay and Convex Hull diagrams.

Given a set of points, these diagrams calculate the minimal regions around the points (Voronoi), an optimal triangulation of the points (Delaunay) or the polygon shape that contains all the points (Convex Hull). So far the library only supports the 2D versions of the diagrams, but it is in part based on the QuickHull3D java library which also handles 3D hulls.

Byron didn’t include any code examples in the current release, so I hacked up a quick demo.

Code: MeshLibDemo.pde

To run this example, download MeshLibDemo.zip and unzip it inside your Processing sketches folder. The Mesh library is included in a “libraries” subfolder, but you’ll have to restart Processing for the library to be recognized.

I’m posting the full code below for easy reference.

Read the rest of this entry »

5 Comments »

I’ve decided to put my Processing hacks from the Code & hacks page and consolidate them into a downloadable archive. Thus the Code & Form code library is born. It will contain various demos and hacks, as well as example code for the unlekkerLib library. It should be much easier to publish code this way, since students etc. can simply download the archive and dump the contents in their sketchbook folder for quick access.

I’ve started a Google Code repository for this purpose, which seems a good way to go. I’m still figuring out SVN etc, hopefully I can switch to publishing the unlekkerLib source “live” through SVN once I figure it out. I’ll also publish archives of code written during past workshops to the repository.

There seems to be a growing list of Processing-related Google Code repositories out there, examples include Toxi’s Toxiclibs, interfascia and jddaniels. Do a search for processing.org to find more.

3 Comments »

The following Processing example shows how to set up a separate thread for loading images into a sketch. I wrote it up in response to this post on the Processing forums, figuring it will be useful to some of my students too.

Code: ThreadImageLoader.pde

Read the rest of this entry »

15 Comments »

Turns out Java has no mechanism for discovering the names of disk volumes. You can use java.io.File.listRoots() to get the root paths for multiple disks, but you can’t auto-discover their names.

Here’s a quick hack that will work on Windows. It uses Runtime.getRuntime().exec() to run “cmd /c dir “, the output of which is parsed to get the volume name.

import java.io.*; 

void setup() {
  println("Testing getVolName");

  String path="D:";
  println("Volume name for "+path+" is: "+
    "'"+getDiskVolumeName(path)+"'");
}

public static String getDiskVolumeName(String path) {
  String volname="Unknown";
  String check="Volume in drive "+path.charAt(0)+" is ";
  try  {
    Process p=Runtime.getRuntime().exec("cmd /c dir "+path);
    //p.waitFor(); 

    BufferedReader reader=new BufferedReader(
      new InputStreamReader(p.getInputStream()));
    String line=reader.readLine();
    while(line!=null) {
      if(line.indexOf(check)!=-1)
        volname=line.substring(line.indexOf(check)+check.length());

//      System.out.println(line);
      line=reader.readLine();
    }
  }
  catch(Exception e1) {
    println("Failure: "+e1.toString());
  }
  return volname;
}

No Comments »

080205_rhinoscript.jpg

Rhinoscript sketch, extruding a revolution surface along random curves. Good cheesy fun.

I had a chance to see a bit more of the impressive tool Rhino 4 during the Generator.x 2.0 workshop, and so I thought I’d have a go at making a simple sketch in Rhinoscript. As it turns out, the fact that Rhinoscript is based on VBScript makes coding feel horrible at first. Seriously, who would want to use syntax like that? It might be easy for beginners to pick up, but it quickly gets painful once you’re dealing with complex API calls and 100+ lines of code.

Nevertheless, frustration soon gives way to amazement at the built-in Rhino library and its vast array of heavy-duty functions for creating and manipulating curves, meshes and NURBS surfaces. In comparison, mesh generation in Processing is enough to give anyone a headache, and I seriously doubt anyone would even attempt to implement NURBS. Even Boolean mesh operations is a staggering task, with no good Java libraries readily available.

While Rhinoscript is firmly a non-realtime tool, its power for pure geometry is amazing. I would definitely use Rhino as a creative tool for digital fabrication projects, where animation is not the goal. There are some excellent RhinoScript resources online, for starters look at RhinoScript 101 and David Rutten’s tutorial. I would also definitely recommend using the Monkey Script editor instead of the built-in editor, it’s more powerful and has a very useful documentation feature.

The script below gives a basic idea of the Rhino syntax, and while it is a basic sketch suffering from 3D clichées, it shows the power and versatility of Rhinoscript. I just wish it wasn’t Visual Basic.

Code: RandRail.rvb

Read the rest of this entry »

6 Comments »

Simple example of how to generate meshes with Processing and then output them to STL using unlekkerLib.

import unlekker.util.*;
import unlekker.geom.*;
import unlekker.data.*;

import processing.opengl.*;

boolean doSTL=false;

void setup() {
  size(400,400, OPENGL);
}

void draw() {
  background(100);

  if(doSTL) {
    beginRaw("unlekker.data.STL","cyl.stl");
  }

  translate(width/2, height/2, 0);
  rotateY(radians(frameCount));
  rotateX(radians(frameCount));

  fill(255,255,255);

  for(int i=0; i<100; i++) {
    pushMatrix();
    translate(random(-300,300),random(-300,300),random(-300,300));
    rotateY(random(PI*2));
    rotateX(random(PI*2));
    cylinder(50,random(50,200));
    popMatrix();
  }

  if(doSTL) {
    endRaw();
    doSTL=false;
  }

}

void keyPressed() {
  if(key=='s') doSTL=true;
}

void cylinder(float w,float h) {
  float px,pz;

  beginShape(QUAD_STRIP);
  for(float i=0; i<13; i++) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,-h,pz);
    vertex(px,h,pz);
  }
  endShape();

  beginShape(TRIANGLE_FAN);
  vertex(0,-h,0);
  for(float i=12; i>-1; i--) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,-h,pz);
  }
  endShape();

  beginShape(TRIANGLE_FAN);
  vertex(0,h,0);
  for(float i=0; i<13; i++) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,h,pz);
  }
  endShape();
}

8 Comments »

The AHO students needed a simple file uploader that would automatically transfer files from a local folder to a web server. The following application will simply watch a given folder and upload any files it contains to the FTP server. Note that it will delete the local copy upon successful upload, so be careful how you use it.

Code: FTPUploader.pde

The code for the application is given below, but downloading the following ZIP will give you the required edtFTPj library files as well as a sample config file:

Read the rest of this entry »

2 Comments »