I have a large experience in doing C++ plug-ins but recently I am doing some custom components for Grasshopper in C#. The programming itself is going pretty well but at the moment I don`t realize the extent to which I can customize GH components. So I will tell you what kind of component I want to create and I would appreciate if you told me if that is possible (if nothing else) so that I know I can pursue it.
I want to make a component with variable inputs and outputs. So the most general case would be:
I right-click on the component and a dialog opens up
In the dialog I can choose different functions, and they have different types of inputs and outputs (some have a Brep and an array of Points as an input and Curves as an output…some have an array of points as an input and as an output, etc…)
After selecting the function my GH component changes its inputs and outputs according to that function…it can “transform”.
So this would be some kind of “super-component” where I could spare having multiple ones for similar operations.
Yes, this is possible. Your component can implement the IGH_VariableParameterComponent interface, which provides a host of methods to dynamically (and safely) change the parameters of a component. While this is typically used for “ZUI” zoomable control (see the Sort List component, for example) it can also be used in tandem with adding/remove parameters from code. The “BANG” Explode Tree component provides an example of a GH component that dynamically adjusts its parameters based on user interaction (right click the component and choose “Match Outputs.” The recommended way to perform an operation like this is something like the following:
For each param you want to add, call CreateParameter on the component
then call Params.RegisterOutputParam on the created param (or RegisterInputParam)
call Params.UnregisterOutputParameter / UnregisterInputParameter on any params being removed
clean up after all modifications by calling Params.OnParametersChanged(), VariableParameterMaintenance() (part of IGH_VariableParameterComponent) and then expire the solution.
It is possible (as per Andy’s answer), but is it really a good idea? If you start to replace parameters you also have to deal with connections that are no longer valid. You potentially erase a lot of user effort when you switch to a different component flavour. Not to mention this way of doing things will be foreign to existing GH users. May I ask what benefits you’re getting out of this approach over the more traditional way of just creating more than one component?
I agree with @DavidRutten - everything what you say is possible, but 1) it takes a significant development effort (I know, I have developed a few component that have variable inputs, it is quite a headache to get right), whereas for a few use cases you can quickly bring together a few components and 2) your users will be confused.
Thanks a bunch to everyone for the answers. David, there is no way for me to explain myself without going into the details of the component I want to make…so I will just avoid it for now because it is a long explanation, and let you “believe” me it is the proper choice. I completely understand that it is “coliding” with the nature of GH…so Ill think about it a bit more…work on it, try to implement it (it will take a while) and we can gladly continue the discussion once I have something concrete.
I will try Andy’s advice and let you all know how the programming is going…
I can see you already got answers and solution to your question, so maybe this reply might not be of any use.
Nevertheless, a bit less pretty workaround is to just change the Name and NickName property of the input/output parameters to _, based on input data.
They are labeled below in green rectangles: