From the course: Grasshopper and Rhino: C# Scripting

The C# component

From the course: Grasshopper and Rhino: C# Scripting

Start my 1-month free trial

The C# component

- [Instructor] To start the course, let's have a look at understanding the basics of the C# component in Grasshopper. The C# script component is found under the Mass tab, then in the script section, and you'll see this C# component. Let's go ahead and place it onto the canvas. The first thing you'll notice is that the component is in a warning state. That is, it's orange. If we hover over the balloon message, we can see that this is because no code has been supplied, and as you can probably guess, this is because we haven't actually scripted any code into our component just yet. As with any other component, we have inputs on the left and outputs on the right. Inputs allow us to input data into our script and outputs allow us to return data that we've created from within the component. We can increase inputs by zooming in and hitting this plus symbol or remove them by hitting the minus symbol, and we can do the same with the outputs. You'll notice that there is a unique output on the C# component, which is this out parameter. Let's go ahead and connect a panel to this output by going to the Params tab, and then the input section and selecting the panel, then input the out parameter into the panel. The output tells us that there is no data collected, which is because the out parameter returns messages from within the C# script, which might be errors that occur in the script or messages that we may want to return to the user. This is very useful for viewing errors that may occur in our script, so keep an eye on it if your component is failing. Before we get into that, let's take a closer look at some of the options we have from the component menus. Let's start with the input parameters. If we right-click the input parameters, you'll notice that the options we have are the same as a lot of other components, such as baking the parameter, changing the input data structure, or setting specific data. Things get a little bit more interesting down the bottom of the list. Here we can change how the data we have input is utilized by the component. Changing the access allows us to specify how the data will be operated on. You can see the default is Item Access. This means that for each item that's input into the component, the script will run. So for example, if we had a list of items, the component would execute for every item in that list. If we were to change it to List Access, the component would operate on the list as a whole item, so it would execute once for that whole list. And lastly, Tree Access means that it would execute for the entire tree that's input, and this would execute once. The last option is the Type Hint option. This allows us to specify the type of data that we are inserting into the component. As the default, it will assume that we're inputting a System.Object, which is the base type for all data. We then have perimeter types, these are not really specific to Rhino, such as Booleans, Integers, Doubles, and Strings for example. Lastly, we have Rhino types such as Points, Planes, Lines, and other geometry. By specifying what type of data we are inputting into the component, we can make our code cleaner and more efficient as we don't need to determine the types from within our script. We will be changing these Type Hints throughout the course. If we right click the output parameters, we can see that we get the same options as we would any other component. It's important to understand that we can rename both of the inputs and outputs on the component. For example, we could change the input parameter to input one. This will then change how we access those parameters from within our script. The last options I want to show are those that we get by right-clicking the component itself. Among options that are similar to most other components, such as renaming, previewing, or baking, we have a few options which are unique, and those are these down here in the middle. One is the Manager Assemblies. This allows us to import other C# libraries or assemblies to use from within our code. While this allows our components to be very flexible, we won't be using this throughout the course, although you might want to access this later on, when you're more familiar with C# scripting. We also have the Remove Out option, this simply hides the out parameter, and then lastly, we have Edit Source. This allows us to edit the C# code from within the component, which we'll jump into next.

Contents