[help]How can I creat python loop between two GhPythonScript?

Hello everyone,

Here is the result of my attempt:


#Script 1:
    if "count" not in sc.sticky.keys():
      sc.sticky["count"] = 0
      for i in range(10):
        sc.sticky["count"] += 1
        a = sc.sticky["count"]
        print a

#Script 2:

if x >0:
    del sc.sticky["count"]
    print "final x = ",x

But the first script still counts 10 times, not 5:

Aren’t the sc.sticky[“count”] changes in the loop synchronized?

What should be done?

Thanks in advance!

Hi,

I don’t get what you are trying to do here?

This part of your code is flawed because it only runs exactly once, when “count” is not yet in the sticky dictionary, and also only outputs data (e.g. 10) that first time.

if "count" not in sc.sticky.keys():
      sc.sticky["count"] = 0
      for i in range(10):
        sc.sticky["count"] += 1
        a = sc.sticky["count"]
        print a

When you rerun the component, you should notice that your output is now None.

Are you attempting to make something like a counter that will trigger an other component once it hits a certain value?

Thank you for your attention, diff.

I want to create a loop with termination condition, The “Anemone” wastes a lot of time when there is a lot of computation, "Galapagos Evolutionary Solver " cannot be calculated automatically when the input data changes.

So I consider using python.

Here’s a custom, Pythonic counter script that should get you going.

custom_counter_0.6.gh (4.5 KB)

It stores the persistent variables as global ones, instead of in sc.sticky, however this works very similarly and can be changed easily, if needed.

1 Like

Thank you.

I tried global variables, but the data in them is still only output once same, and cannot work with grasshopper, I want the next solution will run recursively within the current solution. So like you noticed, I used two GhPythonScript, although it didn’t work as I expected.[:frowning:

I want to do create a recursive component in grasshopper, I envisioned doing this by creating a loop in python.

But at present, it seems that I can only convert all the components that I need into python and write all into a script.

It’s still not exactly clear to me what you attempt to achieve, but I don’t think that recursion can be achieved with Grasshopper components. You need a recursive function within on of the scripting components to accomplish that.

Probably, yes. If you can further explain what your goal is, I can maybe give further advice.

I want "Galapagos Evolutionary Solver " to run automatically without having to click it

That’s rather hard to do, if even feasible, since the solver isn’t meant to be externally triggered.

I’ve come across issues like this in the past and usually I came to the conclusion that it was best to simply implement it all within the scripting component. Given that this is not an easy task, but it’s an opportunity to learn and setup everything like you want it.
There also are many examples out there on how to do an evolutionary solver in Python.

I can use do this in one PythonScript only, but I can’t creat loop with components, that’s my need, otherwise I must convert all the components that I need into python and write all into a script.

It’s not difficult, it’s extremely difficult.

What kind of components would you have to retro-engineer?

I wanted to find a general alternative, not a temporary measure, so…

I still think it’s easier to solve recursion in grasshopper, although it seems hard for now