How to run grasshopper straight from a Python IDE?

As the title suggests, I am trying to build a build a python script to ultimately input geometrical values to a grasshopper model and postprocess the results. For that, and as a first step, I need to be able to run my *.gh model directly from my python IDE. Is there a way to do that?

Thanks.

Ehsan can probably say more, but here is a post that might help:

Hey Scott, Thanks a lot for the reply.
Although I am afraid I donā€™t fully understand the original answer, so perhaps if anyone could help break it down for me a bit, Iā€™d highly appreciate it. Also I am curious how different would the syntax be to run the mentioned commands on Windows instead of Mac.

I havenā€™t figured out a nice ā€œdo what Hopsā€ does method using Rhinoinside that was recommended.

In the mean time you could try:

gh_file_path = r'c:\path\to\gh\file.gh'
subprocess.run(rf'"C:\Program Files\Rhino 8\System\Rhino.exe" /nosplash /runscript="-_grasshopper _editor _load _document _open {gh_file_path}  _enter _exit _enterend"'

The tricky part is creating a .gh file that runs then automatically quits Rhino, to let you return to your Python IDE.

Hey James!
That definitely works! I just added _Exit at the end to close up grasshopper after running the model and outputting the results into a txt file in my case.

Note: for others who are attempting this, itā€™d be a good idea to not include spaces in the *.gh file name, as Rhino will try to use the first ā€˜wordā€™ alone as the file name.

1 Like