Wednesday 4 November 2015

A Third Coming for processing.py (a little unexpected)

Well it seem much kudos must go to Luca Damasco (Google Summer of Code student), the python mode has returned for processing-3.0 . I've just playing with it and it seems to be it might be as well to adopt an approach similar to that I settled on for JRubyArt, and explicitly create a settings method as below, and to set the sketch title in setup:-
   1 '''
   2     A wireframe box with colored edges which expands and contracts according
   3     to time-of-day.
   4     An original implementation of *hms* from http://www.gysin-vanetti.com/hms
   5     (C) Ben Alkov, 2014, licensed as APL 2.0 as part of processing.py
   6     (https://github.com/jdf/processing.py).
   7 '''
   8 
   9 def settings():
  10     size(500, 500, P3D)
  11     smooth(4)
  12 
  13 def setup():
  14     global fillCube, edgeCube
  15     frame.setTitle("Box Clock")
  16     camera(0, 0, 100,
  17            0, 0, 0,
  18            0, 1, 0)
  19 
  20     # Creating a **filled** wireframe cube is non-obvious.
  21     # We need an opaque black cube inside a transparent wireframe cube.
  22     fillCube = createShape(BOX, 2)
  23     edgeCube = makeEdgeCube()
  24 
  25     # The fill color here has to match the `background` from `draw` in order
  26     # for the fill cube to be invisible.
  27     fillCube.setFill(color(10))
  28 
  29 
  30 def draw():
  31     rotateX(sin(frameCount * 0.008))
  32     rotateY(cos(frameCount * 0.008))
  33 
  34     # The fill color here has to match the `fillCube`'s `setFill` color in
  35     # order for the fill cube to be invisible.
  36     background(10)
  37     drawShape()
  38 
  39 
  40 def drawShape():
  41     # `map`; "Re-maps a number from one range to another."
  42     # Scale time units to 3D coordinates.
  43     x = map(second(), 0, 59, 1, 12)
  44     y = map(minute(), 0, 59, 1, 12)
  45     z = map(hour(), 0, 23, 1, 12)
  46 
  47     scale(x, y, z)
  48     shape(fillCube, 0, 0)
  49     shape(edgeCube, 0, 0)
  50 
  51 
  52 def makeEdgeCube():
  53     # Draw a 2x2x2 transparent cube with edges colored according to the
  54     # current time.
  55     Red = color(255, 137, 95)  # Seconds.
  56     Green = color(176, 255, 121)  # Minutes.
  57     Blue = color(56, 76, 204)  # Hours.
  58     edgeCube = createShape()
  59     edgeCube.beginShape(LINES)
  60 
  61     # Seconds - lines along `x`.
  62     edgeCube.stroke(Red)
  63     edgeCube.vertex(-1, 1, 1)
  64     edgeCube.vertex(1, 1, 1)
  65     edgeCube.vertex(-1, -1, 1)
  66     edgeCube.vertex(1, -1, 1)
  67     edgeCube.vertex(-1, -1, -1)
  68     edgeCube.vertex(1, -1, -1)
  69     edgeCube.vertex(-1, 1, -1)
  70     edgeCube.vertex(1, 1, -1)
  71 
  72     # Minutes - lines along `y`.
  73     edgeCube.stroke(Green)
  74     edgeCube.vertex(-1, 1, 1)
  75     edgeCube.vertex(-1, -1, 1)
  76     edgeCube.vertex(1, 1, 1)
  77     edgeCube.vertex(1, -1, 1)
  78     edgeCube.vertex(1, 1, -1)
  79     edgeCube.vertex(1, -1, -1)
  80     edgeCube.vertex(-1, 1, -1)
  81     edgeCube.vertex(-1, -1, -1)
  82 
  83     # Hours - lines along `z`.
  84     edgeCube.stroke(Blue)
  85     edgeCube.vertex(-1, 1, -1)
  86     edgeCube.vertex(-1, 1, 1)
  87     edgeCube.vertex(1, 1, -1)
  88     edgeCube.vertex(1, 1, 1)
  89     edgeCube.vertex(1, -1, -1)
  90     edgeCube.vertex(1, -1, 1)
  91     edgeCube.vertex(-1, -1, -1)
  92     edgeCube.vertex(-1, -1, 1)
  93     edgeCube.endShape()
  94     return edgeCube


Static sketches including "size" as might be expected do not play too well.

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