Using pymunk (physics engine) in NodeBox

NodeBox makes a great environment for data visualizations and generative art. It’s easy to get started in, and you get basic drawing, type, and image manipulation. When you’re ready for more, it’s not too difficult to bring in external Python libraries to connect NodeBox to other systems, or add physics and particle simulation to spice up your visuals.

For those unfamiliar with NodeBox,

“NodeBox is a Mac OS X application that lets you create 2D visuals (static, animated or interactive) using Python programming code and export them as a PDF or a QuickTime movie.”

It uses PyObjC to embed a Python runtime into an OS X native application, and fits into the same toolbox as Processing and openFrameworks. It’s a bit slower to run complex animations in, but you’re coding in Python, you get the gorgeous fonts and anti-aliasing you’d expect on OS X, and it provides easy access to some OS X native libraries, like Core Image. For a quick look at what it can do, check out the NodeBox gallery.

NodeBox includes it’s own Python build, which is nice for portability and reliability, but it uses a custom sys.path that doesn’t look for Python packages you might already have installed on your system. There are a few ways to deal with this:

  1. You can install your packages into NodeBox’s path, ie., ~/Library/Application\ Support/NodeBox/ — meaning that you can use them from NodeBox, but not from other scripts…
  2. You can import sys in your NodeBox code and manually modify the sys.path value to add your existing packages…
  3. You can install packages into your system site-packages directory, and sym-link them from NodeBox’s directory…
  4. You can make NodeBox use your system packages instead of it’s own by sym-linking ~/Library/Application\ Support/NodeBox to your site-packages directory of choice (ex., /Library/Python/2.5/site-packages)

For this exercise, I’ll be adding pymunk (Python bindings for the Chipmonk physics library) to NodeBox using option #3: Installing pymunk globally, and sym-linking from NodeBox’s package directory. This allows me to run the pymunk examples from the command-line (which use PyGame and Pyglet), but still use pymunk from NodeBox. This may not always be the best solution, so you’ll have to pick what’s right for your needs.

Let’s get started.

Pymunk (at the time of writing) includes it’s own copy of the Chipmunk source code, making this whole process rather easy. Once you’ve downloaded and uncompressed the pymunk source, cd into it’s directory and build chipmunk using:

> python setup.py build_chipmunk

Now you can build and install pymunk:

> python setup.py install

This will install an egg (which I normally hate dealing with, but that’s another story.) If you don’t want the egg, just copy the pymunk directory into your site-packages.

Now we’ll add pymunk to NodeBox’s path. My pymunk is in /Library/Python/2.5/site-packages/, so I’ll:

> cd ~/Library/Application\ Support/NodeBox

> ln -s /Library/Python/2.5/site-packages/pymunk-0.8.1-py2.5.egg/pymunk .

Finally, NodeBox needs access to libchipmunk. I used this approach:

> cd /Applications/NodeBox/NodeBox.app/Contents/MacOS/

> ln -s ~/Library/Application\ Support/NodeBox/pymunk/libchipmunk.dylib .

We should be done! Fire up NodeBox and try an include pymunk to see if it loads. If you don’t see any error messages, you’re good to go.

If you’re new to pymunk (as I was until this week), head over to the Slide and Pin Joint tutorial to see how it works. The example is written for PyGame, so you’ll be doing a little rewriting to bring it into NodeBox.

The following screenshot shows the Slide and Pin Joint demo within NodeBox using my take on porting it. I’m having a little trouble with the slide joint, but you can check out my code if you’re curious: slide_and_pinjoint_example.py

pymunk in nodebox screenshot

10 thoughts on “Using pymunk (physics engine) in NodeBox”

  1. Thank you! Very interesting, but nodebox saying this to me:

    — START —
    Loading chipmunk for Darwin (32bit) [/Applications/NodeBox/NodeBox.app/Contents/MacOS/libchipmunk.dylib]
    Traceback (most recent call last):
    File “nodebox/gui/mac/__init__.pyo”, line 358, in _execScript
    File “”, line 8, in
    File “/Library/Python/2.5/site-packages/pymunk-0.8.1-py2.5.egg/pymunk/__init__.py”, line 14, in
    File “/Library/Python/2.5/site-packages/pymunk-0.8.1-py2.5.egg/pymunk/_chipmunk.py”, line 8, in
    File “/Library/Python/2.5/site-packages/pymunk-0.8.1-py2.5.egg/pymunk/libload.py”, line 39, in load_library
    File “/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py”, line 423, in LoadLibrary
    File “/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py”, line 340, in __init__
    OSError: dlopen(/Applications/NodeBox/NodeBox.app/Contents/MacOS/libchipmunk.dylib, 6): image not found

  2. Hi sonomute, sorry to hear that it’s not working. First thing to try would be ensuring that the chipmunk/pymunk build is working on it’s own. In the pymunk source directory, there’s a directory called ‘examples’. Do those examples run? The ‘demo_slide_and_pinjoint.py’ example would be a good reference, since that’s the one I’ve somewhat reimplemented in NodeBox.

    I get a warning about a deprecated use of NSQuickDrawView when I run demo_slide_and_pinjoint.py from the command-line, but it does work.

    The box2d_* demos all run for me fine as well; However, the pymunx_demo*’s all crash because of a missing font.

    1. Cool! Looks it was just a matter of removing the global ‘width’ and ‘height’ variables, and hard-coding the rendering dimensions. Using ‘global’ always feels dirty anyway…

  3. Thanks for sharing that. I really would like to invoke python-fontforge from Nodebox. following your steps I get a message like: SystemError: Missing library: libgunicode

    1. I haven’t used fontforge, but it does look a little more complex to drop-in, given it’s other dependencies. I tried running some quick tests, but ran into the not-too-uncommon 32 vs. 64-bit build issues (on Snow Leopard), and decided against digging deeper.

      Depending on how far you want to take it, you could try going into Nodebox.app and swapping out the python interpreter to same interpreter you used when building fontforge; or building NodeBox from source (see: http://dev.nodebox.net/wiki/Build ), and see if you can link up your libraries that way. (Since you can likely do a full 64-bit build.)

      I realize this isn’t the most helpful answer, but ever since the Snow Leopard update, linking new libraries into older code has become more of a pain.

  4. I tried this in shoebot just now and the slide ends up spinning super fast, any idea why this might be?

    – Maybe some change in pymunk ?

  5. Hi Stu, I haven’t played with Shoebot, and haven’t looked at this code in over a year, so the short answer is that I have no idea. It’s entirely possible that Showbot simple refreshes the graphics canvas faster than Nodebox (which is rather slow at drawing.) You might just tweak the gravity / mass / etc., until you get the behavior you’d like.

  6. Hi,
    3 years later :)

    If anyone wants to use this example in 2013, just remove line 61 with init_pymunk on it.

    Cheers.
    Stuart

Comments are closed.