Open and run a GrassHopper Definition from Rhino [Solved]

I’m trying to open a GrassHopper definition via a menu button, and via an Alias/PythonScript, and in all cases it does open GrassHopper, but but not the “hard coded” GrassHopper definition file. Why not?

Python script

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def RunGHDef():
    Grasshopper = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
    if not Grasshopper: return False
    filePath = "D:\gh\"
    Grasshopper.OpenDocument(chr(34) + filePath + chr(34))

if __name__=="__main__":

From a Menu button I try the following, but same there, GrassHopper starts with an empty canvas, but the GH definition files does not open, although this time the file dialog opens, displaying the last opened folder. But, when I select the gh file file (using the open dialog) it still doesn’t open the file :

 _GrassHopperOpen "D:\gh\"

The following is displayed on the command line after selecting a file to open:

 Unknown command: "D:\gh\"

What am I doing wrong?

// Rolf

1 Like

Hi Rolf

About a year ago, I did a little test to try to use GH as a scripting tool …
Here is the script I used.
As far as I remember, it was able to open the GH definition … (4.8 KB)

I cannot try that here … no Windows at home currently … ( :slight_smile: ), just my scripts on the DropBox …

I think I learnt how to do that from some posts by Giulio …
But am not able to find them now … sorry …

HTH Regards

1 Like

Thank you for your reply, @emilio

I tried your script, but unfortunately it still doesn work. I get the exact same result.

There seems to be a flaw in GrassHopper’s API. For example, look at the resulting message from attempting to open a gh definition (there’s suddenly a \tab character in the file path):

Unable to load D:	mp\

I get the same result message from the two paths in the code below (testing if underscores would play a role in this, but no go.

I have tried with paths containing underscores, spaces, numbers, as well as paths entirely without any such characters, but same thing - no go:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def RunGHDef():
    Gh = Rhino.RhinoApp.GetPlugInObject( 'Grasshopper' )
    if not Gh: 
    defn = "D:\tmp\"
    #defn = "D:\tmp\"
    ok = Gh.OpenDocument( "D:\tmp\" )
    if not ok:
      print( 'Unable to load ' + defn )

OK, I found the problem. Any drive letter must be followed by colon + double backslashes, only one (1) backslash will not no. Like so


     Gh.OpenDocument( "C:\\" )      # OK


     Gh.OpenDocument( "C:\" )       # Fail

Subtle but significant. Sigh. :spy:

Hm, GrassHopper also doesn’t seem to like “…\leading\000 numbers” in the path’s folder names.

I solved it by using a symlink, but … . It seems one have to put “gh-scripts” in a separate folder with a “clean” path.

// Rolf

Yes, that’s how you have to write backslashes in Python strings.
Any backslash has to be written that way.

e.g. from the script I attached:

  defn = 'D:\\Rhino\\gh\\'

… you know …
C (from which Python gets strings syntax) was written for Unix,
where slashes were user in file paths, not backslashes like in Windows …

1 Like

There’s a lot of things I don’t know, but happy still! :head_bandage: :innocent:

// Rolf

Haha …
You’re absolutely right ! :smile: