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!!
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.
import rhinoscriptsyntax as rs
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)
Of course it’s (a little) inefficient, every new feature you add to the loop will cost something
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.
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):