[Bf-blender-cvs] [b53ee963b16] master: PyAPI: expose OperatorType.modal_keymap

Campbell Barton noreply at git.blender.org
Mon Sep 16 08:07:02 CEST 2019


Commit: b53ee963b16d817a6367bd7c73b866036868b2e2
Author: Campbell Barton
Date:   Mon Sep 16 15:59:09 2019 +1000
Branches: master
https://developer.blender.org/rBb53ee963b16d817a6367bd7c73b866036868b2e2

PyAPI: expose OperatorType.modal_keymap

Support assigning modal keymaps once the operator is registered.

===================================================================

M	release/scripts/modules/bpy_types.py
M	source/blender/python/intern/bpy_operator.c

===================================================================

diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py
index a3787506da4..c97b973c9c7 100644
--- a/release/scripts/modules/bpy_types.py
+++ b/release/scripts/modules/bpy_types.py
@@ -544,6 +544,16 @@ class RNAMetaPropGroup(StructMetaPropGroup, RNAMeta):
     pass
 
 
+class RNAMetaOperator(RNAMeta):
+    @property
+    def modal_keymap(cls):
+        return _bpy.ops.modal_keymap_get(cls.bl_idname)
+
+    @modal_keymap.setter
+    def modal_keymap(cls, keymap):
+        _bpy.ops.modal_keymap_set(cls.bl_idname, keymap)
+
+
 # Same as 'Operator'
 # only without 'as_keywords'
 class Gizmo(StructRNA):
@@ -653,7 +663,7 @@ class GizmoGroup(StructRNA):
 
 # Only defined so operators members can be used by accessing self.order
 # with doc generation 'self.properties.bl_rna.properties' can fail
-class Operator(StructRNA, metaclass=RNAMeta):
+class Operator(StructRNA, metaclass=RNAMetaOperator):
     __slots__ = ()
 
     def __getattribute__(self, attr):
diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c
index 5e3b000c604..e04630ca356 100644
--- a/source/blender/python/intern/bpy_operator.c
+++ b/source/blender/python/intern/bpy_operator.c
@@ -50,8 +50,10 @@
 
 #include "BLI_ghash.h"
 
-#include "BKE_report.h"
 #include "BKE_context.h"
+#include "BKE_global.h"
+#include "BKE_main.h"
+#include "BKE_report.h"
 
 /* so operators called can spawn threads which acquire the GIL */
 #define BPY_RELEASE_GIL
@@ -429,6 +431,51 @@ static PyObject *pyop_getrna_type(PyObject *UNUSED(self), PyObject *value)
   return (PyObject *)pyrna;
 }
 
+static PyObject *pyop_type_modal_keymap_get(PyObject *UNUSED(self), PyObject *value)
+{
+  wmOperatorType *ot;
+  if (!(ot = ot_lookup_from_py_string(value, "modal_keymap_get"))) {
+    return NULL;
+  }
+  if (ot->modalkeymap == NULL) {
+    Py_RETURN_NONE;
+  }
+  else {
+    PointerRNA ptr;
+    RNA_pointer_create(G.main->wm.first, &RNA_KeyMap, ot->modalkeymap, &ptr);
+    return pyrna_struct_CreatePyObject(&ptr);
+  }
+}
+
+static PyObject *pyop_type_modal_keymap_set(PyObject *UNUSED(self), PyObject *args)
+{
+  PyObject *ot_idname, *py_keymap;
+  wmOperatorType *ot;
+  if (!PyArg_ParseTuple(args, "OO", &ot_idname, &py_keymap) ||
+      !(ot = ot_lookup_from_py_string(ot_idname, "modal_keymap_set"))) {
+    return NULL;
+  }
+
+  if (py_keymap == Py_None) {
+    ot->modalkeymap = NULL;
+  }
+  else if (BPy_StructRNA_Check(py_keymap) &&
+           RNA_struct_is_a(((BPy_StructRNA *)py_keymap)->ptr.type, &RNA_KeyMap)) {
+    BPy_StructRNA *py_keymap_rna = (BPy_StructRNA *)py_keymap;
+    wmKeyMap *km = py_keymap_rna->ptr.data;
+    if ((km->flag & KEYMAP_MODAL) == 0) {
+      PyErr_SetString(PyExc_ValueError, "Expected a keymap a modal keymap");
+      return NULL;
+    }
+    ot->modalkeymap = km;
+  }
+  else {
+    PyErr_SetString(PyExc_TypeError, "Expected a keymap object or None");
+    return NULL;
+  }
+  Py_RETURN_NONE;
+}
+
 static struct PyMethodDef bpy_ops_methods[] = {
     {"poll", (PyCFunction)pyop_poll, METH_VARARGS, NULL},
     {"call", (PyCFunction)pyop_call, METH_VARARGS, NULL},
@@ -436,6 +483,10 @@ static struct PyMethodDef bpy_ops_methods[] = {
     {"dir", (PyCFunction)pyop_dir, METH_NOARGS, NULL},
     {"get_rna_type", (PyCFunction)pyop_getrna_type, METH_O, NULL},
     {"macro_define", (PyCFunction)PYOP_wrap_macro_define, METH_VARARGS, NULL},
+
+    /* Operator type functions (use to implement 'OperatorType.modal_keymap' property). */
+    {"modal_keymap_get", (PyCFunction)pyop_type_modal_keymap_get, METH_O, NULL},
+    {"modal_keymap_set", (PyCFunction)pyop_type_modal_keymap_set, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL},
 };



More information about the Bf-blender-cvs mailing list