Writing Python Code for Civil 3D Dynamo in Visual Studio Code

Hi,

I am working with Dynamo for Civil 3D. Is it possible to write Python code for custom nodes in Visual Studio Code?
I have seen examples for Revit + Dynamo, but I am not sure if the IronPython stubs (Github) have all assemblies needed for Civil 3D (see Github for a list of supported assemblies).

Cheers!

Yes, but probably not with the quality of life features you’re used to. Intellisense or a language server protocol need to index what packages or .NET assemblies you’re importing, and the default paths in Dynamo are probably not how you have VSCode configured. I’ve never tried to do that myself because, personally, I find the .NET API for C3D requires far too much effort on otherwise billable time to justify its use. Also, as far as I know, you can’t just open up a Python script into the Dynamo editor. You can copy-paste, of course, which is inconvenient. You can also make a module and import it with the Python Script editor Dynamo comes with. Get ready for a long tangent about that:

First, I’m using C3D 2022, and Dynamo supports a CPython3 interpreter, which probably has more of the functionality you actually want (IronPython is not a MSFT priority and it’s far behind CPython). CPython can still use pythonnet to interact with .NET assemblies (if you’re using the CPython3 interpreter in the Dynamo editor, this is what the clr import is)

The Python interpreter is embedded into the AutoCAD executable, so it does not behave like a normal Python installation. For starters, it’s, as of today, Python3.8, which means it doesn’t support newer features like the dictionary union operator or structural pattern matching. Also, if you want to use any downloaded packages (or your own), you have to add the file path to those packages at the top of your script (ie sys.path.append('C:\\Users\\you\\long-file-path\\site-packages'). To my knowledge, there isn’t a way to use something simple like pip for managing your packages in a way that ‘just works’ with Dynamo’s Python interpreter.

Appending a path to normally installed packages has worked for me for some python-only packages, like networkx, but anything with c-extensions, like numpy and friends I’ve had filepath problems.

All that said, so far I’ve made a point to only use C3D-specific APIs using the visual editor or DesignScript, which, aside from the implicit flatmapping and zipping, isn’t that different than other scripting languages.

2 Likes

@sam.kreimeyer thank you for the detailed answer! I could still try to write scripts in VS Code if I only require assemblies that are supported by IronPython stubs, but as you mention, that would most likely lead to some inconvenience when switching back and forth between Dynamo’s editor and VS Code whenever I need C3D functionality. It seems like I should stick to Dynamo’s editor for now, but I keep my fingers crossed for C3D catching up on Revit in terms of VS Code editing.

You’re welcome! Dynamo is still very new for C3D, so I’m hopeful that its Python support will improve with future versions. So far I’ve found using a mix of Dynamo Nodes, DesignScript blocks and Python blocks is a huge improvement over writing .NET plugins or AutoLisp.

1 Like

While Visual Studio is great for C#, PyCharm is fantastic IDE for Python. I have found it to be way more intuitive as it offers better support for Python.

3 Likes

Agreed! Go for PyCharm :slight_smile:

4 Likes