4.6. Modules and libraries
Importing Libraries and Modules in GH-Python
In the context of Python, Modules refer to a Python file wich contains Classes and Functions:
It provide us a variety of different uses such as Math functions among others:
If we want to look at the file path that Python is referencing to import modules, we need to import the module sys. This provides access to variables that the interpreter is working in:
# Import sys module
import sys
print sys.path # It will return al the file paths is searching for modules
import Rhino.Geometry as rg
# Import Math module
import math
print dir(math) # It will return a list of all the different functions contained in this Math module
# Create a Sine wave
length = 50
amplitude = 5
frequency = 0.3
points = []
for x in range (length):
z = amplitude * math.sin(frequency * x)
point = rg.Point3d(x,0,z)
points.append(point)
a= points
In order to import all the functions in our script in once or just specific ones, we can invert the way how we import modules.
from math import * # This will import all the functions in the module Math
from math import sine # This will only import the function sine
In the context of Python, Modules refer to a Python file wich contains Classes ans Functions. It provide us a variety of different uses such as Math functions or working with CSV files among others
IronPython and Python
During all this previous examples, we have used a version of Python known as IronPython that it’s been developed to work in the .NET Framework.
The .NET Framework offer some common tools and libraries necessary to build Microsoft applications. It provides a framework where other .NET languages, such as C# and VB.Net can use a common language in the runtime module or CLR module. The good thing about it is that we can use libraries written in other languages that can be use with one another.
Importing libraries
We can use the CLR library to import for example Excel files into GH-Python. This will allow direct acces to Excel files from our Python scripts.
import Rhino.Geometry as rg
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as excel
# Excel
ex = excel.ApplicationClass()
# Open Workbooks
workbook = ex.Workbooks.open(r"C:\Users\test.xlsx") # r before path to read backslashes
# Read Workbooks
ws = workbook.Worksheets[1]
pX = []
pY = []
pZ = []
for i in range(ws.UsedRange.Rows.Count):
if i ==0:
continue
x = ws.Range("A{}".format(i + 1)).Value2
y = ws.Range("B{}".format(i + 1)).Value2
z = ws.Range("C{}".format(i + 1)).Value2
pX.append(x)
pY.append(y)
pZ.append(z)
workbook.Close(False)
ex.Quit()
Grasshopper library
Other Libraries ready to import is RhinoCommon, which assist with working with Grasshopper in Python. One of this is the Grasshopper library, that provides classes and functions that allow us to work with Grasshopper objects, such as Data Trees
from Grasshopper import DataTree
from Grasshopper.Kernel.Data import GH_Path
from Rhino.Geometry import Point3d
# Adjust to Grid extension
breakLine = 6
# Input: Flatten list of points in a grid
# Empty Data Tree
tree = DataTree[Point3d]()
pathCount = 0
newPath = GH_Path(pathCount)
for num in range(len(points)):
if num % breakLine == 0 and num != 0:
pathCount += 1
newPath = GH_Path(pathCount)
tree.Add(points[num], newPath)
Creating and saving modules
Python allows us to create, store and reuse our own functions by saving and importing our own Python modules.
Lets create and save a python file (TreeFunction.py) under C:\Users\XXXX\AppData\Roaming\McNeel\Rhinoceros\6.0\scripts containing the following code:
from Grasshopper import DataTree
from Grasshopper.Kernel.Data import GH_Path
from Rhino.Geometry import Point3d
def treeBranch(points):
# Adjust to Grid extension
breakNum = 6
# Input: Flatten list of points in a grid
# Empty Data Tree
tree = DataTree[Point3d]()
pathCount = 0
newPath = GH_Path(pathCount)
for num in range(len(points)):
if num % breakNum == 0 and num != 0:
pathCount += 1
newPath = GH_Path(pathCount)
tree.Add(points[num], newPath)
return tree
Once you have done that. Lets import the function into a Python component in Grasshopper by writting:
import TreeFunction as tf
# If you update the script in the text file, we need to import a reload function on the import
reload(tf)
#INPUT: points - list Access - Point3d
a = tf.treeBranch(points)