A mock-up of a 'recompute component' option in the Grasshopper component right-click menu

Force a component to re-compute in Grasshopper

Wouldn’t something like this be useful in Grasshopper?

recompute-component-grasshopper

Often, I am working with data files on my computer through Grasshopper. When those data files change, there may be no easy way for dependent Grasshopper components to update. One solution is to disconnect and re-connect a wire into that component. This is quite tedious though, especially in large documents. It is also possible to recalculate the entire canvas with right click, recompute. But this is not feasible if you have components in your canvas which take a long time to calculate.

You may want to read this post which talks a bit about the nitty-gritty of how Grasshopper does calculations. Basically, for each component, in the background there is a property called ‘expired’. If ‘expired’ is true, then Grasshopper that it has to recalculate that component. So if we can manually set this property for components we want to update, we can force a recalculation on only these components (and the components directly downstream of them).

The following C# script does exactly this. Set up a C# component as in the image with the code at the bottom.

expire-components-grasshopper

Then, select components you want to recompute. (Here, I have selected two path files, which links to files I want to re-read.) Finally, hit the button on the left. The result is that the paths recompute, which then triggers a recalculation of the components downstream. The rest of the document (including some very heavy components) remains untouched.

It’s not quite the right-click option I showed above, but in the meantime it’s a pretty effective solution which I hope is useful.

Code

  private void RunScript(bool expire_them, ref object A)
  {
    
    //fore each component on the canvas
    foreach (IGH_DocumentObject obj in GrasshopperDocument.Objects)
    {
      //if that component is selected
      if(obj.Attributes.Selected)
      {
        //...and if expire_them = true, then expire that component and ask it to recompute
        if(expire_them) obj.ExpireSolution(true);
      }
    }

  }

One thought on “Force a component to re-compute in Grasshopper”

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: