[Bf-blender-cvs] [0f0eeff] master: Refactor/enhance BKE_brush_make_local() and BKE_speaker_make_local().

Bastien Montagne noreply at git.blender.org
Sun Jul 10 17:14:56 CEST 2016


Commit: 0f0eeffeceafdb47b4cdb2975865d4306d0d94b4
Author: Bastien Montagne
Date:   Sun Jul 10 15:49:01 2016 +0200
Branches: master
https://developer.blender.org/rB0f0eeffeceafdb47b4cdb2975865d4306d0d94b4

Refactor/enhance BKE_brush_make_local() and BKE_speaker_make_local().

Now using modern features from libquery/libremap areas.

Provides same kind of fixes/improvements as for BKE_object_make_local() (see rBd1a4ae3f395a6).

===================================================================

M	source/blender/blenkernel/BKE_brush.h
M	source/blender/blenkernel/BKE_speaker.h
M	source/blender/blenkernel/intern/brush.c
M	source/blender/blenkernel/intern/library.c
M	source/blender/blenkernel/intern/speaker.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_brush.h b/source/blender/blenkernel/BKE_brush.h
index 3097857..c7116bf 100644
--- a/source/blender/blenkernel/BKE_brush.h
+++ b/source/blender/blenkernel/BKE_brush.h
@@ -45,7 +45,7 @@ void BKE_brush_init(struct Brush *brush);
 struct Brush *BKE_brush_add(struct Main *bmain, const char *name, short ob_mode);
 struct Brush *BKE_brush_first_search(struct Main *bmain, short ob_mode);
 struct Brush *BKE_brush_copy(struct Main *bmain, struct Brush *brush);
-void BKE_brush_make_local(struct Brush *brush);
+void BKE_brush_make_local(struct Main *bmain, struct Brush *brush);
 void BKE_brush_unlink(struct Main *bmain, struct Brush *brush);
 void BKE_brush_free(struct Brush *brush);
 
diff --git a/source/blender/blenkernel/BKE_speaker.h b/source/blender/blenkernel/BKE_speaker.h
index 1f633e9..89b948a 100644
--- a/source/blender/blenkernel/BKE_speaker.h
+++ b/source/blender/blenkernel/BKE_speaker.h
@@ -34,7 +34,7 @@ struct Speaker;
 void BKE_speaker_init(struct Speaker *spk);
 void *BKE_speaker_add(struct Main *bmain, const char *name);
 struct Speaker *BKE_speaker_copy(struct Main *bmain, struct Speaker *spk);
-void BKE_speaker_make_local(struct Speaker *spk);
+void BKE_speaker_make_local(struct Main *bmain, struct Speaker *spk);
 void BKE_speaker_free(struct Speaker *spk);
 
 #endif
diff --git a/source/blender/blenkernel/intern/brush.c b/source/blender/blenkernel/intern/brush.c
index 5505d38..ee7af7e 100644
--- a/source/blender/blenkernel/intern/brush.c
+++ b/source/blender/blenkernel/intern/brush.c
@@ -38,6 +38,8 @@
 #include "BKE_colortools.h"
 #include "BKE_global.h"
 #include "BKE_library.h"
+#include "BKE_library_query.h"
+#include "BKE_library_remap.h"
 #include "BKE_main.h"
 #include "BKE_paint.h"
 #include "BKE_texture.h"
@@ -216,63 +218,58 @@ void BKE_brush_free(Brush *brush)
 	BKE_previewimg_free(&(brush->preview));
 }
 
+static int extern_local_brush_callback(
+        void *UNUSED(user_data), struct ID *UNUSED(id_self), struct ID **id_pointer, int cd_flag)
+{
+	/* We only tag usercounted ID usages as extern... Why? */
+	if ((cd_flag & IDWALK_USER) && *id_pointer) {
+		id_lib_extern(*id_pointer);
+	}
+	return IDWALK_RET_NOP;
+}
+
 static void extern_local_brush(Brush *brush)
 {
-	id_lib_extern((ID *)brush->mtex.tex);
-	id_lib_extern((ID *)brush->mask_mtex.tex);
-	id_lib_extern((ID *)brush->clone.image);
-	id_lib_extern((ID *)brush->toggle_brush);
-	id_lib_extern((ID *)brush->paint_curve);
+	BKE_library_foreach_ID_link(&brush->id, extern_local_brush_callback, NULL, 0);
 }
 
-void BKE_brush_make_local(Brush *brush)
+void BKE_brush_make_local(Main *bmain, Brush *brush)
 {
+	bool is_local = false, is_lib = false;
 
 	/* - only lib users: do nothing
 	 * - only local users: set flag
 	 * - mixed: make copy
 	 */
 
-	Main *bmain = G.main;
-	Scene *scene;
-	bool is_local = false, is_lib = false;
-
-	if (!ID_IS_LINKED_DATABLOCK(brush)) return;
+	if (!ID_IS_LINKED_DATABLOCK(brush)) {
+		return;
+	}
 
 	if (brush->clone.image) {
-		/* special case: ima always local immediately. Clone image should only
-		 * have one user anyway. */
-		id_clear_lib_data(bmain, &brush->clone.image->id);
-		extern_local_brush(brush);
+		/* Special case: ima always local immediately. Clone image should only have one user anyway. */
+		id_make_local(bmain, &brush->clone.image->id, false);
 	}
 
-	for (scene = bmain->scene.first; scene && ELEM(0, is_lib, is_local); scene = scene->id.next) {
-		if (BKE_paint_brush(&scene->toolsettings->imapaint.paint) == brush) {
-			if (ID_IS_LINKED_DATABLOCK(scene)) is_lib = true;
-			else is_local = true;
+	BKE_library_ID_test_usages(bmain, brush, &is_local, &is_lib);
+
+	if (is_local) {
+		if (!is_lib) {
+			id_clear_lib_data(bmain, &brush->id);
+			extern_local_brush(brush);
+
+			/* enable fake user by default */
+			id_fake_user_set(&brush->id);
 		}
-	}
+		else {
+			Brush *brush_new = BKE_brush_copy(bmain, brush);  /* Ensures FAKE_USER is set */
 
-	if (is_local && is_lib == false) {
-		id_clear_lib_data(bmain, &brush->id);
-		extern_local_brush(brush);
+			brush_new->id.us = 0;
 
-		/* enable fake user by default */
-		id_fake_user_set(&brush->id);
-	}
-	else if (is_local && is_lib) {
-		Brush *brush_new = BKE_brush_copy(bmain, brush);  /* Ensures FAKE_USER is set */
-		id_us_min(&brush_new->id);  /* Remove user added by standard BKE_libblock_copy(). */
-
-		/* Remap paths of new ID using old library as base. */
-		BKE_id_lib_local_paths(bmain, brush->id.lib, &brush_new->id);
-		
-		for (scene = bmain->scene.first; scene; scene = scene->id.next) {
-			if (BKE_paint_brush(&scene->toolsettings->imapaint.paint) == brush) {
-				if (!ID_IS_LINKED_DATABLOCK(scene)) {
-					BKE_paint_brush_set(&scene->toolsettings->imapaint.paint, brush_new);
-				}
-			}
+			/* Remap paths of new ID using old library as base. */
+			BKE_id_lib_local_paths(bmain, brush->id.lib, &brush_new->id);
+
+			BKE_libblock_remap(bmain, brush, brush_new, ID_REMAP_SKIP_INDIRECT_USAGE);
 		}
 	}
 }
diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c
index 9ef8d3a..06b977b 100644
--- a/source/blender/blenkernel/intern/library.c
+++ b/source/blender/blenkernel/intern/library.c
@@ -294,7 +294,7 @@ bool id_make_local(Main *bmain, ID *id, bool test)
 			if (!test) BKE_camera_make_local((Camera *)id);
 			return true;
 		case ID_SPK:
-			if (!test) BKE_speaker_make_local((Speaker *)id);
+			if (!test) BKE_speaker_make_local(bmain, (Speaker *)id);
 			return true;
 		case ID_IP:
 			return false; /* deprecated */
@@ -324,7 +324,7 @@ bool id_make_local(Main *bmain, ID *id, bool test)
 			if (!test) ntreeMakeLocal((bNodeTree *)id, true);
 			return true;
 		case ID_BR:
-			if (!test) BKE_brush_make_local((Brush *)id);
+			if (!test) BKE_brush_make_local(bmain, (Brush *)id);
 			return true;
 		case ID_PA:
 			if (!test) BKE_particlesettings_make_local(bmain, (ParticleSettings *)id);
diff --git a/source/blender/blenkernel/intern/speaker.c b/source/blender/blenkernel/intern/speaker.c
index 4cce085..e1bb099 100644
--- a/source/blender/blenkernel/intern/speaker.c
+++ b/source/blender/blenkernel/intern/speaker.c
@@ -34,6 +34,8 @@
 #include "BKE_animsys.h"
 #include "BKE_global.h"
 #include "BKE_library.h"
+#include "BKE_library_query.h"
+#include "BKE_library_remap.h"
 #include "BKE_main.h"
 #include "BKE_speaker.h"
 
@@ -81,15 +83,23 @@ Speaker *BKE_speaker_copy(Main *bmain, Speaker *spk)
 	return spkn;
 }
 
+static int extern_local_speaker_callback(
+        void *UNUSED(user_data), struct ID *UNUSED(id_self), struct ID **id_pointer, int cd_flag)
+{
+	/* We only tag usercounted ID usages as extern... Why? */
+	if ((cd_flag & IDWALK_USER) && *id_pointer) {
+		id_lib_extern(*id_pointer);
+	}
+	return IDWALK_RET_NOP;
+}
+
 static void extern_local_speaker(Speaker *spk)
 {
-	id_lib_extern((ID *)spk->sound);
+	BKE_library_foreach_ID_link(&spk->id, extern_local_speaker_callback, NULL, 0);
 }
 
-void BKE_speaker_make_local(Speaker *spk)
+void BKE_speaker_make_local(Main *bmain, Speaker *spk)
 {
-	Main *bmain = G.main;
-	Object *ob;
 	bool is_local = false, is_lib = false;
 
 	/* - only lib users: do nothing
@@ -97,44 +107,26 @@ void BKE_speaker_make_local(Speaker *spk)
 	 * - mixed: make copy
 	 */
 
-	if (!ID_IS_LINKED_DATABLOCK(spk)) return;
-	if (spk->id.us == 1) {
-		id_clear_lib_data(bmain, &spk->id);
-		extern_local_speaker(spk);
+	if (!ID_IS_LINKED_DATABLOCK(spk)) {
 		return;
 	}
 
-	ob = bmain->object.first;
-	while (ob) {
-		if (ob->data == spk) {
-			if (ID_IS_LINKED_DATABLOCK(ob)) is_lib = true;
-			else is_local = true;
+	BKE_library_ID_test_usages(bmain, spk, &is_local, &is_lib);
+
+	if (is_local) {
+		if (!is_lib) {
+			id_clear_lib_data(bmain, &spk->id);
+			extern_local_speaker(spk);
 		}
-		ob = ob->id.next;
-	}
+		else {
+			Speaker *spk_new = BKE_speaker_copy(bmain, spk);
 
-	if (is_local && is_lib == false) {
-		id_clear_lib_data(bmain, &spk->id);
-		extern_local_speaker(spk);
-	}
-	else if (is_local && is_lib) {
-		Speaker *spk_new = BKE_speaker_copy(bmain, spk);
-		spk_new->id.us = 0;
-
-		/* Remap paths of new ID using old library as base. */
-		BKE_id_lib_local_paths(bmain, spk->id.lib, &spk_new->id);
-
-		ob = bmain->object.first;
-		while (ob) {
-			if (ob->data == spk) {
-
-				if (!ID_IS_LINKED_DATABLOCK(ob)) {
-					ob->data = spk_new;
-					id_us_plus(&spk_new->id);
-					id_us_min(&spk->id);
-				}
-			}
-			ob = ob->id.next;
+			spk_new->id.us = 0;
+
+			/* Remap paths of new ID using old library as base. */
+			BKE_id_lib_local_paths(bmain, spk->id.lib, &spk_new->id);
+
+			BKE_libblock_remap(bmain, spk, spk_new, ID_REMAP_SKIP_INDIRECT_USAGE);
 		}
 	}
 }




More information about the Bf-blender-cvs mailing list