[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [23987] trunk/blender/source/blender: Bugfix #19663: Renaming named data doesn't fix F-Curves

Joshua Leung aligorith at gmail.com
Tue Oct 20 05:44:37 CEST 2009


Revision: 23987
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=23987
Author:   aligorith
Date:     2009-10-20 05:44:35 +0200 (Tue, 20 Oct 2009)

Log Message:
-----------
Bugfix #19663: Renaming named data doesn't fix F-Curves

RNA Paths used in F-Curve, Drivers, etc. now get renamed when some data that they use gets renamed. This only works when things like Bones, Constraints, Shape Keys, and Modifiers get renamed, but other cases can get added easily. 

The code here only performs simple string replacements, so there is the potential for problems when several sets of data with the same names are present. For example, if there are multiple armatures with bones that have the same names, renaming a bone on one armature (with a bone on another armature having the same name) will break all the drivers on the other one, even though they aren't really connected. However, I don't expect the aforementioned scenario to really be a problem in most production scenarios. 

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_animsys.h
    trunk/blender/source/blender/blenkernel/intern/anim_sys.c
    trunk/blender/source/blender/editors/armature/editarmature.c
    trunk/blender/source/blender/makesrna/RNA_access.h
    trunk/blender/source/blender/makesrna/intern/rna_access.c
    trunk/blender/source/blender/makesrna/intern/rna_constraint.c
    trunk/blender/source/blender/makesrna/intern/rna_key.c
    trunk/blender/source/blender/makesrna/intern/rna_modifier.c

Modified: trunk/blender/source/blender/blenkernel/BKE_animsys.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_animsys.h	2009-10-20 02:20:00 UTC (rev 23986)
+++ trunk/blender/source/blender/blenkernel/BKE_animsys.h	2009-10-20 03:44:35 UTC (rev 23987)
@@ -67,6 +67,7 @@
 /* Add a destination to a KeyingSet */
 void BKE_keyingset_add_destination(struct KeyingSet *ks, struct ID *id, const char group_name[], const char rna_path[], int array_index, short flag, short groupmode);
 
+/* Find the destination matching the criteria given */
 struct KS_Path *BKE_keyingset_find_destination(struct KeyingSet *ks, struct ID *id, const char group_name[], const char rna_path[], int array_index, int group_mode);
 
 /* Copy all KeyingSets in the given list */
@@ -79,6 +80,15 @@
 void BKE_keyingsets_free(struct ListBase *list);
 
 /* ************************************* */
+/* Path Fixing API */
+
+/* Fix all the paths for the given ID+AnimData */
+void BKE_animdata_fix_paths_rename(struct ID *owner_id, struct AnimData *adt, char *prefix, char *oldName, char *newName);
+
+/* Fix all the paths for the entire database... */
+void BKE_all_animdata_fix_paths_rename(char *prefix, char *oldName, char *newName);
+
+/* ************************************* */
 // TODO: overrides, remapping, and path-finding api's
 
 /* ************************************* */

Modified: trunk/blender/source/blender/blenkernel/intern/anim_sys.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/anim_sys.c	2009-10-20 02:20:00 UTC (rev 23986)
+++ trunk/blender/source/blender/blenkernel/intern/anim_sys.c	2009-10-20 03:44:35 UTC (rev 23987)
@@ -238,24 +238,69 @@
 
 /* Path Validation -------------------------------------------- */
 
-#if 0
-/* Check if some given RNA Path needs fixing - free the given path and set a new one as appropriate */
-static char *rna_path_rename_fix (ID *owner_id, PointerRNA *modPtr, char *newName, char *oldpath)
+/* Check if some given RNA Path needs fixing - free the given path and set a new one as appropriate 
+ *
+ * NOTE: it is assumed that the structure we're replacing is <prefix><["><name><"]>
+ * 		i.e. pose.pose_channels["Bone"]
+ */
+static char *rna_path_rename_fix (ID *owner_id, char *prefix, char *oldName, char *newName, char *oldpath)
 {
+	char *prefixPtr= strstr(oldpath, prefix);
+	char *oldNamePtr= strstr(oldpath, oldName);
+	int prefixLen= strlen(prefix);
+	int oldNameLen= strlen(oldName);
 	
+	/* only start fixing the path if the prefix and oldName feature in the path,
+	 * and prefix occurs immediately before oldName (the +2 should take care of any [")
+	 */
+	if ( (prefixPtr && oldNamePtr) && (prefixPtr+prefixLen+2 == oldNamePtr) ) {
+		DynStr *ds= BLI_dynstr_new();
+		char *postfixPtr= oldNamePtr+oldNameLen+2;
+		char *newPath = NULL;
+		char oldChar;
+		
+		/* add the part of the string that goes up to the start of the prefix */
+		if (prefixPtr > oldpath) {
+			oldChar= prefixPtr[0]; 
+			prefixPtr[0]= 0;
+			BLI_dynstr_append(ds, oldpath);
+			prefixPtr[0]= oldChar;
+		}
+		
+		/* add the prefix, and opening brackets */
+		BLI_dynstr_append(ds, prefix);
+		BLI_dynstr_append(ds, "[\"");
+		
+		/* add the new name */
+		BLI_dynstr_append(ds, newName);
+		
+		/* add the closing brackets, then the postfix */
+		BLI_dynstr_append(ds, "\"]");
+		BLI_dynstr_append(ds, postfixPtr);
+		
+		/* create new path, and cleanup old data */
+		newPath= BLI_dynstr_get_cstring(ds);
+		BLI_dynstr_free(ds);
+		
+		MEM_freeN(oldpath);
+		
+		/* return the new path */
+		return newPath;
+	}
 	
-	return oldpath; // FIXME!!!
+	/* the old path doesn't need to be changed */
+	return oldpath;
 }
 
 /* Check RNA-Paths for a list of F-Curves */
-static void fcurves_path_rename_fix (ID *owner_id, PointerRNA *modPtr, char *newName, ListBase *curves)
+static void fcurves_path_rename_fix (ID *owner_id, char *prefix, char *oldName, char *newName, ListBase *curves)
 {
 	FCurve *fcu;
 	
 	/* we need to check every curve... */
 	for (fcu= curves->first; fcu; fcu= fcu->next) {
 		/* firstly, handle the F-Curve's own path */
-		fcu->rna_path= rna_path_rename_fix(owner_id, modPtr, newName, fcu->rna_path);
+		fcu->rna_path= rna_path_rename_fix(owner_id, prefix, oldName, newName, fcu->rna_path);
 		
 		/* driver? */
 		if (fcu->driver) {
@@ -264,14 +309,14 @@
 			
 			/* driver targets */
 			for (dtar= driver->targets.first; dtar; dtar=dtar->next) {
-				dtat->rna_path= rna_path_rename_fix(dtar->id, modPtr, newName, dtar->rna_path);
+				dtar->rna_path= rna_path_rename_fix(dtar->id, prefix, oldName, newName, dtar->rna_path);
 			}
 		}
 	}
 }
 
 /* Fix all RNA-Paths for Actions linked to NLA Strips */
-static void nlastrips_path_rename_fix (ID *owner_id, PointerRNA *modPtr, char *newName, ListBase *strips)
+static void nlastrips_path_rename_fix (ID *owner_id, char *prefix, char *oldName, char *newName, ListBase *strips)
 {
 	NlaStrip *strip;
 	
@@ -279,42 +324,113 @@
 	for (strip= strips->first; strip; strip= strip->next) {
 		/* fix strip's action */
 		if (strip->act)
-			fcurves_path_rename_fix(owner_id, modPtr, newName, &strip->act->curves);
+			fcurves_path_rename_fix(owner_id, prefix, oldName, newName, &strip->act->curves);
 		/* ignore own F-Curves, since those are local...  */
 		
 		/* check sub-strips (if metas) */
-		nlastrips_path_rename_fix(owner_id, modPtr, newName, &strip->strips);
+		nlastrips_path_rename_fix(owner_id, prefix, oldName, newName, &strip->strips);
 	}
 }
 
 /* Fix all RNA-Paths in the AnimData block used by the given ID block
- * 	- the pointer of interest must not have had its new name assigned already, otherwise
- *	  path matching for this will never work
+ * NOTE: it is assumed that the structure we're replacing is <prefix><["><name><"]>
+ * 		i.e. pose.pose_channels["Bone"]
  */
-void BKE_animdata_fix_paths_rename (ID *owner_id, PointerRNA *modPtr, char *newName)
+void BKE_animdata_fix_paths_rename (ID *owner_id, AnimData *adt, char *prefix, char *oldName, char *newName)
 {
-	AnimData *adt= BKE_animdata_from_id(owner_id);
 	NlaTrack *nlt;
 	
 	/* if no AnimData, no need to proceed */
-	if (ELEM4(NULL, owner_id, adt, modPtr, newName))
+	if (ELEM4(NULL, owner_id, adt, oldName, newName))
 		return;
 	
 	/* Active action and temp action */
 	if (adt->action)
-		fcurves_path_rename_fix(owner_id, modPtr, newName, &adt->action->curves);
+		fcurves_path_rename_fix(owner_id, prefix, oldName, newName, &adt->action->curves);
 	if (adt->tmpact)
-		fcurves_path_rename_fix(owner_id, modPtr, newName, &adt->tmpact->curves);
+		fcurves_path_rename_fix(owner_id, prefix, oldName, newName, &adt->tmpact->curves);
 		
 	/* Drivers - Drivers are really F-Curves */
-	fcurves_path_rename_fix(owner_id, modPtr, newName, &adt->drivers);
+	fcurves_path_rename_fix(owner_id, prefix, oldName, newName, &adt->drivers);
 	
 	/* NLA Data - Animation Data for Strips */
-	for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
+	for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next)
+		nlastrips_path_rename_fix(owner_id, prefix, oldName, newName, &nlt->strips);
+}
+
+/* Fix all RNA-Paths throughout the database (directly access the Global.main version)
+ * NOTE: it is assumed that the structure we're replacing is <prefix><["><name><"]>
+ * 		i.e. pose.pose_channels["Bone"]
+ */
+void BKE_all_animdata_fix_paths_rename (char *prefix, char *oldName, char *newName)
+{
+	Main *mainptr= G.main;
+	ID *id;
+	
+	/* macro for less typing 
+	 *	- whether animdata exists is checked for by the main renaming callback, though taking 
+	 *	  this outside of the function may make things slightly faster?
+	 */
+#define RENAMEFIX_ANIM_IDS(first) \
+	for (id= first; id; id= id->next) { \
+		AnimData *adt= BKE_animdata_from_id(id); \
+		BKE_animdata_fix_paths_rename(id, adt, prefix, oldName, newName);\
+	}
+	
+	/* nodes */
+	RENAMEFIX_ANIM_IDS(mainptr->nodetree.first);
+	
+	/* textures */
+	RENAMEFIX_ANIM_IDS(mainptr->tex.first);
+	
+	/* lamps */
+	RENAMEFIX_ANIM_IDS(mainptr->lamp.first);
+	
+	/* materials */
+	RENAMEFIX_ANIM_IDS(mainptr->mat.first);
+	
+	/* cameras */
+	RENAMEFIX_ANIM_IDS(mainptr->camera.first);
+	
+	/* shapekeys */
+	RENAMEFIX_ANIM_IDS(mainptr->key.first);
+	
+	/* metaballs */
+	RENAMEFIX_ANIM_IDS(mainptr->mball.first);
+	
+	/* curves */
+	RENAMEFIX_ANIM_IDS(mainptr->curve.first);
+	
+	/* armatures */
+	RENAMEFIX_ANIM_IDS(mainptr->armature.first);
+	
+	/* meshes */
+	// TODO...
+	
+	/* particles */
+	RENAMEFIX_ANIM_IDS(mainptr->particle.first);
+	
+	/* objects */
+	RENAMEFIX_ANIM_IDS(mainptr->object.first); 
+	
+	/* worlds */
+	RENAMEFIX_ANIM_IDS(mainptr->world.first);
+	
+	/* scenes */
+	for (id= mainptr->scene.first; id; id= id->next) {
+		AnimData *adt= BKE_animdata_from_id(id);
+		Scene *scene= (Scene *)id;
 		
+		/* do compositing nodes first (since these aren't included in main tree) */
+		if (scene->nodetree) {
+			AnimData *adt2= BKE_animdata_from_id((ID *)scene->nodetree);
+			BKE_animdata_fix_paths_rename((ID *)scene->nodetree, adt2, prefix, oldName, newName);
+		}
+		
+		/* now fix scene animation data as per normal */
+		BKE_animdata_fix_paths_rename((ID *)id, adt, prefix, oldName, newName);
 	}
 }
-#endif
 
 /* *********************************** */ 
 /* KeyingSet API */

Modified: trunk/blender/source/blender/editors/armature/editarmature.c
===================================================================
--- trunk/blender/source/blender/editors/armature/editarmature.c	2009-10-20 02:20:00 UTC (rev 23986)
+++ trunk/blender/source/blender/editors/armature/editarmature.c	2009-10-20 03:44:35 UTC (rev 23987)
@@ -5289,9 +5289,8 @@
 		
 		/* now check if we're in editmode, we need to find the unique name */
 		if (arm->edbo) {
-			EditBone	*eBone;
+			EditBone *eBone= editbone_name_exists(arm->edbo, oldname);
 			
-			eBone= editbone_name_exists(arm->edbo, oldname);
 			if (eBone) {
 				unique_editbone_name(arm->edbo, newname, NULL);
 				BLI_strncpy(eBone->name, newname, MAXBONENAME);
@@ -5302,7 +5301,7 @@
 			Bone *bone= get_named_bone(arm, oldname);
 			
 			if (bone) {
-				unique_bone_name (arm, newname);
+				unique_bone_name(arm, newname);
 				BLI_strncpy(bone->name, newname, MAXBONENAME);
 			}
 			else return;
@@ -5379,6 +5378,12 @@
 					   BLI_strncpy(dg->name, newname, MAXBONENAME);
 				}
 			}
+			
+			/* Fix animation data attached to this object */
+			if (ob->adt) {
+				/* posechannels only... */

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list