The most common way to do this is via mel scriptJobs. But via the
It should be noted that if you keep executing the same callback creation code, they'll start to pile up. You'll need to develop a system to track if they've been made, so as to not have a lot of extra duplicate callbacks running in the background.
In the below example, we add a callback function that will be executed just before a new scene is opened via a
It should be noted that the above system is a bit of an example-hack. In practice, I've ran into problems with associating callbacks with a windows existence: A memory leak can (possibly) form from closing a window that a callback is associated with, and not also unregistering the callback. A common warning I'd get in the Output Window is:
Here's some pseudo-code that will assist in the proper creation, and removal, of a callback:
Here is a general list I've come up with for the various classes, and what they can set callbacks for:
(Maya 2011 API docs)
There are a few other classes independent from the Message ones:
And via
OpenMaya
API, you can access many 'Message' classes that provide a lot more functionality to areas that scriptJob
's don't provide.It should be noted that if you keep executing the same callback creation code, they'll start to pile up. You'll need to develop a system to track if they've been made, so as to not have a lot of extra duplicate callbacks running in the background.
In the below example, we add a callback function that will be executed just before a new scene is opened via a
OpenMaya.MSceneMessage
object. As a simple hack, we track its existence with a global mel variable.# Python code import maya.mel as mm import maya.OpenMaya as om def callback(*args): """ Put your callback execution code in here. """ print "CALLBACK!" def makeCallback(): """ Creates the callback, doesn't allow dupes: """ # Pass mel variable to Python: mm.eval("global int $gMyCallbackExists;") callbackExists = int(mm.eval("int $callbackExists = $gMyCallbackExists;")) if not callbackExists: om.MSceneMessage.addCallback(om.MSceneMessage.kBeforeNew, callback) mm.eval("global int $gMyCallbackExists = 1")To create the callback:
makeCallback()And when making a new scene from the main menu:
file -f -new; CALLBACK! // Result: untitled //
It should be noted that the above system is a bit of an example-hack. In practice, I've ran into problems with associating callbacks with a windows existence: A memory leak can (possibly) form from closing a window that a callback is associated with, and not also unregistering the callback. A common warning I'd get in the Output Window is:
swig/python detected a memory leak of type 'MCallbackId *', no destructor found.And it would even crash Maya. This thread addresses some of these issues.
Here's some pseudo-code that will assist in the proper creation, and removal, of a callback:
# Below are methods\attributes on some instance of a class: def makeCallback(self): """ Method that makes the callback. """ # self.callbackId is now of type MCallbackId, which appears to be a swig wrapper of a pointer # to an unsigned integer. self.callbackId = om.MSceneMessage.addCallback(om.MSceneMessage.kBeforeNew, callback) def destroyCallback(self): """ Method called when callback should be destroyed. """ # Properly remove the callback: om.MMessage.removeCallback(self.callbackId) # Call a method on the swig object to 'disown' it, and remove the warning statement: self.callbackId.disown()
Here is a general list I've come up with for the various classes, and what they can set callbacks for:
(Maya 2011 API docs)
- MMessage
- Listed first, this is the base class for all message callbacks (below). Mainly used to remove a message callback, but has other helper methods.
- MAnimMessage
- Callbacks for animation messages: animCuve edited, keyframe edited
- MCameraSetMessage
- Callbacks for
cameraSet
sepcific envent types: Adding\removing camera layers, camera name changes.
- Callbacks for
- MCommandMessage
- Callbacks triggering on mel command execution.
- MConditionMessage
- Callbacks for changes to specific conditions, same conditions found in the scriptJob documentation.
- MContainerMessage
- Callbacks that inform about changes to published attributes on container node types.
- MDagMessage
- Callbacks for dependency graph messages: Parent added, removed. Child added, removed, reordered. Instance added, removed.
- MDGMessage
- Callbacks for dependency graph messages: Time changed, node added, node removed, connection made or broken.
- MEventMessage
- Callbacks for specific events, same events found in the scriptJob documentation.
- MLockMessage
- Callbacks that control how Maya handles locks: On object and attributes.
- MModelMessage
- Callbacks for model related messages: Before and after node duplicate, before dag node added (created), during dag node removed (deleted).
- MNodeMessage
- Callbacks for dependency node messages of specific dependency nodes: Attribute changed, added, or removed. Plug dirty. Before node deleted. Attr keyable state change.
- MObjectSetMessage
- Callbacks for object set messages received by specific sets. Tracks if set members are modified.
- MPaintMessage
- Callbacks for vertex color paint
- MPolyMessage
- Callbacks for poly component id modification messages: Vertex, edge, or face component id's modified.
- MSceneMessage
- Callbacks for scene related messages:
- Before & after: new file, file import, file open, file export, file save, file save-as, file reference, remove-reference, import reference, export reference, unload reference, software render, each software render frame. plugin loaded, plugin unloaded.
- After any operation that changes which files are loaded, when an interactive render is interrupted by the user, on interactive or batch startup after initialization, just before Maya exits.
- MTimerMessage
- Callbacks that are based on a fixed time interval.
- MUiMessage
- Callbacks to track the deletion of UI objects. Window deletion, camera change, 3d view destroyed, 3d view about to render contents, 3d view about to display contents.
- MUserEventMessage
- Register user-defined event types, register callbacks with the user-defined event types, and to post user-defined messages.
- See example usage here: API : How can I author a user event?
There are a few other classes independent from the Message ones:
- MHwrCallback
- Callbacks to gain access to Maya's Hardware Rendering device status. You can be notified of device creation, lost reset and deletion.
- MRenderCallback
- Callbacks to gain access to Maya's rendering information during software rendering. You can modify Maya's shadow maps, RGB pixmap, and depth map to composite your own rendering effects into Maya's rendering.
- MCallbackIdArray
- An array of
MCallbackId
. These can be passed toMMessage
instances.
- An array of
And via
OpenMayaUI
:- MEvent
- Used for querying system events such as mouse presses: Mouse button press, release, hold, drag. Delete/backspace key event. Complete key event. Querying\setting event location.