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 backspaces.net 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.

Slowcessing

The glue code is in slowcessing.py

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:

imagestrip.py

``` {: .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()
    self.append(p.loadImage(path))

def _get_images(self):
    for image in self._search:
        image.download(self._image_size, asynchronous=False)
        self._image_downloaded(image.path)

class WebTest (PJApplet): def setup(self): self.size(400, 400, self.P3D) self.images = ImageQueue(web.morguefile.search("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) ```

slowcessing.py

``` {: .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()
frame.add(panel)
panel.init()

while panel.defaultSize and not panel.finished:
    sleep(0.5)

frame.pack()
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'
sys.path.append(LIB_DIR)

```

Problems

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 :)

Processing...

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

Download

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 setup.py 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)