Processing with Jython and Eclipse

I am always looking for a reason to use Python more often, especially in situations where I’ll need to prototype rapidly, as is often the case when tasked with programming graphics. So, when a client proposed that I use Processing (Java) to build an up-coming project, I immediately thought it would be worth my time to investigate the possibility of writing a Processing app with the Java implementation of Python called Jython. In this post I’ll go through the steps I took to set up a development environment to facilitate this.

Note: I’m working on a Mac and have not tested this on a Windows machine. If anyone attempts to do so based on these instructions please send feedback or leave a comment. Also, this is a work in progress. I have not been able to test all of the features of Processing yet. Please see the caveat at the bottom of this page if you are expecting this to work seamlessly. Lastly, I am aware of the existing pyprocessing project available on Github. It is sweet and seems to work well. If you want to dive right into to using Jython with Processing, check it out. This post is more of a reminder to myself and anyone else of how to set it all up from scratch.

Setting it all up basically involves installing some dependencies and linking them together.

  • Jython – This the Java implementation of Python. Just download the installer and double click it. There are a couple of prompts but the most important one to take note of is where Jython is being installed. You can install it anywhere on your system. Mine, for example, is installed at:
    /Users/makemachine/development/libs/jython/2.5.2/

    Once Jython is installed, you can add it to your path and test it by typing jython into the Terminal. If all is well, you should see the familiar Python interactive shell, however it will actually be the Jython interactive shell. My .bash_profile looks something like this:

    PATH="/Users/makemachine/development/libs/jython/2.5.2/bin:${PATH}"
    export PATH
    
  • Eclipse – If you are not familiar with Eclipse, it is an IDE ( integrated development environment ) that allows you to run, launch, debug and profile code for many differently languages. I installed the “Eclipse IDE for Java Developers” version, which I believe is a fairly basic version of Eclipse.
  • PyDev – PyDev is a plugin for Eclipse that enables some really nice features for writing Python in Eclipse. It can be installed in a couple of different ways. I found that just using Eclipse’s built in plugin manager worked well. Once installed you should restart Eclipse. One way to verify that PyDev is installed is to go to Eclipse » Preferences and look for PyDev in the right hand menu.
  • Processing – Before you can get to writing a Processing app, you will need to actually download Processing. Once it is downloaded, copy it to your Applications directory. I decided to make a copy of the contents of the Processing.app file and save them to another location on my computer. I did this just so I would have the files sand-boxed so that in the event of something breaking, Processing will still work as normal. To do this, right-click the Processing app, choose Show Package Contents, navigate to Contents » Resources » Java. Copy and paste this Java folder somewhere on your system. I placed mine at:
    /Users/makemachine/development/libs/processing

Now that everything is installed you can start a project:

  • Open Eclipse
  • Choose Window » Open Perspective » Other and choose PyDev
  • Right-click in the PyDev Package Explore on the left side of the screen
  • Choose New » Project
  • Choose PyDev Project from the menu
  • Set a name for your project and the place it should live on your computer
  • Choose “Jython” for project type
  • If this is the first time you’ve used PyDev and Jython in Eclipse you will need to set the path to Jython
    • Click “Configure an interpreter”
    • Under Jython interpreters click the “New” button and browse to the location of the jython.jar file in the top level dir where you saved Jython in the first step.
    • Click “Ok”
  • Click “Finish”
  • Next you need to link the Processing files to the project
  • Right-click the Project file in the PyDev Package Explorer and choose “Properties”
  • Under PyDev – PYTHONPATH, choose the External Libraries tab
  • Click the “Add zip/jar/egg” button
  • Navigate to the location where you saved the Processing libraries and select each of the following .jar files
    • core.jar
    • modes/java/libraries/java/opengl/library/gluegen-rt.jar
    • modes/java/libraries/java/opengl/library/jogl.jar
    • modes/java/libraries/java/opengl/library/opengl.jar
  • Click “Ok”
  • Right click the “src” folder in your project and choose New » PyDev module, name it main.py ( or whatever you want )
  • Choose “Module:Main” form the list
  • Click “Ok”

Now you should be able to run compile and run a Processing app! Here is some boilerplate code.

from processing.core import PApplet

class JythonProcessingApp( PApplet ):
    def setup(self):
        self.size(960, 540)
        pass
    
    def update(self, n):
        
        pass
    
    def draw(self):
        self.background(0)
        pass

if __name__ == '__main__':
    PApplet.runSketch( ['A Jython Processing App'], JythonProcessingApp() )

To run it, with main.py open in the editor, click the little green button with a white arrow in the toolbar at the top left of the screen. If everything is working you should see a Processing app open. Right now it is not doing anything except redrawing a black screen on every frame, but that is a good start!

If you want to use Processing in the OPENGL mode there is one last step. You must add the path to the native OpenGL libraries. This is pretty trivial but took me quite a while to figure it out.

  • Click the little down arrow next to the green run button and choose “Run Configurations”
  • Under “Jython Run”, choose your project
  • Selecte the “(x)=Arguments” tab
  • In the VM Arguments box add this:
    -Djava.library.path=YOUR PATH TO PROCESSING LIBS/Java/modes/java/libraries/opengl/library/macosx/

CAVEAT:

This will allow you to run Processing apps in OPENGL mode. That said, I have run into an issue where certain OPENGL commands seem to be failing. Hence, there is more work to be done.

Looking at the instructions above, this seems like a daunting task, but I assure you, that it is nothing more than installing a few things and understanding how to link them together.

Posted in Java, Processing, Python | 4 Comments

4 Responses to Processing with Jython and Eclipse

  1. Matthias says:

    Thanks a lot for your nice post, Jeremy.
    Python is also my favorite programming language.
    I give your recipe a try and finally it worked.
    However, I stumbled over the update() method for a more sophisticated example. The empty update() leaves the sketch’s window dark on my windows pc, removing it solves the problem. In addition, adding a noLoop() in the setup() for a static sketch is a good idea.

  2. Jeremy says:

    Hi Matthias,
    I’m glad to hear that you got this working. Looking forward to seeing where you take it!

  3. Jeramy says:

    This is working so far, but is there any way to import processing libraries?

  4. Jeremy says:

    As far as I know, you should be able to import and use other Java libraries, as long as they are linked up properly in the IDE.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>