[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20648] branches/blender2.5/blender/source /blender: PyRNA

Campbell Barton ideasman42 at gmail.com
Fri Jun 5 14:48:59 CEST 2009


Revision: 20648
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20648
Author:   campbellbarton
Date:     2009-06-05 14:48:58 +0200 (Fri, 05 Jun 2009)

Log Message:
-----------
PyRNA
- Support for python to convert a PyObject into a collection (uses a list of dicts - quite verbose :/)
- Operators can now take collection args when called from python.
- Support for printing operators that use collections (macro recording).
- Added RNA_pointer_as_string which prints all pointer prop values as a python dict.

Example that can run in the in test.py (F7 key)
bpy.ops.VIEW3D_OT_select_lasso(path=[{"loc":(0, 0), "time":0}, {"loc":(1000, 0), "time":0}, {"loc":(1000, 1000), "time":0}], type='SELECT')

for some reason lasso locations always print as 0,0. Need to look into why this is.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/makesrna/RNA_access.h
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h
    branches/blender2.5/blender/source/blender/python/intern/bpy_operator_wrap.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h
    branches/blender2.5/blender/source/blender/python/intern/bpy_ui.c

Modified: branches/blender2.5/blender/source/blender/makesrna/RNA_access.h
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/RNA_access.h	2009-06-05 11:51:27 UTC (rev 20647)
+++ branches/blender2.5/blender/source/blender/makesrna/RNA_access.h	2009-06-05 12:48:58 UTC (rev 20648)
@@ -536,6 +536,7 @@
 
 /* python compatible string representation of this property, (must be freed!) */
 char *RNA_property_as_string(PointerRNA *ptr, PropertyRNA *prop);
+char *RNA_pointer_as_string(PointerRNA *ptr);
 
 /* Function */
 

Modified: branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c	2009-06-05 11:51:27 UTC (rev 20647)
+++ branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c	2009-06-05 12:48:58 UTC (rev 20648)
@@ -2185,6 +2185,47 @@
 
 /* string representation of a property, python
  * compatible but can be used for display too*/
+char *RNA_pointer_as_string(PointerRNA *ptr)
+{
+	DynStr *dynstr= BLI_dynstr_new();
+	char *cstring;
+	
+	PropertyRNA *prop, *iterprop;
+	CollectionPropertyIterator iter;
+	const char *propname;
+	int first_time = 1;
+	
+	BLI_dynstr_append(dynstr, "{");
+	
+	iterprop= RNA_struct_iterator_property(ptr->type);
+	RNA_property_collection_begin(ptr, iterprop, &iter);
+
+	for(; iter.valid; RNA_property_collection_next(&iter)) {
+		prop= iter.ptr.data;
+		propname = RNA_property_identifier(prop);
+		
+		if(strcmp(propname, "rna_type")==0)
+			continue;
+		
+		if(first_time==0)
+			BLI_dynstr_append(dynstr, ", ");
+		first_time= 0;
+		
+		cstring = RNA_property_as_string(&iter.ptr, prop);
+		BLI_dynstr_appendf(dynstr, "\"%s\":%s", propname, cstring);
+		MEM_freeN(cstring);
+		first_time= 0;
+	}
+
+	RNA_property_collection_end(&iter);
+	BLI_dynstr_append(dynstr, "}");	
+	
+	
+	cstring = BLI_dynstr_get_cstring(dynstr);
+	BLI_dynstr_free(dynstr);
+	return cstring;
+}
+
 char *RNA_property_as_string(PointerRNA *ptr, PropertyRNA *prop)
 {
 	int type = RNA_property_type(prop);
@@ -2262,8 +2303,28 @@
 		break;
 	}
 	case PROP_COLLECTION:
-		BLI_dynstr_append(dynstr, "'<COLLECTION>'"); /* TODO */
+	{
+		int first_time = 1;
+		CollectionPropertyIterator collect_iter;
+		BLI_dynstr_append(dynstr, "[");
+		
+		for(RNA_property_collection_begin(ptr, prop, &collect_iter); collect_iter.valid; RNA_property_collection_next(&collect_iter)) {
+			PointerRNA itemptr= collect_iter.ptr;
+			
+			if(first_time==0)
+				BLI_dynstr_append(dynstr, ", ");
+			first_time= 0;
+			
+			/* now get every prop of the collection */
+			cstring= RNA_pointer_as_string(&itemptr);
+			BLI_dynstr_append(dynstr, cstring);
+			MEM_freeN(cstring);
+		}
+		
+		RNA_property_collection_end(&collect_iter);
+		BLI_dynstr_append(dynstr, "]");
 		break;
+	}
 	default:
 		BLI_dynstr_append(dynstr, "'<UNKNOWN TYPE>'"); /* TODO */
 		break;

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c	2009-06-05 11:51:27 UTC (rev 20647)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c	2009-06-05 12:48:58 UTC (rev 20648)
@@ -41,73 +41,6 @@
 
 extern ListBase global_ops; /* evil, temp use */
 
-
-
-/* This function is only used by operators right now
- * Its used for taking keyword args and filling in property values */
-int PYOP_props_from_dict(PointerRNA *ptr, PyObject *kw)
-{
-	int error_val = 0;
-	int totkw;
-	const char *arg_name= NULL;
-	PyObject *item;
-
-	PropertyRNA *prop, *iterprop;
-	CollectionPropertyIterator iter;
-
-	iterprop= RNA_struct_iterator_property(ptr->type);
-	RNA_property_collection_begin(ptr, iterprop, &iter);
-
-	totkw = kw ? PyDict_Size(kw):0;
-
-	for(; iter.valid; RNA_property_collection_next(&iter)) {
-		prop= iter.ptr.data;
-
-		arg_name= RNA_property_identifier(prop);
-
-		if (strcmp(arg_name, "rna_type")==0) continue;
-
-		if (kw==NULL) {
-			PyErr_Format( PyExc_AttributeError, "no args, expected \"%s\"", arg_name ? arg_name : "<UNKNOWN>");
-			error_val= -1;
-			break;
-		}
-
-		item= PyDict_GetItemString(kw, arg_name);
-
-		if (item == NULL) {
-			PyErr_Format( PyExc_AttributeError, "argument \"%s\" missing", arg_name ? arg_name : "<UNKNOWN>");
-			error_val = -1; /* pyrna_py_to_prop sets the error */
-			break;
-		}
-
-		if (pyrna_py_to_prop(ptr, prop, NULL, item)) {
-			error_val= -1;
-			break;
-		}
-
-		totkw--;
-	}
-
-	RNA_property_collection_end(&iter);
-
-	if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
-		PyObject *key, *value;
-		Py_ssize_t pos = 0;
-
-		while (PyDict_Next(kw, &pos, &key, &value)) {
-			arg_name= _PyUnicode_AsString(key);
-			if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
-			arg_name= NULL;
-		}
-
-		PyErr_Format( PyExc_AttributeError, "argument \"%s\" unrecognized", arg_name ? arg_name : "<UNKNOWN>");
-		error_val = -1;
-	}
-
-	return error_val;
-}
-
 static PyObject *pyop_base_dir(PyObject *self);
 static PyObject *pyop_base_rna(PyObject *self, PyObject *pyname);
 static struct PyMethodDef pyop_base_methods[] = {
@@ -148,7 +81,7 @@
 	
 	WM_operator_properties_create(&ptr, opname);
 	
-	error_val= PYOP_props_from_dict(&ptr, kw);
+	error_val= pyrna_pydict_to_props(&ptr, kw, "Converting py args to operator properties: ");
 	
 	if (error_val==0) {
 		ReportList reports;

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h	2009-06-05 11:51:27 UTC (rev 20647)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h	2009-06-05 12:48:58 UTC (rev 20648)
@@ -42,7 +42,4 @@
 
 PyObject *BPY_operator_module(void);
 
-/* fill in properties from a python dict */
-int PYOP_props_from_dict(PointerRNA *ptr, PyObject *kw);
-
 #endif

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_operator_wrap.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_operator_wrap.c	2009-06-05 11:51:27 UTC (rev 20647)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_operator_wrap.c	2009-06-05 12:48:58 UTC (rev 20648)
@@ -272,7 +272,7 @@
 		 * thrown away anyway
 		 *
 		 * If we ever want to do this and use the props again,
-		 * it can be done with - PYOP_props_from_dict(op->ptr, kw)
+		 * it can be done with - pyrna_pydict_to_props(op->ptr, kw, "")
 		 */
 		
 		Py_DECREF(ret);

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2009-06-05 11:51:27 UTC (rev 20647)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2009-06-05 12:48:58 UTC (rev 20648)
@@ -204,6 +204,71 @@
 	return ret;
 }
 
+/* This function is only used by operators right now
+ * Its used for taking keyword args and filling in property values */
+int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const char *error_prefix)
+{
+	int error_val = 0;
+	int totkw;
+	const char *arg_name= NULL;
+	PyObject *item;
+
+	PropertyRNA *prop, *iterprop;
+	CollectionPropertyIterator iter;
+
+	iterprop= RNA_struct_iterator_property(ptr->type);
+	RNA_property_collection_begin(ptr, iterprop, &iter);
+
+	totkw = kw ? PyDict_Size(kw):0;
+
+	for(; iter.valid; RNA_property_collection_next(&iter)) {
+		prop= iter.ptr.data;
+
+		arg_name= RNA_property_identifier(prop);
+
+		if (strcmp(arg_name, "rna_type")==0) continue;
+
+		if (kw==NULL) {
+			PyErr_Format( PyExc_AttributeError, "%s: no keywords, expected \"%s\"", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+			error_val= -1;
+			break;
+		}
+
+		item= PyDict_GetItemString(kw, arg_name);
+
+		if (item == NULL) {
+			PyErr_Format( PyExc_AttributeError, "%s: keyword \"%s\" missing", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+			error_val = -1; /* pyrna_py_to_prop sets the error */
+			break;
+		}
+
+		if (pyrna_py_to_prop(ptr, prop, NULL, item)) {
+			error_val= -1;
+			break;
+		}
+
+		totkw--;
+	}
+
+	RNA_property_collection_end(&iter);
+
+	if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
+		PyObject *key, *value;
+		Py_ssize_t pos = 0;
+
+		while (PyDict_Next(kw, &pos, &key, &value)) {
+			arg_name= _PyUnicode_AsString(key);
+			if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
+			arg_name= NULL;
+		}
+
+		PyErr_Format( PyExc_AttributeError, "%s: keyword \"%s\" unrecognized", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+		error_val = -1;
+	}
+
+	return error_val;
+}
+
 static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw);
 
 PyObject *pyrna_func_to_py(PointerRNA *ptr, FunctionRNA *func)
@@ -447,9 +512,36 @@
 			break;
 		}
 		case PROP_COLLECTION:
