Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArystanbek Dyussenov <arystan.d@gmail.com>2009-09-06 19:13:57 +0400
committerArystanbek Dyussenov <arystan.d@gmail.com>2009-09-06 19:13:57 +0400
commit62138aaa5a7f931fa2ddb4815f755413111cceb0 (patch)
treed675dc3decf8f86ec0dd60a659976723784dd5f4 /source/blender/makesrna
parent3d64d65ad9c43f82bd4b9241b4a7fdde0fd12000 (diff)
Python part of multidim. array support for RNA complete.
Multidim. arrays can now be modified at any level, for example: struc.arrayprop = x struc.arrayprop[i] = x struc.arrayprop[i][j] = x struc.arrayprop[i][j][k] = x etc... Approriate rvalue type/length checking is done. To ensure all works correctly, I wrote automated tests in release/test/rna_array.py. These tests cover: array/item access, assignment on different levels, tests that proper exceptions are thrown on invalid item access/assignment. The tests use properties of the RNA Test struct defined in rna_test.c. This struct is only compiled when building with BF_UNIT_TEST=1 scons arg. Currently unit tests are run manually by loading the script in the Text Editor. Here's the output I have: http://www.pasteall.org/7644 Things to improve here: - better exception messages when multidim. array assignment fails. Those we have currently are not very useful for multidim. - add tests for slice assignment
Diffstat (limited to 'source/blender/makesrna')
-rw-r--r--source/blender/makesrna/RNA_access.h1
-rw-r--r--source/blender/makesrna/SConscript3
-rw-r--r--source/blender/makesrna/intern/SConscript3
-rw-r--r--source/blender/makesrna/intern/makesrna.c1
-rw-r--r--source/blender/makesrna/intern/rna_access.c33
-rw-r--r--source/blender/makesrna/intern/rna_define.c2
-rw-r--r--source/blender/makesrna/intern/rna_internal.h1
-rw-r--r--source/blender/makesrna/intern/rna_internal_types.h8
-rw-r--r--source/blender/makesrna/intern/rna_main.c24
-rw-r--r--source/blender/makesrna/intern/rna_mesh.c2
-rw-r--r--source/blender/makesrna/intern/rna_test.c188
11 files changed, 261 insertions, 5 deletions
diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h
index 98205d17ef3..4107c1346e5 100644
--- a/source/blender/makesrna/RNA_access.h
+++ b/source/blender/makesrna/RNA_access.h
@@ -586,6 +586,7 @@ PropertyUnit RNA_property_unit(PropertyRNA *prop);
int RNA_property_flag(PropertyRNA *prop);
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop);
+int RNA_property_multidimensional_array_length(PointerRNA *ptr, PropertyRNA *prop, int dimension);
int RNA_property_dynamic_array_set_length(PointerRNA *ptr, PropertyRNA *prop, int length);
char RNA_property_array_item_char(PropertyRNA *prop, int index);
unsigned short RNA_property_array_dimension(PropertyRNA *prop, unsigned short dim_size[]);
diff --git a/source/blender/makesrna/SConscript b/source/blender/makesrna/SConscript
index c2790927cef..845abf636e2 100644
--- a/source/blender/makesrna/SConscript
+++ b/source/blender/makesrna/SConscript
@@ -37,4 +37,7 @@ if env['WITH_BF_LCMS']:
if env['WITH_BF_GAMEENGINE']:
defs.append('GAMEBLENDER=1')
+if env['BF_UNIT_TEST']:
+ defs.append('UNIT_TEST')
+
env.BlenderLib ( 'bf_rna', objs, Split(incs), defines=defs, libtype=['core','player'], priority = [165,20] )
diff --git a/source/blender/makesrna/intern/SConscript b/source/blender/makesrna/intern/SConscript
index 9234efa2a5d..569f0547731 100644
--- a/source/blender/makesrna/intern/SConscript
+++ b/source/blender/makesrna/intern/SConscript
@@ -71,6 +71,9 @@ if env['WITH_BF_OPENAL']:
if env['WITH_BF_JACK']:
defs.append('WITH_JACK')
+if env['BF_UNIT_TEST']:
+ defs.append('UNIT_TEST')
+
makesrna_tool.Append(CPPDEFINES=defs)
makesrna_tool.Append (CPPPATH = Split(incs))
diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c
index bb7b6cbcd37..907eba4018f 100644
--- a/source/blender/makesrna/intern/makesrna.c
+++ b/source/blender/makesrna/intern/makesrna.c
@@ -1991,6 +1991,7 @@ RNAProcessItem PROCESS_ITEMS[]= {
{"rna_sequence.c", NULL, RNA_def_sequence},
{"rna_smoke.c", NULL, RNA_def_smoke},
{"rna_space.c", NULL, RNA_def_space},
+ {"rna_test.c", NULL, RNA_def_test},
{"rna_text.c", NULL, RNA_def_text},
{"rna_timeline.c", NULL, RNA_def_timeline_marker},
{"rna_sound.c", NULL, RNA_def_sound},
diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c
index e71dcc2a586..f37fa01480c 100644
--- a/source/blender/makesrna/intern/rna_access.c
+++ b/source/blender/makesrna/intern/rna_access.c
@@ -557,14 +557,24 @@ int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
int RNA_property_dynamic_array_set_length(PointerRNA *ptr, PropertyRNA *prop, int length)
{
- if (prop->setlength)
- return prop->setlength(ptr, length);
+ /* length 0 is not allowed */
+ if (!length)
+ return 0;
+
+ if (prop->getlength) {
+ if (prop->setlength)
+ return prop->setlength(ptr, length);
+ else
+ /* length cannot be changed */
+ return 0;
+ }
else
prop->arraylength= length; /* function parameters only? */
return 1;
}
+/* used by BPY to make an array from the python object */
unsigned short RNA_property_array_dimension(PropertyRNA *prop, unsigned short dimsize[])
{
if (dimsize && prop->arraydimension > 1) {
@@ -573,6 +583,25 @@ unsigned short RNA_property_array_dimension(PropertyRNA *prop, unsigned short di
return prop->arraydimension;
}
+/* Return the size of Nth dimension. */
+int RNA_property_multidimensional_array_length(PointerRNA *ptr, PropertyRNA *prop, int dim)
+{
+ unsigned short i;
+ int len;
+
+ if (dim == 0) {
+ len= RNA_property_array_length(ptr, prop);
+
+ for (i= 0; i < prop->arraydimension - 1; i++)
+ len /= prop->dimsize[i];
+ }
+ else {
+ len= prop->dimsize[dim - 1];
+ }
+
+ return len;
+}
+
char RNA_property_array_item_char(PropertyRNA *prop, int index)
{
const char *vectoritem= "XYZW";
diff --git a/source/blender/makesrna/intern/rna_define.c b/source/blender/makesrna/intern/rna_define.c
index 69e6698bd3b..e415304ab6c 100644
--- a/source/blender/makesrna/intern/rna_define.c
+++ b/source/blender/makesrna/intern/rna_define.c
@@ -1046,6 +1046,8 @@ void RNA_def_property_multidimensional_array(PropertyRNA *prop, int arraylength,
prop->arraydimension= dimension;
+ /* TODO make sure dimsize values are sane */
+
if (dimension > 1)
memcpy(prop->dimsize, dimsize, sizeof(dimsize[0]) * (dimension - 1));
}
diff --git a/source/blender/makesrna/intern/rna_internal.h b/source/blender/makesrna/intern/rna_internal.h
index 13cc2ae9017..7de80843f27 100644
--- a/source/blender/makesrna/intern/rna_internal.h
+++ b/source/blender/makesrna/intern/rna_internal.h
@@ -157,6 +157,7 @@ void RNA_def_sensor(struct BlenderRNA *brna);
void RNA_def_sequence(struct BlenderRNA *brna);
void RNA_def_smoke(struct BlenderRNA *brna);
void RNA_def_space(struct BlenderRNA *brna);
+void RNA_def_test(struct BlenderRNA *brna);
void RNA_def_text(struct BlenderRNA *brna);
void RNA_def_texture(struct BlenderRNA *brna);
void RNA_def_timeline_marker(struct BlenderRNA *brna);
diff --git a/source/blender/makesrna/intern/rna_internal_types.h b/source/blender/makesrna/intern/rna_internal_types.h
index d706fd5ac19..2bd89dbd3bf 100644
--- a/source/blender/makesrna/intern/rna_internal_types.h
+++ b/source/blender/makesrna/intern/rna_internal_types.h
@@ -39,7 +39,12 @@ struct bContext;
struct IDProperty;
struct GHash;
+#ifdef UNIT_TEST
+#define RNA_MAX_ARRAY 64
+#else
#define RNA_MAX_ARRAY 32
+#endif
+
#define RNA_MAX_ARRAY_DIMENSION 3
/* Function Callbacks */
@@ -134,11 +139,10 @@ struct PropertyRNA {
PropertySubType subtype;
/* if an array this is > 0, specifying the length */
unsigned int arraylength;
- /* these, if non-NULL, override arraylength */
+ /* if non-NULL, overrides arraylength. Must not return 0? */
PropArrayLengthGetFunc getlength;
/* if NULL, length cannot be changed by a user */
PropArrayLengthSetFunc setlength;
- /* used only for dynamic arrays for now, default 1 */
unsigned short arraydimension;
/* dimension sizes for dimensions greater than 1, first dimension size is not specified */
unsigned short dimsize[RNA_MAX_ARRAY_DIMENSION - 1];
diff --git a/source/blender/makesrna/intern/rna_main.c b/source/blender/makesrna/intern/rna_main.c
index 82e460ea57d..344135acaff 100644
--- a/source/blender/makesrna/intern/rna_main.c
+++ b/source/blender/makesrna/intern/rna_main.c
@@ -219,6 +219,18 @@ static void rna_Main_wm_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
rna_iterator_listbase_begin(iter, &bmain->wm, NULL);
}
+#ifdef UNIT_TEST
+
+static PointerRNA rna_Test_test_get(PointerRNA *ptr)
+{
+ PointerRNA ret= *ptr;
+ ret.type= &RNA_Test;
+
+ return ret;
+}
+
+#endif
+
#else
void RNA_def_main(BlenderRNA *brna)
@@ -276,6 +288,18 @@ void RNA_def_main(BlenderRNA *brna)
}
RNA_api_main(srna);
+
+#ifdef UNIT_TEST
+
+ RNA_define_verify_sdna(0);
+
+ prop= RNA_def_property(srna, "test", PROP_POINTER, PROP_NONE);
+ RNA_def_property_struct_type(prop, "Test");
+ RNA_def_property_pointer_funcs(prop, "rna_Test_test_get", NULL, NULL);
+
+ RNA_define_verify_sdna(1);
+
+#endif
}
#endif
diff --git a/source/blender/makesrna/intern/rna_mesh.c b/source/blender/makesrna/intern/rna_mesh.c
index f7235db49a5..efd0046d827 100644
--- a/source/blender/makesrna/intern/rna_mesh.c
+++ b/source/blender/makesrna/intern/rna_mesh.c
@@ -1182,7 +1182,7 @@ static void rna_def_mtface(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "UV 4", "");
RNA_def_property_update(prop, 0, "rna_Mesh_update_data");
- prop= RNA_def_property(srna, "uv", PROP_FLOAT, PROP_XYZ);
+ prop= RNA_def_property(srna, "uv", PROP_FLOAT, PROP_NONE);
RNA_def_property_multidimensional_array(prop, 4 * 2, 2, uv_dim);
RNA_def_property_flag(prop, PROP_DYNAMIC);
RNA_def_property_dynamic_array_funcs(prop, "rna_MeshTextureFace_uv_get_length", "rna_MeshTextureFace_uv_set_length");
diff --git a/source/blender/makesrna/intern/rna_test.c b/source/blender/makesrna/intern/rna_test.c
new file mode 100644
index 00000000000..bfaf318018a
--- /dev/null
+++ b/source/blender/makesrna/intern/rna_test.c
@@ -0,0 +1,188 @@
+/**
+ * $Id: rna_test.c $
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Contributor(s): Arystanbek Dyussenov
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/* Defines a structure with properties used for array manipulation tests in BPY. */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "RNA_define.h"
+#include "RNA_types.h"
+
+#include "rna_internal.h"
+
+#define ARRAY_SIZE 3
+#define DYNAMIC_ARRAY_SIZE 64
+#define MARRAY_DIM [3][4][5]
+#define MARRAY_TOTDIM 3
+#define MARRAY_DIMSIZE 4, 5
+#define MARRAY_SIZE(type) (sizeof(type MARRAY_DIM) / sizeof(type))
+#define DYNAMIC_MARRAY_DIM [3][4][5]
+#define DYNAMIC_MARRAY_SIZE(type) (sizeof(type DYNAMIC_MARRAY_DIM) / sizeof(type))
+
+#ifdef RNA_RUNTIME
+
+#ifdef UNIT_TEST
+
+#define DEF_VARS(type, prefix) \
+ static type prefix ## arr[ARRAY_SIZE]; \
+ static type prefix ## darr[DYNAMIC_ARRAY_SIZE]; \
+ static int prefix ## darr_len= ARRAY_SIZE; \
+ static type prefix ## marr MARRAY_DIM; \
+ static type prefix ## dmarr DYNAMIC_MARRAY_DIM; \
+ static int prefix ## dmarr_len= sizeof(prefix ## dmarr);
+
+#define DEF_GET_SET(type, arr) \
+ void rna_Test_ ## arr ## _get(PointerRNA *ptr, type *values) \
+ { \
+ memcpy(values, arr, sizeof(arr)); \
+ } \
+ \
+ void rna_Test_ ## arr ## _set(PointerRNA *ptr, const type *values) \
+ { \
+ memcpy(arr, values, sizeof(arr)); \
+ }
+
+#define DEF_GET_SET_LEN(arr, max) \
+ static int rna_Test_ ## arr ## _get_length(PointerRNA *ptr) \
+ { \
+ return arr ## _len; \
+ } \
+ \
+ static int rna_Test_ ## arr ## _set_length(PointerRNA *ptr, int length) \
+ { \
+ if (length > max) \
+ return 0; \
+ \
+ arr ## _len= length; \
+ \
+ return 1; \
+ } \
+
+DEF_VARS(float, f)
+DEF_VARS(int, i)
+DEF_VARS(int, b)
+
+DEF_GET_SET(float, farr)
+DEF_GET_SET(int, iarr)
+DEF_GET_SET(int, barr)
+
+DEF_GET_SET(float, fmarr)
+DEF_GET_SET(int, imarr)
+DEF_GET_SET(int, bmarr)
+
+DEF_GET_SET(float, fdarr)
+DEF_GET_SET_LEN(fdarr, DYNAMIC_ARRAY_SIZE)
+DEF_GET_SET(int, idarr)
+DEF_GET_SET_LEN(idarr, DYNAMIC_ARRAY_SIZE)
+DEF_GET_SET(int, bdarr)
+DEF_GET_SET_LEN(bdarr, DYNAMIC_ARRAY_SIZE)
+
+DEF_GET_SET(float, fdmarr)
+DEF_GET_SET_LEN(fdmarr, DYNAMIC_MARRAY_SIZE(float))
+DEF_GET_SET(int, idmarr)
+DEF_GET_SET_LEN(idmarr, DYNAMIC_MARRAY_SIZE(int))
+DEF_GET_SET(int, bdmarr)
+DEF_GET_SET_LEN(bdmarr, DYNAMIC_MARRAY_SIZE(int))
+
+#endif
+
+#else
+
+void RNA_def_test(BlenderRNA *brna)
+{
+#ifdef UNIT_TEST
+ StructRNA *srna;
+ PropertyRNA *prop;
+ unsigned short dimsize[]= {MARRAY_DIMSIZE};
+
+ srna= RNA_def_struct(brna, "Test", NULL);
+ RNA_def_struct_sdna(srna, "Test");
+
+ prop= RNA_def_float_array(srna, "farr", ARRAY_SIZE, NULL, 0.0f, 0.0f, "farr", "float array", 0.0f, 0.0f);
+ RNA_def_property_float_funcs(prop, "rna_Test_farr_get", "rna_Test_farr_set", NULL);
+
+ prop= RNA_def_int_array(srna, "iarr", ARRAY_SIZE, NULL, 0, 0, "iarr", "int array", 0, 0);
+ RNA_def_property_int_funcs(prop, "rna_Test_iarr_get", "rna_Test_iarr_set", NULL);
+
+ prop= RNA_def_boolean_array(srna, "barr", ARRAY_SIZE, NULL, "barr", "boolean array");
+ RNA_def_property_boolean_funcs(prop, "rna_Test_barr_get", "rna_Test_barr_set");
+
+ /* dynamic arrays */
+
+ prop= RNA_def_float_array(srna, "fdarr", DYNAMIC_ARRAY_SIZE, NULL, 0.0f, 0.0f, "fdarr", "dynamic float array", 0.0f, 0.0f);
+ RNA_def_property_flag(prop, PROP_DYNAMIC);
+ RNA_def_property_dynamic_array_funcs(prop, "rna_Test_fdarr_get_length", "rna_Test_fdarr_set_length");
+ RNA_def_property_float_funcs(prop, "rna_Test_fdarr_get", "rna_Test_fdarr_set", NULL);
+
+ prop= RNA_def_int_array(srna, "idarr", DYNAMIC_ARRAY_SIZE, NULL, 0, 0, "idarr", "int array", 0, 0);
+ RNA_def_property_flag(prop, PROP_DYNAMIC);
+ RNA_def_property_dynamic_array_funcs(prop, "rna_Test_idarr_get_length", "rna_Test_idarr_set_length");
+ RNA_def_property_int_funcs(prop, "rna_Test_idarr_get", "rna_Test_idarr_set", NULL);
+
+ prop= RNA_def_boolean_array(srna, "bdarr", DYNAMIC_ARRAY_SIZE, NULL, "bdarr", "boolean array");
+ RNA_def_property_flag(prop, PROP_DYNAMIC);
+ RNA_def_property_dynamic_array_funcs(prop, "rna_Test_bdarr_get_length", "rna_Test_bdarr_set_length");
+ RNA_def_property_boolean_funcs(prop, "rna_Test_bdarr_get", "rna_Test_bdarr_set");
+
+ /* multidimensional arrays */
+
+ prop= RNA_def_property(srna, "fmarr", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(float), MARRAY_TOTDIM, dimsize);
+ RNA_def_property_float_funcs(prop, "rna_Test_fmarr_get", "rna_Test_fmarr_set", NULL);
+
+ prop= RNA_def_property(srna, "imarr", PROP_INT, PROP_NONE);
+ RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
+ RNA_def_property_int_funcs(prop, "rna_Test_imarr_get", "rna_Test_imarr_set", NULL);
+
+ prop= RNA_def_property(srna, "bmarr", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_multidimensional_array(prop, MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
+ RNA_def_property_boolean_funcs(prop, "rna_Test_bmarr_get", "rna_Test_bmarr_set");
+
+ /* dynamic multidimensional arrays */
+
+ prop= RNA_def_property(srna, "fdmarr", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_multidimensional_array(prop, DYNAMIC_MARRAY_SIZE(float), MARRAY_TOTDIM, dimsize);
+ RNA_def_property_flag(prop, PROP_DYNAMIC);
+ RNA_def_property_dynamic_array_funcs(prop, "rna_Test_fdmarr_get_length", "rna_Test_fdmarr_set_length");
+ RNA_def_property_float_funcs(prop, "rna_Test_fdmarr_get", "rna_Test_fdmarr_set", NULL);
+
+ prop= RNA_def_property(srna, "idmarr", PROP_INT, PROP_NONE);
+ RNA_def_property_multidimensional_array(prop, DYNAMIC_MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
+ RNA_def_property_flag(prop, PROP_DYNAMIC);
+ RNA_def_property_dynamic_array_funcs(prop, "rna_Test_idmarr_get_length", "rna_Test_idmarr_set_length");
+ RNA_def_property_int_funcs(prop, "rna_Test_idmarr_get", "rna_Test_idmarr_set", NULL);
+
+ prop= RNA_def_property(srna, "bdmarr", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_multidimensional_array(prop, DYNAMIC_MARRAY_SIZE(int), MARRAY_TOTDIM, dimsize);
+ RNA_def_property_flag(prop, PROP_DYNAMIC);
+ RNA_def_property_dynamic_array_funcs(prop, "rna_Test_bdmarr_get_length", "rna_Test_bdmarr_set_length");
+ RNA_def_property_boolean_funcs(prop, "rna_Test_bdmarr_get", "rna_Test_bdmarr_set");
+
+#endif
+}
+
+#endif /* RNA_RUNTIME */
+
+