* 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.
This commit is contained in:
2009-07-10 19:56:13 +00:00
parent a95c68a3ea
commit 2e3e044d27
28 changed files with 383 additions and 241 deletions

View File

@@ -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;*/
}

View File

@@ -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);