Python Scripting in Grasshopper SPM
Python Scripting in Grasshopper SPM
a nifty guide
If youre using a programming language named after a sketch comedy troupe, you better
have a sense of humor.
Python Scripting in Grasshopper
a nifty guide
BACKGROUND
Rhino and Grasshopper use IronPython, not the general purpose parent language.
SYNTAX
Variables inside the scripting editor should be lower_case with multiple words strung
together with underscores.*
For GHPython Input and Output variables, the first letter should be capitalized.
You cannot define a variable that starts with a number.
*Note: You may use camelCase, but know its not considered Pythonic. It really
should only be used to conform to preexisting conditions.
"""
This is a multi-line comment.
Its useful for long comments or
temporarily suspending bits of code.
"""
# Call a function.
short_function_name(var_one)
# Call a function.
long_function_name(var_one, var_two, var_three, var_four)
Example Script:
Iterative Fibonacci Sequence
def fib(n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
return a
Text Display
Icon Display
Python Scripting in Grasshopper
a nifty guide
ANATOMY OF THE GHPYTHON SCRIPTING COMPONENT_PART 02
When you double click in the center of the component, the editor pops out. (It is not as
complex as the C# editor, for better or worse.)
Note 02: You do not need to declare variables. Python does this for
you when you assign them a value. You can, however, cast certain
values (like floats to strings and vice versa).
Description of method.
Loops
for x in range(lower_int_bounds, upper_int_bounds, step_size):
# Do something.
Conditionals
# Import libraries.
if some_condition == True:
# Do something.
elif some_other_condition == True:
# Do something else.
else:
# Do the other thing.
Example Script
# Import libraries.
import math
import rhinoscriptsyntax as rs
import Rhino
The Python script engine is entirely based on RhinoCommon, the geometry library
inside Rhino/Grasshopper.
You should also learn about the methods in the rhinoscriptsyntax module here, and
looking at the examples found here: Rhino.Python rhinoscriptsyntax Module.
Python website
IronPython website
print str(some_variable)
Write the contents of the string to the out output.
rh_doc = Rhino.RhinoDoc.ActiveDoc
Get access to the currently active Rhino Document.
gh_doc = ghenv.Component.OnPingDocument()
Get access to the currently active Grasshopper Document.
Note: Naming this ghdoc is a no-no. That variable contains a reference to the current
Grasshopper document solution. Id use it sparingly.
gh_canvas = Grasshopper.Instances.ActiveCanvas
Get access to the currently active Grasshopper canvas.
import scriptcontext as sc
sc.doc = Rhino.RhinoDoc.ActiveDoc
try:
except:
Handle errors without killing the current execution. Example below.
try:
print my_pt_list[5].ToString() # This could fail if the my_pt_list
array has fewer than six members.
except Exception, e: print str(e) # Print the error.
Python Scripting in Grasshopper
a nifty guide
GHPYTHONLIB: GHCOMP (NODE IN CODE)
GHPythons Node in Code feature allows you to access and execute (almost) any installed
component with a Python script. This is the components module.
# Node in Code
sec = ghcomp.BrepXPlane(B, P)
C = sec[0]
P = sec[1]
The ghpythonlib also has a module called parallel. It contains a single function, run,
which takes a list of data and a single function that should be called for each item in the list
as its inputs. The run function calls this function on as many threads as there are processors
in your computer, then returns the values in the same order as the input list.
Example Script: [Offset on Srf] Component
from ghpythonlib import parallel as ghpllel
import Rhino
tolerance = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance