Translating rhino script to python

Hi,

i am completely new on this forum’s stuff and scripting…
i am working on rhino osx and want to use this script found on the web (drop objects on surface) but it’s in format .rvb and i would like to translate it in python format to run it with pythonscript.

could anyone help me to translate it or does anyone have a .py with the same action?

thank you very much!

Option Explicit
'Script written by Damon Sidel
'Script copyrighted by Damon Sidel
'Script version Monday, May 09, 2011

Call DropOntoSurface()
Sub DropOntoSurface()

	'Get surface to populate
	Dim strSrf : strSrf = Rhino.GetObject("Select the surface to be populated")
	If IsNull(strSrf) Then Exit Sub
        
	'Get the points to drop
	Dim arrObj : arrObj = Rhino.GetObjects("Select the objects to drop")
	If IsNull(arrObj) Then Exit Sub
        
	Call Rhino.EnableRedraw(False)
                
	'Create jittery grid
	Dim obj, k, x, y, z, line, csX, BB, p1, p2
	For Each obj In arrObj
                
		'Object information
		BB = Rhino.BoundingBox(obj)
		p1 = Array((BB(0)(0) + BB(2)(0)) / 2, (BB(0)(1) + BB(2)(1)) / 2, (BB(0)(2) + BB(2)(2)) / 2)
                                
		'Find point on surface to insert block
		line = Rhino.AddLine(p1, Array(p1(0), p1(1), 0))
		csX = Rhino.CurveSurfaceIntersection(line, strSrf)
                        
		If IsArray(csX) Then
			For k = 0 To UBound(csX)
				If csX(k, 0) = 1 Then
					p2 = csX(k, 3)
					Call Rhino.MoveObject(obj, p1, p2)
				Else
					Rhino.Print "Intersection is not a point"
				End If
			Next
		End If
                        
		Call Rhino.DeleteObject(line)
                        
	Next

	Call Rhino.EnableRedraw(True)
        
End Sub

I don’t know how the objects are supposed to be set up to make this work - it looks strange - are you sure you have all the script and 100% correct? If I run the vb Rhinoscript, I get a type mismatch error on line 30…

–Mitch

hi,

i tried it in rhino for windows and it works fine, be sure to have all the objects and the surface above the z:0 i send you again the original script

thanks!

Option Explicit
'Script written by Damon Sidel
'Script copyrighted by Damon Sidel
'Script version Monday, May 09, 2011

Call DropOntoSurface()
Sub DropOntoSurface()

	'Get surface to populate
	Dim strSrf : strSrf = Rhino.GetObject("Select the surface to be populated")
	If IsNull(strSrf) Then Exit Sub
        
	'Get the points to drop
	Dim arrObj : arrObj = Rhino.GetObjects("Select the objects to drop")
	If IsNull(arrObj) Then Exit Sub
        
	Call Rhino.EnableRedraw(False)
                
	'Create jittery grid
	Dim obj, k, x, y, z, line, csX, BB, p1, p2
	For Each obj In arrObj
                
		'Object information
		BB = Rhino.BoundingBox(obj)
		p1 = Array((BB(0)(0) + BB(2)(0)) / 2, (BB(0)(1) + BB(2)(1)) / 2, (BB(0)(2) + BB(2)(2)) / 2)
                                
		'Find point on surface to insert block
		line = Rhino.AddLine(p1, Array(p1(0), p1(1), 0))
		csX = Rhino.CurveSurfaceIntersection(line, strSrf)
                        
		If IsArray(csX) Then
			For k = 0 To UBound(csX)
				If csX(k, 0) = 1 Then
					p2 = csX(k, 3)
					Call Rhino.MoveObject(obj, p1, p2)
				Else
					Rhino.Print "Intersection is not a point"
				End If
			Next
		End If
                        
		Call Rhino.DeleteObject(line)
                        
	Next

	Call Rhino.EnableRedraw(True)
        
End Sub

Hi Jacolnaco
Try the below Python script:

import rhinoscriptsyntax as rs

def DropOntoSurface():
    strSrf=rs.GetObject("Select the surface to be populated",8)
    if strSrf==None:return
    arrObj=rs.GetObjects("Select the objects to drop")
    if arrObj==None:return
    for obj in arrObj:
        bb=rs.BoundingBox(obj)
        pm=(bb[0]+bb[2])/2        
        lpi=rs.ProjectPointToSurface([pm],strSrf,(0,0,1))
        pi=lpi[0]
        rs.MoveObject(obj,rs.VectorCreate(pi,pm))
DropOntoSurface() 

Ciao Vittorio

Nice,

it works fine! Now I have to study a little python to understand what did you write…

Is it possible to set that the objects merges completely into the surface? (the image is the result of your script), it’s for a urban model, so the buildings have to be dropped on the topography, so i can make a boolean with the two entities.

grazzie mille Vittorio,

I have a vb script that I made to do just this, it’s rather more involved as it can also accept meshes as volumes to project as well as to project to… you can also project to multiple overlapping surfaces or meshes. There is a choice to either let the volume stop at “first contact” (the object stays above the surface) or become fully embedded in the surface… I also use this for siting buildings for small scale models.

I can look at “pythonizing” it later as it’s over 100 lines of code…

–Mitch

ProjectVolumesToSurface.rvb(5.1 KB)

OK, so this is just a simple projection script, I don’t know why I thought it was supposed to distribute the objects on the surface, must have confused this with another post…

–Mitch

OK, here is the Python version… In the meantime, I also found a bug in the vb script, I will attach a fixed version of that as well. What I stated above about multiple overlapping surfaces was not 100% correct, it will do them, but the the projected volumes will not stop at the upper surface. That will be corrected in a future edition.

Let me know if you find bugs in either version.

–Mitch

ProjectVolumesToSrf.py(4.0 KB)

ProjectVolumesToSrf.rvb(5.2 KB)

hi,

this works really fine, it was exactly what i was looking for,

thank you very much! i’ll see if it works on my model, 20000 buildings to project on a surface!!

good luck and continuation!