Open and run a GrassHopper Definition from Rhino [Solved]

rhino5

#1

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
    
    Grasshopper.DisableSolver()
    filePath = "D:\gh\my_grasshopper_file.gh"
    Grasshopper.OpenDocument(chr(34) + filePath + chr(34))
        Grasshopper.ShowEditor()

if __name__=="__main__":
    RunGHDef()

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\my_grasshopper_file.gh"

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

 Unknown command: "D:\gh\my_grasshopper_file.gh"

What am I doing wrong?

// Rolf


#2

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 …

c5-ov3.py (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


#3

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\with_underscores.gh

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: 
      return
    
    Gh.LoadEditor()
    Gh.CloseAllDocuments()
    Gh.ShowEditor()
    defn = "D:\tmp\with_underscores.gh"
    #defn = "D:\tmp\withoutunderscores.gh"
    ok = Gh.OpenDocument( "D:\tmp\with_underscores.gh" )
    if not ok:
      print( 'Unable to load ' + defn )
      return

#4

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

Works:

     Gh.OpenDocument( "C:\\simpledef.gh" )      # OK
                          ^

Fails:

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

Subtle but significant. Sigh. :spy:


Edit:
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


#5

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

https://docs.python.org/2.0/ref/strings.html

e.g. from the script I attached:

  defn = 'D:\\Rhino\\gh\\oval.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 …
:slight_smile:


#6

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

// Rolf


#7

Haha …
You’re absolutely right ! :smile: