Scripting Python to add circles to rectangle vertices

Hi there,

I’m very new to python scripting and I haven’t had a lot of time to get to learn it.

I’m trying to add circles to the corners of a rectangle but I’d like to be able to do it without the user having to pick any more points from rhino.

I’ve tried using PolylineVertices but don’t know how to get the AddCircle syntax to understand the data, is there a middle step I need to be doing?

import rhinoscriptsyntax as rs

point1 = rs.GetPoint("Click First corner of rectangle")

point2 = rs.GetPoint("Click Second corner of rectangle")

point3 = rs.GetPoint("Click Third corner of rectangle") 

plane = rs.PlaneFromPoints(point1, point2, point3)

baserectangle = rs.AddRectangle(plane, point1, point3)

offsetrectangle = rs.OffsetCurve(baserectangle, [0,0,0], 0.5)

rs.DeleteObject(baserectangle)

vertices = rs.PolylineVertices(offsetrectangle)```
import rhinoscriptsyntax as rs

point1 = rs.GetPoint("Click First corner of rectangle")
point2 = rs.GetPoint("Click Second corner of rectangle")
point3 = rs.GetPoint("Click Third corner of rectangle") 
plane = rs.PlaneFromPoints(point1, point2, point3)
baserectangle = rs.AddRectangle(plane, point1, point3)
offsetrectangle = rs.OffsetCurve(baserectangle, [0,0,0], 0.5)
rs.DeleteObject(baserectangle)
vertices = rs.PolylineVertices(offsetrectangle)
vertices.pop(0) #remove 'duplicate' start point/end point
for vert in vertices:
    rs.AddCircle(vert, 5) #adds circle with radius 5 at each corner of the offset curve
1 Like

Thank you so much!

Could I ask what this line means? I’ve seen similar lines in other code but am not quite sure what it means/does?

It’s a loop. This is an important computer program structure that allows one to apply an operation or a series of operations to each of the items in a list, one at a time.

Ah amazing thank you! So simple!

In case you fall asleep before getting to it in the wikipedia item, this is specifically an example of an “Iterator-based for-loop”. That means that eg: vertices must be an array or structure (not a fundamental type) type that contains implicit knowledge of how many items it contains to iterate over. The type of vert is automatically defined as the same type as each entry in vertices and becomes the variable that the work defined in the loop is performed upon.

Syntactically it’s very intuitive and makes for easy reading.

1 Like

Yeah, that article was perhaps too detailed. Here is a nicer, simpler one that just covers Python’s For loop structure. OK, it’s aimed at kids, but as beginners, we’re all kids.

1 Like

Boy! When I was a pre-teen I seriously doubt that I knew what words like “iterate”, “traverse”, etc meant in general, much less in the programming sense. :slightly_smiling_face:

Of course that was because there was no such thing as computers and programming in civilian life until I got to college.

Thank you both, this has been very helpful! Wish I had more time to learn scripting properly…