This series is about making animations in Grasshopper. In this video we build a component that makes creating animations very easy—a component that produces a list of counters. These counters can be connected to any inputs on your canvas to create motion and/or simulate the passage of time for presentations or to generate geometry.
Below is the C# script we used to make the Row Your Boat component. This is all of the code from our scripting component.
// Grasshopper Script Instance
#region Usings
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
#endregion
public class Script_Instance : GH_ScriptInstance
{
private void RunScript(
bool reset,
int counters,
double speed,
double waitFor,
ref object a,
ref object b)
{
if (reset){
counterList.Clear();
file_number = 0;
justReset = true;
}
else{
file_number++;
if (counterList.Count < counters){
// Initializing all counters to 0
while (counterList.Count < counters){
counterList.Add(0.0);
}
}
if (justReset){
justReset = false;
}
else{
for (int i = 0; i < counterList.Count; i++){
if (i==0 || counterList[i-1] >= waitFor){
if (counterList[i] < 1){
counterList[i] += speed;
if (counterList[i] > 1){
counterList[i] = 1;
}
}
}
}
}
}
a = counterList;
b = file_number;
}
private List<double> counterList = new List<double>();
private int file_number = 0;
private bool justReset = false; // Flag that indicates if the script was just reset
}