Running a Python File in Rhino from GHPython

Hello everyone

I am currently looking for a way to execute the command “RunPythonScript” in Rhino. Instead of doing this manually, I want to do this inside of a GHPython-Component in my Grasshopper file since I want to do a parameters study and need to execute Python for many different Rhino-compositions.
I did not find a way to do this with Rhino.RhinoDoc.ActiveDoc so I am afraid that I would have to use Rhino Compute or WIP or similar approaches.
Any tips?

you can certainly switch to the Rhinodoc from within gh. What’s your end goal?

The end goal would be to iterate (with Brute Force from TT Toolbox) over every possible geometrical configuration (which is being generated in grasshopper) and run a python script in Rhino in order to make some calculations in a FE-software. The idea is that this occurs automatically every loop in a GHPython component which accesses the active rhino doc but I did not found a way to run a python script in Rhino from within a GHPython component.

Have a look here:

I am aware of the script context, the problem is that I did not find a way to call the function “RunPythonScript” in Rhino from GHPython (with sc.doc = Rhino.RhinoDoc.ActiveDoc :slight_smile: )

Instead of going from gh to Rhino you could make your gh scripts and run them from Rhino through GrasshopperPlayer

This looks nice, but I am looking for a solution in which I do not need to execute commands in Rhino itself. Since I did not find a solution which allows me to run Python Files in Rhino directly with scripting components in GHPython this would not help me.

here this seems to work just fine from within a gh python component:

rs.Command("-RunPythonScript path\to\script.py")
1 Like

Oh great!
For me it partially works if I input rs.Command(“_RunPythonScript path\to\script.py”)
but it only executes “_RunPythonScript” and ignores the path, since it opens my path to my stored scripts and does not execute the script I indicated in the GHPython component. Do you have a Idea what I could change so it does it automatically?
THANKS already!

i get the same result as mentioned above with rs.Command(“_RunPythonScript path/to/script.py”) (both backslash / forward slash)

you need a dash in front of RunPythonScript to invoke the commandline version:

-RunPythonScript or -_RunPythonScript
1 Like

rs.Command(“-_RunPythonScript path\to\script.py”) worked!
Thank u a lot :smiley:

But why don’t you just run the code from GHPython (edit: either by copy/pasting the code or importing the functions/classes you need)? There shouldn’t be a reason to add these hacky layers of indirection (i.e. calling RunPythonScript from GHPython).

1 Like