Triggering event after any mouseclick using Command.mouseClick Event

Triggering event after any mouseclick using Command.mouseClick Event

Anonymous
Not applicable
836 Views
3 Replies
Message 1 of 4

Triggering event after any mouseclick using Command.mouseClick Event

Anonymous
Not applicable

Good evening,

 

I am trying to connect a handler to the Mouse event so that I can monitor how many clicks a user makes in a specific environment in Fusion. Ultimately I would like to do the same with the keyboard. 

 

Problem: When I run the code at the bottom I get the following error:

Handlers not set:
  Traceback (most recent call last):
  File "....", line... in <module>
  command_var.mouseClick.add(onMouseClick)
AttributeError: 'property' object has no attribute 'add'

In the example in the documentation, there is no detail on how the `comand_var` variable was defined.

When I inspect the comand_var variable in VSC I can see that it is referenced to the class  Command() and not the Command object.

 

Why: The project I am working on could use this as a metric of activity of a user in a specific environment. Further work could develop an educational tool or inform educators on students drafting strategies.

 

 

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

# Global variable used to maintain a reference to all event handlers.
try:
    app = adsk.core.Application.get()
    ui  = app.userInterface
    camera = app.cameraChanged
    _activeEditObject = app.activeEditObject

    cmd = adsk.core.Command

    #keyboard = adsk.core.KeyboardEvent

    # Global variable used to maintain a reference to all event handlers.
    handlers = []
    command_var = adsk.core.Command


except:
    if ui:
        ui.messageBox('Error:\n{}'.format(traceback.format_exc()))

# Event handler for the mouseClick event.
class MyMouseClickHandler(adsk.core.MouseEventHandler):
    def __init__(self):
        super().__init__()
        app =adsk.core.Application.get()
        ui = app.userInterface

    def notify(self, args):
        eventArgs = adsk.core.MouseEventArgs.cast(args)
        try:    
            ui = adsk.core.Application.get().userInterface
            ui.messageBox('MyMouseClickHandler triggered')
        except:
            ui = adsk.core.Application.get().userInterface
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


#KeyboardEventHandler
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('Hello addin')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('Stop addin')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


# setup handlers to trigger loggers
try:
    onMouseClick = MyMouseClickHandler()
    ui.messageBox(str(type(command_var)))
    command_var.mouseClick.add(onMouseClick)
    handlers.append(onMouseClick)

except:
    ui.messageBox('Handlers not set: \n{}'.format(traceback.format_exc())) 

 

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

pludikar
Collaborator
Collaborator

Hi,

 

I'm 99.99% certain that the API does not have access to the main F360 application events - in general, it only allows you to access events associated with commands and commandInputs that you create as part of your addIn/script.   You need to create a command/commandInput, and as part of the creation event, you add the mouseClick.  Unfortunately, the API doesn't have the event hooks to allow you to do what you want, at least not without a significant work around (if that is even possible).  This means that the command you create has to be active in order to capture the event. However the major problem is that only one command can be active at the same time!

 

I'm sure there are many of us who would be interested in a work around.

 

Peter

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
0 Likes
Message 3 of 4

goyals
Autodesk
Autodesk

Please take a look at this https://adndevblog.typepad.com/manufacturing/page/6/ explaining how to add mouse click event to a command. Thanks. 



Shyam Goyal
Sr. Software Dev. Manager
0 Likes
Message 4 of 4

Anonymous
Not applicable

EDIT: The MouseClick event should be running in the background so that, for example, the number of times a user clicks on the circle tool in the sketch environment are recorded. I thought I could add the mouseClick handler to Command object of all commands, or a similar workaround.

@goyals 

 

For now, I have managed to use the  ApplicationCommandEventHandler and adding it to the commandStarting object, but this only gets triggered once when a user is creating circles in a sketch environment, so I am unaware of how many times the command is used (The sketch is an example, but definitely a case that I would like to be able to solve).

@pludikar 

 

Thanks

0 Likes