Before we can create objects, we need to store them. In this video, learn how to create and work with variables, including storing and handling and printing variables so we can use the objects in Grasshopper.
- [Instructor] To introduce you to Python, we're gonna start with the basics gradually getting more complex as we work through the course. The first concept we'll be learning about is Variables. To define Variables, they are simply identifiers that point to a reserved space in memory in which we can store values or data such as numbers, letters or objects. Every time we create a new variable, we're essentially reserving a space in memory to store something. Think of a variable as a bucket which we can store whatever object we like. Whenever we need to find some piece of data, we simply look at the appropriate bucket or variable which contains the value.
In scripts, the object inside the bucket may change and so the bucket is simply reassigned to another piece of data or a new bucket is created. In a new Grasshopper file, let's go ahead and place a new Python component and look at using variables. And to start, let's open up the Python component and we'll simply create a variable inside. You may have realized that we have already been using variables without knowing it. When we have assigned an input value to the input X, we're essentially assigning to the variable X which points at the input value.
However we can create variables at any point with no need for input values. To create a variable, we simply need to name it and then assign data to it using the equals symbol. The same way that we have assigned inputs to output parameters. So for example on a new line, let's a Variable named var and we'll assign to that the value zero. This is known as assigning a value to a variable. The variable var is now pointing to the value zero in memory.
So now we can refer to var to use that value. Let's output that variable and see what we get by assigning var to the output a. Go ahead and press OK and now when we mouse over the output a you can see that we have output our variable which is giving us the value zero. We can also update the output parameter from a to the name of our variable from within the script. To do this, right click a and simply rename it var. This is now outputting simply the var variable which again is zero.
For now, I'm gonna keep this as a. In Python, we can reassign variables by simply using the same variable name. For example, let's reassign var to the value one. We can do this because Python is what's known as a dynamically type programming language which means the variable and the data are not bound and so the variable can store any data type it likes. Instead of outputting the new value, let's use the internal print keyword to simply print var.
And when we press test, you can see that var ultimately equals one in the output window. This is because we have reassigned var and var now points to one. As the variable represents the value that it points to, they can be used directly in operations, methods or functions. To demonstrate this, let's add a point from the runner script syntax library and we'll assign the result to the variable point. To do this, let's create a variable name point and then we'll access the runner script syntax library using rs and the function AddPoint.
Instead of only using numbers for the xyz however, let's use the variable var to drive the x coordinate and zero for the y and z. Then let's assign point to the output parameter a and hit OK. Great, so you can see that we have a point output which has been created using a variable and reference to the variable point. When creating variables in Python, there are a few naming conventions to look out for.
Variable names can contain letters, numbers and underscores however we can't use numbers to start. For example, try to create a variable named 4var and assign one to it. Go ahead and test the script and you can see that this produces a syntax error. This is because we have started the line with four so Python doesn't recognize var as being a variable. We also can't use symbols in our variable names. For example, the assignment percent var equals three will produce the same error.
Let's comment out the last assignment to test this. And go ahead and press test and you can see we still get an error. We can however use underscores in our variable names. For example, underscore var equals one doesn't cause an error. Let's try this on a new line. And you can see that I've commented out the last line. Go ahead and press test and this now works.
We also need to be careful when using capitalization too as Python is case sensitive. For example, if we create two variables named hello and Hello equaling one and two respectively, you can see we have two different variables. Let's try that with lower h as the first and uppercase H as the second. And then we'll print both of these variables by saying print hello and print Hello and test.
And you can see that we get two different numbers as we have created two different variables. This can often lead to unexpected errors so it's something to keep in mind and an eye on. Finally in Python there are words known as keywords or reserved words that we simply can't use in Python as Python already has a use for them. We know if a word is a keyword when it turns up blue in the script. So looking at our script already, you can see that we have a couple of keywords such as import and print. If we try to use one of these as a variable, for example, import equals one and test, you can see that we get another syntax error exception.
These are some of the fundamentals that we need to know when working with variables. With this base understanding, let's start working with some of the different data types in Python using variables to store them.
- 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