[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [33917] trunk/blender/source/blender: Drivers Code Cleanups and UI Tweaks:

Joshua Leung aligorith at gmail.com
Tue Dec 28 06:45:25 CET 2010


Revision: 33917
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=33917
Author:   aligorith
Date:     2010-12-28 06:45:15 +0100 (Tue, 28 Dec 2010)

Log Message:
-----------
Drivers Code Cleanups and UI Tweaks:
- Adding drivers from the UI (not from py-scripts though) will now
automatically add a "Transform Channel" driver variable to the newly
created drivers. This makes setting up drivers a bit more convenient
for the most commonly used case.

- Drivers now report their errors using the Reports system instead of
writing these directly to the console.

- Clarified some comments to be more insightful about the "why's" of
some design decisions, and related formatting/cleanup tweaks
- Reduced scope of "path" vars to just the scope they're required in

- Removed some unused defines from a failed experiment in the original
Keying Sets code ("templates" and "template flags") which was
superseeded by the more flexible + nicer "Builtin KeyingSets"

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/fcurve.c
    trunk/blender/source/blender/editors/animation/drivers.c
    trunk/blender/source/blender/editors/include/ED_keyframing.h
    trunk/blender/source/blender/editors/space_graph/graph_buttons.c
    trunk/blender/source/blender/editors/space_outliner/outliner.c
    trunk/blender/source/blender/makesdna/DNA_anim_types.h
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/source/blender/blenkernel/intern/fcurve.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/fcurve.c	2010-12-27 21:58:07 UTC (rev 33916)
+++ trunk/blender/source/blender/blenkernel/intern/fcurve.c	2010-12-28 05:45:15 UTC (rev 33917)
@@ -1329,7 +1329,7 @@
 	
 #ifdef WITH_PYTHON
 	/* since driver variables are cached, the expression needs re-compiling too */
-	if(driver->type==DRIVER_TYPE_PYTHON)
+	if (driver->type==DRIVER_TYPE_PYTHON)
 		driver->flag |= DRIVER_FLAG_RENAMEVAR;
 #endif
 

Modified: trunk/blender/source/blender/editors/animation/drivers.c
===================================================================
--- trunk/blender/source/blender/editors/animation/drivers.c	2010-12-27 21:58:07 UTC (rev 33916)
+++ trunk/blender/source/blender/editors/animation/drivers.c	2010-12-28 05:45:15 UTC (rev 33917)
@@ -39,7 +39,10 @@
 #include "BKE_depsgraph.h"
 #include "BKE_fcurve.h"
 #include "BKE_context.h"
+#include "BKE_report.h"
 
+#include "ED_keyframing.h"
+
 #include "UI_interface.h"
 
 #include "WM_api.h"
@@ -115,7 +118,7 @@
 /* Main Driver Management API calls:
  * 	Add a new driver for the specified property on the given ID block
  */
-short ANIM_add_driver (ID *id, const char rna_path[], int array_index, short UNUSED(flag), int type)
+short ANIM_add_driver (ReportList *reports, ID *id, const char rna_path[], int array_index, short flag, int type)
 {	
 	PointerRNA id_ptr, ptr;
 	PropertyRNA *prop;
@@ -126,7 +129,9 @@
 	/* validate pointer first - exit if failure */
 	RNA_id_pointer_create(id, &id_ptr);
 	if ((RNA_path_resolve(&id_ptr, rna_path, &ptr, &prop) == 0) || (prop == NULL)) {
-		printf("Add Driver: Could not add Driver, as RNA Path is invalid for the given ID (ID = %s, Path = %s)\n", id->name, rna_path);
+		BKE_reportf(reports, RPT_ERROR, 
+			"Could not add Driver, as RNA Path is invalid for the given ID (ID = %s, Path = %s)", 
+			id->name, rna_path);
 		return 0;
 	}
 	
@@ -153,7 +158,10 @@
 			/* set the type of the driver */
 			driver->type= type;
 			
-			/* fill in current value for python */
+			/* creating drivers for buttons will create the driver(s) with type 
+			 * "scripted expression" so that their values won't be lost immediately,
+			 * so here we copy those values over to the driver's expression
+			 */
 			if (type == DRIVER_TYPE_PYTHON) {
 				PropertyType proptype= RNA_property_type(prop);
 				int array= RNA_property_array_length(&ptr, prop);
@@ -180,6 +188,17 @@
 					BLI_snprintf(expression, maxlen, "%.3f", fval);
 				}
 			}
+			
+			/* for easier setup of drivers from UI, a driver variable should be 
+			 * added if flag is set (UI calls only)
+			 */
+			if (flag & CREATEDRIVER_WITH_DEFAULT_DVAR) {
+				/* assume that users will mostly want this to be of type "Transform Channel" too,
+				 * since this allows the easiest setting up of common rig components
+				 */
+				DriverVar *dvar = driver_add_new_variable(driver);
+				driver_change_variable_type(dvar, DVAR_TYPE_TRANSFORM_CHAN);
+			}
 		}
 		
 		/* set the done status */
@@ -193,23 +212,21 @@
 /* Main Driver Management API calls:
  * 	Remove the driver for the specified property on the given ID block (if available)
  */
-short ANIM_remove_driver (struct ID *id, const char rna_path[], int array_index, short UNUSED(flag))
+short ANIM_remove_driver (ReportList *reports, ID *id, const char rna_path[], int array_index, short UNUSED(flag))
 {
 	AnimData *adt;
 	FCurve *fcu;
 	int success= 0;
 	
-	/* get F-Curve
-	 * Note: here is one of the places where we don't want new F-Curve + Driver added!
-	 * 		so 'add' var must be 0
-	 */
 	/* we don't check the validity of the path here yet, but it should be ok... */
 	adt= BKE_animdata_from_id(id);
 	
-	if(adt) {
-		if(array_index == -1) {
+	if (adt) {
+		if (array_index == -1) {
+			/* step through all drivers, removing all of those with the same base path */
 			FCurve *fcu_iter= adt->drivers.first;
-			while((fcu= iter_step_fcurve(fcu_iter, rna_path))) {
+			
+			while ((fcu = iter_step_fcurve(fcu_iter, rna_path)) != NULL) {
 				/* store the next fcurve for looping  */
 				fcu_iter= fcu->next;
 				
@@ -222,8 +239,12 @@
 			}
 		}
 		else {
+			/* find the matching driver and remove it only 
+			 * Note: here is one of the places where we don't want new F-Curve + Driver added!
+			 * 		so 'add' var must be 0
+			 */
 			fcu= verify_driver_fcurve(id, rna_path, array_index, 0);
-			if(fcu) {
+			if (fcu) {
 				BLI_remlink(&adt->drivers, fcu);
 				free_fcurve(fcu);
 				
@@ -262,7 +283,7 @@
 /* Main Driver Management API calls:
  * 	Make a copy of the driver for the specified property on the given ID block
  */
-short ANIM_copy_driver (ID *id, const char rna_path[], int array_index, short UNUSED(flag))
+short ANIM_copy_driver (ReportList *reports, ID *id, const char rna_path[], int array_index, short UNUSED(flag))
 {
 	PointerRNA id_ptr, ptr;
 	PropertyRNA *prop;
@@ -271,7 +292,9 @@
 	/* validate pointer first - exit if failure */
 	RNA_id_pointer_create(id, &id_ptr);
 	if ((RNA_path_resolve(&id_ptr, rna_path, &ptr, &prop) == 0) || (prop == NULL)) {
-		printf("Copy Driver: Could not find Driver, as RNA Path is invalid for the given ID (ID = %s, Path = %s)\n", id->name, rna_path);
+		BKE_reportf(reports, RPT_ERROR,
+			"Could not find Driver to copy, as RNA Path is invalid for the given ID (ID = %s, Path = %s)", 
+			id->name, rna_path);
 		return 0;
 	}
 	
@@ -307,7 +330,7 @@
  * 	Add a new driver for the specified property on the given ID block or replace an existing one
  *	with the driver + driver-curve data from the buffer 
  */
-short ANIM_paste_driver (ID *id, const char rna_path[], int array_index, short UNUSED(flag))
+short ANIM_paste_driver (ReportList *reports, ID *id, const char rna_path[], int array_index, short UNUSED(flag))
 {	
 	PointerRNA id_ptr, ptr;
 	PropertyRNA *prop;
@@ -316,13 +339,15 @@
 	/* validate pointer first - exit if failure */
 	RNA_id_pointer_create(id, &id_ptr);
 	if ((RNA_path_resolve(&id_ptr, rna_path, &ptr, &prop) == 0) || (prop == NULL)) {
-		printf("Paste Driver: Could not add Driver, as RNA Path is invalid for the given ID (ID = %s, Path = %s)\n", id->name, rna_path);
+		BKE_reportf(reports, RPT_ERROR,
+			"Could not paste Driver, as RNA Path is invalid for the given ID (ID = %s, Path = %s)", 
+			id->name, rna_path);
 		return 0;
 	}
 	
 	/* if the buffer is empty, cannot paste... */
 	if (channeldriver_copypaste_buf == NULL) {
-		printf("Paste Driver: No Driver to paste. \n");
+		BKE_report(reports, RPT_ERROR, "Paste Driver: No Driver to paste.");
 		return 0;
 	}
 	
@@ -366,7 +391,6 @@
 {
 	PointerRNA ptr= {{0}};
 	PropertyRNA *prop= NULL;
-	char *path;
 	short success= 0;
 	int index, all= RNA_boolean_get(op->ptr, "all");
 	
@@ -377,10 +401,11 @@
 		index= -1;
 
 	if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
-		path= RNA_path_from_ID_to_property(&ptr, prop);
+		char *path= RNA_path_from_ID_to_property(&ptr, prop);
+		short flags = CREATEDRIVER_WITH_DEFAULT_DVAR;
 		
 		if (path) {			
-			success+= ANIM_add_driver(ptr.id.data, path, index, 0, DRIVER_TYPE_PYTHON);
+			success+= ANIM_add_driver(op->reports, ptr.id.data, path, index, flags, DRIVER_TYPE_PYTHON);
 			
 			MEM_freeN(path);
 		}
@@ -389,7 +414,7 @@
 	if (success) {
 		/* send updates */
 		uiContextAnimUpdate(C);
-
+		
 		DAG_ids_flush_update(CTX_data_main(C), 0);
 		
 		WM_event_add_notifier(C, NC_ANIMATION|ND_FCURVES_ORDER, NULL); // XXX
@@ -422,7 +447,6 @@
 {
 	PointerRNA ptr= {{0}};
 	PropertyRNA *prop= NULL;
-	char *path;
 	short success= 0;
 	int index, all= RNA_boolean_get(op->ptr, "all");
 	
@@ -433,15 +457,16 @@
 		index= -1;
 
 	if (ptr.id.data && ptr.data && prop) {
-		path= RNA_path_from_ID_to_property(&ptr, prop);
-		success= ANIM_remove_driver(ptr.id.data, path, index, 0);
+		char *path= RNA_path_from_ID_to_property(&ptr, prop);
+		
+		success= ANIM_remove_driver(op->reports, ptr.id.data, path, index, 0);
 		MEM_freeN(path);
 	}
 	
 	if (success) {
 		/* send updates */
 		uiContextAnimUpdate(C);
-
+		
 		DAG_ids_flush_update(CTX_data_main(C), 0);
 		
 		WM_event_add_notifier(C, NC_ANIMATION|ND_FCURVES_ORDER, NULL);  // XXX
@@ -470,11 +495,10 @@
 
 /* Copy Driver Button Operator ------------------------ */
 
-static int copy_driver_button_exec (bContext *C, wmOperator *UNUSED(op))
+static int copy_driver_button_exec (bContext *C, wmOperator *op)
 {
 	PointerRNA ptr= {{0}};
 	PropertyRNA *prop= NULL;
-	char *path;
 	short success= 0;
 	int index;
 	
@@ -482,12 +506,12 @@
 	uiContextActiveProperty(C, &ptr, &prop, &index);
 	
 	if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
-		path= RNA_path_from_ID_to_property(&ptr, prop);
+		char *path= RNA_path_from_ID_to_property(&ptr, prop);
 		
 		if (path) {
 			/* only copy the driver for the button that this was involved for */
-			success= ANIM_copy_driver(ptr.id.data, path, index, 0);
-
+			success= ANIM_copy_driver(op->reports, ptr.id.data, path, index, 0);
+			
 			uiContextAnimUpdate(C);
 			
 			MEM_freeN(path);
@@ -515,11 +539,10 @@
 
 /* Paste Driver Button Operator ------------------------ */
 
-static int paste_driver_button_exec (bContext *C, wmOperator *UNUSED(op))
+static int paste_driver_button_exec (bContext *C, wmOperator *op)
 {
 	PointerRNA ptr= {{0}};
 	PropertyRNA *prop= NULL;
-	char *path;
 	short success= 0;
 	int index;
 	
@@ -527,12 +550,12 @@
 	uiContextActiveProperty(C, &ptr, &prop, &index);
 	
 	if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
-		path= RNA_path_from_ID_to_property(&ptr, prop);
+		char *path= RNA_path_from_ID_to_property(&ptr, prop);
 		
 		if (path) {
 			/* only copy the driver for the button that this was involved for */
-			success= ANIM_paste_driver(ptr.id.data, path, index, 0);
-
+			success= ANIM_paste_driver(op->reports, ptr.id.data, path, index, 0);
+			
 			uiContextAnimUpdate(C);
 			
 			MEM_freeN(path);

Modified: trunk/blender/source/blender/editors/include/ED_keyframing.h
===================================================================
--- trunk/blender/source/blender/editors/include/ED_keyframing.h	2010-12-27 21:58:07 UTC (rev 33916)
+++ trunk/blender/source/blender/editors/include/ED_keyframing.h	2010-12-28 05:45:15 UTC (rev 33917)
@@ -209,29 +209,36 @@
 
 /* ************ Drivers ********************** */
 
+/* Flags for use by driver creation calls */
+typedef enum eCreateDriverFlags {

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list