topics.01_running and editing scripts


Editing and Running RhinoScripts


Running your first script

The easiest way to use scripts in Rhinoceros is to enter the script directly in the command line. This has a big disadvantage: each time we want to run our program we need to write it again, since it is not stored anywhere in memory after execution. The way we go around this is by storing the script in the computer's memory (scripts are basically a text file with the ".rvb" extension. The extension tells Rhinoceros that the file contains RhinoScript code. There are several ways you can follow to write and run scripts in Rhino:

First Method: Using the Text Editor


You can try the following simple steps to create your first RhinoScript:

- Open Notepad and write

Rhino.Print "Hello World"

- Save the file as "hello_world.rvb"

- Go to Rhino and go to menu Tools > Plugins > RhinoScript, and click on the 'Load' option.




- This will open a new dialog box with several options. Clicking on 'Add' will open a browser window. Find the hello_world.rvb file and add it. The file will now show up in the left side of the 'Load Script File' dialog box.



- With the file selected press the 'Load' button

- The dialog box should automatically close showing the Rhinoceros interface. In the command line you should see:




Note: If it doesn't work make sure that your notepad editor's preferences are set to UNICODE (otherwise the file will have formatting features that, even if you don't see them, will confuse Rhinoceros).

Second Method: Using the Rhinoceros Editor


Rhinoceros has a simple text editor for editing and running scripts. You can find it under the Tools > Plugins > RhinoScript > Editor.



This will open a window that will provide a slightly better interface than Notepad for writing the scripts (for example it will selectively highlight parts of your script with coded colors making it easier for you to read and edit it). From the editor's window you can directly launch the script into Rhinoceros pressing and do regular file operations such as Save, Open, etc.

Notice how the comments on line 3 are in green, the keyword 'Rhino' is in blue, and the string 'hello world' is in purple.


Third Method: Using 'Monkey'


The most efficient way to work with scripts in Rhino is to use 'Monkey', a plugin that runs inside Rhinoceros and lets you write code, compile it, execute it, debug it (more on this later) in a simple way. It also gives you access to useful things such as a complete RhinoScript methods reference, and something called 'Intellisense', which is a contextual help window that appears next to your cursor giving you hints about the specific line of code that you are writing. Monkey is just a more sophisticated version of the the code editor we saw above.

You can download Monkey for free here. Once you have installed it you can open it simply by typing 'Monkey' in the command line or by going to the Menu Tools > Plugins > Monkey.



You will notice that when you open Monkey some lines of code show up in the interface. These lines are the basic 'stubs' of a typical script. The Option Explicit line cleans the memory of Rhinoceros so we don't need to worry about the values of variables in previously run scripts. The Call Main is a call to the Main subroutine (in line 7). The call means that when the computer runs into that line it will execute the Main Subroutine function first. We will look at this in more detail when we look at functions.

- Go to the menu File > Open and select the hello_world.rvb file you made. Notice that the script will open in another tab without closing the new file with the stubs.

- Run the code by pressing or by going to the menu Source > Run

This will print "hello world" to the command line in Rhinoceros, just as with any of the other previous two methods.


One useful thing about Monkey is the method browser, if you go to the menu View > Method Browser or click Ctr + M. This will open a new section in Monkey that displays all the Rhino methods organized in folders. By double-clicking on any method Monkey will automatically insert it in your code.



In this page you have learned several different methods for writing and using scripts. You are ready to start scripting.

References

http://en.wiki.mcneel.com/default.aspx/McNeel/MonkeyforRhino4.html#Anchor_Download

Categories

Configure Widgets