Grasshopper data types
Downloads material
🦗⬇️⬇️⬇️ Download the gh script file on curves here ⬇️⬇️⬇️🦗 🦗⬇️⬇️⬇️ Download the gh script file on surfaces here ⬇️⬇️⬇️🦗 🦗⬇️⬇️⬇️ Download the gh script file on parametric curves here ⬇️⬇️⬇️🦗 🦗⬇️⬇️⬇️ Download the gh script file on parametric surfaces here ⬇️⬇️⬇️🦗
Reference and bake geometries
The advantage of Grasshopper is that you can interact with objects drawn in Rhino so you don’t need to code everything from scratch. For example, you can draw two points in Rhino and create a line between those two points in Grasshopper. To do so:
- Draw two points in Rhino.
- Drag two “point” components and one “line” component on the Grasshopper canvas.
- Right-click on one of the “point” component and select the option “set one point”.
- Click on the corresponding point in Rhino. This point is now referenced in Grasshopper.
- Do the same for the second point.
Congratulations, you just created your first parametric line! Now, if you move the points in Rhino, the line will automatically be redrawn. However, you may have already realized that Grasshopper objects cannot be selected in the Rhino viewport. They are a bit like ghosts, you can see them but not interact with them… This is because to quickly regenerate new geometries and enable interactive feedback, Grasshopper only computes rough previews of the result. To interact in Rhino with a parametric object created in Grasshopper, you first need to bake that geometry. To do so, right-click on the “line” component and select “bake”.
To summarize:
- To modifiy Rhino objects in Grasshopper, you need to reference them.
- To modifiy Grasshopper objects in Rhino , you need to bake them.
Volatile and persistent data
Now imagine you would like to share your code that draws a parametric line with your colleagues. What would you do? One way would be to share both the .gh (or .ghx) and the .3dm files. However, this is not very practical as there is a risk that the two files might be later separated. A more robust practice is to only send the grasshopper file after internalizing the Rhino input into Grasshopper. To do so, right-click on the “point” components and select “internalize data”.
Now the link between the Rhino and Grasshopper objects is broken so moving the points in Rhino will no longer affect the position of the line. However, the previous location of the points is stored in Grasshopper so the line remains there. A data which is internalized in a Grasshopper component is said to be persistent as opposed to volatile data which is inherited from another source.
Volatile data can either be referenced to a Rhino object or transmitted from another Grasshopper component. Let’s expand our example by connecting the output of the “line” component to the input “C” of the “divide curve” component. This has the effect of dividing the line into ten pieces. The data arriving to the “divide curve” component is volatile as it is inherited from the “line” component. To make it persistent, we need to right-click on the input “C” and again select “internalise data”. This cuts the link between both components but stores the last line geometry in the input “C”.
Internalizing data is very useful to cut an algorithm into several steps or to save intermediary results in dedicated components. However, the more you internalize the data, the less parametric your codes are. It is up to you to find the good balance!
Primitive and geometric data types
The word data is a very broad concept that encompasses many different things. In computer programming, types are used to characterize similar groups of data. In the example above, we manipulated points and lines which are two data types that you can encounter in Grasshopper but there are many others. We can group the main data types present in grasshopper into two categories: Primitive and geometric data types. Primitive data types are common to many programs as they are directly built in the programming language used to code the software. Those include Booleans, Integers, Colors and Strings. On the contrary, geometric data types are only defined inside the Rhino environment and include Points, Vectors, Planes, Curves, Surfaces, Breps. You will find below a brief definition of the most common data types:
Primitive data types
- Boolean: A two-state variable which is either “True” or “False”.
- Integer: A “whole” number including zero, natural numbers and their negative counterparts (…, -3, -2, -1, 0, 1, 2, 3, …).
- Number (or Float in other programming languages): A number which can have a decimal point (…, -3.0, -2.5, 0.00, 0.0001, 1, 2.5, …).
- Color: Three consecutive integers between 0 and 255 representing the RGB (Red, Green, Blue) value of a color For example, black = (0, 0, 0) and red (255, 0, 0).
- Text (or String in other programming languages): A variable that stores a sequence of characters, such as letters, words, symbols, and spaces. By convention, strings are written between quotation marks (e.g. “Hello World”).
Geometric data types
- Point: A three-dimensional point expressed by its xyz coordinates (e.g. {0.12, 1, -5.0}).
- Vector: A three-dimensional vector centered in {0, 0, 0} and expressed by its xyz coordinates (e.g. {0.12, 1, -5.0}).
- Plane: A three-dimensional axis-system represented by its origin and its normal (e.g. O(0.12, 1, -5.0) Z(0, 0, 1)). The variable also stores the values of the X and Y axis.
- Curve: A variable to store a NURBS curve. NURBS curves are defined by a curve degree, control points, knots, and an evaluation rule (More info on NURBS here). They can be open or closed and planar (2D) or non-planar (3D).
- Surface: A variable to store a NURBS surface.
- Brep: A variable to store the boundary representation (brep) of a volume. A brep is a collection of attached surfaces that can form an open or closed volume.
- Mesh: A variable to store a 3D mesh. A mesh is a collection of vertices and polygons that define the shape of an polyhedral object.
- Geometry: A variable to store any type of geometry. While it could be tempting to always use this data type for any kind of geometry, remember that a good scripting practice is to be as specific and explicit as possible to make your code both more readable and more optimized.
Type casting
When coding in Grasshopper (or other scripting environments), it is important to match the data types of the output and input of two consecutive components. To ensure you are passing the correct data type, you can hover any input or output of a component to display a more detailed description of the parameter. In the example below, we see that connected parameters always share the same data type (respectively curves, integers, and points).
However, in some particular cases, Grasshopper is smart enough to automatically convert data to the correct type. For example, if you pass a point to vector parameter, Grasshopper will create a vector with the same XYZ components than the points. The action of converting data from one type to another is called type casting (or type coercion). As points and vectors are both defined as a collection of three numbers, points can easily be casted to vectors and vice versa. In the image below, you can see other examples of type casting that are handled in Grasshopper, such as casting vectors to planes, or breps to meshes. While it is not possible to directly cast a point into a mesh, it is possible to achieve that result through successive casting operations.
Lastly, some data types can only be casted to other types under strict conditions. For example, a curve can only be casted to a surface if the curve is closed.