├── .gitignore ├── LICENSE ├── Lessons ├── lesson00.txt ├── lesson01.py ├── lesson02.py ├── lesson03.py ├── lesson04.py ├── lesson05.py ├── lesson06.py ├── lesson07.py ├── lesson08.py ├── lesson09.py ├── lesson10.py ├── lesson11.py ├── lesson12.py ├── lesson13.py ├── lesson14.py ├── lesson15.py ├── lesson16.py ├── lesson17.py ├── lesson18.py ├── lesson19.py ├── lesson20.py ├── lesson21.py ├── lesson22.py ├── lesson23.py └── lesson24.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | #vim swp files 2 | *.swp -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Designalyze 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Lessons/lesson00.txt: -------------------------------------------------------------------------------- 1 | #http://designalyze.com/int2pythonscripting00_macros 2 | #This first example is not really python code. These are Rhino commandline 3 | #commands. 4 | Intro to Python Lesson 00: Macros! 5 | 6 | !_SelNone 7 | _SelLast 8 | _Copy 9 | _Pause 10 | 0,0,0 11 | 0,0,1 12 | _Enter 13 | _SelLast 14 | _Rotate 15 | 0,0,0 16 | 10 17 | -------------------------------------------------------------------------------- /Lessons/lesson01.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:01 2 | #http://designalyze.com/int2pythonscripting01_datatypes 3 | #Python Data Types 4 | #Use Rhino's Script Debugger to View the Data Types 5 | 6 | import rhinoscriptsyntax as rs 7 | 8 | #Commets # character is use to comment out lines 9 | 10 | #Special Data Type 11 | x = None 12 | 13 | #Number Data Types 14 | x = 0 15 | x = 1 16 | x = -1 17 | x = 1.0 18 | x = 3.14 19 | x = 3.14j 20 | 21 | #Booleans 22 | x = True 23 | x = False 24 | 25 | #Sequence Data Types 26 | x = "Hello World" 27 | x = 'Hello World' 28 | x = ['h', 'e', 'l', 'l', 'o'] 29 | x = [1,2,3,4,5] 30 | x = [1,"2",'3',"Strings"] 31 | x = (1,2,3) 32 | 33 | #Mapping Data Type 34 | x = {0:'w', 1:'o', 2:'r', 3:'l', 4:'d'} 35 | 36 | 37 | x = None 38 | -------------------------------------------------------------------------------- /Lessons/lesson02.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:02 2 | #http://designalyze.com/int2pythonscripting02_variables-io 3 | #Variables and Simple User input/output 4 | import rhinoscriptsyntax as rs 5 | 6 | #String Examples 7 | strGreeting = "Hello World" 8 | print strGreeting 9 | 10 | strInput = rs.GetString("Type String to Print") 11 | print strInput 12 | 13 | #Number Examples 14 | dblRadius01 = 2.0 15 | print dblRadius01 16 | 17 | dblRadius02 = rs.GetReal("Enter a Number for Radius02", 3.0) 18 | print dblRadius02 19 | 20 | #String Formatters 21 | print "Radius01 : %d \nRadius02 : %d" % (dblRadius01, dblRadius02) 22 | strMessage = "Radius01 : %d \nRadius02 : %d" % (dblRadius01, dblRadius02) 23 | 24 | rs.MessageBox(strMessage) 25 | 26 | ''' 27 | Format Symbol 28 | %c character 29 | %s string conversion via str() prior to formatting 30 | %i signed decimal integer 31 | %d signed decimal integer 32 | %u unsigned decimal integer 33 | %o octal integer 34 | %x hexadecimal integer (lowercase letters) 35 | %X hexadecimal integer (UPPERcase letters) 36 | %e exponential notation (with lowercase 'e') 37 | %E exponential notation (with UPPERcase 'E') 38 | %f floating point real number 39 | %g the shorter of %f and %e 40 | %G the shorter of %f and %E 41 | 42 | 43 | Backslash Notation and Descriptions 44 | \a Bell or alert 45 | \b Backspace 46 | \cx Control-x 47 | \e Escape 48 | \f Formfeed 49 | \n Newline 50 | \r Carriage return 51 | \s Space 52 | \t Tab 53 | \v Vertical tab 54 | \x Character x 55 | 56 | ''' 57 | -------------------------------------------------------------------------------- /Lessons/lesson03.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:03 2 | #http://designalyze.com/int2pythonscripting03_arraysandlists 3 | #Arrays/Lists 4 | import rhinoscriptsyntax as rs 5 | 6 | arrPt1 = [1,3,9] 7 | arrPt2 = [4,5,6] 8 | arrPt3 = [-1,-2,-3] 9 | arrPt4 = [7,8,9] 10 | 11 | rs.AddPoint(arrPt1) 12 | rs.AddPoint(arrPt2) 13 | rs.AddPoint(arrPt3) 14 | rs.AddPoint(arrPt4) 15 | 16 | points = [] 17 | points.append(arrPt1) 18 | points.append(arrPt2) 19 | points.append(arrPt3) 20 | points.append(arrPt4) 21 | 22 | print (points) 23 | 24 | print (points[1]) 25 | 26 | rs.AddPolyline(points) 27 | -------------------------------------------------------------------------------- /Lessons/lesson04.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:04 2 | #http://designalyze.com/int2pythonscripting04_listscurvetypes 3 | #Lists of Points + Curve Types 4 | #Bonus simple for loop 5 | import rhinoscriptsyntax as rs 6 | 7 | listPoints = [] 8 | listPoints = rs.GetPoints(True,True,"Pick a starting point", "Keep picking points until you get tired") 9 | 10 | #Curve Types 11 | myPolyline = rs.AddPolyline(listPoints) 12 | myCurve = rs.AddCurve(listPoints) 13 | myIntpCurve = rs.AddInterpCurve(listPoints) 14 | 15 | #Curve Colors 16 | #Colors are Arrays of [r,g,b] 17 | color01 = [0,255,255] #cyan 18 | color02 = [255,0,255] #magenta 19 | color03 = [255,255,0] #yellow 20 | 21 | #Change Color of Curves 22 | rs.ObjectColor(myPolyline, color01) 23 | rs.ObjectColor(myCurve, color02) 24 | rs.ObjectColor(myIntpCurve, color03) 25 | 26 | #Bonus For Loop 27 | for point in listPoints: 28 | rs.AddPoint(point) 29 | -------------------------------------------------------------------------------- /Lessons/lesson05.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:05 2 | #http://designalyze.com/int2pythonscripting05_forloopandmath 3 | #For Loop to Create Points 4 | #For Loop using the range and frange functions 5 | 6 | import rhinoscriptsyntax as rs 7 | import math 8 | 9 | #Simple Count 10 | for i in range(0,50): 11 | print(i) 12 | 13 | #Count by Even Numbers 14 | for i in range(0, 50, 2): 15 | print(i) 16 | 17 | #Count by Odd Numbers 18 | for i in range(1,50,2): 19 | print(i) 20 | 21 | #Using Rhinoscript's frange to step with floats 22 | for d in rs.frange(0.0, 10.0, 0.1): 23 | print(d) 24 | 25 | for d in rs.frange(0.0, 10.0, 0.1): 26 | rs.AddPoint(d,0,0) 27 | 28 | points = [] 29 | 30 | for d in rs.frange(0.0, 10.0, 0.1): 31 | x = d*math.sin(d) 32 | y = d*math.cos(d) 33 | z = 0.0 34 | rs.AddPoint(x,y,z) 35 | pt = (x,y,z) 36 | points.append(pt) 37 | 38 | curve = rs.AddCurve(points) 39 | -------------------------------------------------------------------------------- /Lessons/lesson06.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:06 2 | #http://designalyze.com/int2pythonscripting06_nestedloops-functions 3 | #Nested For Loops 4 | #color points 5 | import rhinoscriptsyntax as rs 6 | 7 | 8 | def createColoredPoint(x,y,z,r,g,b): 9 | currentColor = [r,g,b] 10 | pt = rs.AddPoint(x,y,z) 11 | rs.ObjectColor(pt, currentColor) 12 | 13 | 14 | rs.EnableRedraw(False) 15 | step = 10 16 | 17 | for x in range(0,256, step): 18 | for y in range(0,256, step): 19 | for z in range(0,256,step): 20 | createColoredPoint(x,y,z,x,y,z) 21 | rs.Redraw() 22 | -------------------------------------------------------------------------------- /Lessons/lesson07.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:07 2 | #http://www.designalyze.com/int2pythonscripting07_controlflow01 3 | #If/Else Statements 4 | 5 | import rhinoscriptsyntax as rs 6 | 7 | color01 = [255,0,255] #magenta 8 | color02 = [0,255,255] #cyan 9 | color03 = [125,38,205] #purple 10 | 11 | rs.EnableRedraw(False) 12 | 13 | for x in range(0,100,1): 14 | for y in range(0,100,1): 15 | pt = rs.AddPoint(x,y,0) 16 | if x % 3 == 0 and y % 5 == 0: 17 | rs.ObjectColor(pt,color01) 18 | elif x % 3 == 0 or y % 5 == 0: 19 | rs.ObjectColor(pt,color02) 20 | else: 21 | rs.ObjectColor(pt,color03) 22 | 23 | 24 | rs.Redraw() 25 | -------------------------------------------------------------------------------- /Lessons/lesson08.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:08 2 | #http://www.designalyze.com/int2pythonscripting08_controlflow02 3 | #If Else with Math 4 | #If Else with Boolean Flag 5 | 6 | import rhinoscriptsyntax as rs 7 | import math 8 | 9 | rs.EnableRedraw(False) 10 | 11 | #boolean flag 12 | flip = True 13 | color01 = [255,0,255] 14 | color02 = [0,255, 255] 15 | 16 | for i in rs.frange(0.0, 10.0, 0.1): 17 | ptsForCurve = [] 18 | for j in rs.frange(0.0, 10.0, 0.1): 19 | x = j 20 | y = i 21 | z = math.sin(i)*math.sin(j) 22 | pt = [x,y,z] 23 | ptsForCurve.append(pt) 24 | curve = rs.AddCurve(ptsForCurve) 25 | if flip == True: 26 | rs.ObjectColor(curve, color01) 27 | flip = False 28 | else: 29 | rs.ObjectColor(curve, color02) 30 | flip = True 31 | 32 | rs.Redraw() 33 | -------------------------------------------------------------------------------- /Lessons/lesson09.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:09 2 | #http://www.designalyze.com/int2pythonscripting09_RandomNum01 3 | #Random Numbers 4 | 5 | import rhinoscriptsyntax as rs 6 | import random 7 | 8 | #prints a random floating point number from 0.0 to 1.0 9 | print (random.random()) 10 | #prints a random integer from 0 to 100 11 | print (random.randint(0,100)) 12 | #prints a random floating point number from 0.0 to 100.0 13 | print (random.uniform(0,100)) 14 | 15 | 16 | for i in range(0,10000): 17 | x = random.uniform(0,100) 18 | y = random.uniform(0,100) 19 | z = random.uniform(0,100) 20 | 21 | r = random.randint(0,255) 22 | g = random.randint(0,255) 23 | b = random.randint(0,255) 24 | color = [r,g,b] 25 | pt = rs.AddPoint(x,y,z) 26 | rs.ObjectColor(pt, color) 27 | 28 | -------------------------------------------------------------------------------- /Lessons/lesson10.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:10 2 | #http://www.designalyze.com/int2pythonscripting10_RandomNum02 3 | #Random Numbers 4 | #Random Lines 5 | 6 | import rhinoscriptsyntax as rs 7 | import random 8 | 9 | pts = [] 10 | for i in range(0,100): 11 | x = random.uniform(0,100) 12 | y = random.uniform(0,100) 13 | z = random.uniform(0,100) 14 | pt = [x,y,z] 15 | 16 | pts.append(pt) 17 | 18 | pl = rs.AddPolyline(pts) 19 | crv = rs.AddCurve(pts) 20 | intpcrv = rs.AddInterpCurve(pts) 21 | 22 | color01 = [0,255,255] 23 | color02 = [255,0,255] 24 | color03 = [255,255,0] 25 | 26 | rs.ObjectColor(pl, color01) 27 | rs.ObjectColor(crv, color02) 28 | rs.ObjectColor(intpcrv, color03) 29 | -------------------------------------------------------------------------------- /Lessons/lesson11.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:11 2 | #http://www.designalyze.com/int2pythonscripting11_StringsandLists 3 | #Strings and Lists 4 | 5 | string01 = 'Hello World!' 6 | string02 = "Python Script is Awesome" 7 | 8 | #Accessiong Values in Strings 9 | print "string01[0]: ", string01[0] 10 | print "string02[7:13]: ", string02[7:13] 11 | print "string02 length = ", len(string02) 12 | print "string02[-7:]: = ", string02[-7:] 13 | 14 | #Replace Values 15 | string03 = string02.replace("i","1") 16 | print string03 17 | 18 | #Lists 19 | list01 = ['pt1', 'pt2', 'pt3', 'pt4'] 20 | print list01[2] 21 | print list01[1:3] 22 | print list01[-1] 23 | 24 | #add to list 25 | list01.append('pt5') 26 | print list01 27 | 28 | #remove from list 29 | del list01[2] 30 | print list01 31 | 32 | #list length 33 | print len(list01) 34 | 35 | #iterate a list 36 | for x in list01: 37 | print x 38 | 39 | #membership test 40 | print 'pt2' in list01 41 | print 'pt3' in list01 42 | -------------------------------------------------------------------------------- /Lessons/lesson12.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:12 2 | #http://www.designalyze.com/int2pythonscripting12_VectorTransformation 3 | #Transformations 4 | 5 | import rhinoscriptsyntax as rs 6 | import random 7 | 8 | #ask user to create a point 9 | userPt = rs.GetPoint("create a point") 10 | pt = rs.AddPoint(userPt) 11 | 12 | #create a list of points and append pt to the list 13 | pts = [] 14 | pts.append(pt) 15 | 16 | for i in range(0,100): 17 | xDir = random.uniform(-10.0, 10.0) 18 | yDir = random.uniform(-10.0, 10.0) 19 | zDir = 0.0 20 | vect = (xDir, yDir, zDir) 21 | newPt = rs.CopyObject(pts[-1],vect) 22 | pts.append(newPt) 23 | 24 | myPolyline = rs.AddPolyline(pts) 25 | -------------------------------------------------------------------------------- /Lessons/lesson13.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:13 2 | #http://www.designalyze.com/int2pythonscripting13_SimpleRecursion 3 | 4 | #Factorial 5 | def factorial(n): 6 | if n == 0: 7 | return 1 8 | else: 9 | return n * factorial(n-1) 10 | 11 | 12 | print factorial(5) 13 | 14 | #Fibonacci Sequence 15 | #1,1,2,3,5,8,13,21 16 | 17 | 18 | def fib(n): 19 | if n == 0: 20 | return 0 21 | if n == 1: 22 | return 1 23 | return fib(n-1) + fib(n-2) 24 | 25 | 26 | for i in range(0,15): 27 | print fib(i) 28 | 29 | import rhinoscriptsyntax as rs 30 | 31 | def RecursiveCircle(pt, r): 32 | if r == 0: 33 | return 1 34 | else: 35 | rs.AddCircle(pt, r) 36 | return RecursiveCircle(pt, r-1) 37 | 38 | 39 | pt = rs.GetPoint("Pick starting point") 40 | 41 | RecursiveCircle(pt, 10) 42 | -------------------------------------------------------------------------------- /Lessons/lesson14.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:14 2 | #http://www.designalyze.com/int2pythonscripting13_NotSoSimpleRecursion 3 | 4 | import rhinoscriptsyntax as rs 5 | 6 | def RecursiveScale(objID,scalePt,scaleFact, scaleVect, num): 7 | if num == 0: 8 | return 0 9 | else: 10 | sc = (1.0 / scaleFact) 11 | scaleVect = [x - sc for x in scaleVect] 12 | rs.ScaleObject(objID, scalePt, scaleVect, True) 13 | return RecursiveScale(objID, scalePt, scaleFact, scaleVect, num-1) 14 | 15 | 16 | objID = rs.GetObject() 17 | scalePt = rs.GetPoint("Pick Scale Center") 18 | scaleFact = rs.GetReal("Enter a scale Factor", 10, 0) 19 | scaleVect = [1.0, 1.0, 1.0] 20 | num = rs.GetInteger("Enter a the number of iterations", 10, 1) 21 | 22 | RecursiveScale(objID, scalePt, scaleFact, scaleVect, num) 23 | -------------------------------------------------------------------------------- /Lessons/lesson15.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:15 2 | #http://www.designalyze.com/int2pythonscripting15_ExportPts2CSV 3 | 4 | #Export Points to CSV 5 | 6 | import rhinoscriptsyntax as rs 7 | 8 | #Select our points 9 | pts = rs.GetObjects("Select Points for CSV Export", 1) 10 | 11 | #create a filename variable 12 | filename = rs.SaveFileName("Save CSV file","*.csv||", None, "ptExport", "csv") 13 | 14 | #open the file for writing 15 | file = open(filename, 'w') 16 | 17 | #create and write a headerline for our CSV 18 | headerline = "X,Y,Z,R,G,B\n" 19 | file.write(headerline) 20 | 21 | #print pts 22 | for pt in pts: 23 | ptCoord = rs.PointCoordinates(pt) 24 | x = ptCoord[0] 25 | y = ptCoord[1] 26 | z = ptCoord[2] 27 | color = rs.ObjectColor(pt) 28 | print color 29 | r = color.R 30 | g = color.G 31 | b = color.B 32 | print "x: %.4f, y: %.4f, z: %.4f, r: %d, g: %d, b: %d" %(x,y,z,r,g,b) 33 | line = "%.4f,%.4f,%.4f,%d,%d,%d \n" %(x,y,z,r,g,b) 34 | file.write(line) 35 | 36 | #Close the file after writing! 37 | file.close() 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lessons/lesson16.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:16 2 | #http://www.designalyze.com/int2pythonscripting16_ImportPtsFromCSV 3 | 4 | #Import Points from CSV 5 | 6 | import rhinoscriptsyntax as rs 7 | 8 | #Select a file to open 9 | filename = rs.OpenFileName("Open CSV file","*.csv|", None, None, None) 10 | 11 | #open the file for reading 12 | file = open(filename, 'r') 13 | 14 | lines = file.readlines() 15 | 16 | file.close() 17 | 18 | #delete the first line because it's a header 19 | del lines[0] 20 | #print to check the data 21 | print(lines) 22 | 23 | ptNumber = 0 24 | 25 | for line in lines: 26 | #remove the \n 27 | line = line.strip() 28 | #split the line by the comma 29 | ptInfo = line.split(',') 30 | x = float(ptInfo[0]) 31 | y = float(ptInfo[1]) 32 | z = float(ptInfo[2]) 33 | r = int(ptInfo[3]) 34 | g = int(ptInfo[4]) 35 | b = int(ptInfo[5]) 36 | pt = rs.AddPoint(x,y,z) 37 | color = (r,g,b) 38 | rs.ObjectColor(pt, color) 39 | name = "pt_" + str(ptNumber) 40 | rs.ObjectName(pt,name) 41 | ptNumber += 1 42 | -------------------------------------------------------------------------------- /Lessons/lesson17.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:17 2 | #http://www.designalyze.com/int2pythonscripting17_SimpleGrowth01 3 | 4 | import rhinoscriptsyntax as rs 5 | import random 6 | 7 | def placePt(x_range,y_range,z_range): 8 | x = random.uniform(0,x_range) 9 | y = random.uniform(0,y_range) 10 | z = random.uniform(0,z_range) 11 | pt = [x,y,z] 12 | return pt 13 | 14 | 15 | 16 | ptZero = [50,50,0] 17 | pts = [] 18 | pts.append(ptZero) 19 | circleZero = rs.AddCircle(ptZero,0.5) 20 | 21 | for i in range(0,100): 22 | pt = rs.AddPoint(placePt(100,100,0)) 23 | index = rs.PointArrayClosestPoint(pts,pt) 24 | cp = pts[index] 25 | vect = rs.VectorCreate(cp,pt) 26 | unitVect = rs.VectorUnitize(vect) 27 | subVect = vect - unitVect 28 | newPt = rs.MoveObject(pt,subVect) 29 | rs.AddCircle(newPt,0.5) 30 | pts.append(newPt) 31 | -------------------------------------------------------------------------------- /Lessons/lesson18.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:18 2 | #http://www.designalyze.com/int2pythonscripting18_SimpleGrowth02 3 | 4 | import rhinoscriptsyntax as rs 5 | import random 6 | 7 | def placePt(x_range,y_range,z_range): 8 | x = random.uniform(0,x_range) 9 | y = random.uniform(0,y_range) 10 | z = random.uniform(0,z_range) 11 | pt = [x,y,z] 12 | return pt 13 | 14 | rs.EnableRedraw(False) 15 | 16 | ptZero = [50,50,50] 17 | pts = [] 18 | pts.append(ptZero) 19 | #circleZero = rs.AddCircle(ptZero,0.5) 20 | sphereZero = rs.AddSphere(ptZero, 0.5) 21 | 22 | 23 | 24 | for i in range(0,1000): 25 | pt = rs.AddPoint(placePt(100,100,100)) 26 | index = rs.PointArrayClosestPoint(pts,pt) 27 | cp = pts[index] 28 | vect = rs.VectorCreate(cp,pt) 29 | unitVect = rs.VectorUnitize(vect) 30 | subVect = vect - unitVect 31 | newPt = rs.MoveObject(pt,subVect) 32 | #rs.AddCircle(newPt,0.5) 33 | rs.AddSphere(newPt,0.5) 34 | pts.append(newPt) 35 | 36 | rs.Redraw() 37 | -------------------------------------------------------------------------------- /Lessons/lesson19.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:19 2 | #http://designalyze.com/int2pythonscripting19_SimpleClass 3 | 4 | import rhinoscriptsyntax as rs 5 | 6 | class MyLine: 7 | 8 | def __init__(self, pt1, pt2): 9 | 10 | self.pt1 = pt1 11 | self.pt2 = pt2 12 | 13 | def makeLine(self): 14 | rs.AddLine(self.pt1,self.pt2) 15 | 16 | 17 | line1 = MyLine([0,0,0], [2,2,2]) 18 | line1.makeLine() 19 | 20 | 21 | line2 = MyLine([2,0,0], [2,2,2]) 22 | line2.makeLine() 23 | -------------------------------------------------------------------------------- /Lessons/lesson20.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:20 2 | #http://designalyze.com/int2pythonscripting20_PolygonClass01 3 | import rhinoscriptsyntax as rs 4 | import math 5 | 6 | class MyPolygon: 7 | 8 | def __init__(self,radius,sides): 9 | self.radius = radius 10 | self.sides = sides 11 | theta = (2*math.pi)/self.sides 12 | print theta 13 | pt01 = rs.AddPoint(self.radius,0,0); 14 | pts = [] 15 | pts.append(pt01) 16 | self.origin = [0,0,0] 17 | degrees = theta*(180/math.pi) 18 | print degrees 19 | for i in range(1,self.sides): 20 | tempPt = pts[-1] 21 | newPt = rs.RotateObject(tempPt,self.origin,degrees,None,True) 22 | pts.append(newPt) 23 | pts.append(pt01) 24 | self.polygon = rs.AddPolyline(pts); 25 | 26 | def fillPolygon(self): 27 | return rs.AddPlanarSrf(self.polygon) 28 | 29 | def extrudePolygon(self,height): 30 | startPt = self.origin; 31 | newZ = self.origin[2]+height 32 | endPt = [self.origin[0],self.origin[1],newZ] 33 | return rs.ExtrudeCurveStraight(self.polygon, startPt, endPt) 34 | 35 | 36 | 37 | polygon1 = MyPolygon(5,5) 38 | polygon1.fillPolygon() 39 | polygon1.extrudePolygon(5) 40 | #polygon1.createPolygon() 41 | 42 | #polygon2 = MyPolygon(12,8) 43 | #polygon2.createPolygon() 44 | 45 | #polygon3 = MyPolygon(15,3) 46 | #polygon3.createPolygon() 47 | -------------------------------------------------------------------------------- /Lessons/lesson21.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:21 2 | #http://designalyze.com/int2pythonscripting21_PolygonClass02 3 | import rhinoscriptsyntax as rs 4 | import math 5 | 6 | #class definition 7 | class MyPolygon: 8 | #polygon initialization or constructor method 9 | def __init__(self,radius,sides,origin): 10 | self.radius = radius 11 | self.sides = sides 12 | self.origin = origin 13 | origin = self.origin 14 | theta = (2*math.pi)/self.sides 15 | x = origin[0] + self.radius 16 | y = origin[1] 17 | z = origin[2] 18 | pt01 = rs.AddPoint(x,y,z); 19 | pts = [] 20 | pts.append(pt01) 21 | degrees = theta*(180/math.pi) 22 | for i in range(1,self.sides): 23 | tempPt = pts[-1] 24 | newPt = rs.RotateObject(tempPt,origin,degrees,None,True) 25 | pts.append(newPt) 26 | pts.append(pt01) 27 | self.polygon = rs.AddPolyline(pts); 28 | 29 | 30 | def fillPolygon(self): 31 | return rs.AddPlanarSrf(self.polygon) 32 | 33 | def extrudePolygon(self,height): 34 | startPt = self.origin; 35 | newZ = self.origin[2]+height 36 | endPt = [self.origin[0],self.origin[1],newZ] 37 | return rs.ExtrudeCurveStraight(self.polygon, startPt, endPt) 38 | 39 | 40 | 41 | userpt = rs.GetPoint("Pick a centerpoint") 42 | polygon0 = MyPolygon(6,6,userpt) 43 | polygon0.fillPolygon() 44 | polygon0.extrudePolygon(5) 45 | 46 | 47 | userpt = rs.GetPoint("Pick a centerpoint") 48 | polygon0 = MyPolygon(8,12,userpt) 49 | polygon0.fillPolygon() 50 | polygon0.extrudePolygon(10) 51 | -------------------------------------------------------------------------------- /Lessons/lesson22.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:22 2 | #http://designalyze.com/int2pythonscripting22_DivideSrf2pts 3 | import rhinoscriptsyntax as rs 4 | 5 | srf = rs.GetObject("Pick surface to divide") 6 | udiv = rs.GetInteger("Number of Divisions in U",10) 7 | vdiv = rs.GetInteger("Number of Divisions in V",10) 8 | 9 | u = rs.SurfaceDomain(srf,0) 10 | v = rs.SurfaceDomain(srf,1) 11 | print(str(u)) 12 | print(str(v)) 13 | 14 | pts = [] 15 | 16 | 17 | for i in range(0, udiv+1, 1): 18 | for j in range(0, vdiv+1, 1): 19 | pt = (i/udiv,j/vdiv,0) 20 | srfP = rs.SurfaceParameter(srf,pt) 21 | newpt = rs.EvaluateSurface(srf,srfP[0],srfP[1]) 22 | pts.append(rs.AddPoint(newpt)) 23 | -------------------------------------------------------------------------------- /Lessons/lesson23.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:23 2 | #http://designalyze.com/int2pythonscripting23_MoreRecursionPt1 3 | #Koch Snowflake in python using recursion 4 | import rhinoscriptsyntax as rs 5 | 6 | #get normal/perpendicular vector 7 | def getnormal(pt1,pt2): 8 | dx = pt2[0] - pt1[0] 9 | dy = pt2[1] - pt1[1] 10 | return [-dy,dx,0] 11 | 12 | #split line recursive function 13 | def splitlines(lines, count): 14 | #temp list and clear input list 15 | templines = lines 16 | lines = [] 17 | #make sure we have a way to break the recursion 18 | if count == 0: 19 | return 1 20 | else: 21 | for line in templines: 22 | #get properties of the line (endpts, length, direction, domain) 23 | stpt = rs.CurveStartPoint(line) 24 | endpt = rs.CurveEndPoint(line) 25 | length = rs.Distance(stpt, endpt) 26 | dir1 = rs.VectorCreate(endpt,stpt) 27 | crvdomain = rs.CurveDomain(line) 28 | #parameters for midpt and pts 1/3 and 2/3 along the line 29 | t0 = crvdomain[1] / 2.0 30 | t1 = crvdomain[1] / 3.0 31 | t2 = t1*2 32 | midpt = rs.EvaluateCurve(line, t0) 33 | ptatonethird = rs.EvaluateCurve(line, t1) 34 | ptattwothird = rs.EvaluateCurve(line, t2) 35 | 36 | midpt = rs.AddPoint(midpt) 37 | #call get normal function 38 | normal = getnormal(stpt,endpt) 39 | #move midpt perpendicular to line at 1/3 the length of the line 40 | scaled = rs.VectorScale(normal,0.3333) 41 | rs.MoveObject(midpt,scaled) 42 | #create the 4 newlines and add them to the list 43 | newline1 = rs.AddLine(stpt, ptatonethird) 44 | newline2 = rs.AddLine(ptatonethird, midpt) 45 | newline3 = rs.AddLine(midpt, ptattwothird) 46 | newline4 = rs.AddLine(ptattwothird, endpt) 47 | lines.append(newline1) 48 | lines.append(newline2) 49 | lines.append(newline3) 50 | lines.append(newline4) 51 | #create a list of objects to delete 52 | cleanup = [] 53 | cleanup.append(line) 54 | cleanup.append(midpt) 55 | rs.DeleteObjects(cleanup) 56 | #don't forget to decrement the count otherwise infinite loop 57 | count = count - 1 58 | return splitlines(lines,count) 59 | 60 | 61 | lines = [] 62 | 63 | # get two points to start 64 | count = rs.GetInteger("How many iterations would you like to do?", 3) 65 | pt1 = rs.GetPoint("Pick a start point") 66 | pt2 = rs.GetPoint("Pick an end point") 67 | 68 | line = rs.AddLine(pt1,pt2) 69 | lines.append(line) 70 | splitlines(lines,count) 71 | 72 | -------------------------------------------------------------------------------- /Lessons/lesson24.py: -------------------------------------------------------------------------------- 1 | #Python Workshop Lesson:24 2 | #http://designalyze.com/int2pythonscripting24_MoreRecursionPt2 3 | #cracking algorithm 4 | import rhinoscriptsyntax as rs 5 | import Rhino 6 | import scriptcontext 7 | 8 | 9 | 10 | def crackpolygon(pls, count): 11 | temppls = pls 12 | pls = [] 13 | if count == 0: 14 | return 1 15 | else: 16 | for pl in temppls: 17 | if rs.CloseCurve(pl) == False: 18 | print "Not a closed curve" 19 | else: 20 | centroid = rs.CurveAreaCentroid(pl) 21 | centpt = rs.AddPoint(centroid[0]) 22 | curves = rs.ExplodeCurves(pl) 23 | for crv in curves: 24 | pt1 = rs.CurveStartPoint(crv) 25 | pt2 = rs.CurveEndPoint(crv) 26 | pts = [] 27 | pts.append(pt1) 28 | pts.append(pt2) 29 | pts.append(centpt) 30 | pts.append(pt1) 31 | newpl = rs.AddPolyline(pts) 32 | pls.append(newpl) 33 | rs.DeleteObject(crv) 34 | cleanup = [] 35 | cleanup.append(centpt) 36 | rs.DeleteObjects(cleanup) 37 | count = count - 1 38 | return crackpolygon(pls, count) 39 | 40 | count = rs.GetInteger("How many iterations would you like to do?", 3) 41 | pl = rs.GetCurveObject("pick a closed curve to crack") 42 | plguid = pl[0] 43 | polygons = [] 44 | polygons.append(plguid) 45 | crackpolygon(polygons, count) 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | python-tutorials-for-rhino 2 | ========================== 3 | 4 | #Python Scripting Tutorials for Rhino 5 | 6 | This is a collection of python scripts intended as tutorials for learning python for Rhino. 7 | 8 | Video tutorials for all code samples can be found at [Designalyze](http://www.designalyze.com) 9 | 10 | 11 | ###Lesson List 12 | * Lesson: 00 [Macros](http://designalyze.com/int2pythonscripting00_macros) 13 | * Lesson: 01 [Data Types](http://designalyze.com/int2pythonscripting01_datatypes) 14 | * Lesson: 02 [Varibales and I/O](http://designalyze.com/int2pythonscripting02_variables-io) 15 | * Lesson: 03 [Arrays/Lists](http://designalyze.com/int2pythonscripting03_arraysandlists) 16 | * Lesson: 04 [Points and Curves](http://designalyze.com/int2pythonscripting04_listscurvetypes) 17 | * Lesson: 05 [For loops](http://designalyze.com/int2pythonscripting05_forloopandmath) 18 | * Lesson: 06 [Nested For Loops](http://designalyze.com/int2pythonscripting06_nestedloops-functions) 19 | * Lesson: 07 [If/Else](http://www.designalyze.com/int2pythonscripting07_controlflow01) 20 | * Lesson: 08 [If/Else Part 2](http://www.designalyze.com/int2pythonscripting08_controlflow02) 21 | * Lesson: 09 [Random Numbers](http://www.designalyze.com/int2pythonscripting09_RandomNum01) 22 | * Lesson: 10 [Random Numbers and Lines](http://www.designalyze.com/int2pythonscripting10_RandomNum02) 23 | * Lesson: 11 [Strings and Lists](http://www.designalyze.com/int2pythonscripting11_StringsandLists) 24 | * Lesson: 12 [Transformations](http://www.designalyze.com/int2pythonscripting12_VectorTransformation) 25 | * Lesson: 13 [Simple Recursion](http://www.designalyze.com/int2pythonscripting13_SimpleRecursion) 26 | * Lesson: 14 [Not so simple recursion](http://www.designalyze.com/int2pythonscripting13_NotSoSimpleRecursion) 27 | * Lesson: 15 [Export Points to CSV](http://www.designalyze.com/int2pythonscripting15_ExportPts2CSV) 28 | * Lesson: 16 [Import Points from CSV](http://www.designalyze.com/int2pythonscripting16_ImportPtsFromCSV) 29 | * Lesson: 17 [Simple Growth](http://www.designalyze.com/int2pythonscripting17_SimpleGrowth01) 30 | * Lesson: 18 [Simple Growth 02](http://www.designalyze.com/int2pythonscripting18_SimpleGrowth02) 31 | * Lesson: 19 [Simple Class](http://designalyze.com/int2pythonscripting19_SimpleClass) 32 | * Lesson: 20 [Polygon Class Part 1](http://designalyze.com/int2pythonscripting20_PolygonClass01) 33 | * Lesson: 21 [Polygon Class Part 2](http://designalyze.com/int2pythonscripting21_PolygonClass02) 34 | * Lesson: 22 [Divide Surface](http://www.designalyze.com/int2pythonscripting22_DivideSrf2pts) 35 | * Lesson: 23 [More Recursion Koch Curve](http://designalyze.com/int2pythonscripting23_MoreRecursionPt1) 36 | * Lesson: 24 [More Recursion Cracking](http://designalyze.com/int2pythonscripting24_MoreRecursionPt2) 37 | 38 | --------------------------------------------------------------------------------