[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24623] trunk/blender: make animation system and rna internals use getattr style syntax for user defined properties

Campbell Barton ideasman42 at gmail.com
Tue Nov 17 21:47:03 CET 2009


Revision: 24623
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24623
Author:   campbellbarton
Date:     2009-11-17 21:46:59 +0100 (Tue, 17 Nov 2009)

Log Message:
-----------
make animation system and rna internals use getattr style syntax for user defined properties
bone["foo"] rather then bone.foo
matches python and avoids naming collisions

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/rna_prop_ui.py
    trunk/blender/source/blender/makesrna/intern/rna_access.c
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/release/scripts/modules/rna_prop_ui.py
===================================================================
--- trunk/blender/release/scripts/modules/rna_prop_ui.py	2009-11-17 20:40:39 UTC (rev 24622)
+++ trunk/blender/release/scripts/modules/rna_prop_ui.py	2009-11-17 20:46:59 UTC (rev 24623)
@@ -100,7 +100,7 @@
             if convert_to_pyobject and not hasattr(val_orig, "len"):
                 row.itemL(text=val_draw)
             else:
-                row.itemR(rna_item, key, text="")
+                row.itemR(rna_item, '["' + key + '"]', text="")
                 
             
             row = split.row(align=True)

Modified: trunk/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_access.c	2009-11-17 20:40:39 UTC (rev 24622)
+++ trunk/blender/source/blender/makesrna/intern/rna_access.c	2009-11-17 20:46:59 UTC (rev 24623)
@@ -470,11 +470,21 @@
 
 PropertyRNA *RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
 {
-	PropertyRNA *iterprop= RNA_struct_iterator_property(ptr->type);
-	PointerRNA propptr;
+	if(identifier[0]=='[' && identifier[1]=='"') {
+		/* id prop lookup, not so common */
+		PropertyRNA *r_prop= NULL;
+		PointerRNA r_ptr; /* only support single level props */
+		if(RNA_path_resolve(ptr, identifier, &r_ptr, &r_prop) && r_ptr.type==ptr->type && r_ptr.data==ptr->data)
+			return r_prop;
+	}
+	else {
+		/* most common case */
+		PropertyRNA *iterprop= RNA_struct_iterator_property(ptr->type);
+		PointerRNA propptr;
 
-	if(RNA_property_collection_lookup_string(ptr, iterprop, identifier, &propptr))
-		return propptr.data;
+		if(RNA_property_collection_lookup_string(ptr, iterprop, identifier, &propptr))
+			return propptr.data;
+	}
 	
 	return NULL;
 }
@@ -2293,6 +2303,19 @@
 	return buf;
 }
 
+static int rna_token_strip_quotes(char *token)
+{
+	if(token[0]=='"') {
+		int len = strlen(token);
+		if (len >= 2 && token[len-1]=='"') {
+			/* strip away "" */
+			token[len-1]= '\0';
+			return 1;
+		}
+	}
+	return 0;
+}
+
 /* 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)
 {
@@ -2304,18 +2327,35 @@
 	prop= NULL;
 	curptr= *ptr;
 
-	if(path==NULL)
+	if(path==NULL || *path=='\0')
 		return 0;
 
 	while(*path) {
+		int use_id_prop = (*path=='[') ? 1:0;
+		/* custom property lookup ?
+		 * C.object["someprop"]
+		 */
+
 		/* look up property name in current struct */
-		token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 0);
+		token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), use_id_prop);
 
 		if(!token)
 			return 0;
 
-		prop= RNA_struct_find_property(&curptr, token);
+		if(use_id_prop) { /* look up property name in current struct */
+			IDProperty *group= RNA_struct_idproperties(ptr, 0);
+			if(!group)
+				return 0;
 
+			if(rna_token_strip_quotes(token))
+				prop= (PropertyRNA *)IDP_GetPropertyFromGroup(group, token+1);
+			else
+				prop= NULL;
+		}
+		else {
+			prop= RNA_struct_find_property(&curptr, token);
+		}
+
 		if(token != fixedbuf)
 			MEM_freeN(token);
 
@@ -2343,9 +2383,7 @@
 			len= strlen(token);
 			
 			/* check for "" to see if it is a string */
-			if(len >= 2 && token[0] == '"' && token[len-1] == '"') {
-				/* strip away "" */
-				token[len-1]= 0;
+			if(rna_token_strip_quotes(token)) {
 				RNA_property_collection_lookup_string(&curptr, prop, token+1, &nextptr);
 			}
 			else {
@@ -2370,6 +2408,7 @@
 	return 1;
 }
 
+
 char *RNA_path_append(const char *path, PointerRNA *ptr, PropertyRNA *prop, int intkey, const char *strkey)
 {
 	DynStr *dynstr;
@@ -2504,6 +2543,7 @@
 
 char *RNA_path_from_ID_to_property(PointerRNA *ptr, PropertyRNA *prop)
 {
+	int is_rna = (prop->magic == RNA_MAGIC);
 	const char *propname;
 	char *ptrpath, *path;
 
@@ -2516,12 +2556,15 @@
 	propname= RNA_property_identifier(prop);
 
 	if(ptrpath) {
-		path= BLI_sprintfN("%s.%s", ptrpath, propname);
-		MEM_freeN(ptrpath);
+		path= BLI_sprintfN(is_rna ? "%s.%s":"%s[\"%s\"]", ptrpath, propname);
 	}
-	else
-		path= BLI_strdup(propname);
-	
+	else {
+		if(is_rna)
+			path= BLI_strdup(propname);
+		else
+			path= BLI_sprintfN("[\"%s\"]", propname);
+	}
+
 	return path;
 }
 
@@ -2728,8 +2771,6 @@
 	return 0;
 }
 
-
-
 void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
 {
 	PropertyRNA *prop= RNA_struct_find_property(ptr, name);

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2009-11-17 20:40:39 UTC (rev 24622)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2009-11-17 20:46:59 UTC (rev 24623)
@@ -1410,6 +1410,23 @@
 	return PyBool_FromLong(hidden);
 }
 
+static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *value)
+{
+	char *path= _PyUnicode_AsString(value);
+	PointerRNA r_ptr;
+	PropertyRNA *r_prop;
+
+	if(path==NULL) {
+		PyErr_SetString( PyExc_TypeError, "items() is only valid for collection types" );
+		return NULL;
+	}
+
+	if (RNA_path_resolve(&self->ptr, path, &r_ptr, &r_prop))
+		return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
+
+	Py_RETURN_NONE;
+}
+
 static PyObject *pyrna_struct_dir(BPy_StructRNA *self)
 {
 	PyObject *ret, *dict;
@@ -1507,7 +1524,6 @@
 	return ret;
 }
 
-
 //---------------getattr--------------------------------------------
 static PyObject *pyrna_struct_getattro( BPy_StructRNA *self, PyObject *pyname )
 {
@@ -2119,6 +2135,7 @@
 	{"driver_add", (PyCFunction)pyrna_struct_driver_add, METH_VARARGS, NULL},
 	{"is_property_set", (PyCFunction)pyrna_struct_is_property_set, METH_VARARGS, NULL},
 	{"is_property_hidden", (PyCFunction)pyrna_struct_is_property_hidden, METH_VARARGS, NULL},
+	{"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_O, NULL},
 	{"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},
 	{NULL, NULL, 0, NULL}
 };





More information about the Bf-blender-cvs mailing list