[Bf-blender-cvs] [f35320b] master: Refactor/enhance BKE_material_make_local() and BKE_image_make_local().

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


Commit: f35320bddfe745f348d19476654a34df611b37ba
Author: Bastien Montagne
Date:   Sun Jul 10 16:38:17 2016 +0200
Branches: master
https://developer.blender.org/rBf35320bddfe745f348d19476654a34df611b37ba

Refactor/enhance BKE_material_make_local() and BKE_image_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_armature.h
M	source/blender/blenkernel/BKE_material.h
M	source/blender/blenkernel/BKE_texture.h
M	source/blender/blenkernel/BKE_world.h
M	source/blender/blenkernel/intern/armature.c
M	source/blender/blenkernel/intern/library.c
M	source/blender/blenkernel/intern/material.c
M	source/blender/blenkernel/intern/texture.c
M	source/blender/blenkernel/intern/world.c

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

diff --git a/source/blender/blenkernel/BKE_armature.h b/source/blender/blenkernel/BKE_armature.h
index 8004724..326c335 100644
--- a/source/blender/blenkernel/BKE_armature.h
+++ b/source/blender/blenkernel/BKE_armature.h
@@ -76,7 +76,7 @@ struct bArmature *BKE_armature_from_object(struct Object *ob);
 int  BKE_armature_bonelist_count(struct ListBase *lb);
 void BKE_armature_bonelist_free(struct ListBase *lb);
 void BKE_armature_free(struct bArmature *arm);
-void BKE_armature_make_local(struct bArmature *arm);
+void BKE_armature_make_local(struct Main *bmain, struct bArmature *arm);
 struct bArmature *BKE_armature_copy(struct Main *bmain, struct bArmature *arm);
 
 /* Bounding box. */
diff --git a/source/blender/blenkernel/BKE_material.h b/source/blender/blenkernel/BKE_material.h
index bcb2db7..0bc54ee 100644
--- a/source/blender/blenkernel/BKE_material.h
+++ b/source/blender/blenkernel/BKE_material.h
@@ -57,7 +57,7 @@ struct Material *BKE_material_add(struct Main *bmain, const char *name);
 struct Material *BKE_material_copy(struct Main *bmain, struct Material *ma);
 struct Material *localize_material(struct Material *ma);
 struct Material *give_node_material(struct Material *ma); /* returns node material or self */
-void BKE_material_make_local(struct Material *ma);
+void BKE_material_make_local(struct Main *bmain, struct Material *ma);
 
 /* UNUSED */
 // void automatname(struct Material *);
diff --git a/source/blender/blenkernel/BKE_texture.h b/source/blender/blenkernel/BKE_texture.h
index 37bd25c..56f056c 100644
--- a/source/blender/blenkernel/BKE_texture.h
+++ b/source/blender/blenkernel/BKE_texture.h
@@ -72,7 +72,7 @@ void         BKE_texture_default(struct Tex *tex);
 struct Tex  *BKE_texture_copy(struct Main *bmain, struct Tex *tex);
 struct Tex  *BKE_texture_add(struct Main *bmain, const char *name);
 struct Tex  *BKE_texture_localize(struct Tex *tex);
-void         BKE_texture_make_local(struct Tex *tex);
+void         BKE_texture_make_local(struct Main *bmain, struct Tex *tex);
 void         BKE_texture_type_set(struct Tex *tex, int type);
 
 void         BKE_texture_mtex_default(struct MTex *mtex);
diff --git a/source/blender/blenkernel/BKE_world.h b/source/blender/blenkernel/BKE_world.h
index 197cebc..da5cca0 100644
--- a/source/blender/blenkernel/BKE_world.h
+++ b/source/blender/blenkernel/BKE_world.h
@@ -41,7 +41,7 @@ void BKE_world_init(struct World *wrld);
 struct World *add_world(struct Main *bmian, const char *name);
 struct World *BKE_world_copy(struct Main *bmain, struct World *wrld);
 struct World *localize_world(struct World *wrld);
-void BKE_world_make_local(struct World *wrld);
+void BKE_world_make_local(struct Main *bmain, struct World *wrld);
 
 #endif
 
diff --git a/source/blender/blenkernel/intern/armature.c b/source/blender/blenkernel/intern/armature.c
index 4ed0196..2473dbe 100644
--- a/source/blender/blenkernel/intern/armature.c
+++ b/source/blender/blenkernel/intern/armature.c
@@ -67,6 +67,8 @@
 #include "BKE_global.h"
 #include "BKE_idprop.h"
 #include "BKE_library.h"
