From ed2a3454021c881c4955d6022761aa4b9b45af30 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 31 May 2022 14:07:08 +1000 Subject: 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). --- source/blender/python/intern/bpy.c | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'source/blender/python') 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, -- cgit v1.2.3