[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [34315] trunk/blender/source/blender/ editors/animation: Driver creation hack:

Joshua Leung aligorith at gmail.com
Fri Jan 14 03:06:37 CET 2011


Revision: 34315
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=34315
Author:   aligorith
Date:     2011-01-14 02:06:35 +0000 (Fri, 14 Jan 2011)
Log Message:
-----------
Driver creation hack:

Drivers created from the Properties Editor for Materials and Textures
will now be created on Object-level instead of on their owner
Material/Texture as for their keyframes.

The intention of this hack is to allow users to be able to easily set
up drivers for materials and textures. Without this hack, users would
have had to do this manually via the Datablocks editor (I've described
this method a few times in detail, though this still attracts
complaints), as the way the depsgraph works does not allow ID blocks
other than Objects and directly-linked Object data to be driven. As a
result, although this hack can be done for these two cases, there are
no workarounds possible for Scene and Scene-linked settings.

There are 2 issues that will be noticed with this approach:
1) There may be confusion over why the drivers are found under Object
level and not Material/Texture level.
2) Driver status will not be shown in the buttons, leading to attempts
to try to keyframe the properties directly later and subsequent
confusion when finding that that won't work.

However, these are the sacrifices we'll need to make to get easy-setup
working in the meantime until the proper fixes can be done.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/animation/drivers.c
    trunk/blender/source/blender/editors/animation/keyframing.c

Modified: trunk/blender/source/blender/editors/animation/drivers.c
===================================================================
--- trunk/blender/source/blender/editors/animation/drivers.c	2011-01-14 00:23:21 UTC (rev 34314)
+++ trunk/blender/source/blender/editors/animation/drivers.c	2011-01-14 02:06:35 UTC (rev 34315)
@@ -27,6 +27,7 @@
  * ***** END GPL LICENSE BLOCK *****
  */
  
+#include <stdio.h>
 #include <string.h>
 
 #include "MEM_guardedalloc.h"
@@ -35,12 +36,19 @@
 #include "BLI_utildefines.h"
 
 #include "DNA_anim_types.h"
+#include "DNA_object_types.h"
+#include "DNA_material_types.h"
+#include "DNA_texture_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_space_types.h"
 
 #include "BKE_animsys.h"
 #include "BKE_depsgraph.h"
 #include "BKE_fcurve.h"
 #include "BKE_context.h"
 #include "BKE_report.h"
+#include "BKE_material.h"
+#include "BKE_texture.h"
 
 #include "ED_keyframing.h"
 
@@ -386,6 +394,83 @@
 /* ************************************************** */
 /* UI-Button Interface */
 
