/* Given an array property, creates an N-dimensional tuple of values. */ static PyObject *pyrna_py_from_array_internal(PointerRNA *ptr, PropertyRNA *prop, int dim, int *index) { PyObject *tuple; int i, len; int totdim = RNA_property_array_dimension(ptr, prop, NULL); len = RNA_property_multi_array_length(ptr, prop, dim); tuple = PyTuple_New(len); for (i = 0; i < len; i++) { PyObject *item; if (dim + 1 < totdim) item = pyrna_py_from_array_internal(ptr, prop, dim + 1, index); else { item = pyrna_array_index(ptr, prop, *index); *index = *index + 1; } if (!item) { Py_DECREF(tuple); return NULL; } PyTuple_SET_ITEM(tuple, i, item); } return tuple; }
PyObject *pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index) { int totdim, arraydim, arrayoffset, dimsize[MAX_ARRAY_DIMENSION], i, len; BPy_PropertyArrayRNA *ret = NULL; arraydim = self ? self->arraydim : 0; arrayoffset = self ? self->arrayoffset : 0; /* just in case check */ len = RNA_property_multi_array_length(ptr, prop, arraydim); if (index >= len || index < 0) { /* this shouldn't happen because higher level funcs must check for invalid index */ if (G.debug & G_DEBUG_PYTHON) printf("%s: invalid index %d for array with length=%d\n", __func__, index, len); PyErr_SetString(PyExc_IndexError, "out of range"); return NULL; } totdim = RNA_property_array_dimension(ptr, prop, dimsize); if (arraydim + 1 < totdim) { ret = (BPy_PropertyArrayRNA *)pyrna_prop_CreatePyObject(ptr, prop); ret->arraydim = arraydim + 1; /* arr[3][4][5] * * x = arr[2] * index = 0 + 2 * 4 * 5 * * x = arr[2][3] * index = offset + 3 * 5 */ for (i = arraydim + 1; i < totdim; i++) index *= dimsize[i]; ret->arrayoffset = arrayoffset + index; } else { index = arrayoffset + index; ret = (BPy_PropertyArrayRNA *)pyrna_array_index(ptr, prop, index); } return (PyObject *)ret; }