[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [36342] trunk/blender/source/blender: Adding support for adding copies of existing drivers to other animdata

Joshua Leung aligorith at gmail.com
Tue Apr 26 15:49:40 CEST 2011


Revision: 36342
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=36342
Author:   aligorith
Date:     2011-04-26 13:49:40 +0000 (Tue, 26 Apr 2011)
Log Message:
-----------
Adding support for adding copies of existing drivers to other animdata
blocks via PyAPI/RNA

For example:
ob = bpy.context.active_object # assumes default cube has some drivers
added already before running script
dst = bpy.data.objects["Camera"]

adt = dst.animation_data_create()
for driver in ob.animation_data.drivers:
    new_driver = adt.drivers.from_existing(driver)

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/anim_sys.c
    trunk/blender/source/blender/makesrna/intern/rna_animation.c

Modified: trunk/blender/source/blender/blenkernel/intern/anim_sys.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/anim_sys.c	2011-04-26 13:36:21 UTC (rev 36341)
+++ trunk/blender/source/blender/blenkernel/intern/anim_sys.c	2011-04-26 13:49:40 UTC (rev 36342)
@@ -194,7 +194,7 @@
 	dadt= MEM_dupallocN(adt);
 	
 	/* make a copy of action - at worst, user has to delete copies... */
-	if(do_action) {
+	if (do_action) {
 		dadt->action= copy_action(adt->action);
 		dadt->tmpact= copy_action(adt->tmpact);
 	}
@@ -216,11 +216,11 @@
 	return dadt;
 }
 
-int BKE_copy_animdata_id(struct ID *id_to, struct ID *id_from, const short do_action)
+int BKE_copy_animdata_id (ID *id_to, ID *id_from, const short do_action)
 {
 	AnimData *adt;
 
-	if((id_to && id_from) && (GS(id_to->name) != GS(id_from->name)))
+	if ((id_to && id_from) && (GS(id_to->name) != GS(id_from->name)))
 		return 0;
 
 	BKE_free_animdata(id_to);
@@ -237,13 +237,13 @@
 void BKE_copy_animdata_id_action(struct ID *id)
 {
 	AnimData *adt= BKE_animdata_from_id(id);
-	if(adt) {
-		if(adt->action) {
-			((ID *)adt->action)->us--;
+	if (adt) {
+		if (adt->action) {
+			id_us_min((ID *)adt->action);
 			adt->action= copy_action(adt->action);
 		}
-		if(adt->tmpact) {
-			((ID *)adt->tmpact)->us--;
+		if (adt->tmpact) {
+			id_us_min((ID *)adt->tmpact);
 			adt->tmpact= copy_action(adt->tmpact);
 		}
 	}

Modified: trunk/blender/source/blender/makesrna/intern/rna_animation.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_animation.c	2011-04-26 13:36:21 UTC (rev 36341)
+++ trunk/blender/source/blender/makesrna/intern/rna_animation.c	2011-04-26 13:49:40 UTC (rev 36342)
@@ -457,6 +457,24 @@
 	BKE_nlatrack_set_active(&adt->nla_tracks, track);
 }
 
+
+static FCurve *rna_Driver_from_existing(AnimData *adt, bContext *C, FCurve *src_driver)
+{
+	/* verify that we've got a driver to duplicate */
+	if (ELEM(NULL, src_driver, src_driver->driver)) {
+		BKE_reportf(CTX_wm_reports(C), RPT_ERROR, "No valid driver data to create copy of");
+		return NULL;
+	}
+	else {
+		/* just make a copy of the existing one and add to self */
+		FCurve *new_fcu = copy_fcurve(src_driver);
+		
+		// XXX: if we impose any ordering on these someday, this will be problematic
+		BLI_addtail(&adt->drivers, new_fcu);
+		return new_fcu;
+	}
+}
+
 #else
 
 /* helper function for Keying Set -> keying settings */
@@ -724,7 +742,7 @@
 	
 	func = RNA_def_function(srna, "new", "rna_NlaTrack_new");
 	RNA_def_function_flag(func, FUNC_USE_CONTEXT);
-	RNA_def_function_ui_description(func, "Add a new NLA Tracks");
+	RNA_def_function_ui_description(func, "Add a new NLA Track");
 	RNA_def_pointer(func, "prev", "NlaTrack", "", "NLA Track to add the new one after.");
 	/* return type */
 	parm = RNA_def_pointer(func, "track", "NlaTrack", "", "New NLA Track.");
@@ -745,6 +763,28 @@
 	RNA_def_property_update(prop, NC_ANIMATION|ND_NLA|NA_SELECTED, NULL);
 }
 
+static void rna_api_animdata_drivers(BlenderRNA *brna, PropertyRNA *cprop)
+{
+	StructRNA *srna;
+	PropertyRNA *parm;
+	FunctionRNA *func;
+
+	PropertyRNA *prop;
+	
+	RNA_def_property_srna(cprop, "AnimDataDrivers");
+	srna= RNA_def_struct(brna, "AnimDataDrivers", NULL);
+	RNA_def_struct_sdna(srna, "AnimData");
+	RNA_def_struct_ui_text(srna, "Drivers", "Collection of Driver F-Curves");
+	
+	func = RNA_def_function(srna, "from_existing", "rna_Driver_from_existing");
+	RNA_def_function_flag(func, FUNC_USE_CONTEXT);
+	RNA_def_function_ui_description(func, "Add a new driver given an existing one");
+	RNA_def_pointer(func, "src_driver", "FCurve", "", "Existing Driver F-Curve to use as template for a new one");
+	/* return type */
+	parm = RNA_def_pointer(func, "driver", "FCurve", "", "New Driver F-Curve.");
+	RNA_def_function_return(func, parm);
+}
+
 void rna_def_animdata_common(StructRNA *srna)
 {
 	PropertyRNA *prop;
@@ -774,7 +814,7 @@
 	/* Active Action */
 	prop= RNA_def_property(srna, "action", PROP_POINTER, PROP_NONE);
 	RNA_def_property_flag(prop, PROP_EDITABLE); /* this flag as well as the dynamic test must be defined for this to be editable... */
-	RNA_def_property_pointer_funcs(prop, NULL, NULL, NULL, "rna_Action_id_poll");
+	RNA_def_property_pointer_funcs(prop, NULL, "rna_AnimData_action_set", NULL, "rna_Action_id_poll");
 	RNA_def_property_editable_func(prop, "rna_AnimData_action_editable");
 	RNA_def_property_ui_text(prop, "Action", "Active Action for this datablock");
 	RNA_def_property_update(prop, NC_ANIMATION, NULL); /* this will do? */
@@ -805,6 +845,8 @@
 	RNA_def_property_struct_type(prop, "FCurve");
 	RNA_def_property_ui_text(prop, "Drivers", "The Drivers/Expressions for this datablock");
 	
+	rna_api_animdata_drivers(brna, prop);
+	
 	/* General Settings */
 	prop= RNA_def_property(srna, "use_nla", PROP_BOOLEAN, PROP_NONE);
 	RNA_def_property_boolean_negative_sdna(prop, NULL, "flag", ADT_NLA_EVAL_OFF);




More information about the Bf-blender-cvs mailing list