Successfully debugged Fusion 360 python script in Intellij IDEA :)

Successfully debugged Fusion 360 python script in Intellij IDEA :)

JesusFreke
Advocate Advocate
4,004 Views
14 Replies
Message 1 of 15

Successfully debugged Fusion 360 python script in Intellij IDEA :)

JesusFreke
Advocate
Advocate

Yes!

 

After a number of hours of digging around, I finally managed to find a really really hacky way of getting intellij idea to attach to the python instance running in the fusion 360 process.

fusion360_idea.png

 

I'll continue to investigate more, and hopefully find a better solution. But for now, here is what I did.

 

Hold on to your horses, this is ugly 🙂

 

  1. Find the local directory containing the Fusion360.exe. For me, this is something like: C:\Users\<user>\AppData\Local\Autodesk\webdeploy\production\e99d5d109cb5b98e5cbbe63c19bb4468b3ad2199. Let's call that directory <FusionHome>.
  2. Copy <FusionHome>/Python/python.exe to <FusionHome>/python.exe
  3. Open IDEA (or PyCharm or whatever), and create a new python project containing your fusion script. Go to the module settings (F4)->SDKs and add a new python SDK. Select "System Interpreter", and choose the <FusionHome>/python.exe that we created in step 2.
  4. Choose ok and go back to the module settings. Navigate to "Project Settings->Project", and select the new python sdk as the project sdk. Click apply and ok to exit out of the settings windows.
  5. delete <FusionHome>/python.exe, and copy <FusionHome>/Fusion360.exe to <FusionHome>/python.exe
  6. Run <FusionHome>/python.exe. This will launch fusion 360.
  7. Rename <FusionHome>/python.exe to something different. The name doesn't matter.
  8. Copy <FusionHome>/Python/python.exe to <FusionHome>/python.exe
  9. In fusion 360, go to "Scripts and Addons" and edit one of the python scripts (doesn't matter which). This will cause fusion 360 to load the python dll into its process.
  10. Back in IDEA, set a breakpoint in your script. Then go to Run -> Attach to Process. You should see the "python.exe" process, which is actually the fusion 360 process. Attach to that process.
  11. In fusion 360, run the script you want to debug, that you've set a breakpoint for in IDEA. IDEA should hit the breakpoint and at that point, you can step, inspect values, etc. as per usual.

 

I told you it was ugly 🙂

The problem is that IDEA only shows processes in the "attach to process" screen based on the python sdk that is currently configured in the project. And you can't directly add "Fusion360.exe" as a python sdk, because it apparently does some pattern checks on the name of the executable you select, and complains if you try to choose Fusion360.exe.

 

Also, you don't really want to point IDEA at the Fusion360.exe (or even the copy we made and named python.exe), because it will try to run it as soon as you add as it as sdk. To get version information maybe? Unsure. But in any case, that just launches fusion 360, which is obviously not what IDEA was expecting. So you have to go through the dance of making <FusionHome>/python.exe a real python executable to set up the sdk, then the fusion executable to launch fusion, and then back to the real python executable so IDEA won't just re-launch fusion360 when it runs that python.exe.

 

I'll look into some possible solutions to make this... less ugly :). One possibility would be to write a little IDEA add-on that lets you choose an arbitrary process to attach to.

 

 

 

4,005 Views
14 Replies
Replies (14)
Message 2 of 15

JesusFreke
Advocate
Advocate

Update: I've found a much easier way to get this working.

 

After a bit of slogging through the code of the python plugin for IDEA, I noticed that there was a setting that controlled how IDEA filtered the list of processes to show in the "Attach to Process" dialog. You can go to Settings -> Build, Execution, Deploment -> Python Debugger, and then change the "For Attach To Process show processes with names containing" field to "Fusion360.exe"

 

Now it should show the fusion 360 process in the "Attach to Process" dialog. So you can set a breakpoint in your fusion 360 script in IDEA, attach to the fusion 360 process, and then run the script from fusion 360, and IDEA will stop at the breakpoint and you can continue debugging.

 

Now to figure out how to actually launch a script from IDEA.. I have some ideas on that front as well 😄 (no pun intended!)

 

 

Message 3 of 15

JesusFreke
Advocate
Advocate

Unrelated, but is there no way to edit the original forum post? I was going to put a pointer at the top to check my next reply, for a much better way to do it.

 

I can see how to edit my replies to the original post, but I can't seem to find how to edit the original post...

0 Likes
Message 4 of 15

JesusFreke
Advocate
Advocate

Yet more progress...

 

I've been working on a little IDEA plugin and have managed to be able to launch a script from within IDEA, with the help of an add-on in fusion 360. The plugin is still very rough around the edges though, so I'm hoping to clean things up a bit, and hopefully make it available soon.

Message 5 of 15

Anonymous
Not applicable

What would be involved in setting up PyCharm or IDLE to launch a Fusion Add-In? I am trying to vary a user parameter in Fusion, export as a STEP or STL, determine machinability using a different software package that uses a python API, and then repeat the process...

0 Likes
Message 6 of 15

JesusFreke
Advocate
Advocate

I ended up making a fusion 360 plugin for pycharm/idea (windows only atm). You can develop your script and easily launch it in fusion 360, all from pycharm. See my thread here in the forums:  https://forums.autodesk.com/t5/fusion-360-api-and-scripts/now-available-fusion-360-plugin-for-intell...

Message 7 of 15

tim.collins29V9X
Enthusiast
Enthusiast

Three years later and perhaps things have changed. Any help getting PyCharm to run will be very much appreciated.

Debugging always drops me into Visual Studio Code and I cannot troubleshoot any further. Maybe Webroot is interfering with SSDP?  Everything else on my PC works but I can't say.

When PyCharm starts I get this error message below. Perhaps someone changed the Python interface in Fusion 360 and so broke the plug-in and Fusion 360 has some way to aggressively keep trying to debug?

com.intellij.diagnostic.PluginException: Extension org.jf.fusionIdea.attach.FusionLocalAttachDebuggerProvider does not implement interface com.intellij.xdebugger.attach.XLocalAttachDebuggerProvider (adapter=SimpleConstructorInjectionAdapter(implementation=org.jf.fusionIdea.attach.FusionLocalAttachDebuggerProvider, plugin=PluginDescriptor(name=Fusion 360 Scripting, id=org.jf.fusionIdea, descriptorPath=plugin.xml, path=~\AppData\Roaming\JetBrains\PyCharmCE2021.3\plugins\fusion_idea, version=0.6.3, package=null, isBundled=false)))

0 Likes
Message 8 of 15

JesusFreke
Advocate
Advocate

Hi Tim, happy to help :). Can you share what version of the fusionIdea plugin and pycharm you're using? It sounds like there are some API changes in the IntelliJ that the plugin may need to be updated for.

 

edit: n/m, I took a closer look at the error message and found both versions 🙂

(0.6.3 and 2021.3).

 

I'll take a look and see if the plugin needs to be updated.

0 Likes
Message 9 of 15

tim.collins29V9X
Enthusiast
Enthusiast

Hi there!
I have the latest Fusion 360, as according to them 

"Fusion 360 updates are downloaded and installed automatically. The Autodesk Desktop App is not used to update Fusion 360. 

 

EDITED: this is Fusion 360 intellij plugin as is currently on github freshly loaded with the current github to fusion loader script.  
I also did an update of PyCharm and I still see the issue.
PyCharm 2021.3.2 (Community Edition)Build #PC-213.6777.50, built on January 27, 2022Runtime version: 11.0.13+7-b1751.25 amd64VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.Windows 10 10.0GC: G1 Young Generation, G1 Old GenerationMemory: 2042MCores: 12Non-Bundled Plugins:    org.jf.fusionIdea (0.6.3)
Let me know if I can help.
Tim

0 Likes
Message 10 of 15

JesusFreke
Advocate
Advocate

I released a new version of the plugin (v0.6.4) that should be compatible with the current versions of IDEA and PyCharm. You'll also probably want to update the fusion addin, since I also fixed an annoying bug in it.

 

It may take jetbrains a day or two to approve the new version of the plugin before it appears in the plugin repository. In the meantime, you can download it from github and install it manually.

0 Likes
Message 11 of 15

tim.collins29V9X
Enthusiast
Enthusiast

Thank you! I updated Fusion 360 and I'll watch https://plugins.jetbrains.com/plugin/11343-fusion-360-scripting/versions to see an update. I'll try and remember to change this post into an "it works" when that happens. 

0 Likes
Message 12 of 15

tim.collins29V9X
Enthusiast
Enthusiast

The plugin starts, great.

 

I keep hitting an issue with the code that checks if a directory is a valid add-in project, both at plugin startup time and later when attempting to make a valid project (from a quick peek at github).  This happens both when there is an invalid project (I had a bunch of these!) but curiously also when there are no projects yet. I only got the Fusion 360 Pycharm plugin features to show up after I pasted in some old add-in code. 

When I made a "helloworld.py" file using the instructions this also failed though I cannot explain it. When I used the output of the Fusion360 "Create" command then it seemed to accept it. ???

 

I have several .py files that do not import adsk (plain-old Python objects) so I hope these are passed over harmlessly.

I tried a bunch of things and haven't found a workaround yet.  

java.lang.NullPointerException
at org.jf.fusionIdea.run.FusionRunConfigurationProducer$FusionScriptDeterminator.visitPyFile(FusionRunConfigurationProducer.java:115)
at com.jetbrains.python.psi.impl.PyFileImpl.accept(PyFileImpl.java:257)
at org.jf.fusionIdea.run.FusionRunConfigurationProducer.setupConfigurationFromContext(FusionRunConfigurationProducer.java:88)
at org.jf.fusionIdea.run.FusionRunConfigurationProducer.setupConfigurationFromContext(FusionRunConfigurationProducer.java:55)
at com.intellij.execution.actions.RunConfigurationProducer.createConfigurationFromContext(RunConfigurationProducer.java:102)
at com.intellij.execution.actions.RunConfigurationProducer.findOrCreateConfigurationFromContext(RunConfigurationProducer.java:199)

 

0 Likes
Message 13 of 15

JesusFreke
Advocate
Advocate

Thanks, it took me a little bit to figure out how to reproduce the error. It turned out to be related to imports of the form 

from .. import name

 

I know this used to work, so I'm not sure exactly what changed in the meantime, but it should be fixed now. I released a new version (0.6.5) which addresses this, along with a few other fixes. It should be available in a day or two, or you can download the plugin zip from github and install it manually.

0 Likes
Message 14 of 15

JesusFreke
Advocate
Advocate

The new version has already been approved, so you can go ahead and update from pycharm directly

0 Likes
Message 15 of 15

tim.collins29V9X
Enthusiast
Enthusiast

Success!  I am stepping through my code in the PyCharm debugger. I appreciate that this plugin must have taking a lot of effort. Thank  you for your hard work and quick turn-around on my messages. 

I encourage anyone who lands here to try it out. The usage is a bit different from, but not harder than, using  VS Code, so be sure to follow the instructions.