This article is intended for C Sharp (C#) scripting beginners in the visual scripting language environment Grasshopper for Rhino3D . Loops, also called recursions, can repeat a block of code until a special condition is reached. They are useful as they save time, reduce errors, make the code shorter and more readable.
Why using loops? Let’s start with an example
Allow me to show an example why loops are super practical and let’s create a C# component in Grasshopper. Since we are sticking to the basics, we don’t need any C# input parameters for now. (Controlling a C# scripting component with regular grasshopper components will be covered in a separate blog entry)
Let’s get started:
Get the example file on github
- By double-clicking, we open the Search Feature and type “C#”. This is all we need to select the C# scripting component, which we confirm by pressing ENTER on the keyboard.
- The component appears with the default x and y input parameter, which we do not need. By zooming in on the component, additional parameter options appear.
- We remove both parameters by clicking on the according minus symbol.
- Et voilà – our C# scripting component has no more input parameters.
We are adding now a Panel Component to the “A” Outputparameter and open the Script Editor by double-clicking on the component and copy the code below into the RunScript method of the editor. (Between the { } brackets) and click on the OK button.
List<Point3d> pts = new List<Point3d>();
Point3d pt0 = new Point3d(0, 0, 0);
pts.Add(pt0);
Point3d pt1 = new Point3d(1, 0, 0);
pts.Add(pt1);
Point3d pt2 = new Point3d(2, 0, 0);
pts.Add(pt2);
//...
Point3d pt5 = new Point3d(5, 0, 0);
pts.Add(pt5);
//Output
A = pts;
After creating a list “pts”, we made a series of 4 points with different X coordinates, which we added to the list. In order to make the collection of points available to the Grasshopper environment, we need to pass them to the output parameter “A”.
The code block is still quite understandable. But what if we want to create a series of over 9000 points? That would be a pain in the ass to write and almost impossible to find errors. Here we want to execute the same code block over and over again – a point is created and added to the list that we want to output. Firstly, it is not wise to write repetitive code as this can easily lead to errors, but more importantly, we can be lazy – i mean efficient and write as less code as possible. What a surprise – we can solve this with a for loop, for example.
Creating a Loop inside the Script Editor
To create a for loop quickly, just write “for” in the editor and use then the TAB key on your keyboard. The autocomplete function creates the syntax.
There are 3 statements at a “for loop”:
for (int i = 0; i< length; i++)
{
// recursive code block
}
for (1. Init; 2. Cond; 3. Iter)
{
// recursive code block
}
- Initialisation
Executed once from the execution of the code block. - Condition
Defines the condition for executing the code block. - Iterator
Executed each time after the codeblock is executed.
Enough theory. So let’s take a look at a few examples! Let’s build up a small test setup. Just connect a panel component to the output A of a c# component without Input paramter and copy the code snippets below.
1. ++ Iterator – Count
Let’s start with a small test script:
Just copy the code below into the editor (See Figure 1)
int count = 0;
for (int i = 0; i < 5; i++)
{
count++;
}
A = count;
The code block is looping 5 times. Every loop the value of the variable countincreases +1. Count is passed to the output parameter A, which value is shown inside the GH Panel component.
2. ++ Iterator – List
Let’s create a series of numbers (integers)!
Copy the code below:
List<int> lst = new List<int>();
for (int i = 0; i < 5; i++)
{
lst.Add(i);
}
A = lst;
The code block is looping 5 times. In every Loop i is increasing, till i is not smaller then 5 anymore. The value of i is added to the list “lst“. lst is passed to the output parameter A.
3. — Iterator
We are creating this time a descending list of numbers with the — iterator.
Copy the code, like always, into the script editor:
List<int> lst = new List<int>();
for (int i = 3; i >= 0; i--)
{
lst.Add(i);
}
A = lst;
The code block is looping 4 times. In every Loop i, with the given start value of 3 is decreasing, till i is smaller or even then 0 ( i >= 0). The value of i is added to the list “lst”.
4. += Iterator
Copy the code, like always, into the script editor:
List<int> lst = new List<int>();
for (int i = 0; i < 5; i += 2)
{
lst.Add(i);
}
A = lst;
In every Loop i is increasing by the value of 2, as long as i is smaller then 5. Therefore the code block is looping 3 times. In every loop the value of i is added to the list “lst”.
The first steps have been taken! Congratulations! In the next blog post we will work with rhino geometry and input parameters.
Visit the second part of the tutorial!
If you have suggestions or constructive criticism, please leave a comment or write me at: leonbrohmann(at)gmx.de