Site Tools


Using Numpy SciPy and other CPython libraries in GHPython

There are three ways to use Numpy and Scipy within the GHPyton component in Grasshopper: GH Python Remote, COMPAS, and GHCPython.

This guide only details usage of the first one, GH Python Remote.

The main reasons behind this choice against the other two are:

  1. GHCPython development has been abandoned after its initial alpha release in November 2017.
  2. COMPAS is a much larger framework that is not focused on the task at hand, and the method it uses to connect to CPython is more relevant for a single long-running function than for repeated calls to a python package.

GH Python Remote

GH Python Remote is a special component that lets you use regular Python code directly in the GHPython component, like importing Numpy, Scipy, matlplotib et al. It is developed by Pierre Cuvilliers at MIT.

COMPAS

COMPAS is an open-source, Python-based computational framework for collaboration and research in architecture, engineering and digital fabrication. It is developed by the Block Research Group (https://block.arch.ethz.ch) of ETH Zurich (http://www.ethz.ch).

Instructions to connect from Grasshopper to CPython

Instructions to control Rhino from CPython

GHCPython

CPython plugin for Grasshopper Aug 1st, 2017 GHCPython Github


GH Python Remote

Official installation instructions are available on the plug-in's homepage. This guide is an attempt to make these more accessible for beginner Python users.

GH Python Remote needed is a special component that lets you import and use any Python 2 package, like Numpy or Scipy, directly in the Grasshopper Python component. Because Rhino runs IronPython, which is limited in the plugins it can import, some plugins do not work with it. GH Python Remote will connect Rhino to an external CPython interpreter to overcome this limitation.

Setting this up takes several steps:

  1. (Rhino 5 only) Install Grasshopper and GHPython for Grasshopper
  2. Setup an Anaconda virtual environment running Python 2.7, and the packages you want to use
  3. Install GH Python Remote in Grasshopper
  4. Run the GH Python Remote sample

(Rhino 5 only) Install Grasshopper and GHPython

  1. Make sure the Rhino 5 is SR9 or above. Check Help pulldown > About Rhinoceros > Lower right.
  2. Install Grasshopper or make sure the current Grasshopper is Build 0.9.0076 by opening Grasshopper > Help pulldown > About Grasshoppper.
  3. Download and install GHPython version 0.6.0.3. The install needs to be completed by dragging and dropping the GHA file onto the Grasshopper Canvas, then dropping the Python component on the canvas.

Setup an Anaconda virtual environment running CPython 2.7

1) Install Anaconda python distribution:

  1. Check the option about adding Anaconda to your Windows path.

2) Setup a new Anaconda Environment

  1. Once installed, start up the Anaconda Prompt App.
  2. Setup a new environment named rhinoremote by using the command
    conda create -n rhinoremote python=2.7
  3. Activate the new environment:
    conda activate rhinoremote
  4. Install Numpy, Scipy. Use this to install any other CPython libraries in the rhinoremote environment:
    conda install numpy
    conda install scipy
  5. Record the location of the rhinoremote environment by using the conda command:
    conda env list

It should show something like “C:\Users\steve\AppData\Local\Continuum\anaconda3\envs\rhinoremote”.

Install GH Python Remote

Still in the Anaconda prompt with the rhinoremote environment activated, run:

pip install gh-python-remote --upgrade --no-binary=:all:
python -m ghpythonremote._configure_ironpython_installation

The first line installs gh-python-remote in the current Python interpreter. The second tries to find your Rhinoceros IronPython installation, and install gh-python-remote there.

Using GH Python Remote

  1. Download GHPython Remote Release and extract the folder into the documents folder in windows.
  2. Start Rhino and Grasshopper.
  3. In Grasshopper, open the sample in “Documents\ghpythonremote-1.1.X\ghpythonremote\examples\GH_python_remote.ghx”
  4. If needed (because you did not use the same environment name, or because you did not select to add conda to your Windows Path during Anaconda's installation), change the “location” input to the path you noted in the Anaconda installation step.
  5. Change the “run” input to True.
  6. Now drop a normal Python component on the canvas. You will be able to use numpy in it with the following code:
import scriptcontext
np = scriptcontext.sticky['numpy']
grasshopper/cpython.txt · Last modified: 2020/08/14 (external edit)