This is a small guide for how to write a Grasshopper plug-in, based on my extremely limited experience with both Grasshopper, Visual Studio, and coding. The Grasshopper forums were extremely helpful, but I still had some technical issues I needed friends to figure out with me. So I am writing this small guide to writing your own custom components for Grasshopper.
Note that I am using the following:
- Rhino 5, 64-bit, Educational License for Windows
- Grasshopper 0.9
- Visual Studio 2015 Community (Free)
- Grasshopper Assembly Wizard: this adds a ready made template for GH plugins in Visual Studio
- Some familiarity with C#, and a bit of technical reading comprehension.
I am using C#, but I assume the process is fairly similar for those of you who use Visual Basic. All the software I am using is up to date as of the date of writing (Feb 27, 2016). I also assume the Educational License is identical in functionality to the professional license, and the difference is purely about licensure. Finally, this is more or less an explained version of David Rutten's answer to my question in the Grasshopper forums.
You will need the documentation for the Rhino SDK (RhinoCommon), and the Grasshopper SDK. Unlike the Rhino SDK, you will not find the Grasshopper SDK online. Instead, in Grasshopper, you will find a link to download it in Help > Download SDK Help. This downloads a Windows Help file that contains all the necessary examples and documentation.
It will not hurt if you go through the exquisitely and extensively detailed guide for RhinoCommon found here, including creating your first Windows plug in and the other examples. From the Visual Studio end of things, the process is incredibly similar. It also serves as a quick crash to the functions of Visual Studio if you are, like me, unfamiliar with it. (Feb29 Edit: Check this illustrated guide by Giulio Piancentino.)
So without further ado, you need to do the following:
Create a new project in Visual Studio
If you installed the assembly wizard, you will find the necessary template as shown in this image:
Make sure the file path in Location is easily accessible. Also make sure that the name you use is recognizable. When you click OK, you will see this window:
This dialog basically fills up the code for you. The fields Name, Nickname, and Description are how your component is described in words inside the Grasshopper user interface. Category and Subcategory determine which tab and panel your component shows up. If you tick the Provide sample code tickbox, it will fill your code up with already existing and well explained code for an Archimedean Spiral component. This component, when compiled and installed, will show up under the Curve tab, inside the Primitive panel. For the purposes of this exercise, I will leave the box unticked, but make sure to look at the code provided at your leisure, as it is quite educational. If you did not change of the default settings, your Visual Studio window should look something like this:
You can see all the necessary methods to fill out and how they work. They are self-explanatory enough in my opinion that if you know any C# they are easy enough to follow. Make sure you go through the examples in the Grasshopper SDK help files. With all the little tricks and components on what each method does.
Make sure your debugging settings are correct
This is something I needed to figure out by trial and error. Because the Grasshopper Assembly Wizard was written a while ago, the default setting for debugging is, well, not what you would expect if you are using Rhino 5. Either by pressing Alt + Enter, or right-clicking the project name (the blue-highlighted MyProject2 in the screenshot above), open the project's properties. This will open a new tab, which you do not want to mess with if you do not know what you are doing. (I certainly do not). Click the Debug option. You will see these default options:
You will notice that the default option is for Visual Studio to open Rhino 4. If you are using Rhino 5, like I am, you do not want that for obvious reasons. Find out where Rhino 5 is located on your system, and change the Start external program field to the Rhino 5 file path. On my system it is:
C:\Program Files\Rhinoceros 5 (64-bit)\System\Rhino.exe
This does not open Grasshopper automatically. You will still need to open Grasshopper yourself by typing the command in Rhino. Typing "Grasshopper" in the Command line arguments text box does not seem to work. This does not impede debugging, as Grasshopper hits the Visual Studio breakpoints and everything you would expect. If you know of what to type exactly to have Grasshopper start automatically, please mention it in the comments.
Now your project is set up, more or less, from the Visual Studio side of things. You still need Grasshopper to load the assemblies (dll files) that you are creating with Visual Studio. You need to set that up from Rhino.
Add your project to Grasshopper Developer folders
Remember the location you set up earlier when creating the project? You need to have Rhino read that folder for you to check your code. For our project, on my PC, the folder was in:
C:\Users\Abdurrahman\Documents\visual studio 2015\Projects\MyProject2\MyProject2
You will notice there is a \bin folder. This is the folder you want. The full file path would be:
C:\Users\Abdurrahman\Documents\visual studio 2015\Projects\MyProject2\MyProject2\bin
Copy this path (from your computer, not from above, unless your name is Abdurrahman, too). Now open Rhino and type GrasshopperDeveloperSettings in the command line. You will find it in the autocomplete. This will open this window:
(Feb29 Edit: You must uncheck the tickbox above for debugging). Anyway, if you click the Valid Folders button, you will see where all your existing Grasshopper plugins are installed around the system. This is useless to you, what you want to do is click the Add Folder button, and write the name of your \bin folder above there.
There. You're done. Now Grasshopper will load whatever files are in that folder. Now you will find your custom components as you create them where you specify their location to be. Congratulations.