Add ghPython inputs dynamically from script?

Hey,
Im looking for a way to have a ghPython component add input parameters dynamically depending on some data received from the first input x. I’ve been trying to find ways to do this, but all I can find are about coding .gha plugins in C#. I’ve also been looking some at the ghenv.Component.CreateParameter, but not sure if it can be used in a ghPython component? Is there otherwise a way to achieve something similar?

Example pseudo-code of what I want to do with ghPython:

x = ["A", "B", "C", "D"]        # x revieves a list of strings from another component

for item in x:
    add input parameter item        # iterate over the list, add input parameters with the same name as each string item.

After script has run input parameters to the ghPython component should be x, A, B, C, D.

Many thanks

Jakob

Hi Mertzi

to be honest, you should not attempt that. The Grasshopper SDK does not want you to add inputs or outputs, not perform any topological adjustment to the solution graph during a solution.

It should be possible to do what you are requesting without changing the graph. Maybe if you give the example in details I’d be able to help with that.

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi Giulio,
Thanks for your quick reply.
I understand that its a bad idea to change the number of component parameters during a solution. I’m thinking that maybe this can be avoided by having a update button or toggle connected to the ghPython component, and/or having it ask if it may or may not change it similarly to “ghenv.Component.CanInsertParameter” method? Kind of like how the “explode tree”-components “match outputs” function.

I’ll also try to be more explicit in the image below on what it is that I want the script to do, I hope this helps!

What about using a DataTree? You can have the Class Instance Names as branches on one tree, and in the other tree, the attributes. This way, your DataTree can contain as many Instance Names and Attributes as you like. You can then iterate over these trees in the script.

Hey fraguada, thanks for your reply!!
Yes, that could work, but I guess that would require the user to entwine all the class instance specific data in the right order? Seems less error-prone to guide the user where to connect what data/geometries than for them just “having to know”. Let me know if Im missing your point here, Im still quite new to scripting, so if you can think of a better way to do this I’d be very interested!

Bumping this thread as there was some speculation that it could be done. Giulio, any more thoughts on the topic? I tired posting as detailed information as I could.

This is “possible” in Rhino WIP, because there is Component Mode, which allows to override the component methods to append to the context menu. This is useful to allow the user to call this functionality from outside solution time. If you have access, please let me know.

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hey Giulio,
Thanks for your reply, that’s great to hear!
I do have access to Rhino WIP. How can I access the “Component Mode” to modify the GHpython component?

On the plug menu on the top right, click the icon, then choose “Component Mode”.

Hey @ Piac
When I attempt “component mode” from the Python-node in Rhino WIP/Grasshopper 1 I get the following error:

Runtime error (MissingMethodException): Method not found: 'Void Microsoft.Scripting.Utils.ExceptionUtils.RemoveData(system.Exception, System.Object)'.

I havent edited anything in the python node, just opened it, went to component mode and got the message.'
It appears the issue is related to the first line of the default code:

from ghpythonlib.componentbase import executingcomponent as component

And that all(?) or something in there is missing.
I’ve been looking through your guide on the subject, but couldn’t really find anything on this particular issue.
What have I done wrong? What can I do to fix it?

EDIT
Discovered an old Iron Python version on my computer (2.7.3), uninstalled it and now it seems to work as expected! :slight_smile:

Many thanks

Jakob

I had the same issue with ghpythonlib. Seems IronPython 2.7.3 is not well supported. The newest IronPython 2.7.7 seems to be working great.

If you install IronPython 2.7.7, remember to turn off GAC Assemblies. See more info on the support page, made with Scott’s help.

Hi @piac, as this thread was never answered.
Which method needs to be overridden to access the number of inputs/outputs or to have a boolean to toggle them on and off?
Can I use drop-down menu item (bool) to toggle an input on and off?