Getting Architecture Done
Efficient Methods for Architecture

Create Grasshopper Components with Python

If you are familiar with Grasshopper for Rhino, you may already know that its visual programming interface works with components that get plugged to each other—each of the components being a function that takes and returns parameters as inputs and outputs, respectively.

GhPython is a Grasshopper plugin which brings the flexibility of the python programming language and the rhinoscriptsyntax right into Grasshopper — so you can build custom Grasshopper components by coding scripts in python.

This plugin doesn't come with Grasshopper by default, but you can download it here, and install it by dragging it on top of Grasshopper. The new plugin will appear under the Math components' section the next time you run Grasshopper (or you can run the GrasshopperUnloadPlugin command in Rhino to unload the plugin, then run Grasshopper to open it back again).

Creating a Point with Python in Grasshopper

You can customize the inputs and outputs on the Grasshopper interface. The input names will represent the name of those objects inside the python script. The output will be created setting a variable inside the python script with the same name that you set in the component. Even though they are editable, the inputs in the example are x and y, and the output is a.

Python scripting code to generate a Point in Grasshopper

Create the Grasshopper definition as you see in the screenshot, and then double click the Python component to edit its code. Add the following snippet:

# Load rhinoscriptsyntax library as rs
import rhinoscriptsyntax as rs

# Variables x and y are automatically defined

# We output a point with (x, y, 0) on variable 'a'
a = rs.AddPoint((x, y, 0))

Rhino Visualization of the Point created with Python in Grasshopper

The end result is output a, which is a Grasshopper Point created by our Python component. This point is exactly the same that if it was created with the Grasshopper Construct Point component.

Set InDesign Variables with JSON

As I previously mentioned in Efficient Methods for Architectural Graphics, making your own InDesign document templates is a really good practice — it you will save time when creating similar documents, and it will let you focus on the important part of your document: its content.

When document templates are comprised of multiple text variables, it can be tedious to manually edit all text fields to produce new documents, especially when working with repetitive forms (such as spreadsheets, receipts, or reports).

Text Variables

InDesign allows to create text variables to hold different values of your document as variables. These variables can then be inserted into one or more parts of your document. For instance, you could create a text variable named Name, then insert it wherever your name has to appear in that document. Later on, by modifying the value of the text variable Name, your document would replace all the instances of your name to whatever new name you set.

Text Variables can be created through the menu under Type > Text Variables > Define.., and inserted into a document's text through Type > Text Variables > Insert Variable — only when you are inside a text frame.

Changing Text Variable's Values with JSON

Once your document is set with your desired text variables (let's say we are using two variables named Title and Subtitle, for this example's sake), you can either change their values manually or automate the process by running the script below — this way, your text fields will get filled with data using a JSON object, named settings in this example.

var settings = {title: 'Title',
                    subtitle: 'Your Subtitle Here'};

app.activeDocument.textVariables.item("Title").variableOptions.contents = settings.title;
app.activeDocument.textVariables.item("Subtitle").variableOptions.contents = settings.subtitle;

For those of you who do not code, JSON means JavaScript Object Notation, and it is not more than a syntax to store information. For this script, only simple JSON objects contained in between { and } are needed; writing first the variable name (or key), then a colon, and then the variable's value. Variables need to be separated with commas, and you can add as many as you want. And, in case you have never ran a JavaScript script on InDesign, I will soon explain how to in a future post.

Scripting in Rhino Python: Switching Units

Following up with the Scripting In Rhino Python series of articles, here is a useful snippet of code that automates switching units on a Rhino document.

This script basically switches the units of the current document between meters and millimeters. The important function is rs.UnitSystem(), which returns the current unit measure — or sets it if we give parameters to the function.

import rhinoscriptsyntax as rs

if rs.UnitSystem() == 2:
    # Current unit is mm, Switch to m
    rs.UnitSystem(4, False, True)
else:
    # Current unit is m, switch to mm
    rs.UnitSystem(2, False, True)

What's Next

This article is part of a series of posts about efficient architectural methods, workflows and tools, titled Getting Architecture Done.

If you want to be notified when any other articles of the same series are posted, go ahead an subscribe here.

Scripting in Rhino Python: Introduction

This is a little introduction to Python Scripting in Rhino for beginners.

In previous versions of Rhino, scripting was possible in RhinoScript with VBScript. In Rhino 5.0, Python has been introduced—a powerful object oriented scripting language.

How do I start? Open Rhino 5.0 and type the command EditPythonScript. This will open the Rhino Python Editor's window. On the left, you have the python, rhinoscriptsyntax, scriptcontext and Rhino libraries. On the right, script tabs appear in which you can create new scripts or edit existing ones.

Here are a few examples using really basic features of the rhinoscriptsyntax library.

Hello, Python!

Let's get hands on and try writing and running a simple script.

Just click the New File icon and write the following text. To comment code in Python, the dash (#) is used, this tells the python's code interpreter to ignore those lines.

#The next line will log Hello, Python on the console
print "Hello, Python!"

Now, press the green play button or F5 to make the code run. If everything is right, you will be able to see on the bottom of the window the message -Hello, Python!- displayed on the console.

Create a Point and a Line

The above code just runs python code, without using the library that Rhino offers to interact with the program. The following example will import the RhinoScript library into the python script, and it will then add a Point and a Line to our current Rhino document.

# Import the RhinoScript library to python as the variable rs
import rhinoscriptsyntax as rs

# Add a Point at cartesian coordinates (x, y, z) = (10, 10, 3)
rs.AddPoint([10,10,3])

# Add a Line from (0, 0, 0) to (3, -2, 0)
rs.AddLine([0,0,0], [3,-2,0])

Create a series of Points with a loop

Now, we will create a loop which will add different points to your document. I also introduced here the function rs.EnableRedraw(BOOL), that increases performance when drawing a lot of items into the document by not allowing Rhino to update the view until we reactivate that feature.

# Import the RhinoScript library to python as the variable rs
import rhinoscriptsyntax as rs

# Disable Redraw so Rhino doesn't update every time it creates a new geometry
rs.EnableRedraw(False)

# Definition of variables for our loop/range

# Value at which the loop starts from
start = 0

# Value until which the loop goes to
to = 10

# Step value of the loop
step = 1

# Loop
for i in range(start,to,step):
     rs.AddPoint([i,0,0])

# Enable Redraw so Rhino draws the new geometry
rs.EnableRedraw(True);

The loop runs from the value 0 to 10, with a step of 1 in between each of the cycles. This basically means that the Python scripts will add into our Rhino document the points that range from [0,0,0] to [10,0,0], by adding 1 to the x value of the point each step of the loop and attaching that point into the document. You can try editing the values from, to and step to see what happens.

What's next

This article is part of a series of posts about architectural methods, workflows and tools, titled Getting Architecture Done.

If you want to be notified when any other articles of the same series are posted, go ahead an subscribe here.

In the future, I will share more scripts. I hope this serves as an introduction to see the most basic things you can do with Python in Rhino. If you come from another programming language or don't know how to program, trying small code snippets is a good way to start. Copy and paste code that others have created and try modifying small parts of it.

Also, the reference guide of Python in Rhino 5.0 shows all the functions that rhinoscriptsyntax offers, to see what Rhino allows you to do, and it is accessible through the Help menu in the EditPythonScript window.

If you found this article useful, please share it with people you think may be interested. And tell me what you think!

To receive future updates, subscribe to the newsletter here or follow me on Twitter @nonoesp.