Rhino- Precompute grasshopper python script

Hi all,
I am trying to create a button in Rhino that will recompute whatever grasshopper script I may have open.

I know could just click f5 in Grasshopper, but I am trying to set this up so that I am disconnecting the users from the script. We have a human UI that we load, and if it is closed, I want the user to be able to click a button in rhino to recompute the script while not actually having to interact with GH.

Looking into this, I thought something along the lines of the following should work as a python script:

import Grasshopper as gh

This doesnt give me any errors, but also doesnt seem to work… I’ve tried a number of variations, but think im missing something.

as I am running this in Rhino, do I need to set the context somehow? sc.doc=ghdoc ?


This might do the trick for you:

Hi thanks for the response.
I had seen your post there already. Unfortunately I dont think this works. The difference is that I am trying to run a script in Rhino and not in grasshopper, so im not sure how to load ‘ghenv’?
Any ideas?

If you’re running the script to interact with a Grasshopper document, but from the Rhino UI, the path of least resistance would be to publish the Recompute button to the Remote Control Panel (and maybe even your other UI input controls):

I don’t think there is a straightforward way to interact with Grasshopper from a Rhino button Python script or the EditPythonScript editor. That said you probably want to do something like this, to get to the Grasshopper document you want to recompute:

Hi Anders,
Thanks for this solution, nice idea.
But… We already have a suite of toolbars, and I’d love to be able to add a button to this toolbar, so I ended up playing around with this a bit more.

So inspired by your idea, and your script in grasshopper, I wrote a the script below to use the GH_RhinoScriptInterface.SetSliderValue method and attach a slider into your “recompute” script rather than the button.

The script below, finds the slider (Which I gave the name “recomp”) and then changes it to a value of 1.

import rhinoscriptsyntax as rs
import Grasshopper as gh

components = gh.Instances.ActiveCanvas.Document.Attributes

for comp in components:
    if comp.PathName == "Recomp":
        sliderComp = comp.DocObject
        slider = comp.DocObject.InstanceGuid

GH = rs.GetPlugInObject("Grasshopper")

GH = None

This triggers your component on. But the slider is then “1” and would get stuck in a refresh loop, so I edited your script to also turn the slider off.
This seems to work.
However, after doing all this, I found a MUCH more succinct solution. Ill post this in a separate post so that I can mark it as the solution.

201218_RecomputeSolution_slider.gh (4.3 KB)

Thanks Josh

The following seems to be a nice simple way of doing this. If anyone knows if this will cause any problems, please let me know:

import Grasshopper as gh
1 Like