Confused on how to properly debug with Python

Confused on how to properly debug with Python

soswow
Advocate Advocate
2,437 Views
8 Replies
Message 1 of 9

Confused on how to properly debug with Python

soswow
Advocate
Advocate

I've made a short video to indication my confusion points:

https://www.youtube.com/watch?v=ARjJkeeF1lw&ab_channel=AleksandrMotsjonov

I want to know how people are able to do any development with Current Fusion <> Spyder coupling.

+ to stuff shown in the video there is 50% change Fusion360 will freeze while I Am in debug mode going through lines etc.

 

 

0 Likes
2,438 Views
8 Replies
Replies (8)
Message 2 of 9

marshaltu
Autodesk
Autodesk

Hello,

 

Thank you for sharing your experiences which debugged Fusion 360 addin with Spyder.

 

From the video, the breakpoint you set in the line "ui = app.userInterface" has been hit since you can watch the variable "app" in "Variable explorer". By default, program execution will pause in the first line of Python codes (e.g. "import adsk.core, adsk.fusion, trackback"). When you hit "Continue" in debug toolbar, it went to the first breakpoint in "run" function. It seemed that there was some bug in your application. When you cleared the breakpoint and hit "Continue" again, program execution ran into some errors. I would like to recommend you debug the codes step by step(Command+F10) from that line and then you would find the problematic code line in your codes.

 

It would be great if you can share your codes to me(marshal.tu@autodesk.com). I would be able to help find the problem out.

 

Thanks,

Marshal



Marshal Tu
Fusion Developer
>
0 Likes
Message 3 of 9

soswow
Advocate
Advocate

Ok, let me ask one question in a time:

1) When hit brakepoint, why is the current "running" line is not marked in any way in my editor. I've use many IDEs and it is more then expected I would see how I move through lines.

0 Likes
Message 4 of 9

marshaltu
Autodesk
Autodesk

Hello,

 

Current running line would be highlighted by default when you invoke "debug" in Fusion 360. Unfortunately the highlighted line will be changed once you move your cursor to somewhere else. This is definitely one of the cases which Spyder is not so user-friendly. 

 

The workaround would be to type "l" or "list" in the console. The current running line will be shown with "->" indicator in the output. Also you can just run next step and then highlight will be back to running code line. Please refer to https://pythonconquerstheuniverse.wordpress.com/category/python-debugger/ for more powerful debug command of PDB.

 

(Pdb) list
1 #Author-Autodesk Inc.
2 #Description-Create a spur gear.
3
4 B import adsk.core, adsk.fusion, traceback
5 -> import os, math
6
7 commandId = 'SpurGearCommandIdPy'
8
9 # global set of event handlers to keep them referenced for the duration of the command
10 handlers = []

 

Ideally we should provide the functionality to let customers define their preferred Python IDE. Unfortunately some customization of IDE are needed to let Fusion 360 and corresponding IDE work(debug) together. Spyder is currently only one we did customization and supported.

 

Thanks,

Marshal

 



Marshal Tu
Fusion Developer
>
0 Likes
Message 5 of 9

soswow
Advocate
Advocate

Ok. That's clear, though misfortune Spider lack thisfundamendal feature for debugging.

 

I have related question. How does Fusion runs JS scripts? Does it something to do with node.js? If yes, is there a way to run my script with debug flag (https://nodejs.org/api/debugger.html) so I can connect to this process from IDE of mu choise and do debugging from there through V8 standard debugging protocol?

0 Likes
Message 6 of 9

marshaltu
Autodesk
Autodesk

No, we use Chromium Eembeded Framework(CEF) to run JS codes in Windows and Cocoa WebView in MAC.

 

https://bitbucket.org/chromiumembedded/cef

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/

 

Thanks,

Marshal



Marshal Tu
Fusion Developer
>
0 Likes
Message 7 of 9

KrisKaplan
Autodesk
Autodesk

Spyder debugging can be a little confusing at first, but I can assure you that you can get used to it and be productive.

 

When you start debugging, the first (non-comment) line of code is the 'current line'.  The current line is the next line to be submitted to the interpreter if you 'step', or the first line to run if you 'continue'.  Unfortunately, Spyder uses the same line highlighting for the current debug line as it does for the currently selected line.  So if you click on the script, you will lose the highlighting for the current debugging line until you step or hit a breakpoint and a new current line is highlighted.  So just avoid selecting a new line in the IDE until you first identity (and remember) the current debug line (and as mentioned you can use 'list' to remind you which line it is on).

 

Also, the debugger will only stop on the next line in your script if you step, or on the next breakpoint if you continue (or if it is hit during a step).  If control in your code passes out of your module (such as a return or uncaught exception thrown out of the root block of your main module), then debugging is complete, and there is no current line.  Since Spyder provides very little visual feedback that it is currently debugging, if you don't realize that it happened it can be confusing.  So it is normally a good idea (for more than just debugging) to make sure that all of your code is run in a try-except block, and either put a breakpoint or a messagebox in the except block so while debugging you can see if your script is about to error out and stop debugging.

 

The variable explorer in Spyder is really more useful for the POD variables you might define.  It is not really all that useful to browse the property values you might get if you invoked all the properties on an API reference.  You can get spoiled if you are used to debugging in JavaScript.  When displaying the attributes of an object in JavaScript, it actually invokes the property accessors for every property and displays the results.  But debuggers like Spyder and C++, typically do not do this (as it changes the current stack frame, so you would lose the context of where you are currently broken).  So when you look at references to interface objects, there typically isn't anything very interesting to see on them (other than the member function references).  So in launguages like C++ or Python, you can only see an interface instances properties if you explicitly call those property accessor functions.

 

Kris



Kris Kaplan
0 Likes
Message 8 of 9

Anonymous
Not applicable

Python has a debugger , which is available as a module called pdb . It supports setting conditional breakpoints , stepping through the source code one line at a time, stack inspection, and more.

 

import pdb
msg = "this is a test"
pdb.set_trace()
print(msg)

 

Insert pdb.set_trace() anywhere and it will function as a breakpoint . When you execute the script by python test.py, you will in the debug mode.

 

0 Likes
Message 9 of 9

JesusFreke
Advocate
Advocate

You don't/can't execute Fusion 360 scripts from the command line like that. The scripts are run in a Python runtime that is running inside the Fusion 360 process. Fusion 360 has to load the script and run it within that runtime. Although, it may be possible to set up a remote pdb and do it that way.

0 Likes