Remove usercount handling from BKE_id_expand_local.

Idea looked good, but we have too much custom situations here (some half-fake-sub-ID
being copied with their 'owner', animdata, etc.), let's let datablock copy functions
handle that themselves.

Also allows to safely call BKE_id_expand_local from all copy functions now (only when
copying linked data).
This commit is contained in:
Bastien Montagne
2016-07-11 21:27:15 +02:00
parent 3b8e0606e1
commit 7212ebd09f
24 changed files with 77 additions and 60 deletions

View File

@@ -84,7 +84,7 @@ bool id_make_local(struct Main *bmain, struct ID *id, bool test);
bool id_single_user(struct bContext *C, struct ID *id, struct PointerRNA *ptr, struct PropertyRNA *prop);
bool id_copy(struct Main *bmain, struct ID *id, struct ID **newid, bool test);
void id_sort_by_name(struct ListBase *lb, struct ID *id);
void BKE_id_expand_local(struct ID *id, const bool do_user_count);
void BKE_id_expand_local(struct ID *id);
bool new_id(struct ListBase *lb, struct ID *id, const char *name);
void id_clear_lib_data(struct Main *bmain, struct ID *id);

View File

@@ -113,7 +113,7 @@ void BKE_action_make_local(Main *bmain, bAction *act)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &act->id);
BKE_id_expand_local(&act->id, false);
BKE_id_expand_local(&act->id);
}
else {
bAction *act_new = BKE_action_copy(bmain, act);
@@ -181,9 +181,8 @@ bAction *BKE_action_copy(Main *bmain, bAction *src)
}
}
BKE_id_expand_local(&dst->id, true);
if (ID_IS_LINKED_DATABLOCK(src)) {
BKE_id_expand_local(&dst->id);
BKE_id_lib_local_paths(bmain, src->id.lib, &dst->id);
}

View File

@@ -162,7 +162,7 @@ void BKE_armature_make_local(Main *bmain, bArmature *arm)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &arm->id);
BKE_id_expand_local(&arm->id, false);
BKE_id_expand_local(&arm->id);
}
else {
bArmature *arm_new = BKE_armature_copy(bmain, arm);
@@ -219,9 +219,8 @@ bArmature *BKE_armature_copy(Main *bmain, bArmature *arm)
newArm->act_edbone = NULL;
newArm->sketch = NULL;
BKE_id_expand_local(&newArm->id, true);
if (ID_IS_LINKED_DATABLOCK(arm)) {
BKE_id_expand_local(&newArm->id);
BKE_id_lib_local_paths(bmain, arm->id.lib, &newArm->id);
}

View File

@@ -178,6 +178,15 @@ Brush *BKE_brush_copy(Main *bmain, Brush *brush)
brushn = BKE_libblock_copy(bmain, &brush->id);
if (brush->mtex.tex)
id_us_plus((ID *)brush->mtex.tex);
if (brush->mask_mtex.tex)
id_us_plus((ID *)brush->mask_mtex.tex);
if (brush->paint_curve)
id_us_plus((ID *)brush->paint_curve);
if (brush->icon_imbuf)
brushn->icon_imbuf = IMB_dupImBuf(brush->icon_imbuf);
@@ -188,9 +197,8 @@ Brush *BKE_brush_copy(Main *bmain, Brush *brush)
/* enable fake user by default */
id_fake_user_set(&brush->id);
BKE_id_expand_local(&brushn->id, true);
if (ID_IS_LINKED_DATABLOCK(brush)) {
BKE_id_expand_local(&brushn->id);
BKE_id_lib_local_paths(bmain, brush->id.lib, &brushn->id);
}
@@ -234,7 +242,7 @@ void BKE_brush_make_local(Main *bmain, Brush *brush)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &brush->id);
BKE_id_expand_local(&brush->id, false);
BKE_id_expand_local(&brush->id);
/* enable fake user by default */
id_fake_user_set(&brush->id);

View File

