Integrating External Python Script with Grasshopper - Help Needed

I’m encountering issues running a Python script in Grasshopper, causing system crashes. Seeking advice on creating toggle switches and variables in Grasshopper to run the script externally and display the output (a string) within Grasshopper. Considering options like sending variables to localhost for script integration. Any insights or guidance would be appreciated. Thanks!

Grasshopper has Boolean toggles and variables. It’s an idiom in Grasshopper Python to connect one to x, and do:

if x:

There’s a CPython component in Rhino 8 now too. Running simple external scripts is no longer necessary.

Otherwise, one previous way was to run the script from the command line, by calling it (and the external python run tme) using the subprocess module from GhPython. Or there are various plug-ins that use localhost etc.

To clarify the misunderstanding in my question, what you provided is merely the basics of scripting, which I am already familiar with.
As mentioned earlier, running a script in Grasshopper using CPython causes my laptop to crash. However, when Rhino Grasshopper is open and I execute the script from Visual Studio Code, for example, it works without issues.

I’m uncertain about the root cause of the problem, but I suspect that the Python component in Grasshopper may not be handling the script properly. To address this, I’ve found a workaround by utilizing two text files. One file is used to send values to my script and execute it, while the other file is employed to write the script’s output, which I then read from Grasshopper.

Although this solution works, I was initially exploring a more efficient approach. If @AndersDeleuran has the time, I would appreciate any suggestions or recommendations for an improved method.

We can’t debug an abstract description for you. We can only make suggestions.

If you are familiar with coding, then provide a minimal reproducible example, and people will be able to help you fix it.

1 Like

here : Call external python/CMD from within Grasshopper - #8 by Konrad

Hello everyone, I’ve returned with another issue. I’m currently working on this task, and I’m uncertain whether it’s a bug or not. Can anyone help me figure out how to solve this?

Hey @eirannejad , apologies for tagging you, but could you please take a look at this?

import subprocess
import os

# Set the path to the Python executable
exepath = ""
# Set the path to the script to be run
script_path = ""

    # Execute the script using subprocess
    returned_bin = subprocess.check_output([exepath, script_path], stderr=subprocess.STDOUT)
    returned_val = returned_bin.decode('utf-8')
    # Print the returned output
except subprocess.CalledProcessError as e:
    # Handle subprocess error
    print(f"Error: {e}")
    print(f"Output: {e.output.decode('utf-8')}")

# As I searched on Stack Overflow, it seems to work when I unset this:
# PYTHONHOME = '\\?\C:\Users\..\.rhinocode\py39-rh8'

I’ve resolved the issue - end of thread.

Despite my previous post and subsequent searches, it appears that excluding this code segment might impede script execution (or is due to my lack of understanding).

if os.environ.get('PYTHONHOME'):
    del os.environ['PYTHONHOME']

Moreover, there are other forum posts related to Python2, but some of them do not work correctly on my end. I’ve included the functional file and the external script in the .gh file for potential future use, offering a concise and straightforward approach.

Simply provide the "path of the script you want to run" and "the executable path of the Python installed on your system." Note that I’ve included a simple example for easy comprehension, and it can be adjusted as needed. I hope this alternative method proves helpful. (8.7 KB)