example for user event messages?


#1

hello everyone,
can anybody give example to catch user event? i found this example from maya developers website but how to tell that function to run at on ‘myevent’ occurrence. And is it hard to create events?

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)

#2

The example from the devs website that you have posted here covers it pretty well. Maybe we can help further if you explain what you are trying to do? Why do you need a user event? What is the context?

David


#3

Hi,

i want to print some information to user whenever that event(‘myEvent’) occurs but how function(‘myFunc()’) knows custom event(‘myEvent’) has occured?

For example:-
i want to catch event after maya has launched after 1 hour

hope it clears…


#4

myFunc() doesnt know anything about the event. A callback is created with om.MUserEventMessage.addUserEventCallback. The job of the callback is to wait for the event and when it the event happens it launches myFunc().

Here is some info on scheduling events that might be useful. https://docs.python.org/2/library/sched.html

David


#5

thanks for your resource and i had got one more doubt that scriptJob command runs on only one thread if not on thread then then threads?

for ex:- how “SelectionChanged” event has created?

sorry if i had asked too much…


#6

You have to give more info I’m afraid. selectionChanged - is triggered when the user changes selection.
So if you have troubles to figure that out, please elaborate?

/risto


#7

After rereading your first post and your latest post, I will try to answer both together.

Maya already has a system work working with events through mel or python by using scriptJobs. There is a big list of events that you can choose from to trigger your scriptJob. The code for generating those events is already part of the maya UI. So for example when you select something maya generate a SelectionChanged event. If you have a scriptJob created that is waiting for this event, it will now run.

If you want to create your own events, then there a three things you need to do. 1. Register the event, 2. Tell maya what to do when the event happens, 3. Make the event happen.

To register an even you just need to tell maya… “please register an event called myEvent”. This is done with
om.MUserEventMessage.registerUserEvent(‘myEvent’)
and with that one line part 1 is taken care of.

Telling maya what to do when the event happens is also one line
callbackId =om.MUserEventMessage.addUserEventCallback(‘myEvent’, myFunc)
This tells maya to run a function called myFunc when the event happens.

To make the event happen is just one more line of code
om.MUserEventMessage.postUserEvent(‘myEvent’)

I think that maybe you are wondering where to put that last line? Well it could be anywhere. You could just type it into the script editor and do it there, but probably you want to put it inside another script. I could explain more, but without knowing what you are trying to do, I’d probably just be confusing things.

David


#8

Hello david,

Thanks for you reply and sorry for my late reply…

So then scriptJob command runs on thread?

Basically what i want is a dialog box should popup every 1hr.

  1. So it is possible without using threading?
  2. If i use threads it takes load on maya?

Hope so i’am clear…


#9

Is there some reason this dialog box needs to be run inside maya? If not then you could simply use the operating system scheduler to launch something every hour. What did you want this dialog to do?

David


#10

there is no other reason but i’am trying to create events with maya python api. why is there any drawbacks using events in maya?