Using NodeBox in another application
The NodeBox core is now split off from the UI functionality, so you can use it in any project. There are a few different ways of using NodeBox.
1. The easy way: from NodeBox itself
One of the new features in NodeBox is that you can access the canvas object. It's a list-like structure, containing all your graphical objects ("grobs"). The canvas also has a "save" method, so anywhere in your script you can do:
And it will save what it has drawn up until that point. Do it at the end of your script, and you get the full output.
After saving, you might want to clear out the canvas. You can:
This will remove all grobs already on the canvas; they will not show up in the render.
2. Installing the NodeBox module
If you want to use NodeBox from the command line, you will have to install it. We currently recommend using Subversion to grab a copy:
svn co http://dev.nodebox.net/svn/nodebox/trunk/ nodebox cd libs/Numeric python setup.py install cd ../.. cd libs/pathmatics python setup.py install cd ../.. cd libs/polymagic python setup.py install cd ../.. cd src python setup.py install
The only outside dependency is PyObjC. We recommend the latest version from Subversion.
3. Using the console runner
When you have a set of NodeBox scripts that you want to run to generate images or a movie, use the following commands:
from nodebox import console source = "size(200,200)\nrect(random(WIDTH), random(HEIGHT), 20, 20)" console.make_image(source, "rect.png") console.make_movie(source, "rect.mov", 20, fps=30)
Currently, there is no way to run the console from the command line, but we're working on that.
4. Accessing the context directly
If you want to have direct access to NodeBox's drawing context, you can import everything directly. The API remains the same, but you have to prefix everything with the context. Note also the initialization of Cocoa, necessary for using the API.
from AppKit import NSApplication NSApplication.sharedApplication().activateIgnoringOtherApps_(0) from nodebox.graphics import Context from nodebox.util import random, choice, grid, files ctx = Context() ctx.size(200, 200) ctx.rect(random(ctx.WIDTH), random(ctx.HEIGHT), 100, 100) ctx.save("test.png")
You can also make movies this way:
from AppKit import NSApplication NSApplication.sharedApplication().activateIgnoringOtherApps_(0) from nodebox.graphics import Context from nodebox.util import random, choice, grid, files from nodebox.util.QTSupport import Movie movie = Movie("rects.mov") for i in range(10): ctx = Context() ctx.size(200, 200) ctx.rect(random(ctx.WIDTH), random(ctx.HEIGHT), 100, 100) movie.add(ctx) movie.save()
5. Drawing in a Cocoa drawRect method
If you want to implement your existing drawing using NodeBox, you can do the following in your class's drawRect: method:
def drawRect_(self, rect): from nodebox.graphics import Context from nodebox.util import random ctx.size(*rect) ctx.rect(random(ctx.WIDTH), random(ctx.HEIGHT), 100, 100) ctx.canvas.draw()
Note that creating the Context and drawing it are two separate steps:
- First, the context is created; all drawing commands are translated into graphic objects ("grobs") that are stored on the canvas.
- Then, the canvas is drawn; each grob now renders itself on the current Cocoa graphics context.
This also means you can cache the context (or the canvas) and use it in a drawRect method during a later stage.include("util/comment.php"); ?>