[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24916] trunk/blender/source/blender: Patch #20037: Use named components for Drivers instead of array_index

Joshua Leung aligorith at gmail.com
Thu Nov 26 12:13:11 CET 2009


Revision: 24916
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24916
Author:   aligorith
Date:     2009-11-26 12:13:10 +0100 (Thu, 26 Nov 2009)

Log Message:
-----------
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...

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/fcurve.c
    trunk/blender/source/blender/editors/space_graph/graph_buttons.c
    trunk/blender/source/blender/makesrna/RNA_access.h
    trunk/blender/source/blender/makesrna/intern/rna_access.c

Modified: trunk/blender/source/blender/blenkernel/intern/fcurve.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/fcurve.c	2009-11-26 10:19:09 UTC (rev 24915)
+++ trunk/blender/source/blender/blenkernel/intern/fcurve.c	2009-11-26 11:13:10 UTC (rev 24916)
@@ -822,7 +822,7 @@
 	}
 	
 	/* 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))

Modified: trunk/blender/source/blender/editors/space_graph/graph_buttons.c
===================================================================
--- trunk/blender/source/blender/editors/space_graph/graph_buttons.c	2009-11-26 10:19:09 UTC (rev 24915)
+++ trunk/blender/source/blender/editors/space_graph/graph_buttons.c	2009-11-26 11:13:10 UTC (rev 24916)
@@ -400,7 +400,9 @@
 				
 				/* 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);
 		}
 	}
 	

Modified: trunk/blender/source/blender/makesrna/RNA_access.h
===================================================================
--- trunk/blender/source/blender/makesrna/RNA_access.h	2009-11-26 10:19:09 UTC (rev 24915)
+++ trunk/blender/source/blender/makesrna/RNA_access.h	2009-11-26 11:13:10 UTC (rev 24916)
@@ -615,6 +615,7 @@
 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,8 +719,11 @@
 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);
 

Modified: trunk/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_access.c	2009-11-26 10:19:09 UTC (rev 24915)
+++ trunk/blender/source/blender/makesrna/intern/rna_access.c	2009-11-26 11:13:10 UTC (rev 24916)
@@ -24,6 +24,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 
 #include "MEM_guardedalloc.h"
 
@@ -694,10 +695,58 @@
 		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)
 {
 	IntPropertyRNA *iprop= (IntPropertyRNA*)rna_ensure_property(prop);
@@ -2446,10 +2495,15 @@
 /* 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 @@
 		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);
+				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;
 			}
-			else {
-				/* otherwise do int lookup */
-				intkey= atoi(token);
-				RNA_property_collection_lookup_int(&curptr, prop, intkey, &nextptr);
-			}
 
-			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);
+			}
 		}
 	}
 





More information about the Bf-blender-cvs mailing list