+/* Temporary wrapper for driver operators for buttons to make it easier to create
+ * such drivers by rerouting all paths through the active object instead so that
+ * they will get picked up by the dependency system.
+ *
+ * < C: context pointer - for getting active data 
+ * <> ptr: RNA pointer for property's datablock. May be modified as result of path remapping.
+ * < prop: RNA definition of property to add for
+ *
+ * > returns: MEM_alloc'd string representing the path to the property from the given PointerRNA
+ */
+static char *get_driver_path_hack (bContext *C, PointerRNA *ptr, PropertyRNA *prop)
+{
+	ID *id = (ID *)ptr->id.data;
+	ScrArea *sa = CTX_wm_area(C);
+	
+	/* get standard path which may be extended */
+	char *basepath = RNA_path_from_ID_to_property(ptr, prop);
+	char *path = basepath; /* in case no remapping is needed */
+	
+	/* Remapping will only be performed in the Properties Editor, as only this 
+	 * restricts the subspace of options to the 'active' data (a manageable state)
+	 */
+	// TODO: watch out for pinned context?
+	if ((sa) && (sa->spacetype == SPACE_BUTS)) {
+		Object *ob = CTX_data_active_object(C);
+		
+		if (ob && id) {
+			/* only id-types which can be remapped to go through objects should be considered */
+			switch (GS(id->name)) {
+				case ID_MA: /* materials */
+				{
+					Material *ma = give_current_material(ob, ob->actcol);
+					
+					/* assumes: material will only be shown if it is active objects's active material it's ok */
+					if ((ID*)ma == id) {
+						/* create new path */
+						// TODO: use RNA path functions to construct instead?
+						path = BLI_sprintfN("material_slots[\"%s\"].material.%s",
+							ma->id.name+2, basepath);
+							
+						/* free old one */
+						MEM_freeN(basepath);
+					}
+				}
+					break;
+					
+				case ID_TE: /* textures */
+				{
+					Material *ma = give_current_material(ob, ob->actcol);
+					Tex *tex = give_current_material_texture(ma);
+					
+					/* assumes: texture will only be shown if it is active material's active texture it's ok */
+					if ((ID*)tex == id) {
+						/* create new path */
+						// TODO: use RNA path functions to construct step by step instead?
+						path = BLI_sprintfN("material_slots[\"%s\"].material.texture_slots[\"%s\"].texture.%s", 
+							ma->id.name+2, tex->id.name+2, basepath);
+							
+						/* free old one */
+						MEM_freeN(basepath);
+					}
+				}
+					break;
+			}
+			
+			/* fix RNA pointer, as we've now changed the ID root by changing the paths */
+			if (basepath != path) {
+				/* rebase provided pointer so that it starts from object... */
+				RNA_pointer_create(&ob->id, ptr->type, ptr->data, ptr);
+			}
+		}
+	}
+	
+	/* the path should now have been corrected for use */
+	return path;
+}
+
 /* Add Driver Button Operator ------------------------ */
 
 static int add_driver_button_exec (bContext *C, wmOperator *op)
@@ -397,12 +482,12 @@
 	
 	/* try to create driver using property retrieved from UI */
 	uiContextActiveProperty(C, &ptr, &prop, &index);
-
+	
 	if (all)
 		index= -1;
-
+	
 	if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
-		char *path= RNA_path_from_ID_to_property(&ptr, prop);
+		char *path= get_driver_path_hack(C, &ptr, prop);
 		short flags = CREATEDRIVER_WITH_DEFAULT_DVAR;
 		
 		if (path) {			
@@ -456,9 +541,9 @@
 	
 	if (all)
 		index= -1;
-
+	
 	if (ptr.id.data && ptr.data && prop) {
-		char *path= RNA_path_from_ID_to_property(&ptr, prop);
+		char *path= get_driver_path_hack(C, &ptr, prop);
 		
 		success= ANIM_remove_driver(op->reports, ptr.id.data, path, index, 0);
 		MEM_freeN(path);
@@ -507,7 +592,7 @@
 	uiContextActiveProperty(C, &ptr, &prop, &index);
 	
 	if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
-		char *path= RNA_path_from_ID_to_property(&ptr, prop);
+		char *path= get_driver_path_hack(C, &ptr, prop);
 		
 		if (path) {
 			/* only copy the driver for the button that this was involved for */
@@ -551,7 +636,7 @@
 	uiContextActiveProperty(C, &ptr, &prop, &index);
 	
 	if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
-		char *path= RNA_path_from_ID_to_property(&ptr, prop);
+		char *path= get_driver_path_hack(C, &ptr, prop);
 		
 		if (path) {
 			/* only copy the driver for the button that this was involved for */

Modified: trunk/blender/source/blender/editors/animation/keyframing.c
===================================================================
--- trunk/blender/source/blender/editors/animation/keyframing.c	2011-01-14 00:23:21 UTC (rev 34314)
+++ trunk/blender/source/blender/editors/animation/keyframing.c	2011-01-14 02:06:35 UTC (rev 34315)
@@ -231,7 +231,7 @@
 				dst->vec[0][1] += dy;
 				dst->vec[1][1] += dy;
 				dst->vec[2][1] += dy;
-
+				
 				dst->f1= bezt->f1;
 				dst->f2= bezt->f2;
 				dst->f3= bezt->f3;




More information about the Bf-blender-cvs mailing list