diff options
Diffstat (limited to 'source/blender/python/intern/bpy_rna.c')
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 412 |
1 files changed, 229 insertions, 183 deletions
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 3902a9fd9b5..c2335bea995 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -130,6 +130,70 @@ Mathutils_Callback mathutils_rna_matrix_cb = { (BaseMathSetIndexFunc) NULL }; +PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop) +{ + PyObject *ret= NULL; + +#ifdef USE_MATHUTILS + int type, subtype, totdim; + int len; + + len= RNA_property_array_length(ptr, prop); + type= RNA_property_type(prop); + subtype= RNA_property_subtype(prop); + totdim= RNA_property_array_dimension(ptr, prop, NULL); + + if (type != PROP_FLOAT) return NULL; + + if (totdim == 1 || (totdim == 2 && subtype == PROP_MATRIX)) { + ret = pyrna_prop_CreatePyObject(ptr, prop); + + switch(RNA_property_subtype(prop)) { + case PROP_TRANSLATION: + case PROP_DIRECTION: + case PROP_VELOCITY: + case PROP_ACCELERATION: + case PROP_XYZ: + if(len>=2 && len <= 4) { + PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_array_cb_index, FALSE); + Py_DECREF(ret); /* the vector owns now */ + ret= vec_cb; /* return the vector instead */ + } + break; + case PROP_MATRIX: + if(len==16) { + PyObject *mat_cb= newMatrixObject_cb(ret, 4,4, mathutils_rna_matrix_cb_index, FALSE); + Py_DECREF(ret); /* the matrix owns now */ + ret= mat_cb; /* return the matrix instead */ + } + else if (len==9) { + PyObject *mat_cb= newMatrixObject_cb(ret, 3,3, mathutils_rna_matrix_cb_index, FALSE); + Py_DECREF(ret); /* the matrix owns now */ + ret= mat_cb; /* return the matrix instead */ + } + break; + case PROP_EULER: + case PROP_QUATERNION: + if(len==3) { /* euler */ + PyObject *eul_cb= newEulerObject_cb(ret, mathutils_rna_array_cb_index, FALSE); + Py_DECREF(ret); /* the matrix owns now */ + ret= eul_cb; /* return the matrix instead */ + } + else if (len==4) { + PyObject *quat_cb= newQuaternionObject_cb(ret, mathutils_rna_array_cb_index, FALSE); + Py_DECREF(ret); /* the matrix owns now */ + ret= quat_cb; /* return the matrix instead */ + } + break; + default: + break; + } + } +#endif + + return ret; +} + #endif static StructRNA *pyrna_struct_as_srna(PyObject *self); @@ -144,25 +208,64 @@ static int pyrna_prop_compare( BPy_PropertyRNA * a, BPy_PropertyRNA * b ) return (a->prop==b->prop && a->ptr.data==b->ptr.data ) ? 0 : -1; } -/* For some reason python3 needs these :/ */ -static PyObject *pyrna_struct_richcmp(BPy_StructRNA * a, BPy_StructRNA * b, int op) +static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op) { - int cmp_result= -1; /* assume false */ - if (BPy_StructRNA_Check(a) && BPy_StructRNA_Check(b)) { - cmp_result= pyrna_struct_compare(a, b); + PyObject *res; + int ok= -1; /* zero is true */ + + if (BPy_StructRNA_Check(a) && BPy_StructRNA_Check(b)) + ok= pyrna_struct_compare((BPy_StructRNA *)a, (BPy_StructRNA *)b); + + switch (op) { + case Py_NE: + ok = !ok; /* pass through */ + case Py_EQ: + res = ok ? Py_False : Py_True; + break; + + case Py_LT: + case Py_LE: + case Py_GT: + case Py_GE: + res = Py_NotImplemented; + break; + default: + PyErr_BadArgument(); + return NULL; } - return Py_CmpToRich(op, cmp_result); + Py_INCREF(res); + return res; } -static PyObject *pyrna_prop_richcmp(BPy_PropertyRNA * a, BPy_PropertyRNA * b, int op) +static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op) { - int cmp_result= -1; /* assume false */ - if (BPy_PropertyRNA_Check(a) && BPy_PropertyRNA_Check(b)) { - cmp_result= pyrna_prop_compare(a, b); + PyObject *res; + int ok= -1; /* zero is true */ + + if (BPy_PropertyRNA_Check(a) && BPy_PropertyRNA_Check(b)) + ok= pyrna_prop_compare((BPy_PropertyRNA *)a, (BPy_PropertyRNA *)b); + + switch (op) { + case Py_NE: + ok = !ok; /* pass through */ + case Py_EQ: + res = ok ? Py_False : Py_True; + break; + + case Py_LT: + case Py_LE: + case Py_GT: + case Py_GE: + res = Py_NotImplemented; + break; + default: + PyErr_BadArgument(); + return NULL; } - return Py_CmpToRich(op, cmp_result); + Py_INCREF(res); + return res; } /*----------------------repr--------------------------------------------*/ @@ -246,61 +349,9 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) { PyObject *ret; int type = RNA_property_type(prop); - int len = RNA_property_array_length(ptr, prop); - - if (len > 0) { - /* resolve the array from a new pytype */ - PyObject *ret = pyrna_prop_CreatePyObject(ptr, prop); - -#ifdef USE_MATHUTILS - /* return a mathutils vector where possible */ - if(RNA_property_type(prop)==PROP_FLOAT) { - switch(RNA_property_subtype(prop)) { - case PROP_TRANSLATION: - case PROP_DIRECTION: - case PROP_VELOCITY: - case PROP_ACCELERATION: - case PROP_XYZ: - if(len>=2 && len <= 4) { - PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_array_cb_index, FALSE); - Py_DECREF(ret); /* the vector owns now */ - ret= vec_cb; /* return the vector instead */ - } - break; - case PROP_MATRIX: - if(len==16) { - PyObject *mat_cb= newMatrixObject_cb(ret, 4,4, mathutils_rna_matrix_cb_index, FALSE); - Py_DECREF(ret); /* the matrix owns now */ - ret= mat_cb; /* return the matrix instead */ - } - else if (len==9) { - PyObject *mat_cb= newMatrixObject_cb(ret, 3,3, mathutils_rna_matrix_cb_index, FALSE); - Py_DECREF(ret); /* the matrix owns now */ - ret= mat_cb; /* return the matrix instead */ - } - break; - case PROP_EULER: - case PROP_QUATERNION: - if(len==3) { /* euler */ - PyObject *eul_cb= newEulerObject_cb(ret, mathutils_rna_array_cb_index, FALSE); - Py_DECREF(ret); /* the matrix owns now */ - ret= eul_cb; /* return the matrix instead */ - } - else if (len==4) { - PyObject *quat_cb= newQuaternionObject_cb(ret, mathutils_rna_array_cb_index, FALSE); - Py_DECREF(ret); /* the matrix owns now */ - ret= quat_cb; /* return the matrix instead */ - } - break; - default: - break; - } - } - -#endif - - return ret; + if (RNA_property_array_check(ptr, prop)) { + return pyrna_py_from_array(ptr, prop); } /* see if we can coorce into a python type - PropertyType */ @@ -469,10 +520,11 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v { /* XXX hard limits should be checked here */ int type = RNA_property_type(prop); - int len = RNA_property_array_length(ptr, prop); - if (len > 0) { - char error_str[512]; + + if (RNA_property_array_check(ptr, prop)) { + + /* char error_str[512]; */ int ok= 1; #ifdef USE_MATHUTILS @@ -487,21 +539,10 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v return -1; } /* done getting the length */ - - /* for arrays we have a limited number of types */ - switch (type) { - case PROP_BOOLEAN: - ok= pyrna_py_to_boolean_array(value, ptr, prop, data, error_str, sizeof(error_str)); - break; - case PROP_INT: - ok= pyrna_py_to_int_array(value, ptr, prop, data, error_str, sizeof(error_str)); - break; - case PROP_FLOAT: - ok= pyrna_py_to_float_array(value, ptr, prop, data, error_str, sizeof(error_str)); - break; - } + ok= pyrna_py_to_array(ptr, prop, data, value, error_prefix); + if (!ok) { - PyErr_Format(PyExc_AttributeError, "%.200s %s", error_prefix, error_str); + /* PyErr_Format(PyExc_AttributeError, "%.200s %s", error_prefix, error_str); */ return -1; } } @@ -694,95 +735,95 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v return 0; } -static PyObject * pyrna_prop_to_py_index(PointerRNA *ptr, PropertyRNA *prop, int index) +static PyObject * pyrna_prop_to_py_index(BPy_PropertyRNA *self, int index) { - PyObject *ret; - int type = RNA_property_type(prop); - - /* see if we can coorce into a python type - PropertyType */ - switch (type) { - case PROP_BOOLEAN: - ret = PyBool_FromLong( RNA_property_boolean_get_index(ptr, prop, index) ); - break; - case PROP_INT: - ret = PyLong_FromSsize_t( (Py_ssize_t)RNA_property_int_get_index(ptr, prop, index) ); - break; - case PROP_FLOAT: - ret = PyFloat_FromDouble( RNA_property_float_get_index(ptr, prop, index) ); - break; - default: - PyErr_SetString(PyExc_AttributeError, "not an array type"); - ret = NULL; - break; - } - - return ret; + return pyrna_py_from_array_index(self, index); } -static int pyrna_py_to_prop_index(PointerRNA *ptr, PropertyRNA *prop, int index, PyObject *value) +static int pyrna_py_to_prop_index(BPy_PropertyRNA *self, int index, PyObject *value) { int ret = 0; + int totdim; + PointerRNA *ptr= &self->ptr; + PropertyRNA *prop= self->prop; int type = RNA_property_type(prop); - - /* see if we can coorce into a python type - PropertyType */ - switch (type) { - case PROP_BOOLEAN: - { - int param = PyObject_IsTrue( value ); - - if( param < 0 ) { - PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1"); - ret = -1; - } else { - RNA_property_boolean_set_index(ptr, prop, index, param); - } - break; - } - case PROP_INT: - { - int param = PyLong_AsSsize_t(value); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "expected an int type"); - ret = -1; - } else { - RNA_property_int_set_index(ptr, prop, index, param); + + totdim= RNA_property_array_dimension(ptr, prop, NULL); + + if (totdim > 1) { + /* char error_str[512]; */ + if (!pyrna_py_to_array_index(&self->ptr, self->prop, self->arraydim, self->arrayoffset, index, value, "")) { + /* PyErr_SetString(PyExc_AttributeError, error_str); */ + ret= -1; } - break; } - case PROP_FLOAT: - { - float param = PyFloat_AsDouble(value); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "expected a float type"); + else { + /* see if we can coorce into a python type - PropertyType */ + switch (type) { + case PROP_BOOLEAN: + { + int param = PyObject_IsTrue( value ); + + if( param < 0 ) { + PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1"); + ret = -1; + } else { + RNA_property_boolean_set_index(ptr, prop, index, param); + } + break; + } + case PROP_INT: + { + int param = PyLong_AsSsize_t(value); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "expected an int type"); + ret = -1; + } else { + RNA_property_int_set_index(ptr, prop, index, param); + } + break; + } + case PROP_FLOAT: + { + float param = PyFloat_AsDouble(value); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "expected a float type"); + ret = -1; + } else { + RNA_property_float_set_index(ptr, prop, index, param); + } + break; + } + default: + PyErr_SetString(PyExc_AttributeError, "not an array type"); ret = -1; - } else { - RNA_property_float_set_index(ptr, prop, index, param); + break; } - break; - } - default: - PyErr_SetString(PyExc_AttributeError, "not an array type"); - ret = -1; - break; } return ret; } //---------------sequence------------------------------------------- +static int pyrna_prop_array_length(BPy_PropertyRNA *self) +{ + if (RNA_property_array_dimension(&self->ptr, self->prop, NULL) > 1) + return RNA_property_multi_array_length(&self->ptr, self->prop, self->arraydim); + else + return RNA_property_array_length(&self->ptr, self->prop); +} + static Py_ssize_t pyrna_prop_len( BPy_PropertyRNA * self ) { Py_ssize_t len; if (RNA_property_type(self->prop) == PROP_COLLECTION) { len = RNA_property_collection_length(&self->ptr, self->prop); + } else if (RNA_property_array_check(&self->ptr, self->prop)) { + len = pyrna_prop_array_length(self); } else { - len = RNA_property_array_length(&self->ptr, self->prop); - - if (len==0) { /* not an array*/ - PyErr_SetString(PyExc_AttributeError, "len() only available for collection RNA types"); - return -1; - } + PyErr_SetString(PyExc_AttributeError, "len() only available for collection and array RNA types"); + len = -1; /* error value */ } return len; @@ -801,14 +842,15 @@ static PyObject *prop_subscript_collection_int(BPy_PropertyRNA * self, int keynu PyErr_Format(PyExc_IndexError, "index %d out of range", keynum); return NULL; } + static PyObject *prop_subscript_array_int(BPy_PropertyRNA * self, int keynum) { - int len= RNA_property_array_length(&self->ptr, self->prop); + int len= pyrna_prop_array_length(self); if(keynum < 0) keynum += len; if(keynum >= 0 && keynum < len) - return pyrna_prop_to_py_index(&self->ptr, self->prop, keynum); + return pyrna_prop_to_py_index(self, keynum); PyErr_Format(PyExc_IndexError, "index %d out of range", keynum); return NULL; @@ -855,7 +897,7 @@ static PyObject *prop_subscript_array_slice(BPy_PropertyRNA * self, int start, i start = MIN2(start,stop); /* values are clamped from PySlice_GetIndicesEx */ for(count = start; count < stop; count++) - PyList_SetItem(list, count - start, pyrna_prop_to_py_index(&self->ptr, self->prop, count)); + PyList_SetItem(list, count - start, pyrna_prop_to_py_index(self, count)); return list; } @@ -908,8 +950,8 @@ static PyObject *prop_subscript_array(BPy_PropertyRNA * self, PyObject *key) return prop_subscript_array_int(self, PyLong_AsSsize_t(key)); } else if (PySlice_Check(key)) { - int len= RNA_property_array_length(&self->ptr, self->prop); Py_ssize_t start, stop, step, slicelength; + int len = pyrna_prop_array_length(self); if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0) return NULL; @@ -935,13 +977,12 @@ static PyObject *pyrna_prop_subscript( BPy_PropertyRNA * self, PyObject *key ) { if (RNA_property_type(self->prop) == PROP_COLLECTION) { return prop_subscript_collection(self, key); - } else if (RNA_property_array_length(&self->ptr, self->prop)) { /* arrays are currently fixed length, zero length means its not an array */ + } else if (RNA_property_array_check(&self->ptr, self->prop)) { return prop_subscript_array(self, key); - } else { - PyErr_SetString(PyExc_TypeError, "rna type is not an array or a collection"); - return NULL; - } + } + PyErr_SetString(PyExc_TypeError, "rna type is not an array or a collection"); + return NULL; } static int prop_subscript_ass_array_slice(BPy_PropertyRNA * self, int begin, int end, PyObject *value) @@ -952,7 +993,7 @@ static int prop_subscript_ass_array_slice(BPy_PropertyRNA * self, int begin, int begin = MIN2(begin,end); for(count = begin; count < end; count++) { - if(pyrna_py_to_prop_index(&self->ptr, self->prop, count - begin, value) == -1) { + if(pyrna_py_to_prop_index(self, count - begin, value) == -1) { /* TODO - this is wrong since some values have been assigned... will need to fix that */ return -1; /* pyrna_struct_CreatePyObject should set the error */ } @@ -963,13 +1004,12 @@ static int prop_subscript_ass_array_slice(BPy_PropertyRNA * self, int begin, int static int prop_subscript_ass_array_int(BPy_PropertyRNA * self, int keynum, PyObject *value) { - - int len= RNA_property_array_length(&self->ptr, self->prop); + int len= pyrna_prop_array_length(self); if(keynum < 0) keynum += len; if(keynum >= 0 && keynum < len) - return pyrna_py_to_prop_index(&self->ptr, self->prop, keynum, value); + return pyrna_py_to_prop_index(self, keynum, value); PyErr_SetString(PyExc_IndexError, "out of range"); return -1; @@ -1639,32 +1679,32 @@ static PyObject *pyrna_prop_foreach_set(BPy_PropertyRNA *self, PyObject *args) PyObject *pyrna_prop_iter(BPy_PropertyRNA *self) { /* Try get values from a collection */ - PyObject *ret = pyrna_prop_values(self); + PyObject *ret; + PyObject *iter; - if (ret==NULL) { - /* collection did not work, try array */ - int len = RNA_property_array_length(&self->ptr, self->prop); + if(RNA_property_array_check(&self->ptr, self->prop)) { + int len = pyrna_prop_array_length(self); + int i; + PyErr_Clear(); + ret = PyList_New(len); - if (len) { - int i; - PyErr_Clear(); - ret = PyList_New(len); - - for (i=0; i < len; i++) { - PyList_SET_ITEM(ret, i, pyrna_prop_to_py_index(&self->ptr, self->prop, i)); - } + for (i=0; i < len; i++) { + PyList_SET_ITEM(ret, i, pyrna_prop_to_py_index(self, i)); } } - - if (ret) { - /* we know this is a list so no need to PyIter_Check */ - PyObject *iter = PyObject_GetIter(ret); - Py_DECREF(ret); - return iter; + else if ((ret = pyrna_prop_values(self))) { + /* do nothing */ + } + else { + PyErr_SetString( PyExc_TypeError, "this BPy_PropertyRNA object is not iterable" ); + return NULL; } - PyErr_SetString( PyExc_TypeError, "this BPy_PropertyRNA object is not iterable" ); - return NULL; + + /* we know this is a list so no need to PyIter_Check */ + iter = PyObject_GetIter(ret); + Py_DECREF(ret); + return iter; } static struct PyMethodDef pyrna_struct_methods[] = { @@ -1734,14 +1774,17 @@ PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data) { PyObject *ret; int type = RNA_property_type(prop); - int len = RNA_property_array_length(ptr, prop); int a; - if(len > 0) { + if(RNA_property_array_check(ptr, prop)) { + int len = RNA_property_array_length(ptr, prop); + /* resolve the array from a new pytype */ ret = PyTuple_New(len); + /* kazanbas: TODO make multidim sequences here */ + switch (type) { case PROP_BOOLEAN: for(a=0; a<len; a++) @@ -2406,6 +2449,9 @@ PyObject *pyrna_prop_CreatePyObject( PointerRNA *ptr, PropertyRNA *prop ) pyrna->ptr = *ptr; pyrna->prop = prop; + + pyrna->arraydim= 0; + pyrna->arrayoffset= 0; return ( PyObject * ) pyrna; } |