080205_rhinoscript.jpg

Rhinoscript sketch, extruding a revolution surface along random curves. Good cheesy fun.

I had a chance to see a bit more of the impressive tool Rhino 4 during the Generator.x 2.0 workshop, and so I thought I’d have a go at making a simple sketch in Rhinoscript. As it turns out, the fact that Rhinoscript is based on VBScript makes coding feel horrible at first. Seriously, who would want to use syntax like that? It might be easy for beginners to pick up, but it quickly gets painful once you’re dealing with complex API calls and 100+ lines of code.

Nevertheless, frustration soon gives way to amazement at the built-in Rhino library and its vast array of heavy-duty functions for creating and manipulating curves, meshes and NURBS surfaces. In comparison, mesh generation in Processing is enough to give anyone a headache, and I seriously doubt anyone would even attempt to implement NURBS. Even Boolean mesh operations is a staggering task, with no good Java libraries readily available.

While Rhinoscript is firmly a non-realtime tool, its power for pure geometry is amazing. I would definitely use Rhino as a creative tool for digital fabrication projects, where animation is not the goal. There are some excellent RhinoScript resources online, for starters look at RhinoScript 101 and David Rutten’s tutorial. I would also definitely recommend using the Monkey Script editor instead of the built-in editor, it’s more powerful and has a very useful documentation feature.

The script below gives a basic idea of the Rhino syntax, and while it is a basic sketch suffering from 3D clichées, it shows the power and versatility of Rhinoscript. I just wish it wasn’t Visual Basic.

Code: RandRail.rvb

'RandRail.rvb
'Marius Watz, http://workshop.evolutionzone.com

Option Explicit

Sub Main
	Dim doRender : doRender=1
	Dim num : num=100
	Dim pt, curve(100)

	Call rhino.enableRedraw(False)
	Rhino.Print "--------------------------"

	' delete any existing objects
	Dim oldScene:oldScene=Rhino.AllObjects()
	If isArray(oldScene) Then
		Rhino.DeleteObjects Rhino.AllObjects()
		Rhino.DeleteObjects Rhino.LightObjects()
	End If

	' Set up scene
	Dim light
	light=Rhino.AddPointLight (Array(0,-200,0))
	light=Rhino.AddPointLight (Array(-100,-100,0))

	Rhino.RenderColor 1, RGB(50,50,50)
	Rhino.RenderResolution Array(1200,900)
	Rhino.RenderAntialias 2

	' Create random curves
	Dim cp(3),mult, i
	For i=0 To num
		mult=random(0.75,1.25)
		cp(0)=Array(0,0,0)
		If Rnd>0.5 Then
			cp(1)=Array(0,random(30,50),0)
		Else
			cp(1)=Array(0,random(-30,20),0)
		End If
		If Rnd>0.5 Then
			cp(2)=Array(random(-50,50)*mult,random(30,50),random(-50,50)*mult)
		Else
			cp(2)=Array(random(-50,50)*mult,random(-30,20),random(-50,50)*mult)
		End If
		cp(3)=Array(random(-50,50)*mult,random(-50,50),random(-50,50)*mult)
		curve(i)=Rhino.AddCurve(cp)
		'		Rhino.AddSphere cp(2), dblRadius
	Next

	' Create random rounded profile
	Dim numRot : numRot=Int(random(7,18))
	Dim j, rndCurve(10),profPt(),deg,offs
	ReDim profPt(numRot)

	For j=0 To Ubound(rndCurve)
		For i=0 To numRot
			deg=(2*Rhino.Pi/numRot)*i
			offs=random(5,7)*0.2
			If i Mod 2=0 Then
				offs=random(7,12)*0.2
			End If

			profPt(i)=Array(Cos(deg)*offs, 0 ,Sin(deg)*offs)
		Next
		rndCurve(j)=Rhino.AddCurve(profPt)
	Next

	' Create random RailRefSrf and ExtrudeCurve surfaces
	Dim railAxis(1)
	railAxis(0)=Array(0,0,0)
	railAxis(1)=Array(0,1,0)

	Dim srf,profCurve
	For i=0 To num
		profCurve=rndCurve(Int(random(0,Ubound(rndCurve))))
		srf=Rhino.AddRailRevSrf(curve(i),profCurve,railAxis)
		applyRndMaterial(srf)
		'		srf=Rhino.ExtrudeCurve(profCurve,curve(i))
		'		applyRndMaterial(srf)
	Next

	If doRender=1 Then
		renderView()
	End If

	Call rhino.enableRedraw(True)

