diff options
author | Joshua Leung <aligorith@gmail.com> | 2009-11-26 14:13:10 +0300 |
---|---|---|
committer | Joshua Leung <aligorith@gmail.com> | 2009-11-26 14:13:10 +0300 |
commit | 802cc77f71d6a1deb1e4773f135653f836e84e15 (patch) | |
tree | 9d00299273e07021be27f8de7c6240fb169db6b5 /source | |
parent | baac1f2267631598613a6f189f45f4a5fea21b9e (diff) |
Patch #20037: Use named components for Drivers instead of array_index
This patch, by Elia Sarti (vekoon), simply adds the possibility to specify the final array component of the RNA path in the path itself,
e.g. using location[0] or location["x"] or even location.x,
instead of specifying this using an "array_index"
This should be easier for users to understand the driver system. The array-indices have been kept (but hidden from the UI under standard situations) since they are theoretically a tad faster than the in-path lookups still, and are easier for internal-tools to set for now...
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/intern/fcurve.c | 2 | ||||
-rw-r--r-- | source/blender/editors/space_graph/graph_buttons.c | 4 | ||||
-rw-r--r-- | source/blender/makesrna/RNA_access.h | 6 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_access.c | 141 |
4 files changed, 124 insertions, 29 deletions
diff --git a/source/blender/blenkernel/intern/fcurve.c b/source/blender/blenkernel/intern/fcurve.c index e90fccf6b29..0623a5cbe5e 100644 --- a/source/blender/blenkernel/intern/fcurve.c +++ b/source/blender/blenkernel/intern/fcurve.c @@ -822,7 +822,7 @@ float driver_get_target_value (ChannelDriver *driver, DriverTarget *dtar) } /* get property to read from, and get value as appropriate */ - if (RNA_path_resolve(&id_ptr, path, &ptr, &prop)) { + if (RNA_path_resolve_full(&id_ptr, path, &ptr, &prop, &index)) { switch (RNA_property_type(prop)) { case PROP_BOOLEAN: if (RNA_property_array_length(&ptr, prop)) diff --git a/source/blender/editors/space_graph/graph_buttons.c b/source/blender/editors/space_graph/graph_buttons.c index 97b3dd29ef9..05987087288 100644 --- a/source/blender/editors/space_graph/graph_buttons.c +++ b/source/blender/editors/space_graph/graph_buttons.c @@ -400,7 +400,9 @@ static void graph_panel_drivers(const bContext *C, Panel *pa) /* array index */ // TODO: this needs selector which limits it to ok values - uiItemR(col, "Index", 0, &dtar_ptr, "array_index", 0); + // NOTE: for for now, the array index box still gets shown when non-zero (i.e. for tweaking rigs as necessary) + if (dtar->array_index) + uiItemR(col, "Index", 0, &dtar_ptr, "array_index", 0); } } diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index d35e5632053..3c420535e2b 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -615,6 +615,7 @@ int RNA_property_array_check(PointerRNA *ptr, PropertyRNA *prop); int RNA_property_multi_array_length(PointerRNA *ptr, PropertyRNA *prop, int dimension); int RNA_property_array_dimension(PointerRNA *ptr, PropertyRNA *prop, int length[]); char RNA_property_array_item_char(PropertyRNA *prop, int index); +int RNA_property_array_item_index(PropertyRNA *prop, char name); int RNA_property_string_maxlength(PropertyRNA *prop); @@ -718,7 +719,10 @@ char *RNA_path_append(const char *path, PointerRNA *ptr, PropertyRNA *prop, char *RNA_path_back(const char *path); int RNA_path_resolve(PointerRNA *ptr, const char *path, - PointerRNA *r_ptr, PropertyRNA **r_prop); + PointerRNA *r_ptr, PropertyRNA **r_prop); + +int RNA_path_resolve_full(PointerRNA *ptr, const char *path, + PointerRNA *r_ptr, PropertyRNA **r_prop, int *index); char *RNA_path_from_ID_to_struct(PointerRNA *ptr); char *RNA_path_from_ID_to_property(PointerRNA *ptr, PropertyRNA *prop); diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 669810bb39b..6c0a3c57f65 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -24,6 +24,7 @@ #include <stdlib.h> #include <string.h> +#include <ctype.h> #include "MEM_guardedalloc.h" @@ -694,8 +695,56 @@ char RNA_property_array_item_char(PropertyRNA *prop, int index) return vectoritem[index]; else if ((index < 4) && ELEM(subtype, PROP_COLOR, PROP_RGB)) return coloritem[index]; - else - return '\0'; + + return '\0'; +} + +int RNA_property_array_item_index(PropertyRNA *prop, char name) +{ + PropertySubType subtype= rna_ensure_property(prop)->subtype; + + name= toupper(name); + + /* get index based on string name/alias */ + /* maybe a function to find char index in string would be better than all the switches */ + if (ELEM(subtype, PROP_QUATERNION, PROP_AXISANGLE)) { + switch (name) { + case 'W': + return 0; + case 'X': + return 1; + case 'Y': + return 2; + case 'Z': + return 3; + } + } + else if(ELEM6(subtype, PROP_TRANSLATION, PROP_DIRECTION, PROP_XYZ, PROP_EULER, PROP_VELOCITY, PROP_ACCELERATION)) { + switch (name) { + case 'X': + return 0; + case 'Y': + return 1; + case 'Z': + return 2; + case 'W': + return 3; + } + } + else if (ELEM(subtype, PROP_COLOR, PROP_RGB)) { + switch (name) { + case 'R': + return 0; + case 'G': + return 1; + case 'B': + return 2; + case 'A': + return 3; + } + } + + return -1; } void RNA_property_int_range(PointerRNA *ptr, PropertyRNA *prop, int *hardmin, int *hardmax) @@ -2446,10 +2495,15 @@ static int rna_token_strip_quotes(char *token) /* Resolve the given RNA path to find the pointer+property indicated at the end of the path */ int RNA_path_resolve(PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop) { + return RNA_path_resolve_full(ptr, path, r_ptr, r_prop, NULL); +} + +int RNA_path_resolve_full(PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop, int *index) +{ PropertyRNA *prop; PointerRNA curptr, nextptr; char fixedbuf[256], *token; - int len, intkey; + int type, len, intkey; prop= NULL; curptr= *ptr; @@ -2484,43 +2538,78 @@ int RNA_path_resolve(PointerRNA *ptr, const char *path, PointerRNA *r_ptr, Prope if(!prop) return 0; + type= RNA_property_type(prop); + /* now look up the value of this property if it is a pointer or * collection, otherwise return the property rna so that the * caller can read the value of the property itself */ - if(RNA_property_type(prop) == PROP_POINTER) { + switch (type) { + case PROP_POINTER: nextptr= RNA_property_pointer_get(&curptr, prop); if(nextptr.data) curptr= nextptr; else return 0; - } - else if(RNA_property_type(prop) == PROP_COLLECTION && *path) { - /* resolve the lookup with [] brackets */ - token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 1); - if(!token) - return 0; + break; + case PROP_COLLECTION: + if(*path) { + /* resolve the lookup with [] brackets */ + token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 1); - len= strlen(token); - - /* check for "" to see if it is a string */ - if(rna_token_strip_quotes(token)) { - RNA_property_collection_lookup_string(&curptr, prop, token+1, &nextptr); - } - else { - /* otherwise do int lookup */ - intkey= atoi(token); - RNA_property_collection_lookup_int(&curptr, prop, intkey, &nextptr); + if(!token) + return 0; + + len= strlen(token); + + /* check for "" to see if it is a string */ + if(rna_token_strip_quotes(token)) { + RNA_property_collection_lookup_string(&curptr, prop, token+1, &nextptr); + } + else { + /* otherwise do int lookup */ + intkey= atoi(token); + RNA_property_collection_lookup_int(&curptr, prop, intkey, &nextptr); + } + + if(token != fixedbuf) + MEM_freeN(token); + + if(nextptr.data) + curptr= nextptr; + else + return 0; } - if(token != fixedbuf) - MEM_freeN(token); + break; + default: + if (index==NULL) + break; - if(nextptr.data) - curptr= nextptr; - else - return 0; + *index= -1; + + if (*path) { + if (*path=='[') { + token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 1); + + /* check for "" to see if it is a string */ + if(rna_token_strip_quotes(token)) { + *index= RNA_property_array_item_index(prop, *(token+1)); + } + else { + /* otherwise do int lookup */ + *index= atoi(token); + } + } + else { + token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 0); + *index= RNA_property_array_item_index(prop, *token); + } + + if(token != fixedbuf) + MEM_freeN(token); + } } } |