How to calculate and display in real time while calculation in GHPython?

Hello all!:grinning:

Normally GH will only display the final resualt after it finish the calculation.
But just like the GH plug-in Kangaroo, it can display the resault in real time while the program is running.
How to achieve that. Is it “Threading”?
Could anyone show some simple examples to me. Thanks a lot!!

Maybe Kangaroo is saving resault in something like “sc.sticky”. And once only calculate a small step. Put a timer connect to the plugin so it can finish the calculation. Just a guess.

Hi @RetroPost.LT

you are exactly right, that’s how Kangaroo and other state-in-component add-ons (like Hoopsnake, etc) work.
They perform one (or a few) steps of the overall computation, and then they return the intermediate result. Once the solution is recomputed, if the starting value is not changed, they won’t use this value, but the intermediate value from earlier to carry on with computation.

In Rhino BETA/6 there is now a small alternative. It has Rhino update the UI, and that one will show a progress bar even “while” your computation happens. It has the disadvantage that Grasshopper is not meant to be run on multiple threads/from within events. So if you give any commands in between, they might have bad side effects. I am still showing this to give you an idea. (9.5 KB)

import Rhino
import rhinoscriptsyntax as rs

if activate:
    upper_limit = abs(x)
    Rhino.UI.StatusBar.ShowProgressMeter(0, upper_limit, "Python comp", True, True)
    for i in range(upper_limit):
        if i%10 == 0: Rhino.UI.StatusBar.UpdateProgressMeter(i, True)


Giulio Piacentino
for Robert McNeel & Associates

Thank you Giulio!
This show progress meter is really fun and useful when dealing with lots of data. Thanks for showing this to me.

By the way, is there any convenient way to stop the loop in GHPython? I had to add “sc.escape_test” or “running time check” in every new loop that might looks dangerous. It’s inefficient.

Of course it’s (a little) inefficient, every new feature you add to the loop will cost something :wink:
But if you want it to stop conveniently, that’s the price to pay. Also the progress meter costs, that’s why in the example it only updates every 10 steps.

OK, thank you again! :grinning:

Attached an example of a live Kangaroo2 solver implemented in GHPython (note: it will be slower than the standard component). While one certainly could use sticky (like for instance what we did for the ShapeOp GHPython implementation). I’ve personally quite come to like the if “foo” not in globals(): method for creating persistent variables, as they will only exist within the local scope of their individual component (ala the static mutable variables one can make in the C# component), and is arguably a more elegant solution (certainly terser):

This example also demonstrates how one might write a function that works similar to the standard Grasshopper Timer (i.e. iteratively expiring the component):

Hope that helps… a bit… at least :wink: (8.0 KB)

Edit: This is a pretty old file and might be broken with later K2 versions, that said, the base principles still apply.
Edit II: More old examples here and install instructions/assumptions.


Thanks for your kindness reply AndersDeleuran! That helps a lot:grinning::grinning:

1 Like


1 Like