s206e057 -- lecture 23, 4/29/2020, interaction between python and grasshopper · 2020. 4. 29. ·...

10
S206E057 – Spring 2021 Page 1 (5/26/2021) Copyright ©2021, Chiu-Shui Chan. All Rights Reserved. This lecture covers the techniques of using rhinoscriptsyntax in Rhino through Python for form generation. AddPolyline, AddPoints, AddInterpCurve, and AddLoftSrf are popular methods used and are introduced here briefly for modeling reference. Interactions between Python and Grasshopper are also explained in certain level of detail. Methods of generating polylines through Python codes: Method A: applying control points to generate the curve. See the image on the right. import rhinoscriptsyntax as rs points = [(0,0,0),(20,10,0),(50,-10,0),(100,50,0),(200,0,0),(250,10,0)] rs.AddInterpCurve(points) AddInterpCurve(points) is a function that will generate a curve, which is a non- rational NURBS curve. Method B: applying control points to generate a curve and display its control points in red color. import rhinoscriptsyntax as rs points = [(0,0,0),(20,10,0),(50,-10,0),(100,50,0),(200,0,0),(250,10,0)] LnList = rs.AddInterpCurve(points) rs.ObjectColor(LnList,[0,0,255]) rs.AddPolyline(points) PtsIds = rs.AddPoints(points) rs.ObjectColor(PtsIds, [255,0,0]) #[255,0,0] is the list of RGB color code. Method C: Enclosed curve. import rhinoscriptsyntax as rs points = [(-12,8.5,0),(0,2.5,0),(12,8.5,0),(18,2,0),(0,-20,0),(-18,2,0),(-12, 8.5,0)] cruve1=rs.AddInterpCurve(points,3,3) #3 is the degree of the curve, 1 is polyline. #5 is the knotstyle with sqrt spacing (3 is for uniform spacing) ptsids = rs.AddPoints(points) rs.ObjectColor(ptsids, [255,0,0]) rs.ObjectColor(cruve1, [0,0,255]) ObjectColor(object_ids, color, [red, green, blue]) is a function that will generate RGB colors for the selected objects shown in the wireframe display mode, but, not in the Rendered mode. Method D: Creating a series of columns through Python codes. In this example, users are required for the following data input. 1. Values of radius and the height of the column, 2. Number of columns along the X and Y axis, 3. Intended distance on both X & Y directions, and 4. Location of the lower left corner point to start the column series generation. S206E057 -- Lecture 23, 5/27/2021, Interaction between Python and Grasshopper

Upload: others

Post on 24-Jan-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

  • S206E057 – Spring 2021

    Page 1 (5/26/2021)

    Copyright ©2021, Chiu-Shui Chan. All Rights Reserved. This lecture covers the techniques of using rhinoscriptsyntax in Rhino through Python for form generation. AddPolyline, AddPoints, AddInterpCurve, and AddLoftSrf are popular methods used and are introduced here briefly for modeling reference. Interactions between Python and Grasshopper are also explained in certain level of detail. Methods of generating polylines through Python codes: Method A: applying control points to generate the curve. See the image on the right.

    import rhinoscriptsyntax as rs points = [(0,0,0),(20,10,0),(50,-10,0),(100,50,0),(200,0,0),(250,10,0)] rs.AddInterpCurve(points)

    AddInterpCurve(points) is a function that will generate a curve, which is a non-rational NURBS curve. Method B: applying control points to generate a curve and display its control points in red color.

    import rhinoscriptsyntax as rs points = [(0,0,0),(20,10,0),(50,-10,0),(100,50,0),(200,0,0),(250,10,0)] LnList = rs.AddInterpCurve(points) rs.ObjectColor(LnList,[0,0,255]) rs.AddPolyline(points) PtsIds = rs.AddPoints(points) rs.ObjectColor(PtsIds, [255,0,0]) #[255,0,0] is the list of RGB color code.

    Method C: Enclosed curve.

    import rhinoscriptsyntax as rs points = [(-12,8.5,0),(0,2.5,0),(12,8.5,0),(18,2,0),(0,-20,0),(-18,2,0),(-12, 8.5,0)] cruve1=rs.AddInterpCurve(points,3,3) #3 is the degree of the curve, 1 is polyline. #5 is the knotstyle with sqrt spacing (3 is for uniform spacing) ptsids = rs.AddPoints(points) rs.ObjectColor(ptsids, [255,0,0]) rs.ObjectColor(cruve1, [0,0,255])

    ObjectColor(object_ids, color, [red, green, blue]) is a function that will generate RGB colors for the selected objects shown in the wireframe display mode, but, not in the Rendered mode. Method D: Creating a series of columns through Python codes. In this example, users are required for the following data input.

    1. Values of radius and the height of the column, 2. Number of columns along the X and Y axis, 3. Intended distance on both X & Y directions, and 4. Location of the lower left corner point to start the column series generation.

    S206E057 -- Lecture 23, 5/27/2021, Interaction between Python and Grasshopper

  • S206E057 – Spring 2021

    Page 2 (5/26/2021)

    Creation of columns will be executed by the AddCylinder function and two while loops for generating a series of columns along Y axis (by the imbedded while loop) and move to every next X by the first while loop. This program, intended to be run privately without sharing it as “class”, so, the “if __name__ == “__main__” function was used for calling the “ColSeries” function. Coding and run results are shown in the following. Note: The starting point of while loops could be 0 or 1. If i & j are 0, then while loops should be controlled by i Components folder. Save the gha file in the Components folder, which is: Users/cschan/AppData/Roaming/Grasshopper/Libraries

    http://www.food4rhino.com/project/ghpython

  • S206E057 – Spring 2021

    Page 3 (5/26/2021)

    2. Right-click the file > Properties > make sure there is no "blocked" text (unblock). 3. Restart Rhino and Grasshopper

    Optional: We could use the _GrasshopperDeveloperSettings command to add the output directory for your .gha to the list of directories that Grasshopper pays attention to. For instance, in the following example, the output file will be sent to the folder with path of c:\534. Example one:

    1. Here is the completed Python code of generating 24 pieces of circles, points, and lines from the “for” loop. In the coding, the definition of if-then-else on line five is to set the valuable of x empty first before assigning 24 to it.

    # sample script to show how to use Python in Rhino with rhinoscriptsyntax import math import rhinoscriptsyntax as rs x = None #x could also defined as x=24 here. if x is None: x = 24 circles = [] # the group of circles. radii = [] # the group of radii. for i in range(x): pt = (i, math.cos(i), 0) # a tuple for a point id1 = rs.AddCircle(pt, 0.3) # the x coordinate of a new point is the value of i. circles.append(id1) # add the circle to the list. endPt = rs.PointAdd(pt, (0, 0.3, 0) ) #add the value of 0, 0.3,0 to the xyz of pt. id2 = rs.AddLine(pt, endPt) radii.append(id2)

    2. This coding will be ported into Grasshopper. Thus, Restart Rhino, activate Grasshopper > Maths > Script > select Python Script and drag the component to the Grasshopper screen. Now, the Python component is on the screen.

    3. Double click the Python name area > find the Open Editor function and select it to open the Grasshopper Python Script Editor (or type py) > load (or write) the Python file to the window. This could be done by copy and paste.

    4. The data input methods used in Python are variable declarations. In Grasshopper, it is defined by number sliders or panels. Thus, we have to change the input method in the GHPython script, for instance, adding a number slider to dynamically control the x variable with range of 1

  • S206E057 – Spring 2021

    Page 4 (5/26/2021)

    5. Meanwhile, change the statement of x = 24 on line number 5 as a comment. Result of this will let the Python understand that the data of the variable of x shall be obtained from the number slider mechanism. The default Python component input names of X and Y could be changed to match with the variable names used in Python codes. This could be done by moving the mouse on top of the X (for instance) and right click to change the name on the property window.

    6. At this time, we also have to define the variable type of this x as an integer type. Otherwise, the GH will not be able to process the data. Methods of defining Python coding related variable type are the following:

    Right click x variable > Select Type Hint: Int > and Wire Display > Default.

    7. Put another number slider, which is a floating number representing the variable of y. Connect the output of the slider to the input of y, which controls the number of 0.3 in the statement of id1 = rs.AddCircle(pt, 0.3) on line number 12. So, replace the parameter of 0.3 in the id1 variable with y, as id1 = rs.AddCircle(pt, y).

    8. Add another variable of z by right click the Python text area > select the variable manager and add an extra variable of Z; or zoom to 500% of the screen to see the + - sign for adding variables.

    9. This z variable controls the points’ y coordinate value on line# 11 (see the image below) as pt = (i, math.cos(i)/z, 0).

    10. Add a variable of “a” on line# 19 as a=circles or b=radii (see the image below) to declare the output of GH component to see results. Then, click on the “Test” button on the lower left corner of the Editor and OK to complete the Python codes for GH component. (Note: whenever you want to run the Python codes, just click on the Test button.)

  • S206E057 – Spring 2021

    Page 5 (5/26/2021)

    Generating multiple outputs:

    1. Divide the case of the last Python coding example to one circle GH component for showing circles. This is the first example of showing one output variable.

    2. The second example of showing two output variables of circles and the center point of the circles (CenterPt). These two variables are defined outside of for loop and after the loop is completed, they are available for output.

    3. The third example of output three variables to show the center point of the circle (controlPtslist), the circle (circles), and the end point of the radius (controlPtelist).

    4. We could also combine all output units together to make the program more efficient. Methods are to define four variables of (circles, NorthLine, controlPtslist and controlPtelist) in Python script, operate the functions and statements, then define four output variables in GH component and name them correctly to see geometric results. (Note: the variable input, and execution output of Python codes through GH are more efficient than executing results directly in Python. Also, it is more easy to control the modeling behavior through customized Python coding in GH.) See the results in the following image.

  • S206E057 – Spring 2021

    Page 6 (5/26/2021)

    Example 2: Example of Extruding Rhino surfaces The concept is to explore the different interactions between Rhino model > Python, and Rhino model > Python GH. Results are shown on the right side of the picture. Steps of doing it are explained shortly below. Step 1: Generate a Rhino module

    a. Apply Surface Menu > Corner points to draw two surfaces. b. Use Boolean Difference to create holes on the surface. c. Draw a line, polylilne, or Curve > Free-form > Control points on Front view to represent the distance of

    extrusion. Either geometry should work fine. Step 2: Develop Python codes for extruding the surface There are two variables of curveObjs and extaxis applied here for saving the geometries of the surface and the extrusion line. After the script is done, then run the Python script to see the results. Codes will be exported to GH next.

    import rhinoscriptsyntax as rs curveObjs=rs.GetObject("Select faces to extend") extaxis = rs.GetObject("Select extension reference") Extrusion=rs.ExtrudeSurface(curveObjs,extaxis)

  • S206E057 – Spring 2021

    Page 7 (5/26/2021)

    Step 3: Python Grasshopper coding

    a. Insert a Python Script component in GH (put the following codes to the Python Script window). b. Set up GH parameters of BREP and Curve in GH, select the resulting boolean surface for BREP and the extension

    line for the Curve. c. Change the name of x and y input in GH to match the Python variable names of: “curveObjs” and “extaxis”, the

    input type will be set up as brep and curve automatically. d. Make the Python input code as comments (because we will apply GH input mechanisms), rename the output “a” as

    the variable name of ExtrudeSurface, see the last line below.

    import rhinoscriptsyntax as rs import Rhino.Geometry as rg #curveObjs=rs.GetObject("Select faces to extend") #extaxis = rs.GetObject("Select extension reference") extrusion=rs.ExtrudeSurface(curveObjs,extaxis)

    Note 1: if you received a run time error message of missing a guid for the curve or for the brep, it might be that the ID (represented by the guid) of the geometry was missed when the geometry was unappropriated selected. It will be called only once, and the way to resolve it is to erase the GH component and reinstall it again. Note 2: this is the simplest method of creating floor plane or solid surfaces directly from GH that runs your own python codes, which could be baked to Rhino as its own geometries. Results of the extrusion Example of loft curves to make surfaces: In this example, there are two curves. The intension is to apply AddLoftSrf to generate a face between these two curses. Here are the Python codes:

    import rhinoscriptsyntax as rs import Rhino.Geometry as rg HEX1=rs.GetObject("Select the first curve ") HEX2=rs.GetObject("Select the second curve ") Surface = rs.AddLoftSrf((HEX1,HEX2))[0]

    Or use HEX = rs.GetObjects(“Curves”, 4) Surface = rs.AddLoftSrf(HEX)[0] The codes are converted to Python GH scripts as GH component with HEX1 and HEX2 variables accepting two “Curve” parameter inputs.

  • S206E057 – Spring 2021

    Page 8 (5/26/2021)

    The AddLoftSrf has the format of: rs.AddLoftSrf (object_ids, start=None, end=None…) In this example of rs.AddLoftSrf ((HEX1,HEX2))[0],

    1. (HEX1,HEX2) is seen as a list, that has two members. This list of two members is also treated as the object_ids of the two curves.

    2. The [0] means the first element on the list, which is HEX1, shall be used as the starting curve element for the lofted object creation.

    In Python GH programming syntax, a pair of parenthesis (a,b) makes a “tuple”, whereas [a,b] makes a list. Tuples and lists are used in different situations and for different purposes. Tuples are immutable, and contain a heterogeneous sequence of elements that are accessed via unpacking or indexing. Lists are mutable and their elements are usually homogeneous and are accessed by iterating over the list. Applications: This loft function could be utilized to generate some simple organic forms in architectural design. F Appendix: Three versions of the repeated loops for generating column series: import rhinoscriptsyntax as rs cheight = rs.GetInteger("Heigh:",10,3,50) cradius = rs.GetReal("Radius:",0.5,0.25,4) xnum = rs.GetInteger("X number:",5,3,10) ynum = rs.GetInteger("y number:",5,3,10) xint = rs.GetInteger("X bay:",10,5,20) yint = rs.GetInteger("y bay:",10, 5, 20) stpt = rs.GetPoint("Starting point") #Two of for loops for i in range (xnum): holdy = stpt[1] for i in range (ynum): rs.AddCylinder(stpt,cheight,cradius,True) stpt[1] = stpt[1] + yint stpt[1]=holdy stpt[0] = stpt[0] + xint

  • S206E057 – Spring 2021

    Page 9 (5/26/2021)

    #Two of while loops x = 0 while x < xnum: holdy = stpt[1] y = 0 while y < ynum: rs.AddCylinder(stpt,cheight,cradius,True) stpt[1] = stpt[1] + yint y = y + 1 x = x + 1 stpt[1]=holdy stpt[0] = stpt [0] + xint #One for and one while loop for i in range (xnum): holdy = stpt[1] x = 0 while x < ynum: rs.AddCylinder(stpt,cheight,cradius,True) stpt[1]=stpt[1]+yint x = x + 1 stpt[0] = stpt[0] + xint

    stpt[1] = holdy Appendix B – python in GH Example of putting column python creation in GH component. The Column GH component for running Python codes.

  • S206E057 – Spring 2021

    Page 10 (5/26/2021)

    """Provides a scripting component. Inputs: colHgt: The column height variable xNum: The number of column in X axis variable Output: cList: The column output variable""" __author__ = "cschan" __version__ = "2021.05.25" import rhinoscriptsyntax as rs cList = [] for i in range (xNum): holdy = stPt[1] for i in range (yNum): cItem = rs.AddCylinder(stPt,colHgt,colRadius,True) cList.append(cItem) stPt[1] = stPt[1] + yInt stPt[0] = stPt[0] + xInt stPt[1] = holdy

    Footnote: Please check the assignment handout to find out the expectation and requirements of the final project. The most important thing is to load all files to the same folder and submit the folder for grading. Don’t miss any files.