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

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.

8 Likes