GhPython Script
Mariana H Barroeta
El Espacio Habitable:
Python, Programación para la arquitectura.
2
Primera parte:
El alumno deberá seguir y copiar los ejemplos
presentados en clase. El curso se presentará mediante
ejercicios prácticos enlos que se explicará paso por paso
la lógica que integra cada ejemplo, dada la naturaleza de
nuestro hacer como arquitectosse integrarán ejemplos
lógicos, numéricos y geométricos.
Antes de comenzar de la segunda parte de la materia, se
asigna la traducción de los ejemplos del primer módulo
a la interfaz de Grasshopper. Se entregará un archivo
digital el cual deberáser trabajado en clase.
3
Contenido
Introduction p.02 Sobre el curso
T_03 p.06 Arrays / Lists
T_04 p.08 List of points + curve types
T_05 p.10 For loop to create points
T_06 p.12 Nested for loops
T_07 p.14 Conditionals
T_08 p.16 If / Else with math
T_09 p.18 Random numbers
T_10 p.20 Random lines and colors
T_12 p.22 Transformation
T_13 p.24 Recursion
T_14 p.26 Recursive scale objects
T_17 p.28 Simple growth 01
T_18 p.30 Simple growth 02
T_19 p.32 Roschard test
T_20 p.34 Polygon class 01
T_21 p.36 Polygon class 02
p.04 El Espacio Habitable otoño 2022
T_22 p.38 Divide surface to points
T_23 p.40 Koch snowflake
T_24 p.42 Cracking algorithm
T_26 p.44 Recursive subdivision
T_27 p.46 Recursive Tree Generator
p.05
T_03 Arrays / Lists
import rhinoscriptsyntax as rs
a = []
if runn:
[Link](pt1)
[Link](pt2)
[Link](pt3)
[Link](pt4)
[Link](pt4)
[Link](pt2)
[Link](pt1)
[Link](pt3)
print(a)
print(len(a))
print(a[1])
print(a[len(a)-1])
print(a[3])
print(a[-1])
b = [Link](a)
p.06 El Espacio Habitable otoño 2022
pt1
pt4
pt3
pt2
Polyline from points p.07
T_04 List of points + curve types
import rhinoscriptsyntax as rs
a = []
c = []
if runn:
print (pts)
print (len(pts))
# Curve types
myPolyline = [Link](pts)
myCurve = [Link](pts)
myIntpCurve = [Link](pts) #puntos
de control excentos a la curva, dashed lines
[Link](myPolyline)
[Link](myCurve)
[Link](myIntpCurve)
#Curve Colors
#Colors are Arrays of [r,g,b]
c1 = [Link](0,255,255) #Cyan
c2 = [Link](255,0,255) #Magenta
c3 = [Link](255,255,0) #Yellow
[Link](c1)
[Link](c2)
[Link](c3)
#Bonus for loop
for pt in pts:
[Link](pt)
p.08 El Espacio Habitable otoño 2022
InterpStar p.09
T_05 For loop to create points
import rhinoscriptsyntax as rs
if runn:
pts = []
for i in [Link](start,stop,step):
x = i * [Link](i)
y = i * [Link](i)
z = i + [Link](x)
pt = (x,y,z)
[Link](pt)
[Link](pt)
a = [Link](pts,4)
p.10 El Espacio Habitable otoño 2022
Coil p.11
T_06 Nested for loops
for x in range(count):
for y in range(count):
for z in range(count):
pt = [Link](x,y,z)
color = (x,y,z)
[Link](pt)
[Link](color)
p.12 El Espacio Habitable otoño 2022
Colored points on a cube
p.13
T_07 Conditionals
import rhinoscriptsyntax as rs
pts1 = []
pts2 = []
pts3 = []
col1 = []
col2 = []
col3 = []
[Link](False)
if runn:
for i in range(x):
for j in range(y):
pt = [Link](i,j)
c01 = [Link](255, 0, 255)
c02 = [Link](0, 255, 255)
c03 = [Link](125,38,205)
if i % w == 1 and j % q == 3:
[Link](pt)
[Link](c01)
elif x % s == 0 or y % q == 0:
[Link](pt)
[Link](c02)
else:
[Link](pt)
[Link](c03)
[Link]()
p.14 El Espacio Habitable otoño 2022
If / else statements p.15
carpet design
If / Else with math
import rhinoscriptsyntax as rs
import math as m
#Boolean Flag
flip = True
c1 = [255,0,255]
c2 = [0,255,255]
crvs = []
print(len(crvs))
for i in [Link](0,10,0.1):
ptsForCurve = [] #se vacia cada vez que el loop
corre
for j in [Link](0,10,0.1):
x=i
y=j
z = [Link](i)*[Link](j)
pt = [x,y,z]
[Link](pt)
crv = [Link](ptsForCurve)
[Link](crv)
if flip == True:
[Link](crv,c1)
flip = False
else:
[Link](crv,c2)
flip = True
print(len(crvs))
[Link](crvs)
p.16 El Espacio Habitable otoño 2022
Sine Waves p.17
T_09 Random numbers
import rhinoscriptsyntax as rs
import random as r
if runn:
[Link](False)
pts = []
col = []
for i in range (rx):
x = [Link](0,100)
y = [Link](0,100)
z = [Link](0,100)
rr = [Link](0,255)
g = [Link](0,255)
b = [Link](0,255)
pt = [Link](x,y,z)
[Link](pt)
color = (rr, g, b)
[Link](color)
[Link]()
p.18 El Espacio Habitable otoño 2022
Random points inside a box p.19
T_10 Random lines and colors
import rhinoscriptsyntax as rs
import random as r
[Link](False)
if runn:
pts =[]
for i in range (rx):
x = [Link](0,100)
y = [Link](0,100)
z = [Link](0,100)
pt = [x,y,z]
[Link](pt)
pl = [Link](pts)
crv = [Link](pts)
intpcrv = [Link](pts)
[Link]()
p.20 El Espacio Habitable otoño 2022
rx = x+2 p.21
T_12 Transformation
import rhinoscriptsyntax as rs
import random as r
[Link](False)
[Link](seed)
#Create a list of points and apend pt to the list
pts =[]
[Link](pt)
#vector tienen direccion y magnitud, sirven para
#marcar direcciones o mover objetos
for i in range (rng):
xDir = [Link](-10.0,10.0)
yDir = [Link](-10.0,10.0)
zDir = 0.0
vec = (xDir,yDir,zDir)
newPt = [Link](pts[-1],vec)
[Link](newPt)
pl = [Link](pts)
[Link]()
p.22 El Espacio Habitable otoño 2022
Random Growth p.23
T_13a Recursion
def factorial (n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(7))
#Fibonacci Sequence
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
return fib(n-1) + fib(n-2)
for i in range(rng):
print(fib(i))
#Recursive circle
import rhinoscriptsyntax as rs
circ = []
#recursiveCircle(puntito,radio)
for i in range(rng):
try:
c = [Link](pt,fib(i))
[Link](c)
except:
None
p.24 El Espacio Habitable otoño 2022
Fibonacci Sequence p.25
T_14 Recursive Scale Objects
import rhinoscriptsyntax as rs
[Link](False)
if runn:
a = []
def recursiveScale(objID,scalePt,scaleFact,scaleVect,num):
if num == 0:
return 0
else:
sc = 1.0 / scaleFact
scaleVect = [x - sc for x in scaleVect]
[Link]([Link](objID,scalePt,scaleVect,True))
return recursiveScale(objID,scalePt,scaleFact,scaleVect,num-1)
vec = [1.0,1.0,1.0]
b = recursiveScale(obj,origin,scfac,vec,iter)
[Link]()
p.26 El Espacio Habitable otoño 2022
Paraboloid // Torus // Cylinder p.27
T_17 Simple Growth 01
import rhinoscriptsyntax as rs
import random as r
def placePt(x_range, y_range, z_range):
x = [Link](0,x_range)
y = [Link](0,y_range)
z = [Link](0,z_range)
pt = [x,y,z]
return pt
pts = []
[Link](ptZero)
[Link](ptOne)
[Link](ptTwo)
[Link](ptThree)
a = []
b = []
for i in range(rng):
pt = [Link](placePt(100,100,0))
index = [Link](pts,pt)
cp = pts[index]
vec = [Link](cp,pt)
unitVec = [Link](vec)
subVec = vec - unitVec
newPt = [Link](pt,subVec)
[Link]([Link](newPt,0.5))
[Link](newPt)
[Link]([Link](cp,newPt))
p.28 El Espacio Habitable otoño 2022
Roots p.29
T_12 Simple Growth 02
import rhinoscriptsyntax as rs
import random as r
lines = []
circs = []
sphrs = []
def placePt(x_range, y_range, z_range):
x = [Link](0,x_range)
y = [Link](0,y_range)
z = [Link](0,z_range)
pt = [x,y,z]
return pt
pts = []
[Link](ptZero)
circleZero = [Link](ptZero,rad)
for i in range(rng):
pt = [Link](placePt(100,100,100))
index = [Link](pts,pt)
cp = pts[index]
vec = [Link](cp,pt)
unitVec = [Link](vec)
subVec = vec - unitVec
newPt = [Link](pt,subVec)
circ = [Link](newPt,rad)
[Link](circ)
sphr = [Link](newPt,rad)
[Link](sphr)
[Link](newPt)
line = [Link](cp,newPt)
[Link](line)
p.30 El Espacio Habitable otoño 2022
Molecules p.31
19_ Roschard Test
import rhinoscriptsyntax as rs
import random as r
[Link](sd)
class Walker:
def __init__(self):
self.x = 0
self.y = 0
self.z = 0
def display(self):
shape = [Link](self.x, self.y, self.z)
return shape
def step(self):
stepX = [Link](-1,1)
stepY = [Link](-1,1)
stepZ = [Link](-1,1)
self.x += stepX ## self.x = self.x + stepX
self.y += stepY
self.z += stepZ
w = Walker()
pts = []
for t in range (time):
[Link]()
[Link]([Link]())
crv = [Link](pts)
dell = [Link](pts)
mi = [Link](crv,(1,0,0),(0,0,1), True)
p.32 El Espacio Habitable otoño 2022
Titulo imagen p.33
T_20 Polygon Class 01
import rhinoscriptsyntax as rs
import math as m
class MyPolygon:
def __init__(self,radius,sides):
[Link] = radius
[Link] = sides
theta = (2*[Link])/[Link]
print(theta)
pt01 = [Link]([Link],0,0)
pts = []
[Link](pt01)
[Link] = [0,0,0]
degrees = theta*(180/[Link])
print(degrees)
for i in range (1,[Link]):
tempPt = pts[-1]
newPt = [Link](tempPt,[Link],degrees,None,True)
[Link](newPt)
[Link](pt01)
[Link] = [Link](pts)
def fillPolygon(self):
return [Link]([Link])
def extrudePolygon(self,height):
startPt = [Link]
newZ = [Link][2]+height
endPt = [Link][0],[Link][1],newZ
return [Link]([Link],startPt,endPt)
ply1 = MyPolygon(rad,sides)
[Link]()
ply1 = [Link](zheight)
p.34 El Espacio Habitable otoño 2022
Parametric polygons p.35
T_21 Polygon Class 02
import rhinoscriptsyntax as rs
import math as m
class MyPolygon:
#initialization or construction method
def __init__(self,radius,sides,origin):
[Link] = radius
[Link] = sides
[Link] = origin
origin = [Link]
theta = (2*[Link])/[Link]
x = origin[0]+[Link]
y = origin[1]
z = origin[2]
pt01 = [Link](x,y,z)
pts = []
[Link](pt01)
degrees = theta*(180/[Link])
for i in range(1,[Link]):
tempPt = pts[-1]
newPt = [Link](tempPt,origin,degrees,None,True)
[Link](newPt)
[Link](pt01)
[Link] = [Link](pts)
def fillPolygon(self):
return [Link]([Link])
def extrudePolygon(self,height):
startPt = [Link]
newZ = [Link][2]+height
endPt = [[Link][0],[Link][1],newZ]
return [Link]([Link],startPt,endPt)
ply0 = MyPolygon(rad0, sds0, userPt)
[Link]()
ply0 = [Link](hght0)
p.36 El Espacio Habitable otoño 2022
Parametric polygons p.37
T_22 Divide Surface to Points
import rhinoscriptsyntax as rs
[Link](False)
if runn:
u = [Link](srf,0)
v = [Link](srf,1)
print(u)
print(v)
pts = []
for i in range(0,udiv+1,1):
for j in range(0,vdiv+1,1):
pt = (i/udiv, j/vdiv, 0)
srfP = [Link](srf,pt)
newPt =
[Link](srf,srfP[0],srfP[1])
[Link]([Link](newPt))
[Link]()
p.38 El Espacio Habitable otoño 2022
Evaluated Surfaces p.39
T_23 Koch snowflake
import rhinoscriptsyntax as rs
[Link](False)
#Funcion - get normal/perpendicular vector
def getNormal(pt1,pt2):
dx = pt2[0]- pt1[0]
dy = pt2[1] - pt1[1]
return [-dy,dx,0]
#Function - split line recursive function
def splitLines(lines,count):
#For more information, refer to the gh file
lines = []
line = [Link](pt1,pt2)
line2 = [Link](pt2,pt3)
line3 = [Link](pt3,pt1)
[Link](line)
[Link](line2)
[Link](line3)
#objects = [Link](“Pick some polylines”,4,False,True,True)
#[Link](objects)
if runn:
a = splitLines(lines,count)
[Link]()
p.40 El Espacio Habitable otoño 2022
Snowflakes p.41
T_24 Cracking algorithm
import rhinoscriptsyntax as rs
crack = []
def crackpolygon(pls,count):
temppls = pls
pls = []
if count == 0:
return 1
else:
for pl in temppls:
if [Link](pl) == False:
print(“Not a closed curve”)
else:
print(“Starting to crack curve”)
centroid = [Link](pl)
centPt = [Link](centroid[0])
curves = [Link](pl)
for crv in curves:
pt1 = [Link](crv)
pt2 = [Link](crv)
pts = []
[Link](pt1)
[Link](pt2)
[Link](centPt)
[Link](pt1)
newpl = [Link](pts)
[Link](newpl)
[Link](crv)
cleanup = []
[Link](centPt)
[Link](cleanup)
[Link](pls)
count -= 1
return crackpolygon(pls,count),pls
crackpolygon(polygons,count)
p.42 El Espacio Habitable otoño 2022
Cracked polygons p.43
T_26 Recursive triangle subdivision
import rhinoscriptsyntax as rs
if runn:
poly = []
def recursiveExample():
[Link](False)
subtriangle(triangle,gen)
[Link]()
def subtriangle (triangle,maxGen):
vertexPts = [Link](triangle)
midPt0 = (vertexPts[0]+vertexPts[1])/2
midPt1 = (vertexPts[1]+vertexPts[2])/2
midPt2 = (vertexPts[2]+vertexPts[0])/2
triangle0 = [Link]([vertexPts[0],midPt0,midPt2,vertexPts[0]])
[Link](triangle0)
triangle1 = [Link]([midPt0,vertexPts[1],midPt1,midPt0])
[Link](triangle1)
triangle2 = [Link]([midPt1,vertexPts[2],midPt2,midPt1])
[Link](triangle2)
if maxGen == 0:
return
else:
subtriangle(triangle0,maxGen-1)
subtriangle(triangle1,maxGen-1)
subtriangle(triangle2,maxGen-1)
recursiveExample()
p.44 El Espacio Habitable otoño 2022
Sterpinski p.45
T_27 Recursive
import rhinoscriptsyntax as rs
tree = []
[Link](False)
def main():
branching(line,gen,scale,angle)
def branching(line,maxGen,scale,angle):
startPt = [Link](line)
endPt = [Link](line)
vec = [Link](endPt,startPt)
vec = [Link](vec,scale)
axis = [0,0,1]
vec0 = [Link](vec,angle,axis)
vec1 = [Link](vec,-angle,axis)
newEndPt0 = [Link](endPt,vec0)
newEndPt1 = [Link](endPt,vec1)
line0 = [Link](endPt,newEndPt0)
[Link](line0)
line1 = [Link](endPt,newEndPt1)
[Link](line1)
if maxGen == 0:
return
else:
branching(line0,maxGen-1,scale,angle)
branching(line1,maxGen-1,scale,angle)
main()
[Link]()
p.46 El Espacio Habitable otoño 2022
Fractal tree generator p.47
GhPython Script
Mariana H Barroeta