[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19759] branches/blender2.5/blender: merged pyrna_py_to_param and pyrna_py_to_prop since they are almost the same

Campbell Barton ideasman42 at gmail.com
Thu Apr 16 15:21:18 CEST 2009


Revision: 19759
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19759
Author:   campbellbarton
Date:     2009-04-16 15:21:18 +0200 (Thu, 16 Apr 2009)

Log Message:
-----------
merged pyrna_py_to_param and pyrna_py_to_prop since they are almost the same

Modified Paths:
--------------
    branches/blender2.5/blender/release/ui/buttons_scene.py
    branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h

Modified: branches/blender2.5/blender/release/ui/buttons_scene.py
===================================================================
--- branches/blender2.5/blender/release/ui/buttons_scene.py	2009-04-16 13:10:08 UTC (rev 19758)
+++ branches/blender2.5/blender/release/ui/buttons_scene.py	2009-04-16 13:21:18 UTC (rev 19759)
@@ -40,7 +40,7 @@
 
 		layout.column_flow()
 		layout.itemR(rd, "resolution_x", text="SizeX")
-		layout.itemR(rd, "resolution_x", text="SizeY")
+		layout.itemR(rd, "resolution_y", text="SizeY")
 		layout.itemR(rd, "pixel_aspect_x", text="AspX")
 		layout.itemR(rd, "pixel_aspect_y", text="AspY")
 

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c	2009-04-16 13:10:08 UTC (rev 19758)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c	2009-04-16 13:21:18 UTC (rev 19759)
@@ -80,7 +80,7 @@
 			break;
 		}
 
-		if (pyrna_py_to_prop(ptr, prop, item)) {
+		if (pyrna_py_to_prop(ptr, prop, NULL, item)) {
 			error_val= -1;
 			break;
 		}

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2009-04-16 13:10:08 UTC (rev 19758)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2009-04-16 13:21:18 UTC (rev 19759)
@@ -217,8 +217,10 @@
 	return ret;
 }
 
-int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, PyObject *value)
+
+int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value)
 {
+	/* XXX hard limits should be checked here */
 	int type = RNA_property_type(ptr, prop);
 	int len = RNA_property_array_length(ptr, prop);
 	
@@ -240,7 +242,10 @@
 		switch (type) {
 		case PROP_BOOLEAN:
 		{
-			int *param_arr = MEM_mallocN(sizeof(char) * len, "pyrna bool array");
+			int *param_arr;
+			if(data)	param_arr= (int*)data;
+			else		param_arr= MEM_mallocN(sizeof(char) * len, "pyrna bool array");
+
 			
 			/* collect the variables before assigning, incase one of them is incorrect */
 			for (i=0; i<len; i++) {
@@ -249,22 +254,27 @@
 				Py_DECREF(item);
 				
 				if (param_arr[i] < 0) {
-					MEM_freeN(param_arr);
+					if(data==NULL)
+						MEM_freeN(param_arr);
 					PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence is not a boolean");
 					return -1;
 				}
 			}
-			
-			RNA_property_boolean_set_array(ptr, prop, param_arr);
-			
-			MEM_freeN(param_arr);
+			if(data==NULL) {
+				RNA_property_boolean_set_array(ptr, prop, param_arr);
+				MEM_freeN(param_arr);
+			}
+
 			break;
 		}
 		case PROP_INT:
 		{
-			int *param_arr = MEM_mallocN(sizeof(int) * len, "pyrna int array");
+			int *param_arr;
+			if(data)	param_arr= (int*)data;
+			else		param_arr= MEM_mallocN(sizeof(int) * len, "pyrna int array");
+
 			
-			/* collect the variables before assigning, incase one of them is incorrect */
+			/* collect the variables */
 			for (i=0; i<len; i++) {
 				item = PySequence_GetItem(value, i);
 				param_arr[i] = (int)PyLong_AsSsize_t(item); /* deal with any errors later */
@@ -272,21 +282,26 @@
 			}
 			
 			if (PyErr_Occurred()) {
-				MEM_freeN(param_arr);
+				if(data==NULL)
+					MEM_freeN(param_arr);
 				PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence could not be used as an int");
 				return -1;
 			}
-			
-			RNA_property_int_set_array(ptr, prop, param_arr);
-			
-			MEM_freeN(param_arr);
+			if(data==NULL) {
+				RNA_property_int_set_array(ptr, prop, param_arr);
+				MEM_freeN(param_arr);
+			}
 			break;
 		}
 		case PROP_FLOAT:
 		{
-			float *param_arr = MEM_mallocN(sizeof(float) * len, "pyrna float array");
+			float *param_arr;
+			if(data)	param_arr = (float*)data;
+			else		param_arr = MEM_mallocN(sizeof(float) * len, "pyrna float array");
+
+
 			
-			/* collect the variables before assigning, incase one of them is incorrect */
+			/* collect the variables */
 			for (i=0; i<len; i++) {
 				item = PySequence_GetItem(value, i);
 				param_arr[i] = (float)PyFloat_AsDouble(item); /* deal with any errors later */
@@ -294,14 +309,15 @@
 			}
 			
 			if (PyErr_Occurred()) {
-				MEM_freeN(param_arr);
+				if(data==NULL)
+					MEM_freeN(param_arr);
 				PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence could not be used as a float");
 				return -1;
 			}
-			
-			RNA_property_float_set_array(ptr, prop, param_arr);
-			
-			MEM_freeN(param_arr);
+			if(data==NULL) {
+				RNA_property_float_set_array(ptr, prop, param_arr);				
+				MEM_freeN(param_arr);
+			}
 			break;
 		}
 		}
@@ -318,7 +334,8 @@
 				PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
 				return -1;
 			} else {
-				RNA_property_boolean_set(ptr, prop, param);
+				if(data)	*((int*)data)= param;
+				else		RNA_property_boolean_set(ptr, prop, param);
 			}
 			break;
 		}
@@ -329,7 +346,8 @@
 				PyErr_SetString(PyExc_TypeError, "expected an int type");
 				return -1;
 			} else {
-				RNA_property_int_set(ptr, prop, param);
+				if(data)	*((int*)data)= param;
+				else		RNA_property_int_set(ptr, prop, param);
 			}
 			break;
 		}
@@ -340,7 +358,8 @@
 				PyErr_SetString(PyExc_TypeError, "expected a float type");
 				return -1;
 			} else {
-				RNA_property_float_set(ptr, prop, param);
+				if(data)	*((float*)data)= param;
+				else		RNA_property_float_set(ptr, prop, param);
 			}
 			break;
 		}
@@ -352,7 +371,8 @@
 				PyErr_SetString(PyExc_TypeError, "expected a string type");
 				return -1;
 			} else {
-				RNA_property_string_set(ptr, prop, param);
+				if(data)	*((char**)data)= param;
+				else		RNA_property_string_set(ptr, prop, param);
 			}
 			break;
 		}
@@ -368,7 +388,8 @@
 			} else {
 				int val;
 				if (RNA_property_enum_value(ptr, prop, param, &val)) {
-					RNA_property_enum_set(ptr, prop, val);
+					if(data)	*((int*)data)= val;
+					else		RNA_property_enum_set(ptr, prop, val);
 				} else {
 					char *enum_str= pyrna_enum_as_string(ptr, prop);
 					PyErr_Format(PyExc_AttributeError, "enum \"%s\" not found in (%s)", param, enum_str);
@@ -390,10 +411,30 @@
 				return -1;
 			} else {
 				BPy_StructRNA *param= (BPy_StructRNA*)value;
-
-				if(RNA_struct_is_a(&param->ptr, ptype)) {
-					RNA_property_pointer_set(ptr, prop, param->ptr);
-				} else {
+				int raise_error= 0;
+				if(data) {
+					if(ptype == &RNA_AnyType) {
+						*((PointerRNA*)data)= param->ptr;
+					}
+					else if(RNA_struct_is_a(&param->ptr, ptype)) {
+						*((void**)data)= param->ptr.data;
+					} else {
+						raise_error= 1;
+					}
+				}
+				else {
+					/* data==NULL, assign to RNA */
+					if(RNA_struct_is_a(&param->ptr, ptype)) {
+						RNA_property_pointer_set(ptr, prop, param->ptr);
+					} else {
+						PointerRNA tmp;
+						RNA_pointer_create(NULL, ptype, NULL, &tmp);
+						PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(&tmp));
+						return -1;
+					}
+				}
+				
+				if(raise_error) {
 					PointerRNA tmp;
 					RNA_pointer_create(NULL, ptype, NULL, &tmp);
 					PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(&tmp));
@@ -403,7 +444,7 @@
 			break;
 		}
 		case PROP_COLLECTION:
-			PyErr_SetString(PyExc_AttributeError, "cant assign to collections");
+			PyErr_SetString(PyExc_AttributeError, "cant convert collections yet");
 			return -1;
 			break;
 		default:
@@ -778,7 +819,7 @@
 	}
 		
 	/* pyrna_py_to_prop sets its own exceptions */
-	return pyrna_py_to_prop(&self->ptr, prop, value);
+	return pyrna_py_to_prop(&self->ptr, prop, NULL, value);
 }
 
 PyObject *pyrna_prop_keys(BPy_PropertyRNA *self)
@@ -961,197 +1002,6 @@
 	}
 }
 
-int pyrna_py_to_param(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value)
-{
-	/* XXX hard limits should be checked here */
-	int type = RNA_property_type(ptr, prop);
-	int len = RNA_property_array_length(ptr, prop);
-	
-	if (len > 0) {
-		PyObject *item;
-		int i;
-		
-		if (!PySequence_Check(value)) {
-			PyErr_SetString(PyExc_TypeError, "expected a python sequence type assigned to an RNA array.");
-			return -1;
-		}
-		
-		if ((int)PySequence_Length(value) != len) {
-			PyErr_SetString(PyExc_AttributeError, "python sequence length did not match the RNA array.");
-			return -1;
-		}
-		
-		/* for arrays we have a limited number of types */
-		switch (type) {
-		case PROP_BOOLEAN:
-		{
-			int *param_arr = (int*)data;
-			
-			/* collect the variables before assigning, incase one of them is incorrect */
-			for (i=0; i<len; i++) {
-				item = PySequence_GetItem(value, i);
-				param_arr[i] = PyObject_IsTrue( item );
-				Py_DECREF(item);
-				
-				if (param_arr[i] < 0) {
-					PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence is not a boolean");
-					return -1;
-				}
-			}
-			
-			break;
-		}
-		case PROP_INT:
-		{
-			int *param_arr = (int*)data;
-			
-			/* collect the variables */
-			for (i=0; i<len; i++) {
-				item = PySequence_GetItem(value, i);
-				param_arr[i] = (int)PyLong_AsSsize_t(item); /* deal with any errors later */
-				Py_DECREF(item);
-			}
-			
-			if (PyErr_Occurred()) {
-				PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence could not be used as an int");
-				return -1;
-			}
-
-			break;
-		}
-		case PROP_FLOAT:
-		{
-			float *param_arr = (float*)data;
-			
-			/* collect the variables */
-			for (i=0; i<len; i++) {
-				item = PySequence_GetItem(value, i);
-				param_arr[i] = (float)PyFloat_AsDouble(item); /* deal with any errors later */
-				Py_DECREF(item);
-			}
-			
-			if (PyErr_Occurred()) {
-				PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence could not be used as a float");
-				return -1;
-			}
-			
-			break;
-		}
-		}
-	} else {
-		/* Normal Property (not an array) */
-		
-		/* see if we can coorce into a python type - PropertyType */
-		switch (type) {
-		case PROP_BOOLEAN:
-		{
-			int param = PyObject_IsTrue( value );
-			
-			if( param < 0 ) {
-				PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
-				return -1;
-			} else {
-				*((int*)data)= param;
-			}
-			break;
-		}
-		case PROP_INT:
-		{
-			int param = PyLong_AsSsize_t(value);
-			if (PyErr_Occurred()) {
-				PyErr_SetString(PyExc_TypeError, "expected an int type");
-				return -1;
-			} else {
-				*((int*)data)= param;
-			}
-			break;
-		}
-		case PROP_FLOAT:
-		{

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list