When writing Python scripts in Grasshopper, you generally use the Grasshopper standard editor,
Writing code is dramatically more efficient with an external editor.
This time, I will explain using VS Code as an example.
** Benefits of using an external editor **
--Can complete code for libraries other than Rhino / GH --Code is easier to read --Easy to manage importing another file --You don't have to open Rhino when you just want to see the code ――Using a cool editor raises your tension
Anything is fine as long as you can write Python code, but in order to write more comfortably, prepare the environment by the following procedure.
You can install it from here.
It is recommended to prepare the virtual environment using anaconda.
Many people explain how to build a Python environment in an easy-to-understand manner, so I will omit it.
This article is relatively new and recommended.
** Notes </ font> **
IronPython running on Rhino is python2 series, so it is recommended to build the environment with Python2 </ font>.
By inserting stubs, code completion of Rhino and Grasshopper will be effective.
Please install Rhino-stubs
and Grasshopper-stubs
in the python environment by referring to here
pip install Rhino-stubs
pip install Grasshopper-stubs
(You will get an alert like "python2.7 is no longer supported", but don't worry.)
If you click Python ~ at the bottom left of the screen, a place to select the interpreter will appear at the top of the screen, so select the Python environment created this time.
--Place a component called `Read File`
--Right-click on the component name and uncheck `Per Line``` <br> ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/263714/898ea126-fa0c-a526-90b8-84edd0767827.png) --Right-click on "F" and check
Syncronise
``
By checking this, the code updated by the external editor will be reflected in Grasshopper.
--Right-click "F" → select `Select one existing file`
and select the Python file to load
--Deploy Python components
--Right-click the component name → click Show" code "input parameter
At the top of the input you will see a parameter called `code```. <br> ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/263714/3416b568-b7c0-69d4-ed2c-b1f7849d3e18.png) --Connect "C" in ``` Read File
to code
in Python component
--You have now linked your Python files!
After that, write the code with VS Code and check the result with Grasshopper.
When I change the code in the Grasshopper editor and try to close the editor, the following message is displayed and the link is broken.
For example, even if you enter x
and `` `yin Grasshopper like this ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/263714/723c0270-d200-a27b-bf31-eac32d95cf07.png) I get the error
Undifined variable``` on VS Code because it is not defined in the Python code.
If you want to test the code alone on VS Code, you need to initialize the variables, but if you just run it on Grasshopper, there is no problem.
I showed you how to write Python code on Grasshopper with an external editor. This method is sufficient for a single Python file, but if you try to develop in earnest using a module or configuration file in another file, problems unique to Grasshopper will occur. I will explain that in another article.
http://www.hagiri.org/how-to/249
Recommended Posts