diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-10-31 23:07:25 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-10-31 23:07:25 +0400 |
commit | c7fbeded4c6cce637f4d64c366d12070d15d2f0c (patch) | |
tree | fe1364dcc4b054865b6e7b38f029e1ed54ae920d | |
parent | ae097e72a35f4cff8eaa242b7b17f596c2051907 (diff) |
add IDP_MergeGroup(dst, src, overwrite) function,
like PyDict_Merge()
-rw-r--r-- | source/blender/blenkernel/BKE_idprop.h | 6 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/idprop.c | 24 | ||||
-rw-r--r-- | source/blender/python/generic/idprop_py_api.c | 27 | ||||
-rw-r--r-- | source/blender/python/generic/idprop_py_api.h | 11 |
4 files changed, 58 insertions, 10 deletions
diff --git a/source/blender/blenkernel/BKE_idprop.h b/source/blender/blenkernel/BKE_idprop.h index 3cf4a2c5cdc..027f06ef84f 100644 --- a/source/blender/blenkernel/BKE_idprop.h +++ b/source/blender/blenkernel/BKE_idprop.h @@ -154,6 +154,12 @@ __attribute__((nonnull)) #endif ; +void IDP_MergeGroup(IDProperty *dest, IDProperty *src, const int do_overwrite) +#ifdef __GNUC__ +__attribute__((nonnull)) +#endif +; + /** * This function has a sanity check to make sure ID properties with the same name don't * get added to the group. diff --git a/source/blender/blenkernel/intern/idprop.c b/source/blender/blenkernel/intern/idprop.c index 8ceaab56f83..88f7d8f6191 100644 --- a/source/blender/blenkernel/intern/idprop.c +++ b/source/blender/blenkernel/intern/idprop.c @@ -492,6 +492,30 @@ void IDP_ReplaceInGroup(IDProperty *group, IDProperty *prop) } } +/* + * If a property is missing in \a dest, add it. + */ +void IDP_MergeGroup(IDProperty *dest, IDProperty *src, const int do_overwrite) +{ + IDProperty *prop; + + if (do_overwrite) { + for (prop = src->data.group.first; prop; prop = prop->next) { + IDProperty *copy = IDP_CopyProperty(prop); + IDP_ReplaceInGroup(dest, copy); + } + } + else { + for (prop = src->data.group.first; prop; prop = prop->next) { + if (IDP_GetPropertyFromGroup(dest, prop->name) == NULL) { + IDProperty *copy = IDP_CopyProperty(prop); + dest->len++; + BLI_addtail(&dest->data.group, copy); + } + } + } +} + /* returns 0 if an id property with the same name exists and it failed, * or 1 if it succeeded in adding to the group.*/ int IDP_AddToGroup(IDProperty *group, IDProperty *prop) diff --git a/source/blender/python/generic/idprop_py_api.c b/source/blender/python/generic/idprop_py_api.c index d4ec8137399..05b37514e20 100644 --- a/source/blender/python/generic/idprop_py_api.c +++ b/source/blender/python/generic/idprop_py_api.c @@ -43,10 +43,6 @@ #include "py_capi_utils.h" #endif -extern PyTypeObject BPy_IDArray_Type; -extern PyTypeObject BPy_IDGroup_Iter_Type; -extern PyTypeObject BPy_IDGroup_Type; - /*********************** ID Property Main Wrapper Stuff ***************/ /* ---------------------------------------------------------------------------- @@ -809,17 +805,28 @@ static PyObject *BPy_IDGroup_Update(BPy_IDProperty *self, PyObject *value) PyObject *pkey, *pval; Py_ssize_t i = 0; - if (!PyDict_Check(value)) { + if (BPy_IDGroup_Check(value)) { + BPy_IDProperty *other = (BPy_IDProperty *)value; + if (UNLIKELY(self->prop == other->prop)) { + Py_RETURN_NONE; + } + + /* XXX, possible one is inside the other */ + IDP_MergeGroup(self->prop, other->prop, TRUE); + } + else if (PyDict_Check(value)) { + while (PyDict_Next(value, &i, &pkey, &pval)) { + BPy_IDGroup_Map_SetItem(self, pkey, pval); + if (PyErr_Occurred()) return NULL; + } + } + else { PyErr_Format(PyExc_TypeError, - "expected a dict not a %.200s", + "expected a dict or an IDPropertyGroup type, not a %.200s", Py_TYPE(value)->tp_name); return NULL; } - while (PyDict_Next(value, &i, &pkey, &pval)) { - BPy_IDGroup_Map_SetItem(self, pkey, pval); - if (PyErr_Occurred()) return NULL; - } Py_RETURN_NONE; } diff --git a/source/blender/python/generic/idprop_py_api.h b/source/blender/python/generic/idprop_py_api.h index 99e291f69c0..cb82676c4d9 100644 --- a/source/blender/python/generic/idprop_py_api.h +++ b/source/blender/python/generic/idprop_py_api.h @@ -32,6 +32,17 @@ struct ID; struct IDProperty; struct BPy_IDGroup_Iter; +extern PyTypeObject BPy_IDArray_Type; +extern PyTypeObject BPy_IDGroup_Iter_Type; +extern PyTypeObject BPy_IDGroup_Type; + +#define BPy_IDArray_Check(v) (PyObject_TypeCheck(v, &BPy_IDArray_Type)) +#define BPy_IDArray_CheckExact(v) (Py_TYPE(v) == &BPy_IDArray_Type) +#define BPy_IDGroup_Iter_Check(v) (PyObject_TypeCheck(v, &BPy_IDGroup_Iter_Type)) +#define BPy_IDGroup_Iter_CheckExact(v) (Py_TYPE(v) == &BPy_IDGroup_Iter_Type) +#define BPy_IDGroup_Check(v) (PyObject_TypeCheck(v, &BPy_IDGroup_Type)) +#define BPy_IDGroup_CheckExact(v) (Py_TYPE(v) == &BPy_IDGroup_Type) + typedef struct BPy_IDProperty { PyObject_VAR_HEAD struct ID *id; /* can be NULL */ |