Grasshopper

algorithmic modeling for Rhino

Hi All! 

I ran into a problem which I cannot seem to solve! So this goes out to all the coders out there:

Is there a way to make a ghpython component take a variable number of input parameters / variables? Of course without having to declare additional variables in the code! 

I think that in a custom gh component one would use the GH_Component.GH_InputParamManager to, well, manage the inputs! 

How is this achievable, if at all, in a ghpython component? How can I count and list out the variable input parameters of the ghpython component I am running the code in?

Example python pseudo code (the simplest I can think of! i.e. print out the variable input params):

thisGhPythonComponent = GH_Component() <-- how to get THIS component?

inputManager = thisGhPythonComponent.GH_InputParamManager()

inputCount = inputManager.ParamCount

for i in range(len(inputCount)-1):

    input = inputManager.Param(i)

    print input

Any help would be appreciated! 

Also note that this is purely a Python question! Not looking for solutions in VB or C#...

Views: 5256

Replies to This Discussion

The point being, of course, that adding more inputs will result in printing out more outputs automatically!

Hi Guido,

Check the attached file.

The definition creates python's list of lists and data tree based on input parameters values.

Attachments:

Thank you djordje! 

That is precisely what I was looking for. 

Could you explain where "ghenv" comes from, line 7? 

It's variable, through which you access ghpython component setup. Giulio gave a nice explanation of it, in here, along with some other ghpython related issues.

awesome! 

thx dude!

Hi again djordje! 

I'm now having trouble with quite the opposite problem to the above.  

To put it simply, I would like to generate variable outputs on the component based on an incoming list. 


E.G. input list = [0,1,2,3] 

the output handles on the actual component would then update to have 0,1,2,3 outputs which I could then populate from data generated / manipulated in the script. 

The only way I've had minimal success right  now is by using:


lastOutput=ghenv.Component.Params.Output[len(ghenv.Component.Params.Output)-1]

for i, item in enumerate(input):
    newParam = ghenv.Component.Params.CreateDuplicate(lastOutput)
    newParam.NickName = item
    ghenv.Component.Params.RegisterOutputParam(newParam, i+1)

Which basically duplicates the last output (which is 'out' when you drop in a ghpython component without 'a'). 

Is there a way to create a GH_Param? I can't seem to be able to instance the class to make an empty GH_Param which I can then register as an output param. I do not like the 'CreateDuplicate' method because, obviously, you need something to duplicate in the first place! 

There is a way to add a new output parameter:

# by Anders Holden Deleuran
paramString = gh.Kernel.Parameters.Param_String()
paramString.NickName = "newOutput"
outputIndex = ghenv.Component.Params.Output.Count
ghenv.Component.Params.RegisterOutputParam(paramString, outputIndex)

But it's a bit buggy: after adding it, you can not assign any value to it, nor attach it to a panel. Maybe Giulio or someone else knows, how we could fix this.

So for now I guess you could manually add the same number of output parameters (by clicking on '+' sign) as the number of items from your inputList, and then just change their nick names (btw I changed the names of your initial new output parameter nicknames from 0,1,2,3 to "newOutput1", "newOutput2"... because in python variable names can not begin with a digit):

inputList = ["newOutput1", "newOutput2", "newOutput3", "newOutput4"]
for i in range(ghenv.Component.Params.Output.Count):
    output = ghenv.Component.Params.Output[i]
    output.NickName = inputList[i]

Hello there!

I have stumbled at the exact same problem of dynamically creating output parameters on a ghpython component. The case is as follows:

The input of my ghpython component accepts tree structures, the branch count of which I don't know beforehand, it's variable. The component reads the tree structure and iterates through the branches.

What I'm trying to do is to expose the contents of each branch on a separate output parameter, so that the user doesn't have to manually add & name each parameter, especially since their number is about to differ, based on the tree "fed" as input.

Indeed the method described by djordje creates the outputs, but they are not interactable in any way, they seem locked or something...

Any help from the python guys out there would be greatly appreciated!

I managed to do a really dirty solution (I think) by enclosing the above code by djordje in a function and adding:

ghenv.Component.Params.OnParametersChanged()

right after:

ghenv.Component.Params.RegisterOutputParam(paramString, outputIndex)

This seems to make the output parameter fully functional and interactable.

In my test script, I used a button to call the variable generating function and I also used the sticky dictionary to keep track of how many times I have pressed the button, naming the variables accordingly : var1, var2, var3 etc

It seems to work, but something tells me this is not the right way to do it though!

Attachments:

Most importantly I now have trouble passing data to those dynamically generated variables.

I tried using python's eval() function, right after creating them:

eval("variable_name_string = some_value")

but I'm obviously missing something since the interpreter throws an error at the "=" operator...

I attach a script that creates as many empty outputs as the number of the branches of the input tree.

Attachments:

old thread but it may be useful for someone

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service