PyAPI: add API call to get an operators type
authorCampbell Barton <ideasman42@gmail.com>
Thu, 13 Sep 2018 08:16:06 +0000 (18:16 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 13 Sep 2018 08:16:06 +0000 (18:16 +1000)
Getting the instance leaks memory and was only meant to be used for
generating docs.

release/scripts/modules/bpy/ops.py
release/scripts/modules/rna_info.py
release/scripts/startup/bl_operators/presets.py
release/scripts/startup/bl_operators/wm.py
source/blender/python/intern/bpy_operator.c

index 7c5c76ffdbe26e6648274c1112512ebacc1aaa16..028cce063aca52ce423e2e4ec99d7029b2ebbad0 100644 (file)
@@ -27,6 +27,7 @@ op_poll = ops_module.poll
 op_call = ops_module.call
 op_as_string = ops_module.as_string
 op_get_rna = ops_module.get_rna
+op_get_rna_type = ops_module.get_rna_type
 op_get_instance = ops_module.get_instance
 
 
@@ -193,6 +194,10 @@ class BPyOpsSubModOp:
 
         return ret
 
+    def get_rna_type(self):
+        """Internal function for introspection"""
+        return op_get_rna_type(self.idname())
+
     def get_rna(self):
         """Internal function for introspection"""
         return op_get_rna(self.idname())
index 53d2e2d01515dd3eb8361f0a901f9d857518cdb4..535f2cda62631b89e616c06f5ef9d896d47fdb60 100644 (file)
@@ -731,14 +731,14 @@ def BuildRNAInfo():
         operators = dir(op_mod)
         for op in sorted(operators):
             try:
-                rna_prop = getattr(op_mod, op).get_rna()
+                rna_prop = getattr(op_mod, op).get_rna_type()
             except AttributeError:
                 rna_prop = None
             except TypeError:
                 rna_prop = None
 
             if rna_prop:
-                GetInfoOperatorRNA(rna_prop.bl_rna)
+                GetInfoOperatorRNA(rna_prop)
 
     for rna_info in InfoOperatorRNA.global_lookup.values():
         rna_info.build()
index 8852bca971d6450914d6b9a42aaa5e77885e2185..f5a77e2f306242367fa7170619217f2973df3638 100644 (file)
@@ -628,7 +628,7 @@ class AddPresetOperator(AddPresetBase, Operator):
 
         prefix, suffix = self.operator.split("_OT_", 1)
         op = getattr(getattr(bpy.ops, prefix.lower()), suffix)
-        operator_rna = op.get_rna().bl_rna
+        operator_rna = op.get_rna_type()
         del op
 
         ret = []
index 64a68e41e13ad3884b646d8d2f7bcd06974710fd..381e98c29408194cd4b31acb2f99060088d5c12e 100644 (file)
@@ -612,7 +612,7 @@ class WM_OT_operator_pie_enum(Operator):
         del op_mod_str, ob_id_str
 
         try:
-            op_rna = op.get_rna()
+            op_rna = op.get_rna_type()
         except KeyError:
             self.report({'ERROR'}, "Operator not found: bpy.ops.%s" % data_path)
             return {'CANCELLED'}
@@ -622,7 +622,7 @@ class WM_OT_operator_pie_enum(Operator):
             pie = layout.menu_pie()
             pie.operator_enum(data_path, prop_string)
 
-        wm.popup_menu_pie(draw_func=draw_cb, title=op_rna.bl_rna.name, event=event)
+        wm.popup_menu_pie(draw_func=draw_cb, title=op_rna.name, event=event)
 
         return {'FINISHED'}
 
index 78ae99765865f9aac383db79513fe9df25655298..555aec00b2c9488df805c9f3cefd9a60b813d83f 100644 (file)
@@ -409,6 +409,19 @@ static PyObject *pyop_dir(PyObject *UNUSED(self))
        return list;
 }
 
+static PyObject *pyop_getrna_type(PyObject *UNUSED(self), PyObject *value)
+{
+       wmOperatorType *ot;
+       if ((ot = ot_lookup_from_py_string(value, "get_rna_type")) == NULL) {
+               return NULL;
+       }
+
+       PointerRNA ptr;
+       RNA_pointer_create(NULL, &RNA_Struct, ot->srna, &ptr);
+       BPy_StructRNA *pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
+       return (PyObject *)pyrna;
+}
+
 static PyObject *pyop_getrna(PyObject *UNUSED(self), PyObject *value)
 {
        wmOperatorType *ot;
@@ -466,6 +479,7 @@ static struct PyMethodDef bpy_ops_methods[] = {
        {"call", (PyCFunction) pyop_call, METH_VARARGS, NULL},
        {"as_string", (PyCFunction) pyop_as_string, METH_VARARGS, NULL},
        {"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL},
+       {"get_rna_type", (PyCFunction) pyop_getrna_type, METH_O, NULL},
        {"get_rna", (PyCFunction) pyop_getrna, METH_O, NULL},           /* only for introspection, leaks memory */
        {"get_instance", (PyCFunction) pyop_getinstance, METH_O, NULL}, /* only for introspection, leaks memory */
        {"macro_define", (PyCFunction) PYOP_wrap_macro_define, METH_VARARGS, NULL},