diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2009-07-10 23:56:13 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2009-07-10 23:56:13 +0400 |
commit | 2e3e044d27e90dc87bdce6af9cef77d9543e4d89 (patch) | |
tree | 4d87ed9c283fd99baf9d23d4b8ace0868972f156 /source/blender/python/intern | |
parent | a95c68a3eaa692f742e7f2e626b65daa71727c17 (diff) |
RNA
* Enums can now be dynamically created in the _itemf callback,
using RNA_enum_item(s)_add, RNA_enum_item_end. All places asking
for enum items now need to potentially free the items.
* This callback now also gets context, this was added specifically
for operators. This doesn't fit design well at all, needed to do
some ugly hacks, but can't find a good solution at the moment.
* All enums must have a default list of items too, even with an
_itemf callback, for docs and fallback in case there is no context.
* Used by MESH_OT_merge, MESH_OT_select_similar, TFM_OT_select_orientation.
* Also changes some operator properties that were enums to booleas
(unselected, deselect), to make them consistent with other ops.
Diffstat (limited to 'source/blender/python/intern')
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 34 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_util.c | 3 |
2 files changed, 26 insertions, 11 deletions
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index c6fbda0caef..49bca247431 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -223,10 +223,16 @@ static void pyrna_struct_dealloc( BPy_StructRNA * self ) static char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop) { - const EnumPropertyItem *item; + EnumPropertyItem *item; + char *result; + int free; - RNA_property_enum_items(ptr, prop, &item, NULL); - return (char*)BPy_enum_as_string((EnumPropertyItem*)item); + RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free); + result= (char*)BPy_enum_as_string(item); + if(free) + MEM_freeN(item); + + return result; } PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) @@ -309,14 +315,15 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) const char *identifier; int val = RNA_property_enum_get(ptr, prop); - if (RNA_property_enum_identifier(ptr, prop, val, &identifier)) { + if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) { ret = PyUnicode_FromString( identifier ); } else { - const EnumPropertyItem *item; + EnumPropertyItem *item; + int free; /* don't throw error here, can't trust blender 100% to give the * right values, python code should not generate error for that */ - RNA_property_enum_items(ptr, prop, &item, NULL); + RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free); if(item->identifier) { ret = PyUnicode_FromString( item->identifier ); } @@ -329,6 +336,9 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) ret = PyUnicode_FromString( "" ); } + if(free) + MEM_freeN(item); + /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val); ret = NULL;*/ } @@ -626,7 +636,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v return -1; } else { int val; - if (RNA_property_enum_value(ptr, prop, param, &val)) { + if (RNA_property_enum_value(BPy_GetContext(), ptr, prop, param, &val)) { if(data) *((int*)data)= val; else RNA_property_enum_set(ptr, prop, val); } else { @@ -1818,19 +1828,23 @@ PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data) const char *identifier; int val = *(int*)data; - if (RNA_property_enum_identifier(ptr, prop, val, &identifier)) { + if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) { ret = PyUnicode_FromString( identifier ); } else { - const EnumPropertyItem *item; + EnumPropertyItem *item; + int free; /* don't throw error here, can't trust blender 100% to give the * right values, python code should not generate error for that */ - RNA_property_enum_items(ptr, prop, &item, NULL); + RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free); if(item[0].identifier) ret = PyUnicode_FromString( item[0].identifier ); else ret = PyUnicode_FromString( "" ); + if(free) + MEM_freeN(item); + /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val); ret = NULL;*/ } diff --git a/source/blender/python/intern/bpy_util.c b/source/blender/python/intern/bpy_util.c index bce73b903c0..9f1ef0c6251 100644 --- a/source/blender/python/intern/bpy_util.c +++ b/source/blender/python/intern/bpy_util.c @@ -412,7 +412,8 @@ char *BPy_enum_as_string(EnumPropertyItem *item) char *cstring; for (e= item; item->identifier; item++) { - BLI_dynstr_appendf(dynstr, (e==item)?"'%s'":", '%s'", item->identifier); + if(item->identifier[0]) + BLI_dynstr_appendf(dynstr, (e==item)?"'%s'":", '%s'", item->identifier); } cstring = BLI_dynstr_get_cstring(dynstr); |