Automating Grasshopper Part 5

Course or Collection: 
Video Duration: 
27 minutes
Zach Downey

Unrolling Surfaces and Nesting them with python. In this tutorial we start with the tower that we were working on before and discretize it into quad panels. We automate the nesting of the panels based on their bounding polygons. This isn't super sophisticated nesting but it gets the job done. It's similar to these tutorials Quad Panel Fabrication  and Laser Cutter Workflow but way faster. We actually don't do tabbing in this defintion, maybe in the next one. 

You can get the code for this and all the other lessons on our github.

#Unrolling Surfaces with Python
import rhinoscriptsyntax as rs
import Rhino
#Load Grasshopper Plugin as gh
gh = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
#Turn off Redraw
#Values for GH Sliders
uv = 5
sides = 6
row = uv*uv
#spacing for nesting
offset = 0.25
#Set Sliders Based on Values above
gh.SetSliderValue("SLIDER_GUID", uv)
gh.SetSliderValue("SLIDER_GUID", sides)
#Run it and Bake it
baked = gh.BakeDataInObject("BAKEDOBJECTS_GUID")
#Create an empty list for nesting purposes
#and define the origin for translations
moved = []
origin = [0,0,0]
#Create a couple of Variable for our loop to increment
#We use these to keep track of the rows/columns we are on
columncount = 0
biggestY = 0
totalX = 0
#Iterate through the baked objects
for item in baked:
    #unroll the surfaces and join them.
    #delet the ones we don't need
    unrolltemp = rs.UnrollSurface(item, False)
    joined = rs.JoinSurfaces(unrolltemp)
    #Get the bounding box
    #Bounding Box returns pts we create an empty points list
    boxpts = []
    boxpts = rs.BoundingBox(joined)
    #Get the distance for the x and y values of the box
    xdist = rs.Distance(boxpts[0],boxpts[1])
    ydist = rs.Distance(boxpts[1],boxpts[2])
    #Check to see if the ydist is the biggest in the row
    #we use this to space our rows
    if ydist > biggestY:
        biggestY = ydist
    #create a bounding polyine
    #not necessary but help see the border
    polyline = rs.AddPolyline(boxpts[0:4])
    #define endpoint of xmove vector
    xpt = [xdist+offset,0,0]
    #set totalX
    totalX = totalX + xdist + offset
    #define xmove vector
    xmove = rs.VectorCreate(origin,xpt)
    #add objects to the moved list
    #move all the objects
    #increment the column count
    columncount += 1
    #check to see if the columncount is evenly divisible by the row
    #if it is we need to move the column
    if columncount % row == 0:
        #get the biggestY value and add the offset
        #I call this the formfeed (like a typewriter)
        formfeed = biggestY + offset
        #That makes this the carrigeReturn
        carrigeReturn = [-totalX,formfeed,0]
        move = rs.VectorCreate(origin,carrigeReturn)
        #We then move all the objects back and reset the
        #biggestY and totalX values
        biggestY = 0
        totalX = 0
#finally we delete the baked objects


Please rate this tutorial below. Thanks.

Average: 4.6 (5 votes)

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.