Prerequisites
It is presumed you already have the necessary tools installed and are ready to go. If you are not there yet, see Installing Tools (Mac).
HelloGrasshopper
We will use Visual Studio Code and the dotnet Rhino Grasshopper template to create a new, basic, Grasshopper component called HelloGrasshopper.
If you are familiar with Visual Studio Code, these step-by-step instructions may be overly detailed for you. The executive summary: create a new Solution using the Grasshopper Component dotnet template, build and run, and then make a change.
We are presuming you have never used Visual Studio Code before, so we’ll go through this one step at a time.
Download the required template
- Launch Visual Studio Code.
- Open Visual Studio Code’s Terminal via Terminal (menu entry) > New Terminal, or using the command palette (⌘ ⇧ P) and search for “Terminal”.
- Inside Terminal, run:
dotnet new install Rhino.Templates
Starting the Project
- Create a folder on your mac where you would like your project to live. Name the folder
HelloGrasshopper
. - If you have not done so already, launch Visual Studio Code.
- Now we can open our new folder, navigate to File > Open Folder and choose the folder we just created.
- Open Terminal via Terminal > New Terminal, or using the command palette (⌘ ⇧ P) and search for “Terminal”.
- Enter the following command into the Terminal:
dotnet new grasshopper --version 8 -sample
- In our Folder explorer, we should see the project appear as Visual Studio Code discovers the files.
- Expand the Solution Explorer, this is the best way to interact with C# projects on Mac in Visual Studio Code.
Boilerplate Build
-
Before we do anything, let’s Run and Debug HelloGrasshopper to make sure everything is working as expected. We’ll just build the boilerplate Plugin template. Click the Run and Debug button on the left hand side of Visual Studio Code and then the green play button in the newly opened panel.
-
Rhinoceros and Grasshopper launch.
-
We will find the HelloGrasshopper Component under Category / SubCategory
- Adding the component to the canvas will run the component and output some interesting geometry in the Rhino Viewport
- Press Stop Debugging (⇧ F5), in Visual Studio Code, signified by the Red Square in the debug toolbar. This stops the debugging session. Now let’s n take a look at the Plugin Anatomy.
Component Anatomy
Use the Solution Explorer to expand the Solution (.sln) so that it looks like this…
- The HelloGrasshopper solution (.sln)
- The HelloGrasshopper project (.csproj) has the same name as its parent solution…this is the project that was created for us by the template earlier.
- References: Just as with most projects, you will be referencing other libraries. The template added the necessary references to create a basic Grasshopper component.
- Grasshopper is the Rhino for Mac main grasshopper DLL. Classes in this DLL are subclassed and used by your custom component.
- HelloGrasshopperComponent.cs is where a custom
Grasshopper.Kernal.GH_Component
subclass is defined. Your project may contain multiple subclasses of GHComponent if you want to ship multiple components in a single _gha. - HelloGrasshopperInfo.cs defines general information about this gha.
Debugging
- Add a breakpoint to line 75 of HelloGrasshopperComponent.cs. You set breakpoints in Visual Studio Code by clicking in the gutter to the left of the line numbers.
- Run and Debug. our project. The breakpoint will become an empty circle, this is because our code has not been loaded yet. Once we hit the breakpoint once and continue, the code will be loaded until we end our Debug session.
- Rhino and Grasshopper should open, if Grasshopper does not open, click “New Model” and run the Grasshopper command.
- Place our sample component HelloGrasshopperComponent and as soon as you do, you should hit your breakpoint and rhino/Grasshopper will pause (You may need to drag the Grasshopper window out of the way to see Visual Studio Code)
- With Rhino/Grasshopper paused, in Visual Studio Code we will see Locals under Variables. You can inspect all of the values for the variables in your component.
- Let’s Continue Execution in Rhino and Grasshopper by pressing the Green Play button in the Debug Bar
- Control is passed back to Rhino / Grasshopper and your command finishes. Now Stop (⇧ F5) the debugging session as before.
- Remove the breakpoint you created above by clicking on it in the gutter.
Congratulations! You have just built your first Grasshopper component for Rhino for Mac. Now what?
Next Steps
You’ve built a component library from boilerplate code, but what about putting together a new simple component “from scratch” and adding it to your project? (Component libraries are made up of multiple components after all). Next, check out the Simple Component guide.
Try debugging your new grasshopper plugin on Windows, all plugins using the new templates are now cross-platform by default.
Adding components
A single gha can contain more than one GH_Component derived class (and commonly does). Dotnet has support for adding more custom components to your project.
- Open Visual Studio Code’s Terminal via Terminal (menu entry) > New Terminal, or using the command palette (⌘ ⇧ P) and search for “Terminal”.
- Inside Terminal, run:
dotnet new ghcomponent -n "NewComponent"
- A new component will appear called NewComponent
Related topics
This article is focused on initial setup and debugging a Grasshopper component in Rhino for Mac. For further reading on customizing your component please see: