Skip to content

benfry/processing.py

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Mode for Processing

Write real Processing sketches in Python.

Tested on Mac OS 10.10 and Ubuntu 14.

Build Status FOSSA Status

Quick Start

Processing Development Environment

If you're looking to write Processing sketches in Python, your best bet is to use Python Mode. The project is still in its early days, and documentation is lacking, but there are many example sketches to get you started. In general, the Processing reference works just fine for Python mode.

First, download Processing. Then, install Python Mode:

Then try your first sketch:

def setup():
    size(600, 600)
    colorMode(HSB)
    noStroke()

def draw():
    fill(0x11000000)
    rect(0, 0, width, height)
    fill(frameCount % 255, 255, 255)
    ellipse(mouseX, mouseY, 20, 20)

If you are just getting started, it is a good idea to go through the tutorials on our website, and alternatively some examples.

Processing Basics

What is Processing?

Processing is a graphics library utilized by artists, educators, students, and hobbyists to create sketches without the hassle of traditional graphics libraries. Processing provides a simplified API that allows for visual tasks that would take dozens of lines in other software to be completed in just a couple of lines. Processing is also a community with extensive support in developing, maintaining, as well as creating educational resources for this software. Please see https://processing.org/ for more information.

Should I use Processing?

Processing is the perfect enviornment for programmers of all experience levels. It is a great starting point for programmers with no graphics programming experience. The documentation is a great place to start and learn the basics. Processing is also a vehicle for learning how to code, since you can see the changes you make in your code visually. Here is a great video tutorial for getting started. For users with graphics programming experience, Processing is very effective for fun projects and quick prototypes. For extremely complex, performance heavy, and commerical applications - Processing may not be the best choice.

Structure of a Processing Project
def setup():
   # This code is only run once
   size(800, 800)

def draw():
   # This code is run on a loop
   background(255, 0, 0)

A graphics library has three essential components: before the main loop; the code the consists of the main loop; and code that is executed after the main loop. In processing we write all of the code that will be executed before the main loop in a function that we define as setup. This function is only called once at the start of program execution. Typically we will define the size of the graphics window we want to generate using the called size(w, h), where w is the width we desire (in pixels) and h is the height we desire (in pixels). If we want the window to take up the entire screen we can call fullScreen().

We write the code that we want to continously execute in the function defined as draw. This code is run on a loop and is only terminated if we tell it to (or the program exits in an error). Typically we will define a background using a call to the bacground() function. This function accepts a wide variety of values from RGB, RGBA, HSB, and HEX. By placing background in draw the screen is 'refreshed' each execution, which mean what we drew to the screen last cycle is erased. We can also move background to setup if we wish to not have this behavior.

Drawing Shapes
def setup():
   # This code is only run once
   size(800, 800)

def draw():
   # This code is run on a loop
   background(255, 0, 0)
   fill(0, 0, 0)
   rectMode(CENTER)
   rect(width / 2, height / 2, 10, 20)

One of the most basic things we can do in Processing is draw shapes. There are a wide variety of shapes that we can draw, but for this example we will be drawing a rectangle. Processing uses a Caretsian Coordinate System where the origin (0,0) is in the top left corner, and the maximal (x,y) is in the bottom right corner. The function rect(x,y,w,h) allows us to draw a rectangle. The first and second parameters, x and y, defines the coordinates where we want to draw the rectangle. Processing also provides a variety of defined constants; width and height are the width and height of the current window. By setting rectMode(CENTER) and passing width / 2 and height / 2 as the arguments for x and y we draw a rectangle in the center of the window.

The function fill(r,g,b) allows us to change the color of the rectangle we are drawing. Think of fill as changing the color of the paint brush you are using. Once you call fill every call after will use that color until fill is called again. This is why we call fill before drawing our rectangle. We pass the value (0,0,0) to 'fill` to set the color to black.

Advanced Usage

Using Processing Libraries

Python Mode is implemented in Java, and is designed to be compatible with the existing ecosystem of Processing libraries.

Many libraries need a reference to "the current PApplet", and that's what this is for. Of course, there's no such thing as this in Python; it's just something that processing.py provides for you for compatibility with such libraries.

If you find that some Processing library doesn't work as expected with processing.py, please let us know in the bug tracker.

How do I report bugs or request new features?

Please report any issue in the bug tracker.

How can I create a launcher for my sketch?

Add these lines near the top of your script:

import launcher
launcher.create()
How can I use Ani, or any other library that modifies fields?

Some libraries such as Ani require you to specify a variable name for animation. Unfortunately they cannot access Python variables directly (and Java's built in classes are immutable).

To solve this problem we instead create a mutable PrimitiveFloat object. This object has a field .value, which you can use for these purposes.

import jycessing.primitives.PrimitiveFloat as Float
x = Float(100.0)
Ani.to(x, 200, "value", 50);  # "value" is the name of the Float's internal field

In case you need other primitive values, please let us know!

Why was this project created?

I (Jonathan) recently gave a talk about Processing to a group of rather bright 8th-graders, as part of a computer-programming summer camp they were attending at my office. Their curriculum up to that point had been in Python, which is an eminently sensible choice, given the pedagogical roots of the language.

The kids were really turned on by the demos--I showed them the white glove, and Golan Levin's New Year's cards--but they were bogged down by Processing's C-like syntax, which really seems arcane and unnecessarily complex when you're used to Python.

I shared my experience with Processing creators Ben Fry and Casey Reas, and they told me that, indeed, the original Processing was a fork of "Design By Numbers", with Python and Scheme support hacked in. Support for a multi-lingual programming environment was always part of the plan, so they were enthusiastic about any new attempt at the problem.

I was able to hack up a proof of concept in a couple of hours, and have managed to create something worth sharing in a couple of weeks. I was only able to do it at all thanks to the brilliant and beautiful Jython project.

At the time of Processing's first public release, August of 2001, Jython was too young a project to be used in this way. But now, having done absolutely no work to profile and optimize, I can get hundreds of frames per second of 3D graphics on my linux box. So, kudos to the Processing project, and kudos to Jython!

Credits

Written by Jonathan Feinberg <[email protected]> Launcher & many improvements by Ralf Biedert <[email protected]>

Much of the work in achieving compatibility with Processing 3.x was was done by Luca Damasco (Google Summer of Code student), under the supervision of Golan Levin, with additional support from the Frank-Ratchye STUDIO for Creative Inquiry at Carnegie Mellon University. Without Luca, the porject may well have died.

Also, YourKit, LLC was so kind to sponsor a license for their excellent YourKit Java Profiler. Thank you very much! They've asked me to place this message here in return for their sponsorship:

YourKit supports open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of YourKit Java Profiler and YourKit .NET Profiler, innovative and intelligent tools for profiling Java and .NET applications.

License

FOSSA Status

Packages

No packages published

Languages

  • Python 69.2%
  • Java 26.2%
  • GLSL 3.7%
  • Shell 0.6%
  • Batchfile 0.2%
  • AppleScript 0.1%