Accessing GH canvas componens from Python (outside of GH)

Yes, use the plugin object. eg like this:

import Rhino
gh_plugin = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
if not gh_plugin: return False

if not gh_plugin.IsEditorLoaded: gh_plugin.LoadEditor()

then give it a litle bit of time to open. After this you can load a document like this:

rc = gh_plugin.OpenDocument(gh_file_path_name)

then import the namespace as described above. You can also check if this fails and stop eg use:

try:
    import Grasshopper as gh
    print "OK, imported Grasshopper"
except ImportError:
    print "Error, start Grasshopper first"

Once you have the grasshopper document object eg. mydoc use this:

import System

# create the searchlist
search_name = "MySliderName"
search_list = System.Collections.Generic.List[str]([search_name])

# search and limit the search to one object
found_items = mydoc.FindObjects(search_list, 1)
if not found_items:
   print "Component '{}' not found in document".format(search_name)
   return 

# return the single found object        
return found_items[0]

_
c.

3 Likes