+#include "BKE_library_query.h"
+#include "BKE_library_remap.h"
 #include "BKE_lattice.h"
 #include "BKE_main.h"
 #include "BKE_object.h"
@@ -142,46 +144,34 @@ void BKE_armature_free(bArmature *arm)
 	}
 }
 
-void BKE_armature_make_local(bArmature *arm)
+void BKE_armature_make_local(Main *bmain, bArmature *arm)
 {
-	Main *bmain = G.main;
 	bool is_local = false, is_lib = false;
-	Object *ob;
 
-	if (!ID_IS_LINKED_DATABLOCK(arm))
-		return;
-	if (arm->id.us == 1) {
-		id_clear_lib_data(bmain, &arm->id);
+	/* - only lib users: do nothing
+	 * - only local users: set flag
+	 * - mixed: make copy
+	 */
+
+	if (!ID_IS_LINKED_DATABLOCK(arm)) {
 		return;
 	}
 
-	for (ob = bmain->object.first; ob && ELEM(0, is_lib, is_local); ob = ob->id.next) {
-		if (ob->data == arm) {
-			if (ID_IS_LINKED_DATABLOCK(ob))
-				is_lib = true;
-			else
-				is_local = true;
+	BKE_library_ID_test_usages(bmain, arm, &is_local, &is_lib);
+
+	if (is_local) {
+		if (!is_lib) {
+			id_clear_lib_data(bmain, &arm->id);
 		}
-	}
+		else {
+			bArmature *arm_new = BKE_armature_copy(bmain, arm);
 
-	if (is_local && is_lib == false) {
-		id_clear_lib_data(bmain, &arm->id);
-	}
-	else if (is_local && is_lib) {
-		bArmature *arm_new = BKE_armature_copy(bmain, arm);
-		arm_new->id.us = 0;
+			arm_new->id.us = 0;
 
-		/* Remap paths of new ID using old library as base. */
-		BKE_id_lib_local_paths(bmain, arm->id.lib, &arm_new->id);
+			/* Remap paths of new ID using old library as base. */
+			BKE_id_lib_local_paths(bmain, arm->id.lib, &arm_new->id);
 
-		for (ob = bmain->object.first; ob; ob = ob->id.next) {
-			if (ob->data == arm) {
-				if (!ID_IS_LINKED_DATABLOCK(ob)) {
-					ob->data = arm_new;
-					id_us_plus(&arm_new->id);
-					id_us_min(&arm->id);
-				}
-			}
+			BKE_libblock_remap(bmain, arm, arm_new, ID_REMAP_SKIP_INDIRECT_USAGE);
 		}
 	}
 }
diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c
index 1304de9..faa28f7 100644
--- a/source/blender/blenkernel/intern/library.c
+++ b/source/blender/blenkernel/intern/library.c
@@ -276,10 +276,10 @@ bool id_make_local(Main *bmain, ID *id, bool test)
 			if (!test) BKE_mball_make_local(bmain, (MetaBall *)id);
 			return true;
 		case ID_MA:
-			if (!test) BKE_material_make_local((Material *)id);
+			if (!test) BKE_material_make_local(bmain, (Material *)id);
 			return true;
 		case ID_TE:
-			if (!test) BKE_texture_make_local((Tex *)id);
+			if (!test) BKE_texture_make_local(bmain, (Tex *)id);
 			return true;
 		case ID_IM:
 			if (!test) BKE_image_make_local((Image *)id);
@@ -302,7 +302,7 @@ bool id_make_local(Main *bmain, ID *id, bool test)
 			if (!test) BKE_key_make_local(bmain, (Key *)id);
 			return true;
 		case ID_WO:
-			if (!test) BKE_world_make_local((World *)id);
+			if (!test) BKE_world_make_local(bmain, (World *)id);
 			return true;
 		case ID_SCR:
 			return false; /* can't be linked */
@@ -315,7 +315,7 @@ bool id_make_local(Main *bmain, ID *id, bool test)
 		case ID_GR:
 			return false; /* not implemented */
 		case ID_AR:
-			if (!test) BKE_armature_make_local((bArmature *)id);
+			if (!test) BKE_armature_make_local(bmain, (bArmature *)id);
 			return true;
 		case ID_AC:
 			if (!test) BKE_action_make_local((bAction *)id);
diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c
index 467bf9b..fc7854a 100644
--- a/source/blender/blenkernel/intern/material.c
+++ b/source/blender/blenkernel/intern/material.c
@@ -61,6 +61,8 @@
 #include "BKE_icons.h"
 #include "BKE_image.h"
 #include "BKE_library.h"
+#include "BKE_library_query.h"
+#include "BKE_library_remap.h"
 #include "BKE_main.h"
 #include "BKE_material.h"
 #include "BKE_mesh.h"
@@ -282,170 +284,50 @@ Material *localize_material(Material *ma)
 	return man;
 }
 
-static void extern_local_material(Material *ma)
+static int extern_local_material_callback(
+        void *UNUSED(user_data), struct ID *UNUSED(id_self), struct ID **id_pointer, int cd_flag)
 {
-	int i;
-	for (i = 0; i < MAX_MTEX; i++) {
-		if (ma->mtex[i]) id_lib_extern((ID *)ma->mtex[i]->tex);
+	/* 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;
 }
 
-void BKE_material_make_local(Material *ma)
+static void extern_local_material(Material *ma)
+{
+	BKE_library_foreach_ID_link(&ma->id, extern_local_material_callback, NULL, 0);
+}
+
+void BKE_material_make_local(Main *bmain, Material *ma)
 {
-	Main *bmain = G.main;
-	Object *ob;
-	Mesh *me;
-	Curve *cu;
-	MetaBall *mb;
-	int a;
 	bool is_local = false, is_lib = false;
 
 	/* - only lib users: do nothing
 	 * - only local users: set flag
 	 * - mixed: make copy
 	 */
-	
-	if (!ID_IS_LINKED_DATABLOCK(ma)) return;
 
-	/* One local user; set flag and return. */
-	if (ma->id.us == 1) {
-		id_clear_lib_data(bmain, &ma->id);
-		extern_local_material(ma);
+	if (!ID_IS_LINKED_DATABLOCK(ma)) {
 		return;
 	}
 
-	/* Check which other IDs reference this one to determine if it's used by
-	 * lib or local */
-	/* test objects */
-	ob = bmain->object.first;
-	while (ob) {
-		if (ob->mat) {
-			for (a = 0; a < ob->totcol; a++) {
-				if (ob->mat[a] == ma) {
-					if (ID_IS_LINKED_DATABLOCK(ob)) is_lib = true;
-					else is_local = true;
-				}
-			}
-		}
-		ob = ob->id.next;
-	}
-	/* test meshes */
-	me = bmain->mesh.first;
-	while (me) {
-		if (me->mat) {
-			for (a = 0; a < me->totcol; a++) {
-				if (me->mat[a] == ma) {
-					if (ID_IS_LINKED_DATABLOCK(me)) is_lib = true;
-					else is_local = true;
-				}
-			}
-		}
-		me = me->id.next;
-	}
-	/* test curves */
-	cu = bmain->curve.first;
-	while (cu) {
-		if (cu->mat) {
-			for (a = 0; a < cu->totcol; a++) {
-				if (cu->mat[a] == ma) {
-					if (ID_IS_LINKED_DATABLOCK(cu)) is_lib = true;
-					else is_local = true;
-				}
-			}
-		}
-		cu = cu->id.next;
-	}
-	/* test mballs */
-	mb = bmain->mball.first;
-	while (mb) {
-		if (mb->mat) {
-			for (a = 0; a < mb->totcol; a++) {
-				if (mb->mat[a] == ma) {
-					if (ID_IS_LINKED_DATABLOCK(mb)) is_lib = true;
-					else is_local = true;
-				}
-			}
-		}
-		mb = mb->id.next;
-	}
+	BKE_library_ID_test_usages(bmain, ma, &is_local, &is_lib);
 
-	/* Only local users. */
-	if (is_local && is_lib == false) {
-		id_clear_lib_data(bmain, &ma->id);
-		extern_local_material(ma);
-	}
-	/* Both user and local, so copy. */
-	else if (is_local && is_lib) {
-		Material *ma_new = BKE_material_copy(bmain, ma);
+	if (is_local) {
+		if (!is_lib) {
+			id_clear_lib_data(bmain, &ma->id);
+			extern_local_material(ma);
+		}
+		else {
+			Material *ma_new = BKE_material_copy(bmain, ma);
 
-		ma_new->id.us = 0;
+			ma_new->id.us = 0;
 
-		/* Remap paths of new ID using old library as base. */
-		BKE_id_lib_local_paths(bmain, ma->id.lib, &ma_new->id);
+			/* Remap paths of new ID using old library as base. */
+			BKE_id_lib_local_paths(bmain, ma->id.lib, &ma_new->id);
 
-		/* do objects */
-		ob = bmain->object.first;
-		while (ob) {
-			if (ob->mat) {
-				for (a = 0; a < ob->totcol; a++) {
-					if (ob->mat[a] == ma) {
-						if (!ID_IS_LINKED_DATABLOCK(ob)) {
-							ob->mat[a] = ma_new;

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list