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.
'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






hm….
yes, but….
well,…
usually i’m not afraid of new things, but this is so ugly…..
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.
you can allready have python in rhino via com. vbarrays need a little fix, but then it works perfectly.
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!
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.
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.
The tutorial of David Rutten is now available at http://wiki.mcneel.com/developer/rhinoscript101 (http://reconstructivism.net/ is dead).
[...] scripted layering: code & form [...]
[...] Code and Form: Computational Aesthetics [...]