Connecting Grasshopper and Visual Studio or Visual Studio Code for Python

Hi,

I am thinking about writing the Grasshopper Python codes in Visual Studio or Visual Studio Code. Is there a way to do that?

I know you can have a connection between Rhino and Visual Studio Code but not with the Grasshopper Python per my knowledge. Please let me know if there is anyone who can shed some light on it. Would appreciate that. Thanks!

SR

As far as I know, there is no good solution for this right now. What I sometimes do is reading code saved in external files using the “code” input of ghpython components. You don’t have any code completion then, but can use version controls systems and use editor features like code formatting then.

So you could develop in the gh component and then move to an external .py file.

1 Like

Thank you for the relpy Martin. Let me try what you have just said but I am pretty I will struggle to figure that out as I never had the chance to explore this part. Hopefully will come across some other methods as well.

Cheers!

You could also write your scripts as a module and place it in

%APPDATA%\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib

You can then import your module inside your ghPython script component and use it by just setting up some interfacing functions you prepare to feed in GH inputs etc. If you make changes to your module code and want to update / hotswap it once it has been loaded in your current Rhino session, you can use built in module reload function.

reload(<your module>)
4 Likes

You can also get autocompletion for RhinoCommon/Grasshopper in external Python editors:

2 Likes

For writing standalone Grasshopper Python scripts in Visual Studio Code, relevant answers can be found here and here.

However, when it comes to developing a full Grasshopper plugin with multiple Python components in Visual Studio, similar to the official Grasshopper C# template, the options have been limited—until now.

I recently released two open-source solutions that address this challenge:

  1. PythonConnectedGrasshopperTemplate – Built on PythonConnect, this solution was originally developed in 2021 for Rhino 6, when IronPython was still limited to Python 2. It enables C# components to execute Python 3 scripts asynchronously in a parallel Python process, and was used to develop the early versions of Muscle, a Grasshopper plugin for the structural engineering field.
  2. PythonNETGrasshopperTemplate – Based on Python.NET, this approach is faster than the first one for transferring data between C# and Python, though data conversion is still required. For now, the template uses JSON encoding/decoding, but if anyone is interested in implementing the recommended Python.NET data conversion method, contributions are welcome!

If you’re exploring Grasshopper plugin development in Python and C#, I’d love to hear your thoughts or improvements on these templates! :blush:

4 Likes