====== 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: - 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://www.food4rhino.com/app/gh-python-remote|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. **[[https://compas-dev.github.io/|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). [[https://compas-dev.github.io/main/tutorials/rpc.html|Instructions to connect from Grasshopper to CPython]] [[https://compas-dev.github.io/main/examples/rhino/mesh-smoothing-on-surface.html|Instructions to control Rhino from CPython]] **[[https://www.food4rhino.com/app/ghcpython|GHCPython]]** CPython plugin for Grasshopper Aug 1st, 2017 [[https://github.com/MahmoudAbdelRahman/GH_CPython|GHCPython Github]] ----- ===== GH Python Remote ===== Official installation instructions are available on the [[https://github.com/Digital-Structures/ghpythonremote/blob/master/README.rst|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: - (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://www.rhino3d.com/download/rhino/5.0/sr|Rhino 5 is SR9 or above]]**. Check Help pulldown > About Rhinoceros > Lower right. - [[http://download.rhino3d.com/Grasshopper/1.0/wip/rc/download/|Install Grasshopper]] or make sure the current Grasshopper is **Build 0.9.0076** by opening Grasshopper > Help pulldown > About Grasshoppper. - [[https://www.food4rhino.com/app/ghpython|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: - Install [[https://www.anaconda.com/download/|Anaconda 2 for Windows running Python 2.7]] - 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 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 "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 === - Download [[https://github.com/Digital-Structures/ghpythonremote/releases/latest|GHPython Remote Release]] and extract the folder into the documents folder in windows. - Start Rhino and Grasshopper. - In Grasshopper, open the sample in "Documents\ghpythonremote-1.1.X\ghpythonremote\examples\GH_python_remote.ghx" - 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. - Change the "run" input to True. - 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']