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

ghpython
python

#1

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!!


Print-command to Rhino Commandline
#2

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.


(Giulio Piacentino) #3

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.

show-progress-meter.gh (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):
        
        rs.Sleep(10)
        if i%10 == 0: Rhino.UI.StatusBar.UpdateProgressMeter(i, True)
    
    Rhino.UI.StatusBar.HideProgressMeter()

Giulio


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


Visual indication during GH calculation
#5

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.


(Giulio Piacentino) #6

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.


#7

OK, thank you again! :grinning:


#8

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:

160912_LiveK2Solver_GHPython.gh (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.


More info about Timer component?
Improvement of "Data Recorder Rec"
#9

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


#10

thanks!