[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43308] trunk/blender/source/blender: running operators now uses last used settings, added reset button to set defaults.

Campbell Barton ideasman42 at gmail.com
Wed Jan 11 20:33:14 CET 2012


Revision: 43308
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43308
Author:   campbellbarton
Date:     2012-01-11 19:33:14 +0000 (Wed, 11 Jan 2012)
Log Message:
-----------
running operators now uses last used settings, added reset button to set defaults.

details
- uses redo stack to get recent settings from.
- adds a flag to IDProperties so RNA_property_is_set() can return false even if the property is exists.
- PROP_SKIP_SAVE option skips these settings from getting reset (as with presets).

Modified Paths:
--------------
    trunk/blender/source/blender/editors/interface/interface_layout.c
    trunk/blender/source/blender/makesdna/DNA_ID.h
    trunk/blender/source/blender/makesrna/intern/rna_access.c
    trunk/blender/source/blender/windowmanager/WM_api.h
    trunk/blender/source/blender/windowmanager/intern/wm_event_system.c
    trunk/blender/source/blender/windowmanager/intern/wm_operators.c

Modified: trunk/blender/source/blender/editors/interface/interface_layout.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_layout.c	2012-01-11 18:26:47 UTC (rev 43307)
+++ trunk/blender/source/blender/editors/interface/interface_layout.c	2012-01-11 19:33:14 UTC (rev 43308)
@@ -2742,6 +2742,11 @@
 	return str;
 }
 
+static void ui_layout_operator_buts__reset_cb(bContext *UNUSED(C), void *op_pt, void *UNUSED(arg_dummy2))
+{
+	WM_operator_properties_reset((wmOperator *)op_pt);
+}
+
 /* this function does not initialize the layout, functions can be called on the layout before and after */
 void uiLayoutOperatorButs(const bContext *C, uiLayout *layout, wmOperator *op,int (*check_prop)(struct PointerRNA *, struct PropertyRNA *), const char label_align, const short flag)
 {
@@ -2803,7 +2808,22 @@
 			uiItemL(layout, IFACE_("No Properties"), ICON_NONE);
 		}
 	}
-	
+
+	/* its possible that reset can do nothing if all have PROP_SKIP_SAVE enabled
+	 * but this is not so important if this button is drawn in those cases
+	 * (which isn't all that likely anyway) - campbell */
+	if (op->properties->len) {
+		uiBlock *block;
+		uiBut *but;
+		uiLayout *col; /* needed to avoid alignment errors with previous buttons */
+
+		col= uiLayoutColumn(layout, 0);
+		block= uiLayoutGetBlock(col);
+		but = uiDefIconTextBut(block , BUT, 0, ICON_FILE_REFRESH, "Reset", 0, 0, 18, 20, NULL, 0.0, 0.0, 0.0, 0.0,
+		                       "Reset operator defaults");
+		uiButSetFunc(but, ui_layout_operator_buts__reset_cb, op, NULL);
+	}
+
 	/* set various special settings for buttons */
 	{
 		uiBut *but;

Modified: trunk/blender/source/blender/makesdna/DNA_ID.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_ID.h	2012-01-11 18:26:47 UTC (rev 43307)
+++ trunk/blender/source/blender/makesdna/DNA_ID.h	2012-01-11 19:33:14 UTC (rev 43308)
@@ -86,6 +86,10 @@
 /* IDP_STRING */
 #define IDP_STRING_SUB_UTF8  0 /* default */
 #define IDP_STRING_SUB_BYTE  1 /* arbitrary byte array, _not_ null terminated */
+/*->flag*/
+#define IDP_FLAG_GHOST (1<<7)  /* this means the propery is set but RNA will return
+                                * false when checking 'RNA_property_is_set',
+                                * currently this is a runtime flag */
 
 
 /* add any future new id property types here.*/

Modified: trunk/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_access.c	2012-01-11 18:26:47 UTC (rev 43307)
+++ trunk/blender/source/blender/makesrna/intern/rna_access.c	2012-01-11 19:33:14 UTC (rev 43308)
@@ -226,6 +226,12 @@
 
 /* ID Properties */
 
+static void rna_idproperty_touch(IDProperty *idprop)
+{
+	/* so the property is seen as 'set' by rna */
+	idprop->flag &= ~IDP_FLAG_GHOST;
+}
+
 /* return a UI local ID prop definition for this prop */
 IDProperty *rna_idproperty_ui(PropertyRNA *prop)
 {
@@ -1621,8 +1627,10 @@
 	/* just incase other values are passed */
 	if(value) value= 1;
 
-	if((idprop=rna_idproperty_check(&prop, ptr)))
+	if((idprop=rna_idproperty_check(&prop, ptr))) {
 		IDP_Int(idprop)= value;
+		rna_idproperty_touch(idprop);
+	}
 	else if(bprop->set)
 		bprop->set(ptr, value);
 	else if(prop->flag & PROP_EDITABLE) {
@@ -1698,6 +1706,8 @@
 			IDP_Int(idprop)= values[0];
 		else
 			memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);
+
+		rna_idproperty_touch(idprop);
 	}
 	else if(prop->arraydimension == 0)
 		RNA_property_boolean_set(ptr, prop, values[0]);
@@ -1818,8 +1828,10 @@
 	/* useful to check on bad values but set function should clamp */
 	/* BLI_assert(RNA_property_int_clamp(ptr, prop, &value) == 0); */
 
-	if((idprop=rna_idproperty_check(&prop, ptr)))
+	if((idprop=rna_idproperty_check(&prop, ptr))) {
 		IDP_Int(idprop)= value;
+		rna_idproperty_touch(idprop);
+	}
 	else if(iprop->set)
 		iprop->set(ptr, value);
 	else if(prop->flag & PROP_EDITABLE) {
@@ -1931,7 +1943,9 @@
 		if(prop->arraydimension == 0)
 			IDP_Int(idprop)= values[0];
 		else
-			memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);\
+			memcpy(IDP_Array(idprop), values, sizeof(int)*idprop->len);
+
+		rna_idproperty_touch(idprop);
 	}
 	else if(prop->arraydimension == 0)
 		RNA_property_int_set(ptr, prop, values[0]);
