From f35320bddfe745f348d19476654a34df611b37ba Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Sun, 10 Jul 2016 16:38:17 +0200 Subject: 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). --- source/blender/blenkernel/BKE_armature.h | 2 +- source/blender/blenkernel/BKE_material.h | 2 +- source/blender/blenkernel/BKE_texture.h | 2 +- source/blender/blenkernel/BKE_world.h | 2 +- source/blender/blenkernel/intern/armature.c | 50 +++---- source/blender/blenkernel/intern/library.c | 8 +- source/blender/blenkernel/intern/material.c | 172 ++++-------------------- source/blender/blenkernel/intern/texture.c | 199 ++++------------------------ source/blender/blenkernel/intern/world.c | 66 ++++----- 9 files changed, 119 insertions(+), 384 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/BKE_armature.h b/source/blender/blenkernel/BKE_armature.h index 8004724ddf3..326c335338f 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 bcb2db759df..0bc54ee4a16 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 37bd25cf067..56f056c681a 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 197cebc423e..da5cca09b27 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 4ed0196ba9d..2473dbee2a3 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 1304de95367..faa28f71cfd 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 467bf9bf580..fc7854af713 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; - id_us_plus(&ma_new->id); - id_us_min(&ma->id); - } - } - } - } - ob = ob->id.next; - } - /* do 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)) { - me->mat[a] = ma_new; - id_us_plus(&ma_new->id); - id_us_min(&ma->id); - } - } - } - } - me = me->id.next; - } - /* do 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)) { - cu->mat[a] = ma_new; - id_us_plus(&ma_new->id); - id_us_min(&ma->id); - } - } - } - } - cu = cu->id.next; - } - /* do 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)) { - mb->mat[a] = ma_new; - id_us_plus(&ma_new->id); - id_us_min(&ma->id); - } - } - } - } - mb = mb->id.next; + BKE_libblock_remap(bmain, ma, ma_new, ID_REMAP_SKIP_INDIRECT_USAGE); } } } diff --git a/source/blender/blenkernel/intern/texture.c b/source/blender/blenkernel/intern/texture.c index 877bc0c02b0..aee0efeab58 100644 --- a/source/blender/blenkernel/intern/texture.c +++ b/source/blender/blenkernel/intern/texture.c @@ -59,6 +59,8 @@ #include "BKE_main.h" #include "BKE_library.h" +#include "BKE_library_query.h" +#include "BKE_library_remap.h" #include "BKE_image.h" #include "BKE_material.h" #include "BKE_texture.h" @@ -913,193 +915,50 @@ Tex *BKE_texture_localize(Tex *tex) /* ------------------------------------------------------------------------- */ +static int extern_local_texture_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_texture(Tex *tex) { - id_lib_extern((ID *)tex->ima); + BKE_library_foreach_ID_link(&tex->id, extern_local_texture_callback, NULL, 0); } -void BKE_texture_make_local(Tex *tex) +void BKE_texture_make_local(Main *bmain, Tex *tex) { - Main *bmain = G.main; - Material *ma; - World *wrld; - Lamp *la; - Brush *br; - ParticleSettings *pa; - FreestyleLineStyle *ls; - 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(tex)) return; - if (tex->id.us == 1) { - id_clear_lib_data(bmain, &tex->id); - extern_local_texture(tex); + if (!ID_IS_LINKED_DATABLOCK(tex)) { return; } - - ma = bmain->mat.first; - while (ma) { - for (a = 0; a < MAX_MTEX; a++) { - if (ma->mtex[a] && ma->mtex[a]->tex == tex) { - if (ID_IS_LINKED_DATABLOCK(ma)) is_lib = true; - else is_local = true; - } - } - ma = ma->id.next; - } - la = bmain->lamp.first; - while (la) { - for (a = 0; a < MAX_MTEX; a++) { - if (la->mtex[a] && la->mtex[a]->tex == tex) { - if (ID_IS_LINKED_DATABLOCK(la)) is_lib = true; - else is_local = true; - } - } - la = la->id.next; - } - wrld = bmain->world.first; - while (wrld) { - for (a = 0; a < MAX_MTEX; a++) { - if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) { - if (ID_IS_LINKED_DATABLOCK(wrld)) is_lib = true; - else is_local = true; - } - } - wrld = wrld->id.next; - } - br = bmain->brush.first; - while (br) { - if (br->mtex.tex == tex) { - if (ID_IS_LINKED_DATABLOCK(br)) is_lib = true; - else is_local = true; - } - if (br->mask_mtex.tex == tex) { - if (ID_IS_LINKED_DATABLOCK(br)) is_lib = true; - else is_local = true; - } - br = br->id.next; - } - pa = bmain->particle.first; - while (pa) { - for (a = 0; a < MAX_MTEX; a++) { - if (pa->mtex[a] && pa->mtex[a]->tex == tex) { - if (ID_IS_LINKED_DATABLOCK(pa)) is_lib = true; - else is_local = true; - } - } - pa = pa->id.next; - } - ls = bmain->linestyle.first; - while (ls) { - for (a = 0; a < MAX_MTEX; a++) { - if (ls->mtex[a] && ls->mtex[a]->tex == tex) { - if (ID_IS_LINKED_DATABLOCK(ls)) is_lib = true; - else is_local = true; - } - } - ls = ls->id.next; - } - - if (is_local && is_lib == false) { - id_clear_lib_data(bmain, &tex->id); - extern_local_texture(tex); - } - else if (is_local && is_lib) { - Tex *tex_new = BKE_texture_copy(bmain, tex); - tex_new->id.us = 0; + BKE_library_ID_test_usages(bmain, tex, &is_local, &is_lib); - /* Remap paths of new ID using old library as base. */ - BKE_id_lib_local_paths(bmain, tex->id.lib, &tex_new->id); - - ma = bmain->mat.first; - while (ma) { - for (a = 0; a < MAX_MTEX; a++) { - if (ma->mtex[a] && ma->mtex[a]->tex == tex) { - if (!ID_IS_LINKED_DATABLOCK(ma)) { - ma->mtex[a]->tex = tex_new; - id_us_plus(&tex_new->id); - id_us_min(&tex->id); - } - } - } - ma = ma->id.next; - } - la = bmain->lamp.first; - while (la) { - for (a = 0; a < MAX_MTEX; a++) { - if (la->mtex[a] && la->mtex[a]->tex == tex) { - if (!ID_IS_LINKED_DATABLOCK(la)) { - la->mtex[a]->tex = tex_new; - id_us_plus(&tex_new->id); - id_us_min(&tex->id); - } - } - } - la = la->id.next; + if (is_local) { + if (!is_lib) { + id_clear_lib_data(bmain, &tex->id); + extern_local_texture(tex); } - wrld = bmain->world.first; - while (wrld) { - for (a = 0; a < MAX_MTEX; a++) { - if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) { - if (!ID_IS_LINKED_DATABLOCK(wrld)) { - wrld->mtex[a]->tex = tex_new; - id_us_plus(&tex_new->id); - id_us_min(&tex->id); - } - } - } - wrld = wrld->id.next; - } - br = bmain->brush.first; - while (br) { - if (br->mtex.tex == tex) { - if (!ID_IS_LINKED_DATABLOCK(br)) { - br->mtex.tex = tex_new; - id_us_plus(&tex_new->id); - id_us_min(&tex->id); - } - } - if (br->mask_mtex.tex == tex) { - if (!ID_IS_LINKED_DATABLOCK(br)) { - br->mask_mtex.tex = tex_new; - id_us_plus(&tex_new->id); - id_us_min(&tex->id); - } - } - br = br->id.next; - } - pa = bmain->particle.first; - while (pa) { - for (a = 0; a < MAX_MTEX; a++) { - if (pa->mtex[a] && pa->mtex[a]->tex == tex) { - if (!ID_IS_LINKED_DATABLOCK(pa)) { - pa->mtex[a]->tex = tex_new; - id_us_plus(&tex_new->id); - id_us_min(&tex->id); - } - } - } - pa = pa->id.next; - } - ls = bmain->linestyle.first; - while (ls) { - for (a = 0; a < MAX_MTEX; a++) { - if (ls->mtex[a] && ls->mtex[a]->tex == tex) { - if (!ID_IS_LINKED_DATABLOCK(ls)) { - ls->mtex[a]->tex = tex_new; - id_us_plus(&tex_new->id); - id_us_min(&tex->id); - } - } - } - ls = ls->id.next; + else { + Tex *tex_new = BKE_texture_copy(bmain, tex); + + tex_new->id.us = 0; + + /* Remap paths of new ID using old library as base. */ + BKE_id_lib_local_paths(bmain, tex->id.lib, &tex_new->id); + + BKE_libblock_remap(bmain, tex, tex_new, ID_REMAP_SKIP_INDIRECT_USAGE); } } } diff --git a/source/blender/blenkernel/intern/world.c b/source/blender/blenkernel/intern/world.c index cfd10e7ba35..d62ecb5c9cf 100644 --- a/source/blender/blenkernel/intern/world.c +++ b/source/blender/blenkernel/intern/world.c @@ -46,6 +46,8 @@ #include "BKE_global.h" #include "BKE_icons.h" #include "BKE_library.h" +#include "BKE_library_query.h" +#include "BKE_library_remap.h" #include "BKE_main.h" #include "BKE_node.h" #include "BKE_world.h" @@ -174,48 +176,50 @@ World *localize_world(World *wrld) return wrldn; } -void BKE_world_make_local(World *wrld) +static int extern_local_world_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 expand_local_world(World *wrld) +{ + BKE_library_foreach_ID_link(&wrld->id, extern_local_world_callback, NULL, 0); +} + +void BKE_world_make_local(Main *bmain, World *wrld) { - Main *bmain = G.main; - Scene *sce; 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(wrld)) return; - if (wrld->id.us == 1) { - id_clear_lib_data(bmain, &wrld->id); + + if (!ID_IS_LINKED_DATABLOCK(wrld)) { return; } - - for (sce = bmain->scene.first; sce && ELEM(false, is_lib, is_local); sce = sce->id.next) { - if (sce->world == wrld) { - if (ID_IS_LINKED_DATABLOCK(sce)) is_lib = true; - else is_local = true; + + BKE_library_ID_test_usages(bmain, wrld, &is_local, &is_lib); + + if (is_local) { + if (!is_lib) { + id_clear_lib_data(bmain, &wrld->id); + expand_local_world(wrld); } - } + else { + World *wrld_new = BKE_world_copy(bmain, wrld); - if (is_local && is_lib == false) { - id_clear_lib_data(bmain, &wrld->id); - } - else if (is_local && is_lib) { - World *wrld_new = BKE_world_copy(bmain, wrld); - wrld_new->id.us = 0; - - /* Remap paths of new ID using old library as base. */ - BKE_id_lib_local_paths(bmain, wrld->id.lib, &wrld_new->id); - - for (sce = bmain->scene.first; sce; sce = sce->id.next) { - if (sce->world == wrld) { - if (!ID_IS_LINKED_DATABLOCK(sce)) { - sce->world = wrld_new; - id_us_plus(&wrld_new->id); - id_us_min(&wrld->id); - } - } + wrld_new->id.us = 0; + + /* Remap paths of new ID using old library as base. */ + BKE_id_lib_local_paths(bmain, wrld->id.lib, &wrld_new->id); + + BKE_libblock_remap(bmain, wrld, wrld_new, ID_REMAP_SKIP_INDIRECT_USAGE); } } } -- cgit v1.2.3