End Sub

' Set random materials
Function applyRndMaterial(obj)
	Dim prob,col,material

	material=Rhino.AddMaterialToObject (obj)

	' Coloring: Pink, Orange
	prob=random(0,100)
	If prob<30 Then
		Rhino.MaterialColor material, RGB(255, 0, Int(random(100,150)))
	ElseIf prob<90 Then
		Rhino.MaterialColor material, RGB(255, Int(random(100,254)),0)
	Else
		Rhino.MaterialColor material, RGB(255, 255, 255)
	End If
End Function

Function random(min,max)
	random=Rnd*(max-min)+min
End Function

' Render current viewport
Function renderView()
	Dim view,filename

	view = Rhino.CurrentView
	Rhino.Command "_-Render"
	filename=getRenderFileName("RandRail")
	Dim cmd : cmd="_-SaveRenderWindowAs " & Chr(34) & filename & Chr(34)
	Rhino.Command cmd
End Function

' Get auto-incremented filename
Function getRenderFileName(scriptName)
	Dim index,done, doc, file, temp,imgNum

	done=-1
	index=0
	Do While done=-1
		doc=Rhino.WorkingFolder & "\" & scriptName & padStr(scriptName,index) & ".png"
		file=Rhino.FindFile(doc)
		If IsNull(file)=True Then
			done=1
		Else
			index=index+1
		End If
	Loop

	getRenderFileName=doc
End Function

Function padStr(prefix,val)
	Dim l : l=Len(val)
	If l<1 Then
		padStr="000" & val
	ElseIf l<2 Then
		padStr="00" & val
	ElseIf l<3 Then
		padStr="0" & val
	Else
		padStr="" & val
	End If
End Function	

Main

9 Comments »

There are 9 comments to "My first Rhinoscript…". You may leave your own comment.
1. lenny, February 5th, 2008 at 22:14

hm….
yes, but….
well,…
usually i’m not afraid of new things, but this is so ugly…..

2. didi, February 5th, 2008 at 23:49

Well, RhinoScript is impressive… It’s nice it’s using Open Nurbs which is, well, open as open can get, but deff not for realtime stuff. I started using Rhino a lot in my arch projects (over processing) and it’s wonderful (skipping those dxf exports feels good).

They are also porting Rhino to OSX and they’re thinking of using Python instead of vb… fingers crossed.

3. Silvan, February 6th, 2008 at 19:24

you can allready have python in rhino via com. vbarrays need a little fix, but then it works perfectly.

4. Diatom, May 13th, 2008 at 18:54

This is a brilliant first script. I learned a lot, including how to implement a macro from within rhinoscript, which is huge! One lil problem tho, the copy to clipboard thingy garbled a bunch of characters (), and simple copy and pasting added “#” to the front of every line. Sometimes simple html is not such a bad idea. Anyway thanks for sharing this. I can’t wait to see your second script!

5. marius watz, June 1st, 2008 at 00:50

Hi DoubleL, as far as I know Rhinoscript doesn’t currently support classes or similar advanced language constructs. Personally I find the VBScript syntax quite frustrating, we can only hope they they will switch to Python in the future.

6. DoubleL, September 26th, 2008 at 05:33

Thank you, marius watz.
We are now using VB2006 to control the Rhino which support classes. However it still have some problem. Rhino is too slow to follow VB. So when Rhino is drawing, it will omit a lot of information that VB2006 has send.

7. ambi gux, February 27th, 2010 at 23:38

The tutorial of David Rutten is now available at http://wiki.mcneel.com/developer/rhinoscript101 (http://reconstructivism.net/ is dead).

8. spatial gradients» Blog Archive » laminating, August 23rd, 2010 at 16:42

[...] scripted layering: code & form [...]

9. 10 Rhinoscript resources | Design Reform, November 9th, 2010 at 14:47

[...] Code and Form:  Computational Aesthetics [...]

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">