Log debug messages in Fusion 360

The usual way to debug your Python add-in is to start it from Visual Studio Code, and use print() to write messages to the Debug Console:

DebugPrint

However, sometimes you might just want some insight into what’s going on inside your add-in without starting a debugging session.

One obvious way is to write information into a log file. Depending on your needs that might be the thing to do.

That will not provide real-time information though: you have to open the file in order to see what was written into it.

Another option is to write to the “Text Commands” palette of Fusion 360. If you don’t mind a bit of delay for the messages to appear then just call writeText() function of the palette, otherwise also call adsk.doEvents()

You could create a couple of simple classes to implement what’s needed:

class UiLogger:
def __init__(self, forceUpdate):
app = adsk.core.Application.get()
ui  = app.userInterface
palettes = ui.palettes
self.textPalette = palettes.itemById("TextCommands")
self.forceUpdate = forceUpdate
self.textPalette.isVisible = True
def print(self, text):
self.textPalette.writeText(text)
if (self.forceUpdate):
adsk.doEvents()
class FileLogger:
def __init__(self, filePath):
try:
open(filePath, 'a').close()
self.filePath = filePath
except:
raise Exception("Could not open/create file = " + filePath)
def print(self, text):
with open(self.filePath, 'a') as txtFile:
txtFile.writelines(text + 'rn')

Then you can create an instance of either of them and start logging messages. E.g. using the UiLogger:

logger = UiLogger(True)
# ...
logger.print("Some message")

TextCommands

Or use the FileLogger

logger = FileLogger("/Users/nagyad/Documents/log.txt")
# ...
logger.print("Some message")

LogFile

That palette is not only for writing messages but can also be used to run Python scripts – see Run commands from the “Text Commands” panel


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading