diff options
author | Hans Goudey <h.goudey@me.com> | 2021-07-14 17:51:28 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2021-07-14 17:51:28 +0300 |
commit | c202d3865904903a73a18822613f625a3bee344b (patch) | |
tree | a5c00dae340b6c3795d238acdd639428e3d28daa /source/blender/python/intern/bpy_rna.c | |
parent | 37a5ff4a8470a0040f88228bbf3d439c42389446 (diff) |
Python API: Add functions to ensure and clear IDProperties
This adds id_properties_clear() and id_properties_ensure() functions
to RNA structs. This is meant as an initial change based on discussion
in review of D9697. However, they may be useful in other situations.
The change requires refactoring the internal idproperties callback to
return a pointer to the IDProperty pointer, which actually turns out
to be quite a nice cleanup.
An id_properties attribute could be added in the future potentially.
Differential Revision: https://developer.blender.org/D11908
Diffstat (limited to 'source/blender/python/intern/bpy_rna.c')
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index e6479df2fa8..a8ab54ac3bb 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -4253,6 +4253,56 @@ static PyObject *pyrna_struct_dir(BPy_StructRNA *self) return ret; } +PyDoc_STRVAR(pyrna_struct_id_properties_ensure_doc, + ".. method:: id_properties_ensure()\n" + " :return: the parent group for an RNA struct's custom IDProperties.\n" + " :rtype: :class:`bpy.types.IDPropertyGroup`\n"); +static PyObject *pyrna_struct_id_properties_ensure(BPy_StructRNA *self) +{ + PYRNA_STRUCT_CHECK_OBJ(self); + + if (RNA_struct_idprops_check(self->ptr.type) == 0) { + PyErr_SetString(PyExc_TypeError, "This type doesn't support IDProperties"); + return NULL; + } + + IDProperty *idprops = RNA_struct_idprops(&self->ptr, true); + + /* This is a paranoid check that theoretically might not be necessary. + * It allows the possibility that some structs can't ensure IDProperties. */ + if (idprops == NULL) { + return Py_None; + } + + BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type); + group->owner_id = self->ptr.owner_id; + group->prop = idprops; + group->parent = NULL; + return (PyObject *)group; +} + +PyDoc_STRVAR(pyrna_struct_id_properties_clear_doc, + ".. method:: id_properties_clear()\n" + " :return: Remove the parent group for an RNA struct's custom IDProperties.\n"); +static PyObject *pyrna_struct_id_properties_clear(BPy_StructRNA *self) +{ + PYRNA_STRUCT_CHECK_OBJ(self); + + if (RNA_struct_idprops_check(self->ptr.type) == 0) { + PyErr_SetString(PyExc_TypeError, "This type doesn't support IDProperties"); + return NULL; + } + + IDProperty **idprops = RNA_struct_idprops_p(&self->ptr); + + if (*idprops) { + IDP_FreeProperty(*idprops); + *idprops = NULL; + } + + Py_RETURN_NONE; +} + /* ---------------getattr-------------------------------------------- */ static PyObject *pyrna_struct_getattro(BPy_StructRNA *self, PyObject *pyname) { @@ -5743,6 +5793,14 @@ static struct PyMethodDef pyrna_struct_methods[] = { METH_VARARGS | METH_CLASS, pyrna_struct_bl_rna_get_subclass_doc}, {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL}, + {"id_properties_ensure", + (PyCFunction)pyrna_struct_id_properties_ensure, + METH_NOARGS, + pyrna_struct_id_properties_ensure_doc}, + {"id_properties_clear", + (PyCFunction)pyrna_struct_id_properties_clear, + METH_NOARGS, + pyrna_struct_id_properties_clear_doc}, /* experimental */ /* unused for now */ |