This shows you the differences between two versions of the page.
grasshopper:cpython [2018/08/08] |
grasshopper:cpython [2020/08/14] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Using Numpy SciPy and other CPython libraries in GHPython ====== | ||
+ | |||
+ | There are three ways to use Numpy and Scipy within the GHPyton component in Grasshopper: | ||
+ | |||
+ | This guide only details usage of the first one, GH Python Remote. | ||
+ | |||
+ | The main reasons behind this choice against the other two are: | ||
+ | - GHCPython development has been abandoned after its initial alpha release in November 2017. | ||
+ | - 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. | ||
+ | |||
+ | |||
+ | **[[https:// | ||
+ | |||
+ | 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. | ||
+ | |||
+ | |||
+ | **[[https:// | ||
+ | |||
+ | COMPAS is an open-source, | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | |||
+ | **[[https:// | ||
+ | |||
+ | CPython plugin for Grasshopper | ||
+ | Aug 1st, 2017 | ||
+ | [[https:// | ||
+ | |||
+ | |||
+ | |||
+ | ----- | ||
+ | |||
+ | ===== GH Python Remote ===== | ||
+ | |||
+ | Official installation instructions are available on the [[https:// | ||
+ | |||
+ | 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: | ||
+ | |||
+ | - (Rhino 5 only) Install Grasshopper and GHPython for Grasshopper | ||
+ | - Setup an Anaconda virtual environment running Python 2.7, and the packages you want to use | ||
+ | - Install GH Python Remote in Grasshopper | ||
+ | - Run the GH Python Remote sample | ||
+ | |||
+ | === (Rhino 5 only) Install Grasshopper and GHPython === | ||
+ | |||
+ | - Make sure the **[[https:// | ||
+ | - [[http:// | ||
+ | - [[https:// | ||
+ | |||
+ | === Setup an Anaconda virtual environment running CPython 2.7 === | ||
+ | |||
+ | 1) Install Anaconda python distribution: | ||
+ | |||
+ | - Install [[https:// | ||
+ | - Check the option about adding Anaconda to your Windows path. | ||
+ | |||
+ | 2) Setup a new Anaconda Environment | ||
+ | |||
+ | - Once installed, start up the Anaconda Prompt App. | ||
+ | - Setup a new environment named rhinoremote by using the command <code python> | ||
+ | conda create -n rhinoremote python=2.7 | ||
+ | </ | ||
+ | - Activate the new environment: | ||
+ | conda activate rhinoremote | ||
+ | </ | ||
+ | - Install Numpy, Scipy. Use this to install any other CPython libraries in the rhinoremote environment: | ||
+ | conda install numpy | ||
+ | conda install scipy | ||
+ | </ | ||
+ | - Record the location of the rhinoremote environment by using the conda command:< | ||
+ | conda env list | ||
+ | </ | ||
+ | It should show something like " | ||
+ | |||
+ | === Install GH Python Remote === | ||
+ | |||
+ | Still in the Anaconda prompt with the rhinoremote environment activated, run: | ||
+ | |||
+ | < | ||
+ | pip install gh-python-remote --upgrade --no-binary=: | ||
+ | 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, | ||
+ | |||
+ | === Using GH Python Remote === | ||
+ | |||
+ | - Download [[https:// | ||
+ | - Start Rhino and Grasshopper. | ||
+ | - In Grasshopper, | ||
+ | - 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' | ||
+ | - Change the " | ||
+ | - Now drop a normal Python component on the canvas. You will be able to use numpy in it with the following code: | ||
+ | <code python> | ||
+ | import scriptcontext | ||
+ | np = scriptcontext.sticky[' | ||
+ | </ | ||