[Bf-blender-cvs] [ed2a3454021] master: PyAPI: add _bpy.rna_enum_items_static() for accessing internal enum data

Campbell Barton noreply at git.blender.org
Tue May 31 06:19:38 CEST 2022


Commit: ed2a3454021c881c4955d6022761aa4b9b45af30
Author: Campbell Barton
Date:   Tue May 31 14:07:08 2022 +1000
Branches: master
https://developer.blender.org/rBed2a3454021c881c4955d6022761aa4b9b45af30

PyAPI: add _bpy.rna_enum_items_static() for accessing internal enum data

This is method is intended for internal use
(introspection for generating API docs).

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

M	source/blender/makesrna/intern/rna_key.c
M	source/blender/makesrna/intern/rna_nla.c
M	source/blender/makesrna/intern/rna_rna.c
M	source/blender/python/intern/bpy.c

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

diff --git a/source/blender/makesrna/intern/rna_key.c b/source/blender/makesrna/intern/rna_key.c
index 2f6fb30dc49..b16d127a643 100644
--- a/source/blender/makesrna/intern/rna_key.c
+++ b/source/blender/makesrna/intern/rna_key.c
@@ -26,6 +26,14 @@
 
 #include "rna_internal.h"
 
+const EnumPropertyItem rna_enum_keyblock_type_items[] = {
+    {KEY_LINEAR, "KEY_LINEAR", 0, "Linear", ""},
+    {KEY_CARDINAL, "KEY_CARDINAL", 0, "Cardinal", ""},
+    {KEY_CATMULL_ROM, "KEY_CATMULL_ROM", 0, "Catmull-Rom", ""},
+    {KEY_BSPLINE, "KEY_BSPLINE", 0, "BSpline", ""},
+    {0, NULL, 0, NULL, NULL},
+};
+
 #ifdef RNA_RUNTIME
 
 #  include <stddef.h>
@@ -789,14 +797,6 @@ static char *rna_ShapeKeyPoint_path(const PointerRNA *ptr)
 
 #else
 
-const EnumPropertyItem rna_enum_keyblock_type_items[] = {
-    {KEY_LINEAR, "KEY_LINEAR", 0, "Linear", ""},
-    {KEY_CARDINAL, "KEY_CARDINAL", 0, "Cardinal", ""},
-    {KEY_CATMULL_ROM, "KEY_CATMULL_ROM", 0, "Catmull-Rom", ""},
-    {KEY_BSPLINE, "KEY_BSPLINE", 0, "BSpline", ""},
-    {0, NULL, 0, NULL, NULL},
-};
-
 static const float tilt_limit = DEG2RADF(21600.0f);
 
 static void rna_def_keydata(BlenderRNA *brna)
diff --git a/source/blender/makesrna/intern/rna_nla.c b/source/blender/makesrna/intern/rna_nla.c
index 8aa87c1bcaa..7036680553d 100644
--- a/source/blender/makesrna/intern/rna_nla.c
+++ b/source/blender/makesrna/intern/rna_nla.c
@@ -23,6 +23,49 @@
 #include "WM_api.h"
 #include "WM_types.h"
 
+/* enum defines exported for rna_animation.c */
+const EnumPropertyItem rna_enum_nla_mode_blend_items[] = {
+    {NLASTRIP_MODE_REPLACE,
+     "REPLACE",
+     0,
+     "Replace",
+     "The strip values replace the accumulated results by amount specified by influence"},
+    {NLASTRIP_MODE_COMBINE,
+     "COMBINE",
+     0,
+     "Combine",
+     "The strip values are combined with accumulated results by appropriately using addition, "
+     "multiplication, or quaternion math, based on channel type"},
+    {0, "", 0, NULL, NULL},
+    {NLASTRIP_MODE_ADD,
+     "ADD",
+     0,
+     "Add",
+     "Weighted result of strip is added to the accumulated results"},
+    {NLASTRIP_MODE_SUBTRACT,
+     "SUBTRACT",
+     0,
+     "Subtract",
+     "Weighted result of strip is removed from the accumulated results"},
+    {NLASTRIP_MODE_MULTIPLY,
+     "MULTIPLY",
+     0,
+     "Multiply",
+     "Weighted result of strip is multiplied with the accumulated results"},
+    {0, NULL, 0, NULL, NULL},
+};
+
+const EnumPropertyItem rna_enum_nla_mode_extend_items[] = {
+    {NLASTRIP_EXTEND_NOTHING, "NOTHING", 0, "Nothing", "Strip has no influence past its extents"},
+    {NLASTRIP_EXTEND_HOLD,
+     "HOLD",
+     0,
+     "Hold",
+     "Hold the first frame if no previous strips in track, and always hold last frame"},
+    {NLASTRIP_EXTEND_HOLD_FORWARD, "HOLD_FORWARD", 0, "Hold Forward", "Only hold last frame"},
+    {0, NULL, 0, NULL, NULL},
+};
+
 #ifdef RNA_RUNTIME
 
 #  include <math.h>
@@ -494,49 +537,6 @@ static void rna_NlaTrack_solo_set(PointerRNA *ptr, bool value)
 
 #else
 
-/* enum defines exported for rna_animation.c */
-const EnumPropertyItem rna_enum_nla_mode_blend_items[] = {
-    {NLASTRIP_MODE_REPLACE,
-     "REPLACE",
-     0,
-     "Replace",
-     "The strip values replace the accumulated results by amount specified by influence"},
-    {NLASTRIP_MODE_COMBINE,
-     "COMBINE",
-     0,
-     "Combine",
-     "The strip values are combined with accumulated results by appropriately using addition, "
-     "multiplication, or quaternion math, based on channel type"},
-    {0, "", 0, NULL, NULL},
-    {NLASTRIP_MODE_ADD,
-     "ADD",
-     0,
-     "Add",
-     "Weighted result of strip is added to the accumulated results"},
-    {NLASTRIP_MODE_SUBTRACT,
-     "SUBTRACT",
-     0,
-     "Subtract",
-     "Weighted result of strip is removed from the accumulated results"},
-    {NLASTRIP_MODE_MULTIPLY,
-     "MULTIPLY",
-     0,
-     "Multiply",
-     "Weighted result of strip is multiplied with the accumulated results"},
-    {0, NULL, 0, NULL, NULL},
-};
-
-const EnumPropertyItem rna_enum_nla_mode_extend_items[] = {
-    {NLASTRIP_EXTEND_NOTHING, "NOTHING", 0, "Nothing", "Strip has no influence past its extents"},
-    {NLASTRIP_EXTEND_HOLD,
-     "HOLD",
-     0,
-     "Hold",
-     "Hold the first frame if no previous strips in track, and always hold last frame"},
-    {NLASTRIP_EXTEND_HOLD_FORWARD, "HOLD_FORWARD", 0, "Hold Forward", "Only hold last frame"},
-    {0, NULL, 0, NULL, NULL},
-};
-
 static void rna_def_strip_fcurves(BlenderRNA *brna, PropertyRNA *cprop)
 {
   StructRNA *srna;
diff --git a/source/blender/makesrna/intern/rna_rna.c b/source/blender/makesrna/intern/rna_rna.c
index ae67de9228c..b23c8cbba41 100644
--- a/source/blender/makesrna/intern/rna_rna.c
+++ b/source/blender/makesrna/intern/rna_rna.c
@@ -941,17 +941,29 @@ static int rna_EnumPropertyItem_identifier_length(PointerRNA *ptr)
 
 static void rna_EnumPropertyItem_name_get(PointerRNA *ptr, char *value)
 {
-  strcpy(value, ((EnumPropertyItem *)ptr->data)->name);
+  const EnumPropertyItem *eprop = ptr->data;
+  /* Name can be NULL in the case of separators
+   * which are exposed via `_bpy.rna_enum_items_static`. */
+  if (eprop->name) {
+    strcpy(value, eprop->name);
+  }
+  else {
+    value[0] = '\0';
+  }
 }
 
 static int rna_EnumPropertyItem_name_length(PointerRNA *ptr)
 {
-  return strlen(((EnumPropertyItem *)ptr->data)->name);
+  const EnumPropertyItem *eprop = ptr->data;
+  if (eprop->name) {
+    return strlen(eprop->name);
+  }
+  return 0;
 }
 
 static void rna_EnumPropertyItem_description_get(PointerRNA *ptr, char *value)
 {
-  EnumPropertyItem *eprop = (EnumPropertyItem *)ptr->data;
+  const EnumPropertyItem *eprop = ptr->data;
 
   if (eprop->description) {
     strcpy(value, eprop->description);
@@ -968,9 +980,7 @@ static int rna_EnumPropertyItem_description_length(PointerRNA *ptr)
   if (eprop->description) {
     return strlen(eprop->description);
   }
-  else {
-    return 0;
-  }
+  return 0;
 }
 
 static int rna_EnumPropertyItem_value_get(PointerRNA *ptr)
diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c
index 9d8602d51bd..2e97ae0fc1d 100644
--- a/source/blender/python/intern/bpy.c
+++ b/source/blender/python/intern/bpy.c
@@ -23,6 +23,7 @@
 #include "BKE_global.h" /* XXX, G_MAIN only */
 
 #include "RNA_access.h"
+#include "RNA_enum_types.h"
 #include "RNA_prototypes.h"
 #include "RNA_types.h"
 
@@ -456,6 +457,41 @@ static PyObject *bpy_context_members(PyObject *UNUSED(self))
   return result;
 }
 
+/**
+ * \note only exposed for generating documentation, see: `doc/python_api/sphinx_doc_gen.py`.
+ */
+PyDoc_STRVAR(bpy_rna_enum_items_static_doc,
+             ".. function:: rna_enum_items_static()\n"
+             "\n"
+             "   :return: A dict where the key the name of the enum, the value is a tuple of "
+             ":class:`bpy.types.EnumPropertyItem`.\n"
+             "   :rtype: dict of \n");
+static PyObject *bpy_rna_enum_items_static(PyObject *UNUSED(self))
+{
+#define DEF_ENUM(id) {STRINGIFY(id), id},
+  struct {
+    const char *id;
+    const EnumPropertyItem *items;
+  } enum_info[] = {
+#include "RNA_enum_items.h"
+  };
+  PyObject *result = _PyDict_NewPresized(ARRAY_SIZE(enum_info));
+  for (int i = 0; i < ARRAY_SIZE(enum_info); i++) {
+    /* Include all items (including headings & separators), can be shown in documentation. */
+    const EnumPropertyItem *items = enum_info[i].items;
+    const int items_count = RNA_enum_items_count(items);
+    PyObject *value = PyTuple_New(items_count);
+    for (int item_index = 0; item_index < items_count; item_index++) {
+      PointerRNA ptr;
+      RNA_pointer_create(NULL, &RNA_EnumPropertyItem, (void *)&items[item_index], &ptr);
+      PyTuple_SET_ITEM(value, item_index, pyrna_struct_CreatePyObject(&ptr));
+    }
+    PyDict_SetItemString(result, enum_info[i].id, value);
+    Py_DECREF(value);
+  }
+  return result;
+}
+
 static PyMethodDef meth_bpy_script_paths = {
     "script_paths",
     (PyCFunction)bpy_script_paths,
@@ -510,6 +546,12 @@ static PyMethodDef meth_bpy_context_members = {
     METH_NOARGS,
     bpy_context_members_doc,
 };
+static PyMethodDef meth_bpy_rna_enum_items_static = {
+    "rna_enum_items_static",
+    (PyCFunction)bpy_rna_enum_items_static,
+    METH_NOARGS,
+    bpy_rna_enum_items_static_doc,
+};
 
 static PyObject *bpy_import_test(const char *modname)
 {
@@ -616,6 +658,9 @@ void BPy_init_modules(struct bContext *C)
   PyModule_AddObject(mod,
                      meth_bpy_context_members.ml_name,
                      (PyObject *)PyCFunction_New(&meth_bpy_context_members, NULL));
+  PyModule_AddObject(mod,
+                     meth_bpy_rna_enum_items_static.ml_name,
+                     (PyObject *)PyCFunction_New(&meth_bpy_rna_enum_items_static, NULL));
 
   /* register funcs (bpy_rna.c) */
   PyModule_AddObject(mod,



More information about the Bf-blender-cvs mailing list