HOWTO/Python Module

From Shoebot Wiki

This code is in the process of being reorganised—here’s the rough and dirty way to achieve it right now:

 import shoebot
 from shoebot.core import CairoCanvas, CairoImageSink, NodeBot
 
 outputfile = 'output.svg'
 sink = CairoImageSink(outputfile, "svg", multifile = False)
 canvas = CairoCanvas(sink, enable_cairo_queue=True)
 
 bot = shoebot.core.NodeBot(canvas)
 
 bot.size(150,150)
 bot.fill(1,0,0)
 bot.rect(10,10,100,100)
 
 bot._canvas.flush(frame=0)
 
 bot.fill(0,0.5,0)
 bot.rect(30,30,30,30)
 
 sink.filename = 'output2.svg'
 bot._canvas.flush(frame=0)

( http://pastebin.com/MpZmpvnT )

_____

Shoebot can also be loaded as a module.

After including an import statement,

 import shoebot

a NodeBot object needs to be created, and all further drawing commands can be called on that instance. ‘NodeBot’ implements the Nodebox vocabulary. You can also use DrawBot to use the Drawbot commands.

NodeBot should be called with the output file name as an argument. The first call after that one has to be size(), otherwise you’ll hear Shoebot complaining.

 bot = shoebot.NodeBot(targetfilename="output.svg")
 
 # set size to 150x150
 bot.size(150,150)
 
 # draw a rectangle
 bot.fill(1,0,0)
 bot.rect(10,10,100,100)

When you’re finished with drawing, just call

 bot.finish()

and your output file should be created.

Also, you can save snapshots of the current state of the NodeBot instance like so:

 bot.snapshot("snap.png")

You can also create SVG, PS or PDF snapshots — just specify a filename with the proper extension (.svg, .ps or .pdf).

You can even call external Shoebot/Nodebox scripts from your Python script:

 bot.run('examples/primitives.bot')