Rhino Python Tutorial
Rhino Python Tutorial
Algorithm
Python, Rhino.Python Cellular Automata Implementation Finalization
! Finalization
! ! Programming style Toolbar creation for UI
Pramod Parajuli, 2011 Cellular Automata & PolyMedia Pixel using Rhino.Python
Cellular Automata (Conway's Game of Life) In an unlimited universe, there are four(4) basic rules for a cell;
! ! ! ! Any live cell with fewer than two live neighbours dies, as if caused by underpopulation. Any live cell with two or three live neighbours lives on to the next generation. Any live cell with more than three live neighbours dies, as if by overcrowding. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
In simple terms;
! ! ! ! If already ALIVE and < 2 neighbours, then DIE If already ALIVE and neighbours = 2 or 3, then LIVE If already ALIVE and > 3 neighbours, then DIE If already DEAD and exactly 3 neighbours, then LIVE
GoL - practice
Algorithm
Python, Rhino.Python
Cellular Automata Implementation Finalization
>>> b = a
>>> a[2] = 10
>>> print(b)
???
>>> b = a[:]
>>> a[2] = 30
>>> print(b)
???
Pramod Parajuli, 2011
>>> print(world)
Cellular Automata & PolyMedia Pixel using Rhino.Python Pramod Parajuli, 2011
10
11
>>> c = [ [1, 2, 3], [4, 5, 6]] >>> len(c) ??? >>> zip(*c) ???
12
13
For our world, we need to select only 8 cells around current cell. How to select? We select 3x3 slice and remove the one at the centre. en sum all the 1s in the slice. at will give us the no. of neighbours in the surrounding of a cell.
Pramod Parajuli, 2011
Rhino.Python
Some examples First.py
import rhinoscriptsyntax as rs
rs.AddPoint([1,2,3])
center = [20, 0, 0]
radius = 5
rs.AddSphere(center, radius)
" "
Cellular Automata & PolyMedia Pixel using Rhino.Python
14
Rhino.Python drawSine.py
# settin z to a scaled value with intercept
z = math.sin(currentRadian) * 70
x = currentAngle
y = currentAngle
drawEnd = [x, y, z]
rs.AddLine(drawStart, drawEnd)
drawStart = drawEnd
import rhinoscriptsyntax as rs
import math
def drawSineWave():
print 'Drawing sine wave'
sineHeight = rs.GetReal("Please enter height:")
x = -30
y = -30
z = math.sin(-30 * math.pi / 180.0) * 70
drawStart = [x, y, z]
for currentAngle in range(-90, 90):
currentRadian = currentAngle * math.pi / 180.0
Cellular Automata & PolyMedia Pixel using Rhino.Python
15
Second.py
import rhinoscriptsyntax as rs
rs.EnableRedraw(False)
for i in range(0, 50):
if(i%5 == 0):
else:
rs.AddPoint([i, 0, 0])
rs.AddSphere([i, 0, 0], 0.5)
drawBox.py - demo
rs.EnableRedraw(True)
Pramod Parajuli, 2011
16
def main():
rstCurve = rs.GetObject("Select rst curve")
secondCurve = rs.GetObject("Select second curve")
# creating instance of Tutorial newCurve
# We will be using this instance holds the data about
# curves and also can execute its function on the data.
newCurve = Tutorial('Curve drawing tutorial',
rstCurve, secondCurve)
newCurve.displayName()
newCurve.drawEdgeSurface()
if( __name__ == '__main__' ):
main()
Pramod Parajuli, 2011
17
Cellular Automata & PolyMedia Pixel using Rhino.Python Pramod Parajuli, 2011
18
19
20
21
22
23
24
25
26
27
! !
A function to update graphics on the Rhino3d according to life states in buffer - updateGraphics
A function to update world from buffer - updateGraphics
Pramod Parajuli, 2011
28
29
A function to draw box on the Rhino 3D drawBox(basePoint, length, width, height, objColor)
A function to add two lists numerically by elements (used by drawBox) addLists(list1, list2)
A main program to de ne thread of control - main
Pramod Parajuli, 2011 Cellular Automata & PolyMedia Pixel using Rhino.Python
Polymedia Pixel
Finalization
PolyMedia Pixel Spreading a component object over di erent types of surfaces (polysurface, opensurface) Di erent tasks required to be done to accomplish it
Join and explode surfaces Evaluate surface to retrieve points Explode meshes or mesh objects into sub-meshes Playing with layers Surface normal, and object orientation
30
31
Other utility functions such as view and plane for orientation, boundingbox for scaling etc.
Cellular Automata & PolyMedia Pixel using Rhino.Python Pramod Parajuli, 2011
32
33
34
35
for aSurface in objSurfaces:
DivideSlab(aSurface, 5, newLayer)
rs.LayerVisible(newLayer, True)
rs.EnableRedraw(True)
rs.LayerVisible(oldLayer, False)
36
37
38
39
Check surface type If surface is poly-surface, explode and retain all open surfaces, if not will have only one open surface For all open surface/s, evaluate the surface according to no. of slice user want to do Ask user whether to scale component or not, scale accordingly Make copies of the component object, move them to center of all the evaluated point on the surface, then orient them to surface normal
Pramod Parajuli, 2011 Cellular Automata & PolyMedia Pixel using Rhino.Python Pramod Parajuli, 2011
40
Finalizing
Creating custom toolbar to run script
Edit the icon.
41
Right click on the empty space by the Provide button text and tooltip text.
side of existing tabs Click Insert Toolbar In Select Toolbar box, click New Provide name to the toolbar RhinoTutorial Now you can see newly created toolbar.
"
In the macro library, select New Macro In the command box: include followings:
!-_RunPythonScript ("
# your python code here
)
Finalization
toolbar.
42