@@ -99,9 +99,8 @@ Camera *BKE_camera_copy(Main *bmain, Camera *cam)
camn = BKE_libblock_copy(bmain, &cam->id);
BKE_id_expand_local(&camn->id, true);
if (ID_IS_LINKED_DATABLOCK(cam)) {
BKE_id_expand_local(&camn->id);
BKE_id_lib_local_paths(bmain, cam->id.lib, &camn->id);
}
@@ -126,7 +125,7 @@ void BKE_camera_make_local(Main *bmain, Camera *cam)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &cam->id);
BKE_id_expand_local(&cam->id, false);
BKE_id_expand_local(&cam->id);
}
else {
Camera *cam_new = BKE_camera_copy(bmain, cam);

View File

@@ -177,6 +177,7 @@ Curve *BKE_curve_add(Main *bmain, const char *name, int type)
Curve *BKE_curve_copy(Main *bmain, Curve *cu)
{
Curve *cun;
int a;
cun = BKE_libblock_copy(bmain, &cu->id);
@@ -184,6 +185,9 @@ Curve *BKE_curve_copy(Main *bmain, Curve *cu)
BKE_nurbList_duplicate(&(cun->nurb), &(cu->nurb));
cun->mat = MEM_dupallocN(cu->mat);
for (a = 0; a < cun->totcol; a++) {
id_us_plus((ID *)cun->mat[a]);
}
cun->str = MEM_dupallocN(cu->str);
cun->strinfo = MEM_dupallocN(cu->strinfo);
@@ -192,16 +196,19 @@ Curve *BKE_curve_copy(Main *bmain, Curve *cu)
if (cu->key) {
cun->key = BKE_key_copy(bmain, cu->key);
cun->key->id.us = 0; /* Will be increased again by BKE_id_expand_local. */
cun->key->from = (ID *)cun;
}
cun->editnurb = NULL;
cun->editfont = NULL;
BKE_id_expand_local(&cun->id, true);
id_us_plus((ID *)cun->vfont);
id_us_plus((ID *)cun->vfontb);
id_us_plus((ID *)cun->vfonti);
id_us_plus((ID *)cun->vfontbi);
if (ID_IS_LINKED_DATABLOCK(cu)) {
BKE_id_expand_local(&cun->id);
BKE_id_lib_local_paths(bmain, cu->id.lib, &cun->id);
}
@@ -229,6 +236,7 @@ void BKE_curve_make_local(Main *bmain, Curve *cu)
if (cu->key) {
BKE_key_make_local(bmain, cu->key);
}
BKE_id_expand_local(&cu->id);
}
else {
Curve *cu_new = BKE_curve_copy(bmain, cu);

View File

@@ -98,6 +98,7 @@ Group *BKE_group_copy(Main *bmain, Group *group)
groupn->preview = NULL;
if (ID_IS_LINKED_DATABLOCK(group)) {
BKE_id_expand_local(&groupn->id);
BKE_id_lib_local_paths(bmain, group->id.lib, &groupn->id);
}

View File

@@ -462,6 +462,7 @@ Image *BKE_image_copy(Main *bmain, Image *ima)
nima->preview = BKE_previewimg_copy(ima->preview);
if (ID_IS_LINKED_DATABLOCK(ima)) {
BKE_id_expand_local(&nima->id);
BKE_id_lib_local_paths(bmain, ima->id.lib, &nima->id);
}
@@ -486,7 +487,7 @@ void BKE_image_make_local(Main *bmain, Image *ima)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &ima->id);
BKE_id_expand_local(&ima->id, false);
BKE_id_expand_local(&ima->id);
}
else {
Image *ima_new = BKE_image_copy(bmain, ima);

View File

@@ -171,6 +171,7 @@ Key *BKE_key_copy(Main *bmain, Key *key)
}
if (ID_IS_LINKED_DATABLOCK(key)) {
BKE_id_expand_local(&keyn->id);
BKE_id_lib_local_paths(bmain, key->id.lib, &keyn->id);
}

View File

