Is it possible to run and debug Fusion 360 API scripts and add-ins from Visual Studio Code (VSCode)?

Is it possible to run and debug Fusion 360 API scripts and add-ins from Visual Studio Code (VSCode)?

therealsamchaney
Advocate Advocate
2,532 Views
4 Replies
Message 1 of 5

Is it possible to run and debug Fusion 360 API scripts and add-ins from Visual Studio Code (VSCode)?

therealsamchaney
Advocate
Advocate

I've been trying to optimize my workflow in creating Fusion 360 API scripts and add-ins, and I'm still confused about what the "correct" approach is. It used to be that we would use Spyder which would connect to Fusion, allowing you all of the tools and amenities you'd expect from an IDE.

 

However, now Fusion uses VSCode. I love VSCode so theoretically this would be a change for the better, but the problem is that Fusion doesn't really connect to VSCode completely. When I click Edit in the add-ins menu, it does open the python file in VSCode, but that's about where the connection seems to end. VSCode doesn't recognize the adsk module or any other Fusion specific modules, commands or libraries. So, if you try to run a script or add-in from VSCode, of course it won't work and you'll just get errors. As far as I can tell, you instead have to go into Fusion 360 and open the add-ins menu and run it from there.

The problem with this is that you're coding blind. You can't print to console or use a debugger like normal, and when a script fails, there's no error messages printed in VSCode or in Fusion, so you just have to fill your code with ui.messageBox messages which is messy and slow. If there is a syntax error in your code, when you try to run it, just nothing happens with no info as to why.

I did find that there are two extensions for VSCode, called Autodesk Fusion 360 Post Processor Utility and Fusion 360 Addin Helper. The second one is the most relevant, but still there are significant issues. The main issue is that it often doesn't recognize a file as being a Fusion 360 script or add-in, so it won't activate at all. From the description, it says it only recognizes Fusion files that have a manifest file.

 

The problem with that is that Autodesk also changed the entire architecture of how add-ins and commands are handled with python. Previously, the brunt of the code would be in one the myAddinName.py python file in the root directory of the add-in, and there would be a manifest file  like myAddinName.manifest. The Fusion 360 extension for VSCode handles these oldschool add-ins just fine and brings very helpful functionality like auto-complete and information about Fusion 360 objects.

 

However, now when you make a new add-in, you get a much more complicated folder structure, where every command in your add-in has its own folder within a commands folder. Now, the myScriptName.py file is just boilerplate run and stop functions and you're not even supposed to touch that file at all. This makes the Fusion 360 Addin Helper extension pretty much useless because that's the only file it will recognize, since that's the only one that has a manifest file. I have tried making manifest files for the entry.py files in the command folders but it doesn't work.

 

There just seems to be a huge lack of communication between teams about this stuff. I haven't found a single official Autodesk tutorial about how we're supposed to integrate VSCode and Fusion 360 and it's sorely needed. Autodesk, please get on this!

0 Likes
2,533 Views
4 Replies
Replies (4)
Message 2 of 5

BrianEkins
Mentor
Mentor

Have you seen this topic in the online help? It goes through the steps of using VS Code to debug a script or add-in.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-743C88FB-CA3F-44B0-B0B9-FCC378D0D782

 

Nothing was changed in the internal architecture of how add-ins work. Any existing add-ins will still continue to work. Autodesk just updated the template used when you create a new add-in. It would have been nice to provide an option to create the old or new style. The new style is better when writing a more complex add-in, but when you first look at it, it can be a bit overwhelming.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5

therealsamchaney
Advocate
Advocate

Hi Brian, thanks, I hadn't found that article yet but I just read it tonight and it is helpful. For some reason, the autofill does work sometimes (even if I disable all the Fusion 360 extensions) in my VSCode but not all the time, and unfortunately I'm not sure why. Could you explain exactly how Fusion connects with VSCode so that it knows about the Fusion object model / libraries? It would help me to understand how to troubleshoot it when it's not working.

I know that the actual architecture didn't change, I just meant the organizational paradigm, and the auto-generated folder structure. When I read the description of the VSCode Fusion Addin extension, it sounded like it only recognized a file as being a Fusion document as such if there was a manifest file, which is no longer relevant with the way commands are organized (since the relevant file, the entry.py does not have a manifest file in the folder).

After reading the article you shared, I was able to run the debugger for an add-in, (sort of) and was even able to see a print statement in the debug console. However, now debug has mysteriously stopped working. When I start debugging the script (the main python file in the root directory), the little debug strip menu buttons are all grayed out except for the pause, restart, and stop buttons (so I can't click on step over, or in or out). It's like it thinks it's playing but it's not. Clicking pause does nothing, so does clicking stop. Do you have any idea why this is happening @BrianEkins ? There is an ImportError message that it couldn't load the adsk.core module. There's nothing shown in the debug variables, watch, or call stack, or output or debug console. The call stack says everything is running. In the add-ins menu in Fusion, it does not show my add-in as running. It almost seems like it's stuck in an infinite loop but there are no loops at all. There are a bunch of breakpoints but it's not hitting any of them. It's probably just something I'm missing that should be obvious but I'm not super fluent with VSCode. Any help would be greatly appreciated.

0 Likes
Message 4 of 5

therealsamchaney
Advocate
Advocate

Maybe this can shed some light into the issues I'm having. I just got this error message when I tried to debug in VSCode: "Connect ECONNREFUSED 127.0.0.1:9000". Seems like it's unable to connect to some gateway on port 9000. I would guess that this is how VSCode communicates with Fusion 360's API but really not sure. From what I can find, port 9000 is used by many different applications.

The error message asks if I want to open launch.json. If I do, I see some warnings in that file. The most relevant one is the Windows configuration which it says is missing the attributes "name", "type" and "request"

Below is some more information that might be relevant:
-I'm using Python 3.7.1 64 bit conda for my interpreter (I have others installed I can test if need be)
-My python language server is set to Pylance

-My python linter is set to Pylint

-I've tried rolling back the python extension to the earlier version as suggested by some posts on this forum but it did not fix the issue
-I have tried opening VSCode from within Fusion and without and it doesn't seem to make a difference (I believe this only matters the first time you do it because it installs some necessary things in VSCode)
-The debugging has worked a couple times in the past and I have not found any pattern to when it works or doesn't

0 Likes
Message 5 of 5

kandennti
Mentor
Mentor

@therealsamchaney .

 

In my case, the error appears when VSCode is started alone.
As mentioned here, it is more reliable to start VSCode from Fusion360.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/visual-studio-code-tutorial/m-p/11467769#M... 

 

If the port number is the problem, it can be changed in the preferences.
After changing it, restart Fusion360 and start VSCode from Fusion360.

1.png