Create Visuals with NodeBox! (Part 2: Crash Course)

In Part 2 of “Create Visuals with NodeBox!” we will explore the interface and give examples of correct syntax when dealing with common Python commands, for loops, variables, importing images, and templates. This post is a crash course in programming 2D visuals with NodeBox, perfect for the aspiring artist or Mac tinkerer who wants to play around with the powerful graphical capabilities of an Apple computer.
Just a disclaimer: I am not a programming super star! I am learning NodeBox as I write about it. So, if you are the least bit intimidated by programming, take a deep breath and clear your mind. I am a beginner just like you! I can’t believe how easy it is to program graphics with Python. The language is easy to remember and NodeBox is remarkably stable.
What Does This all Do?
To understand NodeBox, we must first understand the interface we will be using. NodeBox is unlike many applications we use for design, since it asserts simplicity over displaying a bunch of buttons. There are three basic parts to the NodeBox interface:
Code
Code is typed into the upper right portion of the NodeBox window. Any keywords and commands will be highlighted in blue. These are built-in terms NodeBox understands. In the example, def, for, random, fill, rect, and size are all highlighted in blue. Random is a NodeBox command that computes a random number. Rect is a command that outputs a random number.
Output
Once the program is ready for viewing, I can run it by pressing Cmd+R. The output for the program will be displayed to the left.
Messages
Error messages will be displayed on the bottom right of the NodeBox interface. I can figure out how to fix broken code here.
Working with Code in NodeBox
NodeBox makes coding easy. The syntax, or the language, is rather bare, allowing for fewer mistakes when writing code. Lets look at a basic command:
rect(x, y, width, height, roundness=0.0, draw=True)
This is the basic syntax of a command: the name of the command ( parameters ). The name of the command gives us a hint of what it will do. The rect command draws a rectangle. Parameters tell the command how to behave, these are separated by commas inside of brackets. The rect command specifies x and y position, width, height, and edge roundness.
The Coordinate System
2D graphics are displayed using a X, Y coordinate system. The origin point (0,0) for both X and Y is at the top left of the output window. As X increases, the position of an object will move right horizontally across the output window. As Y increases, the position of an object will go down vertically.

Throttle
I can easily change the values of numerical parameters with the throttle. Click on a numerical parameter. Hold down Cmd and move the mouse left to decrease the value by 1 or move right to increase the value by 1. Hold Cmd + Option while dragging to increase and decrease by .01. Cmd + Shift by 10.
Defining a Variable
Variables are handy, since they allow me to store a value for the computer to use. Suppose I want to draw a circle in a random position between 0 and 100. I can define the x position as x = random(1,100). Every time the program runs, x and y are given a random value between 1 and 100.


Indent the last line of this program to make it work.
The above example draws a circle at a random position between in a 100 x 100 pixel space.
The For Loop
Computers love repetition. The For Loops tells the computer to perform a certain task repetitiously. The syntax for the For Loop looks like this:


Notice how the For Loop has a variable and a range. This is written as: For i in range(10):. i is the variable. The range is 10, meaning everything indented below the for loop will be executed ten times. The colon at the end of the for statement is crucial! Do not forget the colon! All the code I want to loop must be indented below the For statement.
The above example draws ten circles instead of just one. When I run the program, a black blob will appear. This is because all ten circles have the same fill color and stroke.


If I define a Fill and Stroke color before the oval is drawn, I can see depth between the circles (assuming the fill and stroke are different colors).
Colors
The RGB system is used anywhere a specific color value can be inserted as a parameter. In the above example, I filled every circle with red using the command fill(1, 0, 0). Color values are based on a numerical value of 0 to 1. Green = (0, 1, 0). Blue = (0, 0, 1). White = (1, 1, 1). Black = (0, 0, 0).
Templates
Templating is a way I can define the certain look of an object once and then call upon that object as many times as I like. This would be called a function in traditional programming. Suppose I want to draw 100 images. I don’t want to write the code from each image 100 times. I can just designate the specific design of one image, then use a For Loop to draw 100 of them.

To run this program, save this code as a project file. Download the first image in this post of the NodeBox logo. Create a folder called images where the project file is saved. Put the downloaded nodeboxapp.png file into the images folder. Run the program. It is important to use nodeboxapp.png, since it contains a transparency that the program will need to run. NodeBox looks for images in the folder where the project is saved first and then in the Home Directory.
Notice the def statements? These define a template and have the following structure: def template_name (variables). After each def statement is a colon! Do not forget the colon! The properties of the def statement are then indented beneath the initial syntax. In the above example, def nodeboxlogo (x,y): defines a template for the following image: image(”images/nodeboxapp.png”, x, y, alpha=0.7). So, whenever I call upon nodeboxlogo, it will always draw the image with an alpha of 0.7. The Alpha is the transparency of the image. This image is stored on my hard drive, in a folder called images that is in the same folder as the project file. See how nodeboxlogo has two variables, x and y? At the end of the program, I call upon nodeboxlogo to draw itself at a random position 10 times in a For Loop. x then equals random(0, sizewidth).
Variable Output Size
The size command designates the size of the output window. A good rule of thumb when designing programs for computer screens is to make the output size variable. In the very beginning of the program, I defined two variables: sizewidth=400 and sizeheight=600. I put these two variables into the size command, allowing me to control the output size from the very top of the program. Another time I call upon these two variables is at the bottom of the program, where I assign a random position for each nodeboxlogo.png. This is so the image never escapes the dimensions of the output screen. Otherwise, every time I wanted to change the output size, I would then have to change the range of the random values. This applies to the size of the background rectangle as well. It is superior to have the position of all the objects scaled to the overall dimensions of the output window. With a little tweaking, I could even make it so each NodeBox image always stays within the bounds of the canvas. This would require a little math and variable dimensions for the image. Understand how this is helpful?
Export to PDF
I can export my composition to PDF by selecting File: Export as PDF… The exported dimensions will be dictation by the size command. In the export options, I can designate the number of pages. This is nice with a randomized program, since it will run the program as many times as I like, outputting each randomization as a separate PDF.
In Part 3, I will dive into NodeBox math and drawing paths! Check out the tutorials at the Official NodeBox website.
What did you learn in this post?
Or did it leave you with further questions?
Comment here to Ask Steve Blue!
Written by Steve Blue
steveblue@iuseapple.com




0 Responses to “Create Visuals with NodeBox! (Part 2: Crash Course)”
Leave a Response