hello,
I have a question about loops in gh.python.
The loop is supposed to do something until the input is y, in this case(print"why").however it doesn’t do the tasks (print"why") until my input is “y”.and then it will print all of them at once. I was wondering if there is a way to see the results each time I run the loop instead of waiting until it ends and then see them all together?!
while True:
x=rs.GetString(“u happy?”)
if x==‘y’:
break
if x==‘n’:
print(“why”)
Hi Nazanin,
this message is the same I emailed to you in response to this question via email.
The command line never interacts with Grasshopper without blocking its solving thread. This is a design decision of the Grasshopper programming environment. GhPython follows this decision.
Thanks,
Giulio
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
thanks for your reply,
is there any other way to make the python component in grasshopper wait for our input in a for loop or while loop,and give us the result each time the loop runs?!because to me it seems like it waits until the loop breaks and then give us the result. I am not sure if this is the way that python works in grasshopper, or there is a way to fix it.
so what I’m asking is that is there any other way to write the code in a way to wait for an input each time it runs the loop(and also give me an output each time it runs it).
if I just get for example a number slider and connect it to my python component,it doesnt wait for me to change the parameter for each time it runs the loop(which in this case can cause into a crash)
thanks again
Hi Nazanin,
well, I’m sorry, but there is nothing to fix, really, as Grasshopper works this way by design. You should not interact/change things while the solution is running; or the price to pay might be a crash. Grasshopper will not redraw till you answer the command line request, as they are on the same thread. Also, as a User Interface guideline, you should avoid command line commands and picking commands, when running a Grasshopper solution. This means inside a GhPython component, too.
If you really think you should disregard this advice, which if for your own good (why waste time in something like this when Grasshopper can do all you request in its own loop?), then check this example. I will not provide support for bugs or problems you might have if you go down this road, as it really it is not the way things are designed to work.
Thanks,
Giulio
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
commandlineout.gh (5.8 KB)
thanks again,
I tried running the code that you sent, however , it doesn’t print anything!!!
thank you for your advice, I should probably find another way to run my code, right now I’m thinking of using python IDLE and manually copy pasting my output into grasshopper for use,each time that I run my loop.
Hi again Nazanin
You cannot stop the Grasshopper solution to show an intermediate result. If this will work, it will only work in the command line, doing something “outside” the Grasshopper solution. This is how Grasshopper works.
You can see the printed results after Grasshopper finishes computing if the myout.write
method will still call sys.__stdout__(text)
, after what it is currently doing.
I need to tell you, though, that I do not understand exactly what problem we are actually trying to solve. If you have something practical at hand, I’ll try to help. If it’s a theory on combining graphical with command line programming, I am sorry but I don’t know how to help more.
GhPython is not the Python interactive console, it simply Python as an engine to solve one particular geometry component in Grasshopper.
Let me know if I’m confused.
This has a parallel discussion on http://www.grasshopper3d.com/forum/topics/loops-in-gh-python
hello,
so I generated a code that creates a list of 6 random dictionaries everytime according to some inputs from the user,and the things it returns in each loop will turn on the gate to some different designs.what I was trying to do was to get the user’s input each time and show the results each time the loop runs.However with your explanation I reallized that this is not possible. so what I am doing now is to run the code in python idle and get the input in there and create the dictionaries.then copy paste the dictionaries into grasshopper and feed it to my python component there to show the designs.
I also added the code that I wrote and am trying to run, if you run it from python IDLE you will see what I was trying to do. I appreciate your feedback ttt.py (5.4 KB)
hint: when it asks to choose your favorite design just enter a number between 1 and 6.
I don’t know what you are trying, but GH has a component called [Read File]. When you write to a file from Rhino or IDLE GH has read the updated file before you switched windows…
wow this helped alot, now I can send the outputs directly to grasshopper!awesome!!!
Hello
My aplogies if the question is too silly or has already been answered.
Regarding the Loop in GhPython:
Can someone please let me know why the yellow panels show only the last value of i & j?
Thanks
i’m not exactly sure how you intend to use the results but your example could be done something like :
@jeff_hammond
Thanks,
- I got “IronPython.Runtime.List” instead of what you got in your panel.
- I was thinking of a surface to be divided in U & V directions. (2 * 3)
Then offset each subset surface with the “j” value.
you left the print line in yours (the one in the loop)
do you want to use a python component for this? or are other methods fine too?
Nothing changes,Even without the print line in the loop:
Grasshopper itself suffices to get it done, but
Since it’s just a matter of self-developing python knowledge, I would prefer to try python,
Thanks
The ghpython component does not give the output, until the complete script is completed. if that makes sense.
@nazanin_tabatabaei
Thanks
still confusing to me though!
@jeff_hammond
Solved!
Thanks
The list-of-list issue is a long-known one. You need to create datatrees in order to have Grasshopper recognize lists of lists (not a Python issue, it’s common to all components). See http://www.grasshopper3d.com/xn/detail/2985220:Comment:239629. And the Gist page:
https://gist.github.com/piac/ef91ac83cb5ee92a1294
Giulio
–
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
EDIT: In Rhino 6 and onward, there is also a new treehelpers
module already included with every copy of Rhino.