Automatically merge multiple outputs

Hi, community, I’m looking for a method to get the variable outputs from one component automatically merge (track the all the outputs of first node and connect them to single input of second component). I want the script to be able to adapt to different output count of the component output:

This solution will save a lot of manual wires connecting, any advice on this?

1 Like

That’s a big component you have there! :scream:

Try this, it works with nicknames. Receiver must be a generic “Data” parameter… for now.
c# connect comps
c# auto connect many.gh (11.7 KB)

Not deeply tested… test it on a safe context.

Code:

using System.Linq;

  private void RunScript(string nickname_From, string nickname_To, bool connect)
  {
    if(connect){
      compFrom = (IGH_Component) this.GrasshopperDocument.Objects.FirstOrDefault(o => o.NickName == nickname_From);
      compTo = (Grasshopper.Kernel.Parameters.Param_GenericObject) this.GrasshopperDocument.Objects.FirstOrDefault(o => o.NickName == nickname_To);
      if(compFrom != null && compTo != null){
        this.GrasshopperDocument.ScheduleSolution(5, Connect);
      }
    }
  }
  // <Custom additional code> 
  private IGH_Component compFrom;
  private Grasshopper.Kernel.Parameters.Param_GenericObject compTo;
  private void Connect(GH_Document doc){
    foreach( Grasshopper.Kernel.IGH_Param param in compFrom.Params.Output){
      compTo.AddSource(param);
    }
  }
5 Likes

Oh my… Thank you soo much, @maje90 ! This works perfect!


I’m not into C# yet but thats probably a good point to start from :slight_smile:
I just wonder if that component “Updates” the connection? I mean when press&hold toggle-value is false, does it disconnect and then reconnect again meanwhile?

This version uses the position to determine which components are to connect.
It search 10pixel away from top left corner of c# script to find “from” and on top right for “to”.
“From” should be a generic non-parameter component, “To” should be a parameter (any, probably).
c# connect comps 2
c# auto connect many 2.gh (8.5 KB)
If it only finds the parameter, it will remove all its sources…

Different from using nicknames. Maybe better, maybe worse…

Code:

using System.Drawing;

  private void RunScript(bool connect)
  {
    if(connect){
      RectangleF rect = this.Component.Attributes.Bounds;
      compFrom = (IGH_Component) this.GrasshopperDocument.FindObject(new PointF(rect.Left - 10, rect.Top - 10), 9);
      paramTo = (IGH_Param) this.GrasshopperDocument.FindObject(new PointF(rect.Right + 10, rect.Top - 10), 9);
      if(paramTo != null){
        this.GrasshopperDocument.ScheduleSolution(5, Connect);
      }
    }
  }

  // <Custom additional code> 
  private IGH_Component compFrom;
  private IGH_Param paramTo;
  private void Connect(GH_Document doc){
    paramTo.RemoveAllSources();
    if(compFrom != null && compFrom.Params.Output.Count > 0){
      foreach( Grasshopper.Kernel.IGH_Param param in compFrom.Params.Output){
        paramTo.AddSource(param);
      }
    }
  }

It seems not. Any new connection that did already exist is ignored… it seems.

The button become “True” once you click it, and already trigger a recomputation “wave” to its recipients … “holding” usually will not mean anything. Then, when you release the click, it will trigger again and become “False”, but then the c# script will just skip all of its internal code because of the initial “if(” …

3 Likes

Amazing components, @maje90 , and thank you for sharing! These are more than enough to solve my issues for the moment. :pray: Have a nice day! :blush: