diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-07-29 08:11:58 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-07-30 04:57:43 +0300 |
commit | ddcb6b1023bf3089cd6d2db3012b68b9ec2b980f (patch) | |
tree | 3b6f98987a5f8ab1b09edb628d2c346cb51fcf82 /source/blender/python/intern/bpy_rna.c | |
parent | 5280d4bf0bf6e76d370bdf17d85299bcec608607 (diff) |
Cleanup: replace macros with converter callbacks for bpy.props
Macros were used for expanding shared logic for some properties.
Replace this with Python converters & a funciton that handles
deferred registration.
Add generic converter functions for RNA enums:
- pyrna_enum_value_parse_string
- pyrna_enum_bitfield_parse_set
Diffstat (limited to 'source/blender/python/intern/bpy_rna.c')
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 7ecdbe4b4d9..e45cd02a5bf 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -817,6 +817,44 @@ int pyrna_enum_value_from_id(const EnumPropertyItem *item, return 0; } +/** + * Use with #PyArg_ParseTuple's `O&` formatting. + */ +int pyrna_enum_value_parse_string(PyObject *o, void *p) +{ + const char *identifier = PyUnicode_AsUTF8(o); + if (identifier == NULL) { + PyErr_Format(PyExc_TypeError, "expected a string enum, not %.200s", Py_TYPE(o)->tp_name); + return 0; + } + struct BPy_EnumProperty_Parse *parse_data = p; + if (pyrna_enum_value_from_id( + parse_data->items, identifier, &parse_data->value, "enum identifier") == -1) { + return 0; + } + parse_data->is_set = true; + return 1; +} + +/** + * Use with #PyArg_ParseTuple's `O&` formatting. + */ +int pyrna_enum_bitfield_parse_set(PyObject *o, void *p) +{ + if (!PySet_Check(o)) { + PyErr_Format(PyExc_TypeError, "expected a set, not %.200s", Py_TYPE(o)->tp_name); + return 0; + } + + struct BPy_EnumProperty_Parse *parse_data = p; + if (pyrna_set_to_enum_bitfield( + parse_data->items, o, &parse_data->value, "enum identifier set") == -1) { + return 0; + } + parse_data->is_set = true; + return 1; +} + /* NOTE(campbell): Regarding comparison `__cmp__`: * checking the 'ptr->data' matches works in almost all cases, * however there are a few RNA properties that are fake sub-structs and |