#HourofCode Intro to Python Scripting in Rhino3d
This tutorial was recorded from my CUNY Introduction to Computation and Fabrication class. We recorded this session for the nationwide #HourofCode Computer Science Education Week. It's basically a live demo of the first 6 or so Designalyze Python videos. This was the first time most of my students have experienced pure programming that wasn't through Grasshopper. I use a couple of Grasshopper anaologies to help explain how some of the python functions work. Hopefully, this video helps contribute to the #HourofCode initiative and even more than that, hopefully you learn or are at least inspired to begin programming. Enjoy and happy coding.
#Source Code for the Examples can be found in each of the first six Designalyze tutorials.
#Python Data Types #Use Rhino's Script Debugger to View the Data Types import rhinoscriptsyntax as rs #Commets # character is use to comment out lines #Special Data Type x = None #Number Data Types x = 0 x = 1 x = -1 x = 1.0 x = 3.14 x = 3.14j #Booleans x = True x = False #Sequence Data Types x = "Helo World" x = 'Hello World' x = ['h', 'e', 'l', 'l', 'o'] x = [1,2,3,4,5] x = [1,"2",'3',"Strings"] x = (1,2,3) #Mapping Data Type x = {0:'w', 1:'o', 2:'r', 3:'l', 4:'d'} x = None
#Arrays/Lists import rhinoscriptsyntax as rs arrPt1 = [1,3,9] arrPt2 = [4,5,6] arrPt3 = [-1,-2,-3] arrPt4 = [7,8,9] rs.AddPoint(arrPt1) rs.AddPoint(arrPt2) rs.AddPoint(arrPt3) rs.AddPoint(arrPt4) points = [] points.append(arrPt1) points.append(arrPt2) points.append(arrPt3) points.append(arrPt4) print (points) print (points[1]) rs.AddPolyline(points)
#For Loop to Create Points #For Loop using the range and frange functions import rhinoscriptsyntax as rs import math #Simple Count for i in range(0,50): print(i) #Count by Even Numbers for i in range(0, 50, 2): print(i) #Count by Odd Numbers for i in range(1,50,2): print(i) #Using Rhinoscript's frange to step with floats for d in rs.frange(0.0, 10.0, 0.1): print(d) for d in rs.frange(0.0, 10.0, 0.1): rs.AddPoint(d,0,0) points = [] for d in rs.frange(0.0, 10.0, 0.1): x = d*math.sin(d) y = d*math.cos(d) z = 0.0 rs.AddPoint(x,y,z) pt = (x,y,z) points.append(pt) curve = rs.AddCurve(points)
#Nested For Loops #color points import rhinoscriptsyntax as rs def createColoredPoint(x,y,z,r,g,b): currentColor = [r,g,b] pt = rs.AddPoint(x,y,z) rs.ObjectColor(pt, currentColor) rs.EnableRedraw(False) step = 10 for x in range(0,256, step): for y in range(0,256, step): for z in range(0,256,step): createColoredPoint(x,y,z,x,y,z) rs.Redraw()
Want to Contribute?
Want to be an author? Drop us a line here we'd love to have you.
Already have a video you'd like to post? Send us a link and we'll get you going.
:)