Monday, 9 June 2014

Testing recursion limits python mode in the processing ide

Python is well known for not dealing with recursion too well, it seems python mode in the processing mode is even worse:-

   1 """
   2 spiral.pyde python mode sketch
   3 features fairly extreme recursion for
   4 python hence we try increase limit
   5 but it doesn't work so we capture error
   6 """
   7 MAX_RECURSION = 1000
   8 
   9 
  10 def setup():
  11     """
  12     processing setup
  13     """
  14     size(400, 400)
  15     translate(100, 330)
  16     rotate(0.3)
  17     fill(255, 0, 0, 0)
  18     background(0)
  19     noStroke()
  20     smooth()
  21     fill(255, 0, 0, 20)  # transparency makes for almost '3d' look
  22     import sys         # increase recursion limit or sketch will fail
  23     sys.setrecursionlimit(MAX_RECURSION)
  24     try:               # handle potential index error
  25         shell(-0.008, 1.5, 25)
  26     except RuntimeError:
  27         print "Sketch exceeds set recursion limit"
  28     else:
  29         pass  # Sketch finishes before recursion limit is reached
  30 
  31 
  32 def shell(rot, disp, sz):
  33     """
  34     Recursive shell shape limited by sz
  35     """
  36     REDUCE = 0.999
  37     MIN_SIZE = 0.8   # about right for processing
  38     if (sz > MIN_SIZE):
  39         sz *= REDUCE
  40         disp *= REDUCE
  41         translate(disp, 0)
  42         rotate(rot)
  43         ellipse(disp, 0, sz, sz)
  44         return shell(rot, disp, sz)  # recursive call
  45     else:
  46         return  # break recursive loop on size
  47 


To get python syntax highlighting in jEdit with the *.pyde file add the following to the mode catalog:-
   1 <?xml version="1.0"?>
   2 <!DOCTYPE MODES SYSTEM "catalog.dtd">
   3 
   4 <MODES>
   5 
   6 <!-- Add lines like the following, one for each edit mode you add: -->
   7 <!-- <MODE NAME="foo" FILE="foo.xml" FILE_NAME_GLOB="*.foo" /> -->
   8 
   9 <MODE NAME="processing.py" FILE="python.xml" FILE_NAME_GLOB="*.pyde" />
  10 </MODES>

The latest way to import a java library processing python mode

What you need to do is place the library folder (from vanilla processing) in a libraries folder (under PythonMode), on linux this is sketchbook/modes/PythonMode/libraries/peasy/library/peasycam.jar. The latest python processing way of calling libary is as for vanilla processing to use the processing menu add library method, which adds the following to your sketch add_library('peasycam')







I have since updated many more of my processing.py examples to run using the latest python mode in the processing ide.

Monday, 21 April 2014

Running hilbert fractal sketch in the processing ide

Here is an example of a python sketch running from the processing ide. The main applet file has the .pyde extension whereas other file can have the regular .py extension. the grammar file gets compiled and stored locally grammar$py.class


Performance is acceptable (significantly less good than ruby-processing), much better than pyprocessing. For more info on scope of processing.py see FAQ here. In summary no SciPy no Numpy but yes to java libraries.

Friday, 4 April 2014

Is the latest realisation for processing.py

Upate 8 June 2014 
 Some people (in the academic world, Golan Levin for one) seem to be pretty excited about the python mode in the processing ide, and this time it is being developed by Jonathan Feinberg.
Before you know it there will be a ruby mode for processing. May'be that might help ruby get some traction in the academic world (where "Windows" reigns supreme, even in bloody China, but probably not in Brazil).  Ruby has been notoriously unix centric (and benefits from the rational file structure and many damn useful unix tools) and for that reason some people find it difficult to use ruby on windows.

There are significant limitations to working in the processing ide with both languages (and even java processing for that matter). Which is why Jonathan is still offering a standalone processing.py.
Ruby-processing is quite heavily embedded in the ruby eco-system (all jruby compatible gems are available, in addition to most java and processing libraries), and is likely to remain somewhat separate from the ruby mode in the processing ide (which is currently in development by Tyfkda).

