RhinoScript to Python help

Hi folks - long time no see! I have 2500 holes to place at point locations in my file. I found this old Rhinoscript by Damon - any chance I could get it rewritten in Python so I can use it in Mac Rhino?

Option Explicit
'Script written by Damon Sidel
'Script copyrighted by Damon Sidel
'Script version Sunday, April 19, 2009 7:22:34 PM

Call CopyToPoints()
Sub CopyToPoints()

	Dim pts : pts = Rhino.GetPointCoordinates("Select points",True)
	If IsNull(pts) Then Exit Sub

	Dim	copyObj : copyObj = Rhino.GetObject("Select object to copy")
	If IsNull(copyObj) Then Exit Sub

	Dim copyObjOrigin : copyObjOrigin = Rhino.GetPointCoordinates("Select point at origin of object being copied")
	If IsNull(copyObjOrigin) Then Exit Sub

	Call Rhino.EnableRedraw(False)

	Dim arrPt
	If IsArray(pts) Then
		For Each arrPt In pts
			Call Rhino.CopyObject(copyObj,copyObjOrigin(0),arrPt)
		Next
	End If

Thanks for any help offered in the form of yummy virtual beer.

Does this work:

import rhinoscriptsyntax as rs

pts = rs.GetPointCoordinates("Select points",True)
if pts is None: sys.exit()

copyObj = rs.GetObject("Select object to copy")
if copyObj is None: sys.exit()

copyObjOrigin = rs.GetPoint("Select point at origin of object being copied")
if copyObjOrigin is None: sys.exit()

rs.EnableRedraw(False)

for arrPt in pts:
    vec = rs.VectorCreate(arrPt, copyObjOrigin)
    rs.CopyObject(copyObj,vec)

To get familiar with Rhino.Python start with this page: http://developer.rhino3d.com/guides/rhinopython/

@markleichliter, below is another version which handles redraw when hundrets of points are involved:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def CopyObjectToPoints():
    pts = rs.GetObjects("Points to copy to", rs.filter.point, True, True, False)
    if not pts: return
    
    copyObj = rs.GetObject("Select object to copy")
    if not copyObj: return
    
    copyObjOrigin = rs.GetPoint("Origin of object to copy")
    if not copyObjOrigin: return
    
    rs.EnableRedraw(False)
    for pt_obj in pts:
        pt = rs.PointCoordinates(pt_obj)
        rs.CopyObject(copyObj, pt - copyObjOrigin)
    rs.EnableRedraw(True)

CopyObjectToPoints()

@Alain, there is a bug in rs.GetPointCoordinates which applies to Rhino 5 and 6. If you run the method and press Enter when it asks for the points without selecting any, it gives this error:

Message: iteration over non-sequence of type NoneType

_
c.

1 Like

Hi Scott - thanks man!
This -almost- works. It doesn’t seem to take into account any transforms the have been applied to the points - the copied geometry appears at the original location of the points (I’ve copied and pasted the original geometry, with the points, from another file, then moved said geo to be closer to the origin).

I’ll try Clement’s version and see if the same issue happens

Thanks Clement - your version is exhibiting the same results as Scott’s. See pic.

Scott, take note how Clement uses a function and simply returns out of it in error cases. This is much better than calling sys.exit()

@markleichliter, both scripts seem to work here. Are you sure you pick things in the right order ?

Create a circle somewhere then deselect it and start the script i posted:

  1. Pick the point objects or group of point objects to copy to
  2. Pick the circle
  3. Pick the center of the circle

It should copy the circle to all points picked in step 1. If not, please post your file.

_
c.

It works if the circle is centered on the world origin, but if drawn anywhere else, the script creates the copies offset from the points the equivalent distance from the points as the circle is from the world origin. (Wow that is barely coherent)

I can make this work just fine - Thank You!

@markleichliter, strange. This is what i get using both scripts on Windows, the circle is far from origin:

May this be a Mac only problem as i cannot reproduce it on Windows ?

_
c.

Must have been something quirky in that one file, as I’ve burned through placing 2000 of the 2500 holes with no issues. Thanks again for the help, Clement. :beers:

Logged it. Thanks!