@@ -2054,6 +2068,8 @@
 			IDP_Float(idprop)= value;
 		else
 			IDP_Double(idprop)= value;
+
+		rna_idproperty_touch(idprop);
 	}
 	else if(fprop->set) {
 		fprop->set(ptr, value);
@@ -2185,6 +2201,8 @@
 			for(i=0; i<idprop->len; i++)
 				((double*)IDP_Array(idprop))[i]= values[i];
 		}
+
+		rna_idproperty_touch(idprop);
 	}
 	else if(prop->arraydimension == 0)
 		RNA_property_float_set(ptr, prop, values[0]);
@@ -2372,9 +2390,11 @@
 
 	BLI_assert(RNA_property_type(prop) == PROP_STRING);
 
-	if((idprop=rna_idproperty_check(&prop, ptr)))
+	if((idprop=rna_idproperty_check(&prop, ptr))) {
 		/* both IDP_STRING_SUB_BYTE / IDP_STRING_SUB_UTF8 */
 		IDP_AssignString(idprop, value, RNA_property_string_maxlength(prop) - 1);
+		rna_idproperty_touch(idprop);
+	}
 	else if(sprop->set)
 		sprop->set(ptr, value); /* set function needs to clamp its self */
 	else if(prop->flag & PROP_EDITABLE) {
@@ -2446,8 +2466,10 @@
 
 	BLI_assert(RNA_property_type(prop) == PROP_ENUM);
 
-	if((idprop=rna_idproperty_check(&prop, ptr)))
+	if((idprop=rna_idproperty_check(&prop, ptr))) {
 		IDP_Int(idprop)= value;
+		rna_idproperty_touch(idprop);
+	}
 	else if(eprop->set) {
 		eprop->set(ptr, value);
 	}
@@ -2516,6 +2538,7 @@
 
 	if((/*idprop=*/ rna_idproperty_check(&prop, ptr))) {
 		/* not supported */
+		/* rna_idproperty_touch(idprop); */
 	}
 	else {
 		PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;
@@ -4415,7 +4438,8 @@
 int RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
 {
 	if(prop->flag & PROP_IDPROPERTY) {
-		return (rna_idproperty_find(ptr, prop->identifier) != NULL);
+		IDProperty *idprop = rna_idproperty_find(ptr, prop->identifier);
+		return ((idprop != NULL) && !(idprop->flag & IDP_FLAG_GHOST));
 	}
 	else {
 		return 1;

Modified: trunk/blender/source/blender/windowmanager/WM_api.h
===================================================================
--- trunk/blender/source/blender/windowmanager/WM_api.h	2012-01-11 18:26:47 UTC (rev 43307)
+++ trunk/blender/source/blender/windowmanager/WM_api.h	2012-01-11 19:33:14 UTC (rev 43308)
@@ -199,6 +199,7 @@
 
 void		WM_operator_properties_alloc(struct PointerRNA **ptr, struct IDProperty **properties, const char *opstring); /* used for keymap and macro items */
 void		WM_operator_properties_sanitize(struct PointerRNA *ptr, const short no_context); /* make props context sensitive or not */
+void        WM_operator_properties_reset(struct wmOperator *op);
 void		WM_operator_properties_create(struct PointerRNA *ptr, const char *opstring);
 void		WM_operator_properties_create_ptr(struct PointerRNA *ptr, struct wmOperatorType *ot);
 void		WM_operator_properties_free(struct PointerRNA *ptr);

Modified: trunk/blender/source/blender/windowmanager/intern/wm_event_system.c
===================================================================
--- trunk/blender/source/blender/windowmanager/intern/wm_event_system.c	2012-01-11 18:26:47 UTC (rev 43307)
+++ trunk/blender/source/blender/windowmanager/intern/wm_event_system.c	2012-01-11 19:33:14 UTC (rev 43308)
@@ -729,6 +729,46 @@
 	}
 }
 
+static int wm_operator_init_from_last(wmWindowManager *wm, wmOperator *op)
+{
+	int change= FALSE;
+	wmOperator *lastop;
+
+	for(lastop= wm->operators.last; lastop; lastop= lastop->prev) {
+		/* equality check is a bit paranoid but just incase */
+		if((op != lastop) && (op->type == (lastop->type))) {
+			break;
+		}
+	}
+
+	if (lastop && op != lastop) {
+		PropertyRNA *iterprop;
+		iterprop= RNA_struct_iterator_property(op->type->srna);
+
+		RNA_PROP_BEGIN(op->ptr, itemptr, iterprop) {
+			PropertyRNA *prop= itemptr.data;
+
+			if((RNA_property_flag(prop) & PROP_SKIP_SAVE) == 0) {
+				const char *identifier= RNA_property_identifier(prop);
+				IDProperty *idp_src= IDP_GetPropertyFromGroup(lastop->properties, identifier);
+				if(idp_src) {
+					IDProperty *idp_dst = IDP_CopyProperty(idp_src);
+
+					/* note - in the future this may need to be done recursively,
+					 * but for now RNA doesn't access nested operators */
+					idp_dst->flag |= IDP_FLAG_GHOST;
+
+					IDP_ReplaceInGroup(op->properties, idp_dst);
+					change= TRUE;
+				}
+			}
+		}
+		RNA_PROP_END;
+	}
+
+	return change;
+}
+
 static int wm_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event, PointerRNA *properties, ReportList *reports, short poll_only)
 {
 	wmWindowManager *wm= CTX_wm_manager(C);
@@ -741,6 +781,11 @@
 	if(WM_operator_poll(C, ot)) {
 		wmOperator *op= wm_operator_create(wm, ot, properties, reports); /* if reports==NULL, theyll be initialized */
 		
+		/* initialize setting from previous run */
+		if(wm->op_undo_depth == 0 && (ot->flag & OPTYPE_REGISTER)) { /* not called by py script */
+			wm_operator_init_from_last(wm, op);
+		}
+
 		if((G.f & G_DEBUG) && event && event->type!=MOUSEMOVE)
 			printf("handle evt %d win %d op %s\n", event?event->type:0, CTX_wm_screen(C)->subwinactive, ot->idname); 
 		

Modified: trunk/blender/source/blender/windowmanager/intern/wm_operators.c
===================================================================
--- trunk/blender/source/blender/windowmanager/intern/wm_operators.c	2012-01-11 18:26:47 UTC (rev 43307)
+++ trunk/blender/source/blender/windowmanager/intern/wm_operators.c	2012-01-11 19:33:14 UTC (rev 43308)
@@ -638,6 +638,25 @@
 	RNA_STRUCT_END;
 }
 
+/* remove all props without PROP_SKIP_SAVE */
+void WM_operator_properties_reset(wmOperator *op)
+{
+	if (op->ptr->data) {
+		PropertyRNA *iterprop;
+		iterprop= RNA_struct_iterator_property(op->type->srna);
+
+		RNA_PROP_BEGIN(op->ptr, itemptr, iterprop) {
+			PropertyRNA *prop= itemptr.data;
+
+			if((RNA_property_flag(prop) & PROP_SKIP_SAVE) == 0) {
+				const char *identifier = RNA_property_identifier(prop);
+				RNA_struct_idprops_unset(op->ptr, identifier);
+			}
+		}
+		RNA_PROP_END;
+	}
+}
+
 void WM_operator_properties_free(PointerRNA *ptr)
 {
 	IDProperty *properties= ptr->data;



More information about the Bf-blender-cvs mailing list