Turn off & on Grasshopper preview from Python script

Hi guys,
I’m working on a script for a workflow which is a combo of Rhino & Grasshopper and am wondering if there is a way to disable and enable the grasshopper preview from a python script? Note: NOT from GHpython.
Thanks!
Siemen

1 Like

Hi @siemen, one easy way would be to use th GH plugin object and the GH_RhinoScriptInterface to get basic control.

_
c.

Hi @clement. Thanks for your reply. But unless I’m looking wrong I don’t really see an option for changing the grasshopper preview?

In terms of using the GH plugin object. How would you go about using this?
I have one way to access these which I believe I found earlier from you somewhere on the forum:

Import Rhino
Grasshopper = Rhino.RhinoApp.GetPlugInObject(“Grasshopper”)
Grasshopper.RunSolver(True)

But how did you find out about this? And is there another way? How do you access those classes using Python and how do you know you need to do it like that?

Curious why you would do that when there is a button top right of gh to do that? Or are you trying to make something trigger that?

The python script asks for user input in Rhino and then shows a limited preview of how it will look and the option to change the order of the input, so when finishing the command the input from the user is being passed on to grasshopper to do the more complicated and precise work. If there’s already input set into grasshopper I get the preview from grasshopper + the preview from the python script which gets confusing. The end user doesn’t know grasshopper and turning off preview before running the command in Rhino and turning it back on afterwards is cumbersome.

I guess I could delete the previous input from grasshopper but it would be nice if I could just turn off the preview so that the user can still cancel the command and keep the previous input.

Hi @siemen, i am using GH and controlling it from python to send data or retrieve data from it. The RhinoScriptInterface is rather limited, but works ok for simple tasks, eg. assigning objects to component inputs by Id, or by setting slider values.

To get more control, you might want to look into importing the Grasshopper namespace in your python script. For what you wrote above, a possible way would be to get hold of the opened grasshopper documents and find the one you are targeting, eg. using this code:

import Grasshopper

# the name of the document you're searching for
document_name = "MyDocument"

# get all open documents
doc_list = Grasshopper.GH_InstanceServer.DocumentServer.GetEnumerator()
if not doc_list: return

# find the one you're interested in    
mydoc = None
for document in doc_list:
    if document.DisplayName.startswith(document_name):
        mydoc = document
        break

if not mydoc: return
if mydoc.ObjectCount == 0: return

Then, if you found your document, find the component you’re interested in, one way would be like this:

import System

# the component name you're searching for
search_name = "MyComponentName"

# search in your document
search_list = System.Collections.Generic.List[str]([search_name])
found_items = mydoc.FindObjects(search_list, 1)
if not found_items:
    print "Component '{}' not found in document".format(search_name)
    return
        
obj = found_items[0]

Once you’ve found the component (you may validate using obj.InstanceGuid), you can access it’s visibility state like this and control the preview:

obj.Hidden = True
mydoc.NewSolution(False)

does this help ?
_
c.

2 Likes

Thanks a lot @clement, again. I think I can try to work from that. It would have been easier if I could just turn of the whole grasshopper preview temporary but I guess I’ll need to try this out.

Hi @siemen, i am not sure but guess it requires at least one GH document. If you have it, you might use below to turn preview off:

my_doc.ExpireSolution()
my_doc.ExpirePreview(True)

and to turn it back on would be this:

my_doc.NewSolution(True)

_
c.

That’s it, thanks a lot!
But… it seems like it doesn’t hide the preview of the vector display components. Any idea about how to solve that as well other than finding the component and disabling it?

Hi @siemen, i would locate the components via Id. Unless @DavidRutten knows a simpler way in the SDK to enable preview of everything without going through document and components one by one.
_
c.