Transmit data without wires in Grasshopper using the C# component

Grasshopper without wires: Transmit data to any component

The philosophy of data in Grasshopper is quite simple: data flows from left to right, and it flows along the wires. That is, until you break it.

Based upon a slider permutation component created by David Rutten, and modified by Mostapha Roudsari, I have created a component that can transmit numbers to any component in your canvas, even when there is no wire connection.

grasshopper-transmit-data

The value is entered in the bottom left. The component then looks for any ‘Num’ components, and send the value to them. If there are multiple components, it will find them and update them accordingly.

grasshopper-transmit-data-2

By default, if nothing is connected to that ‘input’ input, the component will update all Nums. If you only want to update certain components, connect these to ‘input’.

grasshopper-transmit-data-3

The component can potentially be created to work with any component. By finding the class of any component in Grasshopper, we can interact programmatically with it.

The component is written in the C# component and can be opened and edited directly within Grasshopper.

Download


Source code

    List<System.Guid> guids = new List<System.Guid>(); //connected components

    Grasshopper.Kernel.IGH_Param selNumsInput = Component.Params.Input[0];
    IList<Grasshopper.Kernel.IGH_Param> sources = selNumsInput.Sources;
    bool isAnythingConnected = sources.Any();

    if(isAnythingConnected && (bool) startme)
    {
      foreach (var source in sources)
      {
        IGH_DocumentObject component = source.Attributes.GetTopLevel.DocObject;

        //check if valid number
        bool validNumber = true;
        Grasshopper.Kernel.Parameters.Param_Number myNum = component as Grasshopper.Kernel.Parameters.Param_Number;
        if(myNum == null) validNumber = false; //check if it's a num component
        else if(myNum.DataType == Grasshopper.Kernel.GH_ParamData.remote) validNumber = false; //check if free
        if (validNumber)
        {
          guids.Add(myNum.InstanceGuid); //a list of free num component GUIDs
        }
      }


    }

    //this loop can probably be merged with the above, unless I'm missing something...
    List<Grasshopper.Kernel.Parameters.Param_Number> nums = new List<Grasshopper.Kernel.Parameters.Param_Number>();
    foreach (IGH_DocumentObject docObject in GrasshopperDocument.Objects) //for every GH component in document
    {
      Grasshopper.Kernel.Parameters.Param_Number num = docObject as Grasshopper.Kernel.Parameters.Param_Number; //try convert to num
      if(num != null) //if that component is a num
      {
        if(isAnythingConnected) //...and nums are connected to this C# component
        {
          if(guids.Contains(num.InstanceGuid)) nums.Add(num); //...then if that num is connected, add to 'nums'
        }
        else if (num.DataType != GH_ParamData.remote)
        {
          nums.Add(num); //else add all free num components
        }

      }
    }

    //nums is the list of all the components we want to update.

    Grasshopper.Kernel.Types.GH_Number ghnum = new Grasshopper.Kernel.Types.GH_Number(val);
    foreach (Grasshopper.Kernel.Parameters.Param_Number obj in nums)
    {
      if((bool) startme)
      {
        obj.PersistentData.Clear();
        obj.PersistentData.Append(ghnum);
        obj.ExpireSolution(true);
      }
    }

    A = nums;

One thought on “Grasshopper without wires: Transmit data to any component”

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: