Python Add In Logging Window

Python Add In Logging Window

mpieper
Contributor Contributor
944 Views
3 Replies
Message 1 of 4

Python Add In Logging Window

mpieper
Contributor
Contributor

What are the options for adding in a window with which I can log progress / output of python scripts?  I'm not seeing anything obvious in the userInterface API, but, alas, have not plumbed the depths. 

 

Having a script / stdout window would be a huge boon to development on more complicated scripts.

0 Likes
945 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor

Hi @mpieper .

 

I use the TextCommands output palette.

def dumpMsg(msg :str):
    adsk.core.Application.get().userInterface.palettes.itemById('TextCommands').writeText(str(msg))


This is convenient because you can see the progress without leaving the GUI.

0 Likes
Message 3 of 4

mpieper
Contributor
Contributor

Okay so that's getting me closer.

Direct pings to the function work.  I'd like to wrap it in a handler so I can just use logging like I normally would. e.g.

 

import logging
import adsk.core, adsk.fusion, adsk.cam, traceback

class Fusion360Handler(logging.Handler):
    def emit(self, record):
        msg = self.format(record)
        adsk.core.Application.get().userInterface.palettes.itemById('TextCommands').writeText(str(msg))

def run(context):
    #Grab local logger (NOTE: Cannot put this out of function scope)
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    #Instantiate handler, set level, and add to logger.
    handler = Fusion360Handler()
    handler.setLevel(logging.DEBUG)
    logger.addHandler(handler)

    logger.info("HELLO") #THIS FAILS

 

 

Here's a basic dumb sample script.  It's failing due to there being A LOT of extra loggers floating around with similar names / there seems to be multiple entries into the "run" function (e.g. enter into the function "run" multiple times)?  I pared it down a little further by forcing the logger to take a unique instance/name, and even then it bugs out on logger.info.

 

Line 1524, in handle  self.callHandlers(record)  File "C:\Users\mpiep\AppData\Local\Autodesk\webdeploy\production\c1a39fe96c80078ad566b938d0f03989f4b85b09\Python\lib\logging\__init__.py", line 1585, in callHandlers  if record.levelno >= hdlr.level: AttributeError: type object 'Fusion360Handler' has no attribute 'level'

 

Despite me looking at the handler directly and confirming it does in fact have a level attribute.  I have not looked under the covers to see how these scripts are patched in....

0 Likes
Message 4 of 4

JesusFreke
Advocate
Advocate

You might take a look at my fusion add-in for intellij IDEA. It wraps stdout, so anything you log in the add-in goes to the terminal in IDEA. It's pretty convenient during development! But it does involve setting up a completely separate development environment (IDEA instead of VSCode), so maybe not worth it if you just want logging.