diff options
author | Campbell Barton <ideasman42@gmail.com> | 2008-12-02 17:36:35 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2008-12-02 17:36:35 +0300 |
commit | 9f27be3b2dcf4cd9abfacaca0871340811586664 (patch) | |
tree | 373df23dcf530e9840f4ba0cf1dbc6e1c4ca7c09 /source | |
parent | 54908979c54ab332156f438e995d5dce8ee8420c (diff) |
Added RNA functions from PyRNA
* RNA_property_enum_value
* RNA_property_enum_identifier
To get an enum string from a value and a value from an enum.
BPy_StructRNA types (objects, meshes, images etc) can now be used as dictionary keys.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/makesrna/RNA_access.h | 3 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_access.c | 35 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 44 |
3 files changed, 53 insertions, 29 deletions
diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 3b685792d33..8c296dce5f2 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -174,6 +174,9 @@ void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *soft int RNA_property_string_maxlength(PointerRNA *ptr, PropertyRNA *prop); void RNA_property_enum_items(PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **item, int *totitem); +int RNA_property_enum_value(PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value); +int RNA_property_enum_identifier(PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier); + const char *RNA_property_ui_name(PointerRNA *ptr, PropertyRNA *prop); const char *RNA_property_ui_description(PointerRNA *ptr, PropertyRNA *prop); diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 9f07d2a3311..2c9cb9f4dfa 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -401,6 +401,40 @@ void RNA_property_enum_items(PointerRNA *ptr, PropertyRNA *prop, const EnumPrope *totitem= eprop->totitem; } +int RNA_property_enum_value(PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value) +{ + const EnumPropertyItem *item; + int totitem, i; + + RNA_property_enum_items(ptr, prop, &item, &totitem); + + for (i=0; i<totitem; i++) { + if (strcmp(item[i].identifier, identifier)==0) { + *value = item[i].value; + return 1; + } + } + + return 0; +} + +int RNA_property_enum_identifier(PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier) +{ + const EnumPropertyItem *item; + int totitem, i; + + RNA_property_enum_items(ptr, prop, &item, &totitem); + + for (i=0; i<totitem; i++) { + if (item[i].value==value) { + *identifier = item[i].identifier; + return 1; + } + } + + return 0; +} + const char *RNA_property_ui_name(PointerRNA *ptr, PropertyRNA *prop) { PropertyRNA *oldprop= prop; @@ -764,6 +798,7 @@ int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop) return eprop->defaultvalue; } + void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value) { EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop; diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 5f9a5b195c0..ab95c01cc74 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -54,6 +54,12 @@ static PyObject *pyrna_prop_repr( BPy_PropertyRNA * self ) return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\" ]", RNA_struct_identifier(&self->ptr), RNA_property_identifier(&self->ptr, self->prop) ); } +static long pyrna_struct_hash( BPy_StructRNA * self ) +{ + return (long)self->ptr.data; +} + + static PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) { PyObject *ret; @@ -86,26 +92,17 @@ static PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) break; } case PROP_ENUM: - { - const EnumPropertyItem *item; - int totitem, i, val; - - val = RNA_property_enum_get(ptr, prop); - - RNA_property_enum_items(ptr, prop, &item, &totitem); - - for (i=0; i<totitem; i++) { - if (item[i].value == val) - break; - } + { + const char *identifier; + int val = RNA_property_enum_get(ptr, prop); - if (i<totitem) { - ret = PyUnicode_FromString( item[i].identifier ); + if (RNA_property_enum_identifier(ptr, prop, val, &identifier)) { + ret = PyUnicode_FromString( identifier ); } else { PyErr_Format(PyExc_AttributeError, "enum \"%d\" not found", val); ret = NULL; } - + break; } case PROP_POINTER: @@ -287,19 +284,8 @@ static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, PyObject *value) PyErr_SetString(PyExc_TypeError, "expected a string type"); return -1; } else { - const EnumPropertyItem *item; - int totitem, i, val; - - RNA_property_enum_items(ptr, prop, &item, &totitem); - - for (i=0; i<totitem; i++) { - if (strcmp(item[i].identifier, param)==0) { - val = item[i].value; - break; - } - } - - if (i<totitem) { + int val; + if (RNA_property_enum_value(ptr, prop, param, &val)) { RNA_property_enum_set(ptr, prop, val); } else { PyErr_Format(PyExc_AttributeError, "enum \"%s\" not found", param); @@ -926,7 +912,7 @@ PyTypeObject pyrna_struct_Type = { /* More standard operations (here for binary compatibility) */ - NULL, /* hashfunc tp_hash; */ + ( hashfunc )pyrna_struct_hash, /* hashfunc tp_hash; */ NULL, /* ternaryfunc tp_call; */ NULL, /* reprfunc tp_str; */ ( getattrofunc ) pyrna_struct_getattro, /* getattrofunc tp_getattro; */ |