diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-12-08 12:40:30 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-12-08 12:40:30 +0300 |
commit | fccceaa87fb1e883c5d29113dc8b7d33786c8c1e (patch) | |
tree | e45312a32b8815807f968918044cc264443b8ab9 /source/blender/python | |
parent | c146ce977cea962e82f9b542bdf4d342d6ec0d2a (diff) |
- pyrna support for (value in array), currently only 1 dimensional arrays.
- use python malloc's in bpy_array.c
- automatically blending bone locations is disabled if the target bone has locked location
- neck had incorrect roll
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/intern/bpy_array.c | 97 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 35 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.h | 1 |
3 files changed, 115 insertions, 18 deletions
diff --git a/source/blender/python/intern/bpy_array.c b/source/blender/python/intern/bpy_array.c index f11c95e7ed5..5f228836b42 100644 --- a/source/blender/python/intern/bpy_array.c +++ b/source/blender/python/intern/bpy_array.c @@ -32,8 +32,6 @@ #include "BKE_global.h" -#include "MEM_guardedalloc.h" - #define MAX_ARRAY_DIMENSION 10 typedef void (*ItemConvertFunc)(PyObject *, char *); @@ -258,7 +256,7 @@ static int py_to_array(PyObject *py, PointerRNA *ptr, PropertyRNA *prop, char *p if (totitem) { if (!param_data || RNA_property_flag(prop) & PROP_DYNAMIC) - data= MEM_callocN(item_size * totitem, "pyrna primitive type array"); + data= PyMem_MALLOC(item_size * totitem); else data= param_data; @@ -273,7 +271,7 @@ static int py_to_array(PyObject *py, PointerRNA *ptr, PropertyRNA *prop, char *p else { /* NULL can only pass through in case RNA property arraylength is 0 (impossible?) */ rna_set_array(ptr, prop, data); - MEM_freeN(data); + PyMem_FREE(data); } } @@ -513,3 +511,94 @@ PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop) return pyrna_prop_CreatePyObject(ptr, prop); } + +/* TODO, multi-dimensional arrays */ +int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value) +{ + int len= RNA_property_array_length(ptr, prop); + int type; + int i; + + if(len==0) /* possible with dynamic arrays */ + return 0; + + if (RNA_property_array_dimension(ptr, prop, NULL) > 1) { + PyErr_SetString(PyExc_TypeError, "PropertyRNA - multi dimensional arrays not supported yet"); + return -1; + } + + type= RNA_property_type(prop); + + switch (type) { + case PROP_FLOAT: + { + float value_f= PyFloat_AsDouble(value); + if(value_f==-1 && PyErr_Occurred()) { + PyErr_Clear(); + return 0; + } + else { + float tmp[32]; + float *tmp_arr; + + if(len * sizeof(float) > sizeof(tmp)) { + tmp_arr= PyMem_MALLOC(len * sizeof(float)); + } + else { + tmp_arr= tmp; + } + + RNA_property_float_get_array(ptr, prop, tmp_arr); + + for(i=0; i<len; i++) + if(tmp_arr[i] == value_f) + break; + + if(tmp_arr != tmp) + PyMem_FREE(tmp_arr); + + return i<len ? 1 : 0; + } + break; + } + case PROP_BOOLEAN: + case PROP_INT: + { + int value_i= PyLong_AsSsize_t(value); + if(value_i==-1 && PyErr_Occurred()) { + PyErr_Clear(); + return 0; + } + else { + int tmp[32]; + int *tmp_arr; + + if(len * sizeof(int) > sizeof(tmp)) { + tmp_arr= PyMem_MALLOC(len * sizeof(int)); + } + else { + tmp_arr= tmp; + } + + if(type==PROP_BOOLEAN) + RNA_property_boolean_get_array(ptr, prop, tmp_arr); + else + RNA_property_int_get_array(ptr, prop, tmp_arr); + + for(i=0; i<len; i++) + if(tmp_arr[i] == value_i) + break; + + if(tmp_arr != tmp) + PyMem_FREE(tmp_arr); + + return i<len ? 1 : 0; + } + break; + } + } + + /* should never reach this */ + PyErr_SetString(PyExc_TypeError, "PropertyRNA - type not in float/bool/int"); + return -1; +} diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index f8e87912f1e..92bd686114d 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -1167,21 +1167,28 @@ static PyMappingMethods pyrna_prop_as_mapping = { static int pyrna_prop_contains(BPy_PropertyRNA *self, PyObject *value) { PointerRNA newptr; /* not used, just so RNA_property_collection_lookup_string runs */ - char *keyname = _PyUnicode_AsString(value); - if(keyname==NULL) { - PyErr_SetString(PyExc_TypeError, "PropertyRNA - key in prop, key must be a string type"); - return -1; - } + if (RNA_property_type(self->prop) == PROP_COLLECTION) { + /* key in dict style check */ + char *keyname = _PyUnicode_AsString(value); - if (RNA_property_type(self->prop) != PROP_COLLECTION) { - PyErr_SetString(PyExc_TypeError, "PropertyRNA - key in prop, is only valid for collection types"); - return -1; - } + if(keyname==NULL) { + PyErr_SetString(PyExc_TypeError, "PropertyRNA - key in prop, key must be a string type"); + return -1; + } - if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr)) - return 1; + if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr)) + return 1; + } + else if (RNA_property_array_check(&self->ptr, self->prop)) { + /* value in list style check */ + return pyrna_array_contains_py(&self->ptr, self->prop, value); + } + else { + PyErr_SetString(PyExc_TypeError, "PropertyRNA - type is not an array or a collection"); + return -1; + } return 0; } @@ -2264,6 +2271,9 @@ static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set) } } + if(array) + PyMem_Free(array); + if(PyErr_Occurred()) { /* Maybe we could make our own error */ PyErr_Print(); @@ -2275,9 +2285,6 @@ static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set) return NULL; } - if(array) - PyMem_Free(array); - Py_RETURN_NONE; } diff --git a/source/blender/python/intern/bpy_rna.h b/source/blender/python/intern/bpy_rna.h index 0e40bf7258c..37f6af36726 100644 --- a/source/blender/python/intern/bpy_rna.h +++ b/source/blender/python/intern/bpy_rna.h @@ -103,5 +103,6 @@ int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, in PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop); PyObject *pyrna_py_from_array_index(BPy_PropertyRNA *self, int index); PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop); +int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value); #endif |