This tutorial is for Python Mode in Processing 2+. If you see any errors or have comments, please let us know. This tutorial was adapted from the book, Getting Started with Processing, by Casey Reas and Ben Fry, O’Reilly / Make 2010. Copyright © 2010 Casey Reas and Ben Fry. All rights reserved.
Welcome to Python Mode for Processing!
Start by visiting http://processing.org/download and selecting the Mac, Windows, or Linux version, depending on what machine you have. Installation on each machine is straightforward:
With any luck, the main Processing window will now be visible. Everyone's setup is different, so if the program didn't start, or you're otherwise stuck, visit the troubleshooting page for possible solutions.
Processing doesn't include support for the Python programming language by default. In order to enable Python support, you'll need to install an add-on called Python Mode. You can do this by clicking on the drop-down menu on the right side of the tool bar and selecting "Add Mode..." A window with the title "Mode Manager" will appear. Scroll down until you see "Python" and press "Install." More information here.
After you've installed Python Mode, you can switch back and forth between the Python and Java versions of Processing using the drop-down menu in the tool bar. If you find yourself getting strange syntax errors or exceptions when running your program, make sure you have the right mode selected!
Your First Program
You're now running the Processing Development Environment (or PDE), with Python Mode installed. There's not much to it; the large area is the Text Editor, and there's a row of buttons across the top; this is the toolbar. Below the editor is the Message Area, and below that is the Console. The Message Area is used for one line messages, and the Console is used for more technical details.
In the editor, type the following:
ellipse(50, 50, 80, 80)
This line of code means "draw an ellipse, with the center 50 pixels over from the left and 50 pixels down from the top, with a width and height of 80 pixels." Click the Run button, which looks like this:
If you've typed everything correctly, you'll see this appear in the Display Window:
If you didn't type it correctly, the Message Area will turn red and complain about an error. If this happens, make sure that you've copied the example code exactly: the numbers should be contained within parentheses and have commas between each of them, and the line should end with a semicolon.
One of the most difficult things about getting started with programming is that you have to be very specific about the syntax. The Processing software isn't always smart enough to know what you mean, and can be quite fussy about the placement of punctuation. You'll get used to it with a little practice.
Next, we'll skip ahead to a sketch that's a little more exciting. Delete the text from the last example, and try this:
def setup(): size(480, 120) def draw(): if mousePressed: fill(0) else: fill(255) ellipse(mouseX, mouseY, 80, 80)
This program creates a window that is 480 pixels wide and 120 pixels high, and then starts drawing white circles at the position of the mouse. When a mouse button is pressed, the circle color changes to black. We'll explain more about the elements of this program in detail later. For now, run the code, move the mouse, and click to experience it.
Note: Be careful about how you indent each line! Indentation is important in Python, and indenting incorrectly or inconsistently can cause your program to work differently from how you intended, or not work at all. You can use any number of spaces, as long as you're consistent about the number of spaces you use. (Many Python programmers prefer four spaces, others prefer two.) You can also use the tab key to indent your code, but don't use tabs and spaces for indentation in the same program.
So far we've covered only the Run button, though you've probably guessed what the Stop button next to it does:
If you don't want to use the buttons, you can always use the Sketch menu, which reveals the shortcut Ctrl-R (or Cmd-R on the Mac) for Run. Below Run in the Sketch menu is Present, which clears the rest of the screen to present your sketch all by itself:
You can also use Present from the toolbar by holding down the Shift key as you click the Run button.
The next command that's important is Save. It's the downward arrow on the toolbar:
You can also find it under the File menu. By default, your programs are saved to the "sketchbook," which is a folder that collects your programs for easy access. Clicking the Open button on the toolbar (the arrow pointing up) will bring up a list of all the sketches in your sketchbook, as well as a list of examples that are installed with the Processing software:
It's always a good idea to save your sketches often. As you try different things, keep saving with different names, so that you can always go back to an earlier version. This is especially helpful if — no, when — something breaks. You can also see where the sketch is located on the disk with Show Sketch Folder under the Sketch menu.
You can also create a new sketch by pressing the New button on the toolbar:
Examples and Reference
Learning how to program with Processing and Python involves exploring lots of code: running, altering, breaking, and enhancing it until you have reshaped it into something new. With this in mind, the Processing software download includes dozens of examples that demonstrate different features of the software. To open an example, select Examples from the File menu or click the Open icon in the PDE. The examples are grouped into categories based on their function, such as Form, Motion, and Image. Find an interesting topic in the list and try an example.
The Processing Reference explains every code element with a description and examples. The reference programs are much shorter (usually four or five lines) and easier to follow than the longer code found in the Examples folder. We recommend keeping the reference open while you're reading this book and while you're programming. It can be navigated by topic or alphabetically; sometimes it's fastest to do a text search within your browser window.
The reference was written with the beginner in mind; we hope that we've made it clear and understandable. We're grateful to the many people who've spotted errors over the years and reported them. If you think you can improve a reference entry or you find a mistake, please let us know by clicking on the link at the top of each reference page.
This tutorial is for Processing 2+. If you see any errors or have comments, please let us know. This tutorial was adapted from the book, Getting Started with Processing, by Casey Reas and Ben Fry, O’Reilly / Make 2010. Copyright © 2010 Casey Reas and Ben Fry. All rights reserved.