Getting Started: 3 - The rhinoscript package
byon 04-14-2010 at 02:32 AM (2743 Views)
By now you should be able to print text to the command line and write some simple python functions, but you may be getting a little bored with that. It’s time to start working with Geometry in Rhino. This is where the rhinoscript package comes into play.
Introducing the rhinoscript package
On your computer, the rhino/python plug-in creates a directory (package) named rhinoscript which contains a number of python script files (modules).
- On Windows, this directory is located in your local roaming user directory. You can find this directory by looking at the Paths… under the Options menu in the python editor. On my computer, this directory is
- On OSX, this directory is located in the directory created when you run the IronPython.macrhi installer. On my Mac, this directory is
These files define a large set of functions for working with Rhino. Help pages for these functions can be found at http://www.rhino3d.com/5/ironpython/index.html
Importing the rhinoscript package will give your script access to these functions.
# add some points to Rhino using the rhinoscript packageimport rhinoscriptcount = rhinoscript.userinterface.GetInteger("Number of points")if( count!=None ):for i in range(count):x = iy = iif( i > count/2 ): y = count-irhinoscript.geometry.AddPoint([x,y,0])
Not too shabby, huh?
The big list – rhinoscriptsyntax.rs
Boy, typing all of those words to add a point and get an integer sure make my fingers tired. If you don’t like to have to type so much, there is a module included with your installation named rhinoscriptsyntax.py whose sole purpose is to merge all of the rhinoscript functions into a single “namespace.” This makes it easier to use functions without having to figure out the specific module it is defined in.
# use rhinoscriptsyntax to get all the functions in one shotimport rhinoscriptsyntax as rscount = rs.GetInteger("Number of points")if( count!=None ):for i in range(count):x = iy = iif( i > count/2 ): y = count-irs.AddPoint([x,y,0])
The RhinoScript Legacy
For a long time now, Rhino for Windows has included a scripting system named “RhinoScript” and is based on the VBScript scripting language. RhinoScript includes around 1000 functions for allowing VBScripts to interact with Rhino. The python rhinoscript package was designed to mimic the functionality of this library to allow for easy transition between the two languages and because these are the functions that users have asked for.
The current state of the python rhinoscript package is somewhere around 400 functions that are matches to their VBScript counterparts. This list grows with every work-in-progress release of Rhino, based on requests from users. If there is a function that you need that is not implemented in the rhinoscript package, let me know and I’ll see what I can do to add it.
One way to play with and learn about the functions in the rhinoscript package is by examining the online help http://www.rhino3d.com/5/ironpython/index.html. Each function help topic includes a sample script (typically very small) that uses the function. You may want to type some of these samples in to your favorite python editor and attempt to run them.
There are also a number of sample scripts that ship Rhino. If you enter the “RunPythonScript” command, you’ll notice that there is a subdirectory named “samples” which contain scripts you can run. Almost all of these scripts use the rhinoscript package.