Intro to Python Scripting: 24 Recursion Cracking
In this tutorial we create a cracking algorithm using recursion in python. This tutorial is inspired by the Aranda/Lasch Pamphlet Architecture 27 : Tooling. It's an awesome book, I highly reccommend. Anyway, we start with a polygon and find it's area centroid. We then explode the polygon and extract each of the line segements. For each line segment we get the start and endpoints of the line and create a new polygon from those points and the centroid. We then add all of those new polygons to the polygon list and repeat until the count gets to zero.
#cracking algorithm import rhinoscriptsyntax as rs import Rhino import scriptcontext def crackpolygon(pls, count): temppls = pls pls = [] if count == 0: return 1 else: for pl in temppls: if rs.CloseCurve(pl) == False: print "Not a closed curve" else: #print "Cool" centroid = rs.CurveAreaCentroid(pl) centpt = rs.AddPoint(centroid[0]) curves = rs.ExplodeCurves(pl) for crv in curves: #print crv pt1 = rs.CurveStartPoint(crv) pt2 = rs.CurveEndPoint(crv) pts = [] pts.append(pt1) pts.append(pt2) pts.append(centpt) pts.append(pt1) newpl = rs.AddPolyline(pts) pls.append(newpl) rs.DeleteObject(crv) cleanup = [] cleanup.append(centpt) #cleanup.append(curves) rs.DeleteObjects(cleanup) count = count - 1 return crackpolygon(pls, count) count = rs.GetInteger("How many iterations would you like to do?", 3) pl = rs.GetCurveObject("pick a closed curve to crack") plguid = pl[0] polygons = [] polygons.append(plguid) crackpolygon(polygons, count)
Want to Contribute?
Want to be an author? Drop us a line here we'd love to have you.
Already have a video you'd like to post? Send us a link and we'll get you going.
:)