Package: gh-python-remote
Connect an external python instance to Grasshopper, and vice-versa.This lets you run any Python package directly from Grasshopper, including numpy and scipy!InstallationRequires a Python 2.7 installation, not compatible with Python 3. Compatible with Mac and Windows with Rhino 7.1. Install the software dependencies:Before installing gh-python-remote inRhino 7, you will need to install Python 2, Rhino 7, and open Grasshopper in Rhino 7 at least once.Before installing gh-python-remote inRhino 6, you will need to install Python 2, Rhino 6, and open Grasshopper in Rhino 6 at least once.Before installing gh-python-remote inRhino 5, you will need to install Python 2, Rhino 5, Grasshopper and GHPython, and drop the GHPython component on the Grasshopper canvas in Rhino 5 at least once.Install the following:Python 2.7:gh-python-remote was developed with theAnacondaandMinicondadistributions in mind, but any Python 2.7 distribution works. If you already have Anaconda installed with Python 3, you do not need to reinstall it, you can create a virtual environment as explained below.If you want to be able to name virtual environments in gh-python-remote by their conda name, select “Add conda to my PATH” when prompted during Anaconda’s installation.On Mac, the python distributed with the OS is outdated and difficult to use by the end-user. It ishighlyrecommended to use a conda- orbrew-based Python.Pythonvirtual environment(optional):Isolate dependencies for each project by creating a new virtual environment. If you use Anaconda or Miniconda, creating a virtual environment is easy.Open the Windows command prompt, or Mac terminal (or the Anaconda prompt if you chose not to add conda to your PATH during insallation)Run the following command:condacreate--namerhinoremotepython=2.7numpyscipyThis will create a new virtual environment namedrhinoremote, and install numpy and scipy in it.Rhinoceros3D:Version 7 is supported on Windows and Mac. Version 5 and 6 on Windows should work, but are not supported.Grasshopper:On Rhino 6 and 7, it is already installed. On Rhino 5, install version 0.9.0076.Open it at least once before continuing.GH Python:On Rhino 6 and 7, it is already installed. On Rhino 5, install version 0.6.0.3.On Rhino 5, drop it on the Grasshopper canvas at least once before continuing.2. Install gh-python-remote:From the Windows command prompt, or Mac terminal (or the special Anaconda, or Python prompt if pip is not in your path by default), run:(If you are using a virtual environment, remember toactivateit first. With the conda virtual environment from above, you would need to runconda activate rhinoremotein the command prompt.)pipinstallgh-python-remote--upgradepython-mghpythonremote._configure_ironpython_installationThis will install gh-python-remote for Rhino 7, and install the gh-python-remote UserObject in all Grasshopper versions.Theghpythonremote._configure_ironpython_installationscript takes an optional location argument that can be5,6,7(default), or the path to a target IronPython package directory.For example, to install for Rhino 5, replace the second command with:python-mghpythonremote._configure_ironpython_installation5To install to another location:python-mghpythonremote._configure_ironpython_installation^"%APPDATA%\McNeel\Rhinoceros\7.0\Plug-ins\^
  IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib"UsageAll the examples files are copied to%APPDATA%\Grasshopper\UserObjects\gh-python-remote\exampleson Windows, and~/Grasshopper/UserObjects/gh-python-remote/exampleson Mac. You can also download them from thegithub repo.From Grasshopper to PythonStep-by-stepOpen the example fileGH_python_remote.ghxin Grasshopper, or drop the gh-python-remote component on the canvas.Use thelocationinput to define the location of the Python interpreter you want to connect to.Use themodulesinput to define the modules you want to access in the GHPython component.ChangeruntoTrueto connect.In the GHPython component, the imported modules will now be available via the sticky dictionary. For example if you are trying to use Numpy:importscriptcontextnp=scriptcontext.sticky['numpy']NotesCreating remote array-like objects from large local lists is slow. For example,np.array(range(10000))takes more than 10 seconds. To solve this, you need to first send the list to the remote interpreter, then create the array from this remote object:importscriptcontextasscimportghpythonremotenp=sc.sticky['numpy']rpy=sc.sticky['rpy']r_range=ghpythonremote.deliver(rpy,range(10000))np.array(r_range)Additionally, Grasshopper does not recognize remote list objects as lists. They need to be recovered to the local interpreter first:importscriptcontextasscimportghpythonremotefromghpythonlib.treehelpersimportlist_to_tree# Rhino 6 only!np=sc.sticky['numpy']a=np.arange(15).reshape((3,5))a=ghpythonremote.obtain(a.tolist())a=list_to_tree(a,source=[0,0])ghpythonlib.treehelpersis Rhino 6 only, see thetreehelpers gistfor an equivalent implementation if you need it on Rhino 5.Quick-ref:*marks an input that is only available by editing the gh-python-remote UserObject, or inGH_python_remote.ghx.Arguments:*code (string):Path to theGH_to_python.pycode file.location (string):Path to a python executable, or to a folder containingpython.exe, or the name of a conda-created virtual environment prefixed byconda://(conda://env_name, requirescondaavailable in your PATH). If empty, finds python from your windows%PATH%.run (boolean):Creates the connection, and imports new modules, when turned to True. Kills the connection, and deletes the references to the imports, when turned to False.modules (string list):List of module names to import in the remote python. They will be added to thescriptcontext.stickydictionary, allowing them to be reused from other python components in the same Grasshopper document. Submodules (for examplenumpy.linalg) have to be added explicitly to this list to be available later, and importing the parent package is also required even if only the submodule is used.*log_level (string from [‘NOTSET’, ‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’]):Logging level to use for the local IronPython and the remote python instance.*working_dir (string):Working directory for the remote python instance.Returns:out (string):Console output with DEBUG information.linked_modules (string list):List of imported module names.rpy (rpyc connection object):The object representing the remote Python interpreter.import_statements (string):What to use in the GHPython component to actually use the imported modules.From Python to GrasshopperYou can also use gh-python-remote to programmatically control a Rhinoceros instance, and connect to it via Python. Have a look atexamples/python_to_GH.pyfor a full working example.LicenseLicensed under theMIT license.Changelog1.4.6 (2022-11-21)FixMissing comma…1.4.5 (2022-11-21)NewPropagate error messages through UserObject cluster (#35).FixFix dependency version requirements, pin to the last versions to support Python 2 (#45).1.4.4 (2021-02-12)FixMissing comma…1.4.3 (2021-02-12)FixAdd test cluster to installed files (#40).1.4.2 (2021-02-02)FixFix a typo in helpers logging format1.4.1 (2021-01-27)FixFix a small typo that created a false warning on MacOS install1.4.0 (2021-01-27)NewAdd support for Rhino 7.Add support for MacOS.1.3.1 (2020-04-01)ChangesAllow access to GH user objects from CPython. Access them from a PythonToGrasshopperRemote object aspy2gh.gh_remote_userobjects.<UO Name>Remove GhCompService.get_component. Access remote GH components from a PythonToGrasshopperRemote object aspy2gh.gh_remote_components.<Component Name>FixFix CPython -> Rhino Python connections for Rhino 6.1.3.0 (2020-03-20)NewAdd option to select Rhino version in CPy to GH (#29).ChangesBetter Rhino path finder.Catch conda env list failures.Upgrade to RPyC 4.1.4FixFix missing file at installation.Fix async request timeout (#14).Repair log level passing to pythonservice.py.1.2.1 (2019-07-24)FixCleanup examples directory, and fix missing file at installation (#26).Update logger names in .ghuser file1.2.0 (2019-07-08)NewNow fully compatible with Rhino 6.ChangesRevert using a special RPyC distribution, now explicitly using upstream.Upgrade to RPyC 4.1.0.Hide rpyc behind a special ghpythonremote import.rpyc.utilsfunctionsdeliverandobtainare available asghpythonremote.deliverandghpythonremote.obtain;rpycshould be imported withfrom ghpythonremote import rpycif ever needed.Expand the environment path in CPython subprocess, trying to match what conda would do, helping Numpy finds its DLLs.FixRepair pip install command on recent pip with pip.main deprecated.Repair incompatibilities with IronPython 2.7.0.Repair incompatibilities with RPyC 4.1.0, using monkey-patched compatibilty fixes for IronPython.Remove unnecessary pip install parameters.1.1.4 (2018-02-28)FixStop using theexposedprefix to remote attributes, per rpyc v4.1.1.3 (2018-02-27)FixRenameasyncimport from rpyc toasync_.Bump rpyc version topilcru/rpyc@3.4.6to fix IronPython 2.7.0 dump bytes issues.1.1.2 (2018-02-23)FixUse https file location for the dependencies, to remove the need for git when installing.1.1.0 (2018-02-14)NewDocumentedobtainanddeliverfeatures of rpyc to speedup remote array-like objects creation and retrieval.ChangesUse the v4.0.0 pre-release of rpyc to fix IronPython <-> CPythonstrunpickling issues.Improve error messages when connection is lost.FixRepair the GH to python example, where argument passing (for the port configuration) was broken.1.0.4 (2017-10-06)FixFix quote escaping issue in pip install command for IronPython.1.0.3 (2017-10-02)First public release.