Setting a user event within Maya

I was looking through some interesting questions that have come into ADN in the past, and thought I would share this one, it was about the possibility of setting a user event with a scriptJob (Maya Command).

There are no MEL commands to create user events and the scriptJob command cannot be used to wait for them.

However, you can do both in a Python script using API calls.

Here's how you would register a new user event type called 'myEvent':

    import maya.OpenMaya as om
    om.MUserEventMessage.registerUserEvent('myEvent')

To have a function called 'myFunc' execute whenever the event occurs:

    def myFunc(data):
        print('Got a myEvent event!')

    callbackId =om.MUserEventMessage.addUserEventCallback('myEvent', myFunc)

To send a 'myEvent' event:

    om.MUserEventMessage.postUserEvent('myEvent')

To remove the callback function when done:

    om.MUserEventMessage.removeCallback(callbackId)

Enjoy

~Kristine


Comments

One response to “Setting a user event within Maya”

  1. Nice simple breakdown, thanks. I’ve not used that type of callback yet, but now it’s a lot more approachable. Thanks :)

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading