Write Processing sketches in Python
jdf, updated
🕥
2023-03-05 20:40:17
Python Mode for Processing
Write real Processing sketches in Python.
Tested on Mac OS 10.10 and Ubuntu 14.

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](http://processing.org/download). Then, install
Python Mode:
Then try your first sketch:
```python
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](http://py.processing.org/tutorials/), and alternatively some [examples](mode/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](https://processing.org/environment/#overview) 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](http://learningprocessing.com/videos/) 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
```python
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](https://en.wikipedia.org/wiki/RGB_color_model), [RGBA](https://en.wikipedia.org/wiki/RGBA_color_model), [HSB](https://en.wikipedia.org/wiki/HSL_and_HSV), and [HEX](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet). 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
```python
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](https://processing.org/reference/#shape) that we can draw, but for this example we will be drawing a rectangle. Processing uses a [Caretsian Coordinate System](https://en.wikipedia.org/wiki/Cartesian_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](http://processing.org/reference/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](http://github.com/jdf/processing.py/issues).
How do I report bugs or request new features?
Please report any issue in the [bug tracker](http://github.com/jdf/processing.py/issues).
### How can I create a launcher for my sketch? ###
Add these lines near the top of your script:
```python
import launcher
launcher.create()
```
How can I use Ani, or any other library that modifies fields?
Some libraries such as [Ani](http://www.looksgood.de/libraries/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.
```python
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](http://github.com/jdf/processing.py/issues)!
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 project might 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

Issues
opened on 2023-02-16 09:36:58 by jonascj
Just found out Python Mode for Processing 4 is available (and works well under Windows 11).
But Processing 4.1.3 does not remember that Python Mode for Processing 4 (PMFP4) was the most recently used mode and after an application restart Processing 4.1.3 opens up in Java Mode again.
In version 3.5.4 the file c:\users\<username>\AppData\Roaming\preferences.txt
would hold a line mode.last=jycessing.mode.PythonMode
after closing Processing with Python Mode active. Now in version 4.1.3 this line stays un-updated as mode.last=processing.mode.java.JavaMode
.
Changing the line manually to mode.last=jycessing.mode.PythonMode
works with Processing 4.1.3 and PMFP4 on Windows 11.
Maybe it is a Processing issue and not a PMFP4 issue...
opened on 2023-01-31 05:50:10 by rors
Running Processing 3.5.4 on MacOS 13.1 Ventura, Intel. Most sketches seem to run fine, but when trying to run in P3D
mode, the sketch window does open and no errors are displayed in the console. I do see the circular Processing icon in the launchpad however and need to force quit it to close it.
```python
def setup():
size(800,800,P3D)
def draw():
background(255)
```
Java Control Panel seems to indicate that I'm running Java 1.8.0_361 (/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
).
Adding these lines to the top of the script does not modify the behavior in any discernible way:
python
from java.lang import System
System.setProperty("jogl.disable.openglcore", "false")
Thanks.
opened on 2023-01-15 20:35:54 by talisman
Hi ,
Is it possible to get python mode to run on Processing 4 ?
Are there plans to make it available for Processing 4 ?
I am willing to help with the effort if there is such .
Thanks , Jonathan Talisman
opened on 2023-01-15 14:11:34 by Adwait369
unable to install python mode in processing 3.5.4. What to do?
opened on 2023-01-15 03:21:29 by benfry
If you wind up doing another build for the 3.x version, inside https://github.com/jdf/processing.py/blob/master/mode/mode.properties for the Processing 3 version, please set maxRevision=270
so that it doesn't load when users switch to Processing 4.
It's properly set in the contribs.txt
file (not sure if @prisonerjohn just did that himself) so that it won't be installed by Processing 4, but it still winds up being used, which can create confusion when lots of errors appear. (Processing 4.1.2 adds additional checks to prevent loading, but it's still stymied by the lack of maxRevision
here.)
opened on 2022-08-25 15:59:26 by nsandell123
It says you have to do "Add Mode", but in version 4.0, it says "Manage Mode"
See here: https://py.processing.org/tutorials/gettingstarted/ and in the github read me
