Variable and types, introduction of rhinoscriptsyntax

Installation of GhPython

  1. Download GhPython 0.5.1.0 (GhPython.gha) from http://www.food4rhino.com/project/ghpython
  2. Run Rhino 5. (Make sure you are running Rhino 5. GhPython does not work with Rhino 4)
  3. Command:Grasshopper -> File -> Special Folders -> Components Folder
  4. Copy (or move) the GhPython.gha file to the specified Components Folder
  5. Restart Rhino 5.
  6. Command:Grasshopper
  7. Check if there is a new component "Python Script" in the Script group in the Math tab.

1. Variable and types

Command:Grasshopper->Maths->Script->Python
Place a Python component in your GH document.
If you double-click the python component, an editor will appear.
Type

a=1
print a
print type(a)
a="Hello"
print a
print type(a)

Hit the Test button.
Check if the output is
|1
|<type 'int'>
|Hello
|<type 'str'>

Congraturation :)
**Note that the type of the variable a changed. Python is "dynamic typing", whereas C, C++, Java, C# are "static typing"
If you connect a Panel component to the "out" slot of the python component, you can see the output like this.
1.png

2. Inputs and outputs, basic arithmetic operations

Zoom in to the GhPython component and add b to the output variables.
2.png
Connect sliders to the slots whose names are x and y.
Tyy

a=x+y
b=x*y

You may get
3.png

3. import math

"import something" syntax enables functions provided by the module "something"

input: x
output: cosX, sinX

import math
cosX=math.cos(x)
sinX=math.sin(x)

4.png

4. import rhinoscriptsyntax as rs

input
"import LooooongTeeeeerm as LT" can define an alias (short name) of the module "LoooooongTeeeeerm"
**rhinoscriptsyntax is very important in that it provides almost all the geometric manipulations supported by Rhino.

input: x
output: a

import math
import rhinoscriptsyntax as rs
cosX=math.cos(x)
sinX=math.sin(x)
a=rs.AddPoint(cosX,sinX,0)

Now, you can see a point geometry around the origin. Congraturation!
5.png
You can connect many sliders like this.
6.png
Then, you get this.
7.png

5. Playing around rhinoscriptsyntax (Circles)

8.png
import math
import rhinoscriptsyntax as rs
circle1=rs.AddCircle(C,x)
circle2=rs.AddCircle3Pt(P1,P2,P3)

11.png12.png

5. Playing around rhinoscriptsyntax (Polyline)

[(1,2,3),(2,2,3),(3,4,3),(5,3,4)] will be further discussed in later sessions.

import rhinoscriptsyntax as rs
a=rs.AddPolyline([(1,2,3),(2,2,3),(3,4,3),(5,3,4)])

13.png