backspaces
Junior Member
Offline
Posts: 66
Re: LiveCode
Reply #35 - Nov 25th , 2006, 7:20pm
(Sorry to spam the list, but I thought some folks could use this. PLEASE let me know if you can improve this .. I plan to put it on my web site as a mini-tutorial.) Here's the deal: I sometimes want to use Processing in an interpretive environment. For example, I'm studying transforms and the camera, and being able to manipulate the camera in an interpreted environment is way nice! I tried beanshell, but ran into trouble with using P3D. I've got a message into their list to see if I just made a dumb mistake. But in the mean time, I decided to use Jython: its so clean and uncluttered, and is used quite a bit by my co-workers at redfish.com. My solution was to build a simple Jython package, proctools, which has a single procedure which creates a JFrame with a PApplet. The procedure takes arguments for the window size, a 2D/3D flag, and a frameRate number .. where "0" means calling noLoop() .. letting you call redraw() in the Jython session instead. The PApplet has a no-op draw() method initially. You simply replace that method within your interactive session. The code is here, and also at the end of this message if you'd prefer to cut/paste: http://backspaces.net/files/proctools.py To use proctools, download the file to a convenient directory. I prefer starting with an empty one, because Jython will create a few files of its own. You can also add data to this directory such as fonts and images etc. Before you start jython, make sure you've set the CLASSPATH: export CLASSPATH=~/local/processing/lib/core.jar:. Then start jython, importing proctools. In the following sample session, I build a simple scene for camera navigation experiments. Note that I've included Arial-Black-24.vlw in the directory I'm using: from proctools import startProcessing p = startProcessing(600,400,True,2) p.textFont(p.loadFont("Arial-Black-24.vlw")) def drawScene(p): p.background(200) p.noStroke() p.fill(200,0,0) p.sphere(50) p.stroke(0) p.fill(255, 255, 255, 200) p.rect(-200,-200,400,400) len = p.width/2 p.line(0,0,0,len,0,0) p.line(0,0,0,0,len,0) p.line(0,0,0,0,0,len) p.fill(255,0,0) p.text("X",len,0,0) p.text("Y",0,len,0) p.text("Z",0,0,len) p.drawProc = drawScene len = p.width*.75 p.camera(len,len,len, 0,0,0, 0,0,-1) .. and so on.. Let me know if this works for you, and any improvements you spot .. this is VERY raw, and I'm definitely a Python newbie. I'd love to figure out how to avoid the p.xxx everywhere, for example .. like javascript's "with" statement. Owen The code for proctools.py: # Enable use of processing and local classes (such as proxies) and packages. # export CLASSPATH=~/local/processing/lib/core.jar:. from javax.swing import JFrame from processing.core import PApplet class Processing(PApplet): def __init__(self, w, h, use3D, rate): self.w = w self.h = h self.use3D = use3D self.rate = rate self.drawProc = None def setup(self): if self.use3D: self.size(self.w, self.h, self.P3D) # JAVA2D P3D OK else: self.size(self.w, self.h, self.JAVA2D) # JAVA2D P3D OK if self.rate==0: self.noLoop() else: self.frameRate(2) def draw(self): if self.drawProc == None: pass else: self.drawProc(self) def startProcessing(width, height, use3D, rate): p = Processing(width, height, use3D, rate) frame = JFrame(title="Processing", resizable = 0, defaultCloseOperation=JFrame.EXIT_ON_CLOSE) #p.frame = frame; p.init() while p.defaultSize and not p.finished: pass frame.add(p) frame.pack() frame.visible = 1 return p