4.3.18 MUserEventMessage

The Python bindings of the MUserEventMessage class also allow communicating with C++ plugins. On the Python side, the client data that is passed to the callbacks is represented as a string containing the binary data (you can use the standard Python module struct to construct or parse such strings). In order to process those strings the size of the binary data has to be known in advance. So before you register a callback you must declare the data size using the setUserEventSize() method (which is specific to the Python bindings). Any client data passed to postEvent() (no matter if the Python version or the C++ version of postEvent() was called) will be wrapped into a Python string of the specified size and passed to the callback. If no client data was specified when calling postEvent() a callback will just receive the client data that has been passed to the addUserEventCallback() method (which may be any Python object).

The following MUserEventMessage methods differ from their C++ counterparts or are specific to the Python bindings:

setUserEventSize( event, size)
This is an additional static method that you have to use to declare the size (in bytes) of the client data that a callback will receive. The Python callbacks will receive a string as argument that contains the binary data that was passed to postEvent() (either the Python version or the C++ version).

Note: You must call this function before registering a callback using addUserEventCallback(). The function cannot be used to change the size of any previously registered callback.

getUserEventSize( event)
Returns the client data size of a particular user event.

postUserEvent( event, clientData=None)
An event posted by this method can also be received by C++ code (which doesn't have to be aware of the source of the event being Python code). If clientData is provided it must be a string containing the binary data that the registered callbacks will receive. You can create the string using the Python struct module. If no client data is provided the object that has been passed to addUserEventCallback() is received by the Python callbacks (this object may be any Python object, not just a string).