-			PyErr_SetString(PyExc_AttributeError, "cant convert collections yet");
-			return -1;
+		{
+			int seq_len, i;
+			PyObject *item;
+			PointerRNA itemptr;
+			
+			/* convert a sequence of dict's into a collection */
+			if(!PySequence_Check(value)) {
+				PyErr_SetString(PyExc_TypeError, "expected a sequence of dicts for an RNA collection");
+				return -1;
+			}
+			
+			seq_len = PySequence_Length(value);
+			for(i=0; i<seq_len; i++) {
+				item= PySequence_GetItem(value, i);
+				if(item==NULL || PyDict_Check(item)==0) {
+					PyErr_SetString(PyExc_TypeError, "expected a sequence of dicts for an RNA collection");
+					Py_XDECREF(item);
+					return -1;
+				}
+				
+				RNA_property_collection_add(ptr, prop, &itemptr);
+				if(pyrna_pydict_to_props(&itemptr, item, "Converting a python list to an RNA collection")==-1) {
+					Py_DECREF(item);
+					return -1;
+				}
+				Py_DECREF(item);
+			}
+			
 			break;
+		}
 		default:
 			PyErr_SetString(PyExc_AttributeError, "unknown property type (pyrna_py_to_prop)");
 			return -1;

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h	2009-06-05 11:51:27 UTC (rev 20647)

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list