Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/intern/bpy_msgbus.c
- This file was added.
| /* | |||||
| * ***** BEGIN GPL LICENSE BLOCK ***** | |||||
| * | |||||
| * This program is free software; you can redistribute it and/or | |||||
| * modify it under the terms of the GNU General Public License | |||||
| * as published by the Free Software Foundation; either version 2 | |||||
| * of the License, or (at your option) any later version. | |||||
| * | |||||
| * This program is distributed in the hope that it will be useful, | |||||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
| * GNU General Public License for more details. | |||||
| * | |||||
| * You should have received a copy of the GNU General Public License | |||||
| * along with this program; if not, write to the Free Software Foundation, | |||||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||||
| * | |||||
| * ***** END GPL LICENSE BLOCK ***** | |||||
| */ | |||||
| /** \file blender/python/intern/bpy_msgbus.c | |||||
| * \ingroup pythonintern | |||||
| * This file defines '_bpy_msgbus' module, exposed as 'bpy.msgbus'. | |||||
| */ | |||||
| #include <Python.h> | |||||
| #include "../generic/python_utildefines.h" | |||||
| #include "BLI_utildefines.h" | |||||
| #include "BKE_context.h" | |||||
| #include "WM_api.h" | |||||
| #include "WM_types.h" | |||||
| #include "WM_message.h" | |||||
| #include "RNA_access.h" | |||||
| #include "RNA_define.h" | |||||
| #include "RNA_enum_types.h" | |||||
| #include "bpy_util.h" | |||||
| #include "bpy_rna.h" | |||||
| #include "bpy_intern_string.h" | |||||
| #include "bpy_manipulator_wrap.h" /* own include */ | |||||
| #include "bpy_msgbus.h" /* own include */ | |||||
| /* TODO: de-duplicate */ | |||||
| /* could be moved into bpy_utils */ | |||||
| static void printf_func_error(PyObject *py_func) | |||||
| { | |||||
| /* since we return to C code we can't leave the error */ | |||||
| PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func); | |||||
| PyErr_Print(); | |||||
| PyErr_Clear(); | |||||
| /* use py style error */ | |||||
| fprintf(stderr, "File \"%s\", line %d, in %s\n", | |||||
| _PyUnicode_AsString(f_code->co_filename), | |||||
| f_code->co_firstlineno, | |||||
| _PyUnicode_AsString(((PyFunctionObject *)py_func)->func_name) | |||||
| ); | |||||
| } | |||||
| /* -------------------------------------------------------------------- */ | |||||
| /** \name XXX | |||||
| * \{ */ | |||||
| #define BPY_MSGBUS_USER_DATA_LEN 2 | |||||
| /* Follow wmMsgNotifyFn spec */ | |||||
| static void bpy_msgbus_notify( | |||||
| bContext *C, wmMsgSubscribeKey *UNUSED(msg_key), wmMsgSubscribeValue *msg_val) | |||||
| { | |||||
| PyGILState_STATE gilstate; | |||||
| bpy_context_set(C, &gilstate); | |||||
| PyObject *user_data = msg_val->user_data; | |||||
| BLI_assert(PyTuple_GET_SIZE(user_data) == BPY_MSGBUS_USER_DATA_LEN); | |||||
| PyObject *callback_args = PyTuple_GET_ITEM(user_data, 0); | |||||
| PyObject *callback_notify = PyTuple_GET_ITEM(user_data, 1); | |||||
| const bool is_write_ok = pyrna_write_check(); | |||||
| if (!is_write_ok) { | |||||
| pyrna_write_set(true); | |||||
| } | |||||
| PyObject *ret = PyObject_CallObject(callback_notify, callback_args); | |||||
| if (ret == NULL) { | |||||
| printf_func_error(callback_notify); | |||||
| } | |||||
| else { | |||||
| if (ret != Py_None) { | |||||
| PyErr_SetString(PyExc_ValueError, "the return value must be None"); | |||||
| printf_func_error(callback_notify); | |||||
| } | |||||
| Py_DECREF(ret); | |||||
| } | |||||
| bpy_context_clear(C, &gilstate); | |||||
| if (!is_write_ok) { | |||||
| pyrna_write_set(false); | |||||
| } | |||||
| } | |||||
| /* Follow wmMsgSubscribeValueFreeDataFn spec */ | |||||
| static void bpy_msgbus_subscribe_value_free_data( | |||||
| struct wmMsgSubscribeKey *UNUSED(msg_key), struct wmMsgSubscribeValue *msg_val) | |||||
| { | |||||
| PyGILState_STATE gilstate = PyGILState_Ensure(); | |||||
| Py_DECREF(msg_val->owner); | |||||
| Py_DECREF(msg_val->user_data); | |||||
| PyGILState_Release(gilstate); | |||||
| } | |||||
| PyDoc_STRVAR(bpy_msgbus_subscribe_rna_doc, | |||||
| ".. function:: subscribe_rna(data, owner, args, notify)\n" | |||||
| "\n" | |||||
| " :arg data: Data being subscribed to.\n" | |||||
| " :type data: StructRNA, PropertyRNA or ID type\n" | |||||
| " :arg owner: Handle for this subscription (compared by identity).\n" | |||||
| " :type owner: Any type.\n" | |||||
| "\n" | |||||
| " Returns a new vector int property definition.\n" | |||||
| ); | |||||
| static PyObject *bpy_msgbus_subscribe_rna(PyObject *UNUSED(self), PyObject *args, PyObject *kw) | |||||
| { | |||||
| PyObject *data_generic = NULL; | |||||
| PyObject *owner = NULL; | |||||
| PyObject *callback_args = NULL; | |||||
| PyObject *callback_notify = NULL; | |||||
| static const char *_keywords[] = { | |||||
| "data", | |||||
| "owner", | |||||
| "args", | |||||
| "notify", | |||||
| NULL, | |||||
| }; | |||||
| static _PyArg_Parser _parser = {"$OOO!O:subscribe_rna", _keywords, 0}; | |||||
| if (!_PyArg_ParseTupleAndKeywordsFast( | |||||
| args, kw, &_parser, | |||||
| &data_generic, &owner, | |||||
| &PyTuple_Type, &callback_args, | |||||
| &callback_notify)) | |||||
| { | |||||
| return NULL; | |||||
| } | |||||
| /* Note: we may want to have a way to pass this in. */ | |||||
| bContext *C = (bContext *)BPy_GetContext(); | |||||
| struct wmMsgBus *mbus = CTX_wm_message_bus(C); | |||||
| wmMsgParams_RNA msg_key_params = {0}; | |||||
| wmMsgSubscribeValue msg_val_params = {0}; | |||||
| if (BPy_PropertyRNA_Check(data_generic)) { | |||||
| BPy_PropertyRNA *data_prop = (BPy_PropertyRNA *)data_generic; | |||||
| PYRNA_PROP_CHECK_OBJ(data_prop); | |||||
| msg_key_params.ptr = data_prop->ptr; | |||||
| msg_key_params.prop = data_prop->prop; | |||||
| } | |||||
| else if (BPy_StructRNA_Check(data_generic)) { | |||||
| /* note, this isn't typically used since we don't edit structs directly. */ | |||||
| BPy_StructRNA *data_srna = (BPy_StructRNA *)data_generic; | |||||
| PYRNA_STRUCT_CHECK_OBJ(data_srna); | |||||
| msg_key_params.ptr = data_srna->ptr; | |||||
| } | |||||
| /* TODO - property / type, not instance. */ | |||||
| else if (PyType_Check(data_generic)) { | |||||
| StructRNA *data_type = pyrna_struct_as_srna(data_generic, false, "subscribe_rna"); | |||||
| if (data_type == NULL) { | |||||
| return NULL; | |||||
| } | |||||
| msg_key_params.ptr.type = data_type; | |||||
| } | |||||
| else if (PyTuple_CheckExact(data_generic)) { | |||||
| if (PyTuple_GET_SIZE(data_generic) == 2) { | |||||
| PyObject *data_type_py = PyTuple_GET_ITEM(data_generic, 0); | |||||
| PyObject *data_prop_py = PyTuple_GET_ITEM(data_generic, 1); | |||||
| StructRNA *data_type = pyrna_struct_as_srna(data_type_py, false, "subscribe_rna"); | |||||
| if (data_type == NULL) { | |||||
| return NULL; | |||||
| } | |||||
| if (!PyUnicode_CheckExact(data_prop_py)) { | |||||
| PyErr_SetString(PyExc_ValueError, "Expected property to be a string"); | |||||
| return NULL; | |||||
| } | |||||
| PointerRNA data_type_ptr = { .type = data_type, }; | |||||
| const char *data_prop_str = _PyUnicode_AsString(data_prop_py); | |||||
| PropertyRNA *data_prop = RNA_struct_find_property(&data_type_ptr, data_prop_str); | |||||
| if (data_prop == NULL) { | |||||
| PyErr_Format( | |||||
| PyExc_TypeError, | |||||
| "Struct does not contain property %.200s", | |||||
| data_prop_str); | |||||
| return NULL; | |||||
| } | |||||
| msg_key_params.ptr.type = data_type; | |||||
| msg_key_params.prop = data_prop; | |||||
| } | |||||
| else { | |||||
| PyErr_SetString(PyExc_ValueError, "Expected a pair (type, property_id)"); | |||||
| return NULL; | |||||
| } | |||||
| } | |||||
| if (!PyFunction_Check(callback_notify)) { | |||||
| PyErr_Format( | |||||
| PyExc_TypeError, | |||||
| "notify expects a function, found %.200s", | |||||
| Py_TYPE(callback_notify)->tp_name); | |||||
| return NULL; | |||||
| } | |||||
| /* owner can be anything. */ | |||||
| { | |||||
| msg_val_params.owner = owner; | |||||
| Py_INCREF(owner); | |||||
| } | |||||
| { | |||||
| PyObject *user_data = PyTuple_New(2); | |||||
| PyTuple_SET_ITEMS( | |||||
| user_data, | |||||
| Py_INCREF_RET(callback_args), | |||||
| Py_INCREF_RET(callback_notify)); | |||||
| msg_val_params.user_data = user_data; | |||||
| } | |||||
| msg_val_params.notify = bpy_msgbus_notify; | |||||
| msg_val_params.free_data = bpy_msgbus_subscribe_value_free_data; | |||||
| WM_msg_subscribe_rna_params(mbus, &msg_key_params, &msg_val_params, __func__); | |||||
| WM_msg_dump(mbus, __func__); | |||||
| Py_RETURN_NONE; | |||||
| } | |||||
| PyDoc_STRVAR(bpy_msgbus_clear_by_owner_doc, | |||||
| ".. function:: clear_by_owner(owner)\n" | |||||
| "\n" | |||||
| " Clear all subscribers using this owner.\n" | |||||
| ); | |||||
| static PyObject *bpy_msgbus_clear_by_owner(PyObject *UNUSED(self), PyObject *owner) | |||||
| { | |||||
| bContext *C = (bContext *)BPy_GetContext(); | |||||
| struct wmMsgBus *mbus = CTX_wm_message_bus(C); | |||||
| WM_msgbus_clear_by_owner(mbus, owner); | |||||
| Py_RETURN_NONE; | |||||
| } | |||||
| static struct PyMethodDef BPy_msgbus_methods[] = { | |||||
| {"subscribe_rna", (PyCFunction)bpy_msgbus_subscribe_rna, METH_VARARGS | METH_KEYWORDS, bpy_msgbus_subscribe_rna_doc}, | |||||
| {"clear_by_owner", (PyCFunction)bpy_msgbus_clear_by_owner, METH_O, bpy_msgbus_clear_by_owner_doc}, | |||||
| {NULL, NULL, 0, NULL} | |||||
| }; | |||||
| static struct PyModuleDef _bpy_msgbus_def = { | |||||
| PyModuleDef_HEAD_INIT, | |||||
| .m_name = "msgbus", | |||||
| .m_methods = BPy_msgbus_methods, | |||||
| }; | |||||
| PyObject *BPY_msgbus_module(void) | |||||
| { | |||||
| PyObject *submodule; | |||||
| submodule = PyModule_Create(&_bpy_msgbus_def); | |||||
| return submodule; | |||||
| } | |||||
| /** \} */ | |||||