Intro to Python Scripting: 03 Arrays and Lists

Software: 
Tag: 
Video Duration: 
20 minutes
Author: 
Zach Downey

In this tutorial we cover a some basic arrays/lists. We look at a couple of ways to define a list and how to access the parameters within the list. We also look at the RhinoScript AddPoint method. We use the debugger and breakpoints to step through the code to figure out what is going on when you ask Rhino to create a point using that method. There are some pretty cool things going on under the hood. For example, you can pass a comma separated string to the AddPoint method (something like ["1,2,3"] ) and it will figure out what you mean. Even though you probably shouldn't do that.

Below is a diagram of an array. 

In Python we can access the individual elements of the array by using square [] brackets. For instance, if we want the thrid element we would access it by a[2] and we would get the value 4. 

#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)

Rating

Please rate this tutorial below. Thanks.

5
Average: 4.8 (14 votes)

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.

:)