@@ -139,6 +139,7 @@ Lamp *BKE_lamp_copy(Main *bmain, Lamp *la)
lan->preview = BKE_previewimg_copy(la->preview);
if (ID_IS_LINKED_DATABLOCK(la)) {
BKE_id_expand_local(&lan->id);
BKE_id_lib_local_paths(bmain, la->id.lib, &lan->id);
}
@@ -189,7 +190,7 @@ void BKE_lamp_make_local(Main *bmain, Lamp *la)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &la->id);
BKE_id_expand_local(&la->id, false);
BKE_id_expand_local(&la->id);
}
else {
Lamp *la_new = BKE_lamp_copy(bmain, la);

View File

@@ -286,7 +286,6 @@ Lattice *BKE_lattice_copy(Main *bmain, Lattice *lt)
if (lt->key) {
ltn->key = BKE_key_copy(bmain, ltn->key);
ltn->key->id.us = 0; /* Will be increased again by BKE_id_expand_local. */
ltn->key->from = (ID *)ltn;
}
@@ -298,9 +297,8 @@ Lattice *BKE_lattice_copy(Main *bmain, Lattice *lt)
ltn->editlatt = NULL;
BKE_id_expand_local(&ltn->id, true);
if (ID_IS_LINKED_DATABLOCK(lt)) {
BKE_id_expand_local(&ltn->id);
BKE_id_lib_local_paths(bmain, lt->id.lib, &ltn->id);
}
@@ -353,7 +351,7 @@ void BKE_lattice_make_local(Main *bmain, Lattice *lt)
if (lt->key) {
BKE_key_make_local(bmain, lt->key);
}
BKE_id_expand_local(&lt->id, false);
BKE_id_expand_local(&lt->id);
}
else {
Lattice *lt_new = BKE_lattice_copy(bmain, lt);

View File

@@ -251,22 +251,11 @@ void id_fake_user_clear(ID *id)
}
}
static int id_expand_local_callback(void *user_data, struct ID *UNUSED(id_self), struct ID **id_pointer, int cd_flag)
static int id_expand_local_callback(
void *UNUSED(user_data), struct ID *UNUSED(id_self), struct ID **id_pointer, int UNUSED(cd_flag))
{
const bool do_user_count = (user_data != NULL);
/* We tag all ID usages as extern, and increase usercount in case it was requested. */
if (*id_pointer) {
if (do_user_count && (cd_flag & IDWALK_USER) &&
/* XXX This is a hack - animdata copying needs a good check and cleanup, it's done with way too much
* various functions currently, so for now we assume actions' usercount is already handled here... */
(GS((*id_pointer)->name) != ID_AC))
{
id_us_plus(*id_pointer);
}
else {
id_lib_extern(*id_pointer);
}
id_lib_extern(*id_pointer);
}
return IDWALK_RET_NOP;
@@ -274,12 +263,10 @@ static int id_expand_local_callback(void *user_data, struct ID *UNUSED(id_self),
/**
* Expand ID usages of given id as 'extern' (and no more indirect) linked data. Used by ID copy/make_local functions.
*
* \param do_user_count If true, increase usercount of refcounted datablocks used by given id (use it with copied id).
*/
void BKE_id_expand_local(struct ID *id, const bool do_user_count)
void BKE_id_expand_local(ID *id)
{
BKE_library_foreach_ID_link(id, id_expand_local_callback, SET_INT_IN_POINTER((int)do_user_count), 0);
BKE_library_foreach_ID_link(id, id_expand_local_callback, NULL, 0);
}
/* calls the appropriate make_local method for the block, unless test. Returns true

View File

@@ -219,6 +219,7 @@ FreestyleLineStyle *BKE_linestyle_copy(struct Main *bmain, FreestyleLineStyle *l
BKE_linestyle_geometry_modifier_copy(new_linestyle, m);
if (ID_IS_LINKED_DATABLOCK(linestyle)) {
BKE_id_expand_local(&new_linestyle->id);
BKE_id_lib_local_paths(bmain, linestyle->id.lib, &new_linestyle->id);
}

View File

@@ -854,6 +854,7 @@ Mask *BKE_mask_copy(Main *bmain, Mask *mask)
id_fake_user_set(&mask->id);
if (ID_IS_LINKED_DATABLOCK(mask)) {
BKE_id_expand_local(&mask_new->id);
BKE_id_lib_local_paths(bmain, mask->id.lib, &mask_new->id);
}

View File

@@ -246,8 +246,9 @@ Material *BKE_material_copy(Main *bmain, Material *ma)
man->preview = BKE_previewimg_copy(ma->preview);
BLI_listbase_clear(&man->gpumaterial);
if (ID_IS_LINKED_DATABLOCK(ma)) {
BKE_id_expand_local(&man->id);
BKE_id_lib_local_paths(bmain, ma->id.lib, &man->id);
}
@@ -302,7 +303,7 @@ void BKE_material_make_local(Main *bmain, Material *ma)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &ma->id);
BKE_id_expand_local(&ma->id, false);
BKE_id_expand_local(&ma->id);
}
else {
Material *ma_new = BKE_material_copy(bmain, ma);

View File

@@ -105,19 +105,22 @@ MetaBall *BKE_mball_add(Main *bmain, const char *name)
MetaBall *BKE_mball_copy(Main *bmain, MetaBall *mb)
{
MetaBall *mbn;
int a;
mbn = BKE_libblock_copy(bmain, &mb->id);
BLI_duplicatelist(&mbn->elems, &mb->elems);
mbn->mat = MEM_dupallocN(mb->mat);
for (a = 0; a < mbn->totcol; a++) {
id_us_plus((ID *)mbn->mat[a]);
}
mbn->editelems = NULL;
mbn->lastelem = NULL;
BKE_id_expand_local(&mbn->id, true);
if (ID_IS_LINKED_DATABLOCK(mb)) {
BKE_id_expand_local(&mbn->id);
BKE_id_lib_local_paths(bmain, mb->id.lib, &mbn->id);
}
@@ -142,7 +145,7 @@ void BKE_mball_make_local(Main *bmain, MetaBall *mb)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &mb->id);
BKE_id_expand_local(&mb->id, false);
BKE_id_expand_local(&mb->id);
}
else {
MetaBall *mb_new = BKE_mball_copy(bmain, mb);

View File

@@ -496,11 +496,16 @@ Mesh *BKE_mesh_add(Main *bmain, const char *name)
Mesh *BKE_mesh_copy(Main *bmain, Mesh *me)
{
Mesh *men;
int a;
const int do_tessface = ((me->totface != 0) && (me->totpoly == 0)); /* only do tessface if we have no polys */
men = BKE_libblock_copy(bmain, &me->id);
men->mat = MEM_dupallocN(me->mat);
for (a = 0; a < men->totcol; a++) {
id_us_plus((ID *)men->mat[a]);
}
id_us_plus((ID *)men->texcomesh);
CustomData_copy(&me->vdata, &men->vdata, CD_MASK_MESH, CD_DUPLICATE, men->totvert);
CustomData_copy(&me->edata, &men->edata, CD_MASK_MESH, CD_DUPLICATE, men->totedge);
@@ -522,13 +527,11 @@ Mesh *BKE_mesh_copy(Main *bmain, Mesh *me)
if (me->key) {
men->key = BKE_key_copy(bmain, me->key);
men->id.us = 0; /* Will be increased again by BKE_id_expand_local. */
men->key->from = (ID *)men;
}
BKE_id_expand_local(&men->id, true);
if (ID_IS_LINKED_DATABLOCK(me)) {
BKE_id_expand_local(&men->id);
BKE_id_lib_local_paths(bmain, me->id.lib, &men->id);
}
@@ -573,7 +576,7 @@ void BKE_mesh_make_local(Main *bmain, Mesh *me)
if (me->key) {
BKE_key_make_local(bmain, me->key);
}
BKE_id_expand_local(&me->id, false);
BKE_id_expand_local(&me->id);
}
else {
Mesh *me_new = BKE_mesh_copy(bmain, me);

View File

@@ -1292,6 +1292,7 @@ static bNodeTree *ntreeCopyTree_internal(bNodeTree *ntree, Main *bmain, bool ski
newtree->interface_type = NULL;
if (ID_IS_LINKED_DATABLOCK(ntree)) {
BKE_id_expand_local(&newtree->id);
BKE_id_lib_local_paths(bmain, ntree->id.lib, &newtree->id);
}
@@ -1968,7 +1969,7 @@ void ntreeMakeLocal(Main *bmain, bNodeTree *ntree, bool id_in_mainlist)
if (is_local) {
if (!is_lib) {
id_clear_lib_data_ex(bmain, (ID *)ntree, id_in_mainlist);
BKE_id_expand_local(&ntree->id, false);
BKE_id_expand_local(&ntree->id);
}
else {
bNodeTree *ntree_new = ntreeCopyTree(bmain, ntree);

View File

@@ -1166,11 +1166,11 @@ Object *BKE_object_copy_ex(Main *bmain, Object *ob, bool copy_caches)
copy_object_lod(obn, ob);
/* Copy runtime surve data. */
obn->curve_cache = NULL;
if (ID_IS_LINKED_DATABLOCK(ob)) {
BKE_id_expand_local(&obn->id);
BKE_id_lib_local_paths(bmain, ob->id.lib, &obn->id);
}
@@ -1204,7 +1204,7 @@ void BKE_object_make_local(Main *bmain, Object *ob)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &ob->id);
BKE_id_expand_local(&ob->id, false);
BKE_id_expand_local(&ob->id);
}
else {
Object *ob_new = BKE_object_copy(bmain, ob);

View File

@@ -3329,14 +3329,14 @@ ParticleSettings *BKE_particlesettings_copy(Main *bmain, ParticleSettings *part)
if (part->mtex[a]) {
partn->mtex[a] = MEM_mallocN(sizeof(MTex), "psys_copy_tex");
memcpy(partn->mtex[a], part->mtex[a], sizeof(MTex));
id_us_plus((ID *)partn->mtex[a]->tex);
}
}
BLI_duplicatelist(&partn->dupliweights, &part->dupliweights);
BKE_id_expand_local(&partn->id, true);
if (ID_IS_LINKED_DATABLOCK(part)) {
BKE_id_expand_local(&partn->id);
BKE_id_lib_local_paths(bmain, part->id.lib, &partn->id);
}
@@ -3361,7 +3361,7 @@ void BKE_particlesettings_make_local(Main *bmain, ParticleSettings *part)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &part->id);
BKE_id_expand_local(&part->id, false);
BKE_id_expand_local(&part->id);
}
else {
ParticleSettings *part_new = BKE_particlesettings_copy(bmain, part);

View File

@@ -74,9 +74,11 @@ Speaker *BKE_speaker_copy(Main *bmain, Speaker *spk)
spkn = BKE_libblock_copy(bmain, &spk->id);
BKE_id_expand_local(&spkn->id, true);
if (spkn->sound)
id_us_plus(&spkn->sound->id);
if (ID_IS_LINKED_DATABLOCK(spk)) {
BKE_id_expand_local(&spkn->id);
BKE_id_lib_local_paths(G.main, spk->id.lib, &spkn->id);
}
@@ -101,7 +103,7 @@ void BKE_speaker_make_local(Main *bmain, Speaker *spk)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &spk->id);
BKE_id_expand_local(&spk->id, false);
BKE_id_expand_local(&spk->id);
}
else {
Speaker *spk_new = BKE_speaker_copy(bmain, spk);

View File

@@ -492,6 +492,7 @@ Text *BKE_text_copy(Main *bmain, Text *ta)
init_undo_text(tan);
if (ID_IS_LINKED_DATABLOCK(ta)) {
BKE_id_expand_local(&tan->id);
BKE_id_lib_local_paths(bmain, ta->id.lib, &tan->id);
}

View File

@@ -872,12 +872,13 @@ Tex *BKE_texture_copy(Main *bmain, Tex *tex)
texn->nodetree = ntreeCopyTree(bmain, tex->nodetree);
}
texn->preview = BKE_previewimg_copy(tex->preview);
if (ID_IS_LINKED_DATABLOCK(tex)) {
BKE_id_expand_local(&texn->id);
BKE_id_lib_local_paths(bmain, tex->id.lib, &texn->id);
}
texn->preview = BKE_previewimg_copy(tex->preview);
return texn;
}
@@ -935,7 +936,7 @@ void BKE_texture_make_local(Main *bmain, Tex *tex)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &tex->id);
BKE_id_expand_local(&tex->id, false);
BKE_id_expand_local(&tex->id);
}
else {
Tex *tex_new = BKE_texture_copy(bmain, tex);

View File

@@ -143,6 +143,7 @@ World *BKE_world_copy(Main *bmain, World *wrld)
BLI_listbase_clear(&wrldn->gpumaterial);
if (ID_IS_LINKED_DATABLOCK(wrld)) {
BKE_id_expand_local(&wrldn->id);
BKE_id_lib_local_paths(bmain, wrld->id.lib, &wrldn->id);
}
@@ -193,7 +194,7 @@ void BKE_world_make_local(Main *bmain, World *wrld)
if (is_local) {
if (!is_lib) {
id_clear_lib_data(bmain, &wrld->id);
BKE_id_expand_local(&wrld->id, false);
BKE_id_expand_local(&wrld->id);
}
else {
World *wrld_new = BKE_world_copy(bmain, wrld);