The Python component in Grasshopper is a built-in interpreter with its own script editor. Walk through the basics of using the Python component in Grasshopper, including inputting and accessing data from Grasshopper, outputting data, commenting out lines, and error handling.
- [Voiceover] Let's now have a look at some of the basics when using the Python component in Grasshopper. Let's start by launching Grasshopper, by clicking the launch Grasshopper icon in the standard toolbar. The Python script component can be found in the Maths tab, and then script section, and Gh Python script. Let's go ahead and select that component and click to place it onto our canvas. The default component contains two inputs on the left, which are X and Y, and two outputs on the right, which are out and A.
Inputs and outputs can be increased and decreased, by simply zooming in and using the plus symbol to increase, and minus symbol to decrease. Inputs and outputs can also be renamed by right-clicking the input and changing the name at the top. For now, I'm gonna leave mine as X. Les start by inputting a number slider into the input X. To place the slider, let's double left-click the canvas and write three, enter.
This will create a default slider with the range zero to 10. And then, let's simply plug that into the input X. At the moment, we can see nothing is happening with our Python component. If we mouse over the warning bubble, it's quite clear that there's no script to execute. We'll fix that in a moment. First, let's have a look at the type and access hints. Go ahead and right-click the input X, which has the number input to it. If you look down at the bottom of the menu, you will notice three types of access.
Item access, list access, and tree access. These allow us to select how we want the Python script to operate on the data that we're inputting. As we're only inputting one number, let's leave this on item access, as we're inputting a singular item. If we were inputting a list or a tree, we might select one of the other two. However, the item access can be used for all types, which means it will operate on every item in a list or tree as if it were a single item input.
Next, let's mouse over Type hint on the menu. Here is where we can specify the type of data that is being input into the Python component. By default, it is set to ghdoc Object. This is useful when working with the Rhino script syntax library, which we'll learn more about in the next two videos. For now however, let's change this to INT, as we're inputting the number as an integer. By specifying the type of data we're inputting, Python will know how to treat and access the data.
To access the scripting environment, we simply double left-click the Python icon on the component. This pulls up the script editor where we can edit our script. This is called the interpreter, as it interprets our Python code line by line, and executes it. The white space in the middle is where we can add our code. At the bottom are two tabs, which provide information on the execution of our script, with the output tab and help documentation with the help tab.
We'll be using both of these throughout the course. At the top are a series of different menus, the file menu will allow us to run, import, and save our scripts. We can find editing tools in the edit menu, and find and replace, and some option setting tools, in the tools menu. Including the ability to set template code, which we'll do shortly. The Mode menu is for advanced scripting, which we won't be using in this course. And finally, the Help menu provides help documentation, by going to Help for rhinoscriptsyntax.
Along with samples and access to the forums, and Python documentation. We'll be accessing this Help for rhinoscriptsyntax later on in the course. In that actual scripting area of our script, this first thing you'll notice are the lines of green text at the top. This is simply a comment, or a multi-line comment, which can be created using triple quotation marks at the start and end, or we can create a single line comment by using the pound symbol. For example, pound, this is a comment.
This line will then not be executed when we run the script. The advantage of using these, are to tell the user what is happening, especially if more than one user is using the same script. Any comment within the script will not be executed by the script when it's run. The multi-line comments at the top of the Python script, are particularly used for as they allow us to document what our Python script is doing. For example, if we update the description to be something else, for example, this is a simple component, and then hitting okay, and hovering over the Python script component, you can see that the description of the component has been updated to this is a simple component.
The same goes for inputs and outputs. So inserting a multi-line comment at the top of a Python script is useful for documenting what a Python component does and the inputs and outputs it requires. On lines 10 and 11 of the script, there's information about the script, the author and the version. This is simply the default script which asks for this information, and while it's useful, it's not vital to run our scripts. So for the time being, let's remove lines 10 and 11, and eight while we're at it, and we'll check how the set template code function works, by clicking tools, set template code.
This opens up a window to check if we really want to change the default code. I'm gonna click yes, so now whenever we create a new Python component, this will be our default script. On line eight, you'll notice that we are using the word import, and then rhinoscriptsyntax. We'll look at importing in more detail soon. For our first script, let's simply output the number that we've input, to the input X. To do this, we simply need to assign the data to an output, which in this case, we can use A.
So on a new line, let's write A equals X. This is the act of assigning the data X, to the output A. To execute this script, go ahead and click okay in the top right corner, and it looks like our Python component has no errors, so let's place a panel component by double left-clicking on the canvas, and writing panel, and connecting the output A to our panel. Great, so it looks like the input data three, is being output from our Python script.
If you try adjusting the input, you'll see that the output is also updated. The other output parameter out, is used for debugging and printing from the Python component. Let's jump back into Python to see how this works. Debugging is essentially the process of fixing bugs and errors that might occur in our script, we'll know when the component has failed, as it will usually turn red. Let's intentionally create an error by assigning to the output A, an input that doesn't exist.
For example, J, I'll do that by deleting the X and updating it with J, and then hit okay. And you can see that the component has failed, as it's turned red. If we hover over the bubble, it gives us a small description of what has happened, that J is not defined. We can get more detail about the the error by looking at the out parameter. So go ahead and plug the out parameter into the panel. This is giving us a little bit more detail about what's happening, there's actually an unbound name exception, and the error has occurred on line 10.
We can use this information to fix the error. While scripting in Python, there are also times when we need the output or display pieces of data from our script, we can do this using the keyword print. Open up Python, and let's simply print the input X. To do this, on line 10, let's remove A equals J, and we'll replace it with the word print, followed by the item we want to display, in our case, X. And then, when we press test in the top right corner, you can see that the data is printed to the output window at the bottom.
This is very useful for checking data when we need to. If we press okay, you can see that anything that we print within our scripts, is also added to the out parameter. The out parameter is extremely useful for printing data and finding errors that may occur in our script. So, make sure to keep an eye on it if your component is failing.
- Python components
- Accessing the Rhino API
- Python programming basics
- Importing modules and libraries
- Grasshopper workflows
- Evaluating surfaces
- Creating and splitting surfaces
- Transforming geometry
- Baking geometry from Python into Rhino
- Adding text objects
- Exporting rendered frames for animation