I want to open a Grasshopper file that lies in the same folder as my Rhino file with a Rhino button. What I have sofar is as below, it requires the full path of grasshopper which is not ideal for my case.
Command for Rhino Button:
-_Grasshopper
: Banner
: Disable
-_Grasshopper _Document
: Close
: Enter
-_Grasshopper _Document _Open “C:*full path of GH file”
: Enter
Anyone knows if it is possible to achieve this in the commandline or has used other workarounds? Thank you in advance.
You need to make a python script and find the path of the open file.
But you need to know the name of the grasshopper script though.
Test this out:
(Change “grasshopperscript.gh” to your preferred file name.
### Open Grasshopper script
### by Holo 2025
# -*- coding: utf-8 -*-
import rhinoscriptsyntax as rs
import os
def openGrasshopperScript():
### Define the scriptname
GHdocument = "grasshopperscript.gh"
### Check if file is saved and get document path
documentpath = rs.DocumentPath()
if documentpath == None:
print("Error: File is not saved, so no path located")
return
### Combine the path and the document name
filepath = chr(34)+documentpath+ GHdocument+chr(34)
print filepath
### Run the grasshopper stuff
rs.Command("-_Grasshopper _Banner _Disable _Enter", False)
rs.Command("-_Grasshopper _Document _Close _Enter", False)
rs.Command("-_Grasshopper _Document _Open "+filepath+" _Enter", False)
openGrasshopperScript()
And here is a more advanced version that checks all files in the directory of the current Rhino file.
If none it ends, if only one it uses that and if multiple it presents a list.
### Open Grasshopper script
### by Holo 2025
# -*- coding: utf-8 -*-
import rhinoscriptsyntax as rs
import os
def openGrasshopperScriptMultiple():
### Check if file is saved and get document path
documentpath = rs.DocumentPath()
if documentpath == None:
print ("Error: File is not saved, so no path located")
return
### Make a list of grasshopper files in that directory
list = []
for file in os.listdir(documentpath):
if file.endswith(".gh"): list.append(file)
### If no GH files then return
if len(list) == 0:
print ("Error: No grasshopper files in directory")
return
### If only one GH file then use this one
if len(list) == 1:
GHdocument = list[0]
### If multiple GH files then present a listbox to choose from
else:
GHdocument = rs.ListBox(list, "Choose what grasshopper definition to open", "Multiple grasshopper files in directory", 0)
if GHdocument == None:
return
### Combine the path and the document name
filepath = chr(34)+documentpath+ GHdocument+chr(34)
print (filepath)
### Run the grasshopper stuff
rs.Command("-_Grasshopper _Banner _Disable _Enter", False)
rs.Command("-_Grasshopper _Document _Close _Enter", False)
rs.Command("-_Grasshopper _Document _Open "+filepath+" _Enter", False)
openGrasshopperScriptMultiple()