s206e057 -- lecture 23, 5/26/2016, interaction between...

8
S206E057 – Spring 2016 Page 1 (5/1/2016) Copyright ©2016, Chiu-Shui Chan. All Rights Reserved. This lecture covers the techniques of using rhinoscriptsyntax in Python particularly for form generation. AddPolyline, AddPoints, AddInterpCurve, and AddLoftSrf are popular methods used and are introduced here briefly for our reference. Then the interaction between Python and Grasshopper are explained in detail. Methods of generating polylines through Python codes: Method A: applying control points to generate the curve. See the image. 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 periodic 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) rs.AddInterpCurve(points) rs.AddPolyline(points) pts_ids = rs.AddPoints(points) rs.ObjectColor(pts_ids, [255,0,0]) 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. pts_ids = rs.AddPoints(points) rs.ObjectColor(pts_ids, [255,0,0]) ObjectColor(object_ids, color, [red, green, blue]) is a function that will generate RGB colors for the selected objects shown in the wireframe mode, but, not in the Rendered mode. Method D: Creating a series of columns through Python codes. In this example, users are required to input: 1. The values of radius and the height of the column, 2. The number of columns along the X and Y axis, 3. The intended distance on both X & Y directions, and 4. The location of the lower left corner point to start the column series. Creation of columns will be executed by the AddCylinder function and two while loops to control the X and maintain the same X coordination while generating the columns along the Y direction. 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. S206E057 -- Lecture 23, 5/26/2016, Interaction between Python and Grasshopper

Upload: hadan

Post on 01-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: S206E057 -- Lecture 23, 5/26/2016, Interaction between ...cschan.public.iastate.edu/WU/TU_Lect23_Python_Grasshopper.pdf · This lecture covers the techniques of using ... You can

S206E057 – Spring 2016

Page 1 (5/1/2016)

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

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 periodic 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) rs.AddInterpCurve(points) rs.AddPolyline(points) pts_ids = rs.AddPoints(points) rs.ObjectColor(pts_ids, [255,0,0])

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. pts_ids = rs.AddPoints(points) rs.ObjectColor(pts_ids, [255,0,0])

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

1. The values of radius and the height of the column, 2. The number of columns along the X and Y axis, 3. The intended distance on both X & Y directions, and 4. The location of the lower left corner point to start the column series.

Creation of columns will be executed by the AddCylinder function and two while loops to control the X and maintain the same X coordination while generating the columns along the Y direction. 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.

S206E057 -- Lecture 23, 5/26/2016, Interaction between Python and Grasshopper

Page 2: S206E057 -- Lecture 23, 5/26/2016, Interaction between ...cschan.public.iastate.edu/WU/TU_Lect23_Python_Grasshopper.pdf · This lecture covers the techniques of using ... You can

Arch534 – Spring 2016

Page 2 (5/1/2016)

How to connect Python to Grasshopper in Rhino? There is a Python interpreter in Grasshopper, now in version 0.5.101 called GhPython. The Python syntax in Grasshopper could be found in Food4Rhino Web page. GhPython has the following nature:

• The GhPython component controls the values of inputs to and outputs from the components. • It includes libraries to the .NET SDK and huge number of Python add-on functions. • It integrates with the new Python Script Editor included in Rhino 5.

To install this Grasshopper add-on: if you are new, then you have to connect to the Internet at “food4Python” page, which should be registered as its member first. The GhPython is an open source; its membership and registration are both for free. You can get a version of this add-on ready for installation from http://www.food4rhino.com/project/ghpython. But, a copy is saved on the My.Files server. Here are the sequences of installation.

1. In Grasshopper, choose File > Special Folders > Components folder. Save the gha file in the Components folder, which is: Users/cschan/AppData/Roaming/Grasshopper/Libraries

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.

Page 3: S206E057 -- Lecture 23, 5/26/2016, Interaction between ...cschan.public.iastate.edu/WU/TU_Lect23_Python_Grasshopper.pdf · This lecture covers the techniques of using ... You can

S206E057 – Spring 2016

Page 3 (5/1/2016)

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 > 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<20<50 and link the slider output to the Python GH component’s x input.

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:

Page 4: S206E057 -- Lecture 23, 5/26/2016, Interaction between ...cschan.public.iastate.edu/WU/TU_Lect23_Python_Grasshopper.pdf · This lecture covers the techniques of using ... You can

Arch534 – Spring 2016

Page 4 (5/1/2016)

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 a=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.)

Page 5: S206E057 -- Lecture 23, 5/26/2016, Interaction between ...cschan.public.iastate.edu/WU/TU_Lect23_Python_Grasshopper.pdf · This lecture covers the techniques of using ... You can

S206E057 – Spring 2016

Page 5 (5/1/2016)

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.

Page 6: S206E057 -- Lecture 23, 5/26/2016, Interaction between ...cschan.public.iastate.edu/WU/TU_Lect23_Python_Grasshopper.pdf · This lecture covers the techniques of using ... You can

Arch534 – Spring 2016

Page 6 (5/1/2016)

Example 2: Example of Extruding Rhino surfaces The concept is to explore the different interactions between Rhino model > Python, and Rhino model > Python GH. Here we have to also call another Rhino library names: Rhino.Geometry to be used in the coding. Result 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 import Rhino.Geometry as rg curveObjs=rs.GetObject("Select faces to extend") extaxis = rs.GetObject("Select extension reference") Extrusion=rs.ExtrudeSurface(curveObjs,extaxis)

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.

Page 7: S206E057 -- Lecture 23, 5/26/2016, Interaction between ...cschan.public.iastate.edu/WU/TU_Lect23_Python_Grasshopper.pdf · This lecture covers the techniques of using ... You can

S206E057 – Spring 2016

Page 7 (5/1/2016)

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]

The codes are converted to Python GH scripts as GH component with HEX1 and HEX2 variables accepting two “Curve” parameter inputs. 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.

Page 8: S206E057 -- Lecture 23, 5/26/2016, Interaction between ...cschan.public.iastate.edu/WU/TU_Lect23_Python_Grasshopper.pdf · This lecture covers the techniques of using ... You can

Arch534 – Spring 2016

Page 8 (5/1/2016)

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 The Column GH component for running the Python codes. Footnote: After the objects were created, some would like to make the objects colorful through the function of ObjectColor. Of course, the function of ObjectColor is available in Python coding environment. But, it has difficulties to run it inside the Python GH script. From the http://www.grasshopper3d.com/forum/topics/bake-tags-w-color Web page, information shows that “it is currently impossible to assign colors (or material or layers) to Grasshopper objects because the Grasshopper SDK does not pass this information from an object to another. On the other hand, we could bake objects with a color or a layer into the Rhino document.” This is an alternative solution. Expectation and requirements of the final project should check the assignment handout. Here are the summaries for your reference. -- Lectures covered this semester have three modules of Rhino modeling, GH algorithmic modeling and Python programming. Here are the outline of the expected final submission.

1. First level is to have the Rhino model completed for advanced level of parametric operation. Products are Rhino model files.

2. The second level is expected to use GH generate the entire Rhino model. Products would be the gy files.

3. Third level is expected to have at least one Python script done on generating one architectural form of your building. The program should be bug free with clear comments on side to explain the coding methods. Products are the py & gy files.

4. Complete the final with a project summary.

Please load all the files to the server for grading.