Processing with Jython and Nodebox/Shoebot libraries

Wed 10 February 2010
By stu

Update: 26/April/2010

Problems I was having with incomplete images have been fixed in the current version of the web library, available in shoebots mecurial repository.

Using Processing from Jython is a promising idea, so I took the base from this post on where they explained how to use Jython and built on it a little.

This is great as Shoebot/Nodebox have great libraries for data manipulation, while processing is more focused on graphics.

{: .alignnone .size-medium .wp-image-235 width="300" height="203"}

The result is the attached Netbeans project which demonstrates using the nodebox web library and  drawing with processing.


The glue code is in

Theres a special version of PApplet (PJApplet), and 'pj_frame' which can put this in a JFrame.

The other method is 'shoebot_imports' adds the shoebot imports to the library path

In case anybody doesn't want to download the whole project, heres the code:

``` {: .brush:python} from slowcessing import PJApplet, pj_frame, shoebot_imports from processing.opengl import *

shoebot_imports() import web import thread

class ImageQueue(list): """ Download images in the background and add them to a list """ def init(self, search, size): list.init(self) self._search = search self._image_size = size thread.start_new_thread(self._get_images, ())

def _image_downloaded(self, path):
    p = PJApplet()

def _get_images(self):
    for image in self._search:, asynchronous=False)

class WebTest (PJApplet): def setup(self): self.size(400, 400, self.P3D) self.images = ImageQueue("sweets", max=1), size='small')

def draw(self): self.background(0); y = (self.height * 0.2) - self.mouseY * (len(self.images) * 0.58) for image in self.images: self.image(image, 20, y) y += image.height

if name == 'main': pj_frame(WebTest) ```

``` {: .brush:python} from javax.swing import JFrame

from processing.core import PApplet

class PJApplet(PApplet): # rqd due to PApplet's using frameRate and frameRate(n) etc. def getField(self, name): return PApplet.getDeclaredField(name).get(self)

def pj_frame(pj_applet, **kwargs): from time import sleep

frame = JFrame(kwargs.get('title', 'Slowcessing'))
frame.defaultCloseOperation = kwargs.get('defaultCloseOperation', JFrame.EXIT_ON_CLOSE)
frame.resizable = kwargs.get('resizable', False)

panel = pj_applet()

while panel.defaultSize and not panel.finished:

frame.visible = 1

return frame

def shoebot_imports(): """ Allow import of the shoebot libraries """ ##APP = 'shoebot' import sys DIR = sys.prefix + '/share/shoebot/locale' ##locale.setlocale(locale.LC_ALL, '') ##gettext.bindtextdomain(APP, DIR) ###gettext.bindtextdomain(APP) ##gettext.textdomain(APP) ##_ = gettext.gettext

LIB_DIR = sys.prefix + '/share/shoebot/lib'



There are some things I couldn't work :

The callback to say that images have been downloaded happens before the whole file is available, for this reason there are grey parts on the images on the first run.

Nodebox web...

While I did manage to fix things to get this working in Jython and get Morguefile working, I had a lot of trouble understanding what was going on here.

Cheers to Tom De Smedt for fixing these the areas of nodebox-web that I couldn't :)


Some parts of PApplet to do with image loading seem to be static, which may also explain problems I was getting with reentrancy.


If you want to have a go, you'll need to:

Install Netbeans 6.8

Install Jython (2.5 or higher) by installing the Netbeans python module

Add python to the path (if using Netbeans it's copy is where Netbeans is installed).

Get nodebox-web by downloading shoebot and install it with:

jython install

In Netbeans, add all the jars in the processing\lib folder to the Jython classpath, and opengl\library\opengl.jar

{: .alignnone .size-medium .wp-image-236 width="300" height="219"}

Download the PythonOnProcessing (tested on Netbeans 6.8)