Using the REST-API with python from within Grasshopper
One alternative for those interested in using the REST-API from within
Grasshopper is to use python. However, to run the examples shown in the
REST-API Documentation, additional steps are needed to
configure the python environment used by Grasshopper (IronPython).
Specifically, the library requests (version 2.18.4) is needed.
The steps to set up the environment are described below.
- Install IronPython 2.7
- Install
requestslibrary - Copy files to Rhino python directory
C:\Program Files\Rhino 6\Plug-ins\IronPythonC:\Users\<User>\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (some hash)- Modify the python examples
- Done!
Download IronPython from
https://github.com/IronLanguages/ironpython2/releases/tag/ipy-2.7.11
and install it as any normal program.
The default installation in Windows is C:\Program Files\IronPython 2.7\.
$SYSTEM_IRONPYTHON = "C:\Program Files\IronPython 2.7\"
Open powershell or cmd and do:
"C:\Program Files\IronPython 2.7\Scripts\pip.exe" install --user requests==2.18.4
This command installs the request library in the IronPython installation, as well as any library required by request.
These libraries are installed in the following folder: C:\Users\<User>\AppData\Roaming\Python\IronPython27\.
$LOCAL_IRONPYTHON = "C:\Users\<User>\AppData\Roaming\Python\IronPython27\"
Files will be copied to two folders:
$SYSTEM_RHINO = "C:\Program Files\Rhino 6\Plug-ins\IronPython"
$LOCAL_RHINO = "C:\Users\<User>\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (some hash)"
Copy the files with the following commands in powershell:
$LOCAL_IRONPYTHON = "C:\Users\<User>\AppData\Roaming\Python\IronPython27\" $SYSTEM_IRONPYTHON = "C:\Program Files\IronPython 2.7\" $LOCAL_RHINO = "C:\Users\<User>\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (some hash)" $SYSTEM_RHINO = "C:\Program Files\Rhino 6\Plug-ins\IronPython" cp -r $LOCAL_IRONPYTHON/Lib/site-packages/certifi $LOCAL_RHINO/settings/lib/certify cp -r $LOCAL_IRONPYTHON/Lib/site-packages/idna $LOCAL_RHINO/settings/lib/idna cp -r $LOCAL_IRONPYTHON/Lib/site-packages/chardet $LOCAL_RHINO/settings/lib/chardet cp -r $LOCAL_IRONPYTHON/Lib/site-packages/requests $LOCAL_RHINO/settings/lib/requests cp -r $LOCAL_IRONPYTHON/Lib/site-packages/urllib3 $LOCAL_RHINO/settings/lib/urllib3 cp $SYSTEM_IRONPYTHON/Lib/stringprep.py $LOCAL_RHINO/settings/lib/stringprep.py cp $SYSTEM_IRONPYTHON/Lib/ssl.py $LOCAL_RHINO/settings/lib/ssl.py cp $SYSTEM_IRONPYTHON/Lib/encodings/idna.py $SYSTEM_RHINO/Lib/encodings/idna.py
For some reason there is a problem with the validation of the SSL certificate of the website.
To avoid an error message, modify the respective request function to add the option verify=False, i.e.
...
res = requests.post(url=URL, json=task_data,
headers={"Authorization": "your-secret-key"},
verify=False)