Wednesday, 11 September 2013

Macro and Console Commando Files for jEdit

Now you can use jEdit as your ide for processing.py development get the macro and commando files here and here. In my previous posts I included use of a range of processing libraries (toxi, generative design etc). Jonathan Feinberg wouldn't touch them with a barge pole being vehemently anti GPL. However he has made it relatively easy to use the contributed libraries just copy them to processing.py libraries folder (pity he couldn't pick them up from where they get installed by regular processing as we do in ruby-processing). Proof that it all works here for code see previous posting or get it from github here:-

Saturday, 6 April 2013

Using retained shape for performance in processing.py

   1 # CubicGridRetained
   2 #
   3 # You may need to increase the maximum available memory
   4 # by passing -mx1000m to jvm (in run script if you use that)
   5 #
   6 BOX_SIZE = 20
   7 MARGIN = BOX_SIZE * 2
   8 DEPTH = 400
   9 boxFill = None
  10 grid = None
  11 fcount = 0
  12 lastm = 0
  13 frate =  0
  14 FINT = 3
  15 
  16 def setup(): 
  17   size(640, 360, P3D)
  18   frameRate(60)
  19   noSmooth()
  20   noStroke()
  21   global grid
  22   grid = createShape(GROUP)
  23 
  24   # Build grid using multiple 
  25   for i in range(-DEPTH / 2 + MARGIN, DEPTH / 2 - MARGIN, BOX_SIZE):
  26     for j in range(-height + MARGIN, height - MARGIN, BOX_SIZE):
  27       for k in range(-width + MARGIN,  width - MARGIN, BOX_SIZE):
  28         # Base fill color on counter values, abs function
  29         # ensures values stay within legal range
  30         boxFill = color(abs(i), abs(j), abs(k), 50)
  31         sz = [BOX_SIZE, BOX_SIZE, BOX_SIZE]
  32         cube = createShape(BOX, sz)
  33         cube.setFill(boxFill)
  34         cube.translate(k, j, i)
  35         grid.addChild(cube)
  36 
  37 def draw(): 
  38   background(255)
  39 
  40   hint(DISABLE_DEPTH_TEST)
  41 
  42   # Center and spin grid
  43   pushMatrix()
  44   translate(width / 2, height / 2, -DEPTH)
  45   rotateY(frameCount * 0.01)
  46   rotateX(frameCount * 0.01)
  47   global grid
  48   shape(grid)
  49   popMatrix()
  50 
  51   hint(ENABLE_DEPTH_TEST)
  52   global fcount, lastm, frate
  53   fcount += 1
  54   m = millis()
  55   if (m - lastm > 1000 * FINT): 
  56     frate = float(fcount) / FINT
  57     fcount = 0
  58     lastm = m
  59     print("fps: %d" %  frate)
  60   
  61   fill(0)
  62   text("fps: %d" %  frate, 10, 20)
  63 

Processing.py now updated

Thanks to work by Ralf Biedert processing.py has recently been update to use jython-2.7b1 and processing-2.0b8.  It might be interesting to follow his fork in the meantime it has been pulled into the original by Jonathan Feinberg. So you can even update it yourself to the development version of vanilla processing if you wish (strange thing is why opengl is still kept in libraries folder instead of alongside core as P2D and P3D are both rendered using opengl). Here is another Shader example translated to processing.py.
   1 monjori = None
   2 
   3 def setup():
   4   size(640, 360, P2D)
   5   noStroke()
   6   global monjori
   7   monjori = loadShader("monjori.glsl")
   8   monjori.set("resolution", float(width), float(height))
   9   
  10 
  11 def draw():
  12   monjori.set("time", millis() / 1000.0)
  13   shader(monjori) 
  14   # The rect is needed to make the fragment shader go through every pixel of
  15   # the screen, but is not used for anything else since the rendering is entirely
  16   # generated by the shader.  
  17   fill(0)
  18   rect(0, 0, width, height)