[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [13240] branches/blender2.5/blender/source /blender: More "data types" for the Operator property system.

Diego Borghetti (Plumiferos) bdiego at gmail.com
Tue Jan 15 05:49:10 CET 2008


Revision: 13240
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=13240
Author:   bdiego
Date:     2008-01-15 05:49:09 +0100 (Tue, 15 Jan 2008)

Log Message:
-----------
More "data types" for the Operator property system.

Now you can set/get: float, arrays (int and float) and string.

The only special function is OP_get_string, it is special
because return a pointer to the IDProperty data, so you can't
change/resize/free the string.

It's possible "fix" this with:
 1) Return a "const char"
 2) Return a duplicate string

All this new function are not in use yet, but i make a simple test
with the "move areas" operator (add a property of every type and then
print the result in the other size) and work fine, more test are welcome.

Other thing to check is the new OP_free_property function, because this
properties are only local to the operator, i choice free all this in the
"exit callback" of every operator (only move areas have property now), 
so comment about this are welcome too :)

Also add some notes to the WM_api.h file.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/editors/screen/screen_edit.c
    branches/blender2.5/blender/source/blender/windowmanager/WM_api.h
    branches/blender2.5/blender/source/blender/windowmanager/intern/wm.c
    branches/blender2.5/blender/source/blender/windowmanager/intern/wm_operators.c

Modified: branches/blender2.5/blender/source/blender/editors/screen/screen_edit.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/screen/screen_edit.c	2008-01-14 20:57:43 UTC (rev 13239)
+++ branches/blender2.5/blender/source/blender/editors/screen/screen_edit.c	2008-01-15 04:49:09 UTC (rev 13240)
@@ -956,7 +956,8 @@
 	/* this makes sure aligned edges will result in aligned grabbing */
 	removedouble_scrverts(C->screen);
 	removedouble_scredges(C->screen);
-	
+
+	OP_free_property(op);	
 	return 1;
 }
 

Modified: branches/blender2.5/blender/source/blender/windowmanager/WM_api.h
===================================================================
--- branches/blender2.5/blender/source/blender/windowmanager/WM_api.h	2008-01-14 20:57:43 UTC (rev 13239)
+++ branches/blender2.5/blender/source/blender/windowmanager/WM_api.h	2008-01-15 04:49:09 UTC (rev 13240)
@@ -86,17 +86,52 @@
  *
  * Some notes to take care:
  *
- * OP_set_int try to append a new property to the operator,
- * if the property already exist, just replace it with the
+ * All the OP_set_* functions append a new property to the operator,
+ * if the property already exist, just replace it with the new
  * value in other case make a new property and append it.
  *
- * OP_get_int return 0 on success (found the property) or
- * != 0 if can't found the property in the operator.
+ * The OP_get_string function is a "special case", this function
+ * return a pointer to property data, so don't change/resize/free
+ * the string, because probably we get a segfault.
+ * I really think that is better duplicate the string, so we are
+ * really sure that the property data don't change.
+ *
+ * OP_get_int/float/array return 0 on success (found the property)
+ * or != 0 if can't found the property in the operator.
  * The property value are store in the "value" pointer.
+ *
+ * Both array function copy the property data into the "array"
+ * pointer, but you need init the len pointer to the "array" size.
+ *
+ * For example:
+ *	int vec[] = { 1, 2, 3, 4 };
+ *	OP_set_int_array (op, "vector", vec, 4);
+ *
+ *	...
+ *
+ *	short len;
+ *	int vec[4];
+ *	len= 4; <---- set the size!!
+ *	OP_get_int_array (op, "vector", vec, &len);
  */
 void OP_set_int(wmOperator *op, char *name, int value);
+void OP_set_float(wmOperator *op, char *name, float value);
+void OP_set_string(wmOperator *op, char *name, char *str);
+void OP_set_int_array(wmOperator *op, char *name, int *array, short len);
+void OP_set_float_array(wmOperator *op, char *name, float *array, short len);
+
 int OP_get_int(wmOperator *op, char *name, int *value);
+int OP_get_float(wmOperator *op, char *name, float *value);
+char *OP_get_string(wmOperator *op, char *name);
+int OP_get_int_array(wmOperator *op, char *name, int *array, short *len);
+int OP_get_float_array(wmOperator *op, char *name, float *array, short *len);
 
+/*
+ * Need call this function in the "exit callback"
+ * of the operator, but only if you use the property system.
+ **/
+void OP_free_property(wmOperator *op);
+
 			/* OpenGL wrappers, mimicing opengl syntax */
 void		wmLoadMatrix		(wmWindow *win, float mat[][4]);
 void		wmGetMatrix			(wmWindow *win, float mat[][4]);

Modified: branches/blender2.5/blender/source/blender/windowmanager/intern/wm.c
===================================================================
--- branches/blender2.5/blender/source/blender/windowmanager/intern/wm.c	2008-01-14 20:57:43 UTC (rev 13239)
+++ branches/blender2.5/blender/source/blender/windowmanager/intern/wm.c	2008-01-15 04:49:09 UTC (rev 13240)
@@ -125,7 +125,6 @@
 /* context is allowed to be NULL, do net free wm itself (library.c) */
 void wm_close_and_free(bContext *C, wmWindowManager *wm)
 {
-	wmOperator *op;
 	wmWindow *win;
 	
 	while((win= wm->windows.first)) {
@@ -133,19 +132,6 @@
 		wm_window_free(C, win);
 	}
 	
-	op= wm->operators.first;
-	while(op) {
-		/*
-		 * Need this, because if the operator don't have
-		 * properties also don't have group.
-		 */
-		if(op->properties) {
-			IDP_FreeGroup(op->properties);
-			op->properties= NULL;
-		}
-		op= op->next;
-	}
-
 	BLI_freelistN(&wm->operators);
 	
 	BLI_freelistN(&wm->windowkeymap);

Modified: branches/blender2.5/blender/source/blender/windowmanager/intern/wm_operators.c
===================================================================
--- branches/blender2.5/blender/source/blender/windowmanager/intern/wm_operators.c	2008-01-14 20:57:43 UTC (rev 13239)
+++ branches/blender2.5/blender/source/blender/windowmanager/intern/wm_operators.c	2008-01-15 04:49:09 UTC (rev 13240)
@@ -158,6 +158,17 @@
 }
 
 /* ***** Property API, exported ***** */
+void OP_free_property(wmOperator *op)
+{
+	IDP_FreeProperty(op->properties);
+	/*
+	 * This need change, when the idprop code only
+	 * need call IDP_FreeProperty. (check BKE_idprop.h)
+	 */
+	MEM_freeN(op->properties);
+	op->properties= NULL;
+}
+
 void OP_set_int(wmOperator *op, char *name, int value)
 {
 	IDPropertyTemplate val;
@@ -171,6 +182,72 @@
 	IDP_ReplaceInGroup(op->properties, prop);
 }
 
+void OP_set_float(wmOperator *op, char *name, float value)
+{
+	IDPropertyTemplate val;
+	IDProperty *prop;
+
+	if(!op->properties)
+		op_init_property(op);
+
+	val.f= value;
+	prop= IDP_New(IDP_FLOAT, val, name);
+	IDP_ReplaceInGroup(op->properties, prop);
+}
+
+void OP_set_int_array(wmOperator *op, char *name, int *array, short len)
+{
+	IDPropertyTemplate val;
+	IDProperty *prop;
+	short i;
+	int *pointer;
+
+	if(!op->properties)
+		op_init_property(op);
+
+	val.array.len= len;
+	val.array.type= IDP_INT;
+	prop= IDP_New(IDP_ARRAY, val, name);
+
+	pointer= (int *)prop->data.pointer;
+	for(i= 0; i < len; i++)
+		pointer[i]= array[i];
+	IDP_ReplaceInGroup(op->properties, prop);
+}
+
+void OP_set_float_array(wmOperator *op, char *name, float *array, short len)
+{
+	IDPropertyTemplate val;
+	IDProperty *prop;
+	short i;
+	float *pointer;
+
+	if(!op->properties)
+		op_init_property(op);
+
+	val.array.len= len;
+	val.array.type= IDP_FLOAT;
+	prop= IDP_New(IDP_ARRAY, val, name);
+
+	pointer= (float *) prop->data.pointer;
+	for(i= 0; i < len; i++)
+		pointer[i]= array[i];
+	IDP_ReplaceInGroup(op->properties, prop);
+}
+
+void OP_set_string(wmOperator *op, char *name, char *str)
+{
+	IDPropertyTemplate val;
+	IDProperty *prop;
+
+	if(!op->properties)
+		op_init_property(op);
+
+	val.str= str;
+	prop= IDP_New(IDP_STRING, val, name);
+	IDP_ReplaceInGroup(op->properties, prop);
+}
+
 int OP_get_int(wmOperator *op, char *name, int *value)
 {
 	IDProperty *prop= op_get_property(op, name);
@@ -182,3 +259,61 @@
 	}
 	return (status);
 }
+
+int OP_get_float(wmOperator *op, char *name, float *value)
+{
+	IDProperty *prop= op_get_property(op, name);
+	int status= 1;
+
+	if ((prop) && (prop->type == IDP_FLOAT)) {
+		(*value)= *(float*)&prop->data.val;
+		status= 0;
+	}
+	return (status);
+}
+
+int OP_get_int_array(wmOperator *op, char *name, int *array, short *len)
+{
+	IDProperty *prop= op_get_property(op, name);
+	short i;
+	int status= 1;
+	int *pointer;
+
+	if ((prop) && (prop->type == IDP_ARRAY)) {
+		pointer= (int *) prop->data.pointer;
+
+		for(i= 0; (i < prop->len) && (i < *len); i++)
+			array[i]= pointer[i];
+
+		(*len)= i;
+		status= 0;
+	}
+	return (status);
+}
+
+int OP_get_float_array(wmOperator *op, char *name, float *array, short *len)
+{
+	IDProperty *prop= op_get_property(op, name);
+	short i;
+	float *pointer;
+	int status= 1;
+
+	if ((prop) && (prop->type == IDP_ARRAY)) {
+		pointer= (float *) prop->data.pointer;
+
+		for(i= 0; (i < prop->len) && (i < *len); i++)
+			array[i]= pointer[i];
+
+		(*len)= i;
+		status= 0;
+	}
+	return (status);
+}
+
+char *OP_get_string(wmOperator *op, char *name)
+{
+	IDProperty *prop= op_get_property(op, name);
+	if ((prop) && (prop->type == IDP_STRING))
+		return ((char *) prop->data.pointer);
+	return (NULL);
+}





More information about the Bf-blender-cvs mailing list