Fix dpesgraph wrongly refcounting NLA strip actions when duplicating IDs.

NLA strips are users of their action, so we need to pass along ID
management flags.

This commit also cleans up a bit things by passing along ID_CREATE/COPY
flags instead of dummy booleans...
This commit is contained in:
Bastien Montagne 2018-11-07 13:55:29 +01:00
parent a0d8e52b54
commit 4779165ca1
11 changed files with 65 additions and 54 deletions

@ -70,10 +70,10 @@ bool BKE_animdata_set_action(struct ReportList *reports, struct ID *id, struct b
void BKE_animdata_free(struct ID *id, const bool do_id_user); void BKE_animdata_free(struct ID *id, const bool do_id_user);
/* Copy AnimData */ /* Copy AnimData */
struct AnimData *BKE_animdata_copy(struct Main *bmain, struct AnimData *adt, const bool do_action, const bool do_id_user); struct AnimData *BKE_animdata_copy(struct Main *bmain, struct AnimData *adt, const int flag);
/* Copy AnimData */ /* Copy AnimData */
bool BKE_animdata_copy_id(struct Main *bmain, struct ID *id_to, struct ID *id_from, const bool do_action, const bool do_id_user); bool BKE_animdata_copy_id(struct Main *bmain, struct ID *id_to, struct ID *id_from, const int flag);
/* Copy AnimData Actions */ /* Copy AnimData Actions */
void BKE_animdata_copy_id_action(struct Main *bmain, struct ID *id, const bool set_newid); void BKE_animdata_copy_id_action(struct Main *bmain, struct ID *id, const bool set_newid);

@ -51,9 +51,9 @@ void BKE_nlastrip_free(ListBase *strips, struct NlaStrip *strip, bool do_id_user
void BKE_nlatrack_free(ListBase *tracks, struct NlaTrack *nlt, bool do_id_user); void BKE_nlatrack_free(ListBase *tracks, struct NlaTrack *nlt, bool do_id_user);
void BKE_nla_tracks_free(ListBase *tracks, bool do_id_user); void BKE_nla_tracks_free(ListBase *tracks, bool do_id_user);
struct NlaStrip *BKE_nlastrip_copy(struct Main *bmain, struct NlaStrip *strip, const bool use_same_action); struct NlaStrip *BKE_nlastrip_copy(struct Main *bmain, struct NlaStrip *strip, const bool use_same_action, const int flag);
struct NlaTrack *BKE_nlatrack_copy(struct Main *bmain, struct NlaTrack *nlt, const bool use_same_actions); struct NlaTrack *BKE_nlatrack_copy(struct Main *bmain, struct NlaTrack *nlt, const bool use_same_actions, const int flag);
void BKE_nla_tracks_copy(struct Main *bmain, ListBase *dst, ListBase *src); void BKE_nla_tracks_copy(struct Main *bmain, ListBase *dst, ListBase *src, const int flag);
struct NlaTrack *BKE_nlatrack_add(struct AnimData *adt, struct NlaTrack *prev); struct NlaTrack *BKE_nlatrack_add(struct AnimData *adt, struct NlaTrack *prev);
struct NlaStrip *BKE_nlastrip_new(struct bAction *act); struct NlaStrip *BKE_nlastrip_new(struct bAction *act);

@ -266,11 +266,18 @@ void BKE_animdata_free(ID *id, const bool do_id_user)
/* Copying -------------------------------------------- */ /* Copying -------------------------------------------- */
/* Make a copy of the given AnimData - to be used when copying datablocks */ /**
AnimData *BKE_animdata_copy(Main *bmain, AnimData *adt, const bool do_action, const bool do_id_user) * Make a copy of the given AnimData - to be used when copying datablocks.
* \param flag Control ID pointers management, see LIB_ID_CREATE_.../LIB_ID_COPY_... flags in BKE_library.h
* \return The copied animdata.
*/
AnimData *BKE_animdata_copy(Main *bmain, AnimData *adt, const int flag)
{ {
AnimData *dadt; AnimData *dadt;
const bool do_action = (flag & LIB_ID_COPY_ACTIONS) != 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0;
const bool do_id_user = (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0;
/* sanity check before duplicating struct */ /* sanity check before duplicating struct */
if (adt == NULL) if (adt == NULL)
return NULL; return NULL;
@ -288,7 +295,7 @@ AnimData *BKE_animdata_copy(Main *bmain, AnimData *adt, const bool do_action, co
} }
/* duplicate NLA data */ /* duplicate NLA data */
BKE_nla_tracks_copy(bmain, &dadt->nla_tracks, &adt->nla_tracks); BKE_nla_tracks_copy(bmain, &dadt->nla_tracks, &adt->nla_tracks, flag);
/* duplicate drivers (F-Curves) */ /* duplicate drivers (F-Curves) */
copy_fcurves(&dadt->drivers, &adt->drivers); copy_fcurves(&dadt->drivers, &adt->drivers);
@ -301,19 +308,23 @@ AnimData *BKE_animdata_copy(Main *bmain, AnimData *adt, const bool do_action, co
return dadt; return dadt;
} }
bool BKE_animdata_copy_id(Main *bmain, ID *id_to, ID *id_from, const bool do_action, const bool do_id_user) /**
* \param flag Control ID pointers management, see LIB_ID_CREATE_.../LIB_ID_COPY_... flags in BKE_library.h
* \return true is succesfully copied.
*/
bool BKE_animdata_copy_id(Main *bmain, ID *id_to, ID *id_from, const int flag)
{ {
AnimData *adt; AnimData *adt;
if ((id_to && id_from) && (GS(id_to->name) != GS(id_from->name))) if ((id_to && id_from) && (GS(id_to->name) != GS(id_from->name)))
return false; return false;
BKE_animdata_free(id_to, do_id_user); BKE_animdata_free(id_to, (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0);
adt = BKE_animdata_from_id(id_from); adt = BKE_animdata_from_id(id_from);
if (adt) { if (adt) {
IdAdtTemplate *iat = (IdAdtTemplate *)id_to; IdAdtTemplate *iat = (IdAdtTemplate *)id_to;
iat->adt = BKE_animdata_copy(bmain, adt, do_action, do_id_user); iat->adt = BKE_animdata_copy(bmain, adt, flag);
} }
return true; return true;
@ -373,7 +384,7 @@ void BKE_animdata_merge_copy(
if (src->nla_tracks.first) { if (src->nla_tracks.first) {
ListBase tracks = {NULL, NULL}; ListBase tracks = {NULL, NULL};
BKE_nla_tracks_copy(bmain, &tracks, &src->nla_tracks); BKE_nla_tracks_copy(bmain, &tracks, &src->nla_tracks, 0);
BLI_movelisttolist(&dst->nla_tracks, &tracks); BLI_movelisttolist(&dst->nla_tracks, &tracks);
} }

@ -1416,18 +1416,6 @@ void *BKE_id_new_nomain(const short type, const char *name)
return id; return id;
} }
/* by spec, animdata is first item after ID */
/* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
static void id_copy_animdata(Main *bmain, ID *id, const bool do_action, const bool do_id_user)
{
AnimData *adt = BKE_animdata_from_id(id);
if (adt) {
IdAdtTemplate *iat = (IdAdtTemplate *)id;
iat->adt = BKE_animdata_copy(bmain, iat->adt, do_action, do_id_user);
}
}
void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag) void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag)
{ {
ID *new_id = *r_newid; ID *new_id = *r_newid;
@ -1478,16 +1466,17 @@ void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int fla
} }
#endif #endif
/* the duplicate should get a copy of the animdata */ if (id_can_have_animdata(new_id)) {
if ((flag & LIB_ID_COPY_NO_ANIMDATA) == 0) {
BLI_assert((flag & LIB_ID_COPY_ACTIONS) == 0 || (flag & LIB_ID_CREATE_NO_MAIN) == 0);
id_copy_animdata(bmain, new_id,
(flag & LIB_ID_COPY_ACTIONS) != 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0,
(flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0);
}
else if (id_can_have_animdata(new_id)) {
IdAdtTemplate *iat = (IdAdtTemplate *)new_id; IdAdtTemplate *iat = (IdAdtTemplate *)new_id;
iat->adt = NULL;
/* the duplicate should get a copy of the animdata */
if ((flag & LIB_ID_COPY_NO_ANIMDATA) == 0) {
BLI_assert((flag & LIB_ID_COPY_ACTIONS) == 0 || (flag & LIB_ID_CREATE_NO_MAIN) == 0);
iat->adt = BKE_animdata_copy(bmain, iat->adt, flag);
}
else {
iat->adt = NULL;
}
} }
if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0) { if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0) {

@ -163,12 +163,15 @@ void BKE_nla_tracks_free(ListBase *tracks, bool do_id_user)
* Copy NLA strip * Copy NLA strip
* *
* \param use_same_action When true, the existing action is used (instead of being duplicated) * \param use_same_action When true, the existing action is used (instead of being duplicated)
* \param flag Control ID pointers management, see LIB_ID_CREATE_.../LIB_ID_COPY_... flags in BKE_library.h
*/ */
NlaStrip *BKE_nlastrip_copy(Main *bmain, NlaStrip *strip, const bool use_same_action) NlaStrip *BKE_nlastrip_copy(Main *bmain, NlaStrip *strip, const bool use_same_action, const int flag)
{ {
NlaStrip *strip_d; NlaStrip *strip_d;
NlaStrip *cs, *cs_d; NlaStrip *cs, *cs_d;
const bool do_id_user = (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0;
/* sanity check */ /* sanity check */
if (strip == NULL) if (strip == NULL)
return NULL; return NULL;
@ -180,12 +183,14 @@ NlaStrip *BKE_nlastrip_copy(Main *bmain, NlaStrip *strip, const bool use_same_ac
/* handle action */ /* handle action */
if (strip_d->act) { if (strip_d->act) {
if (use_same_action) { if (use_same_action) {
/* increase user-count of action */ if (do_id_user) {
id_us_plus(&strip_d->act->id); /* increase user-count of action */
id_us_plus(&strip_d->act->id);
}
} }
else { else {
/* use a copy of the action instead (user count shouldn't have changed yet) */ /* use a copy of the action instead (user count shouldn't have changed yet) */
strip_d->act = BKE_action_copy(bmain, strip_d->act); BKE_id_copy_ex(bmain, &strip_d->act->id, (ID **)&strip_d->act, flag, false);
} }
} }
@ -197,7 +202,7 @@ NlaStrip *BKE_nlastrip_copy(Main *bmain, NlaStrip *strip, const bool use_same_ac
BLI_listbase_clear(&strip_d->strips); BLI_listbase_clear(&strip_d->strips);
for (cs = strip->strips.first; cs; cs = cs->next) { for (cs = strip->strips.first; cs; cs = cs->next) {
cs_d = BKE_nlastrip_copy(bmain, cs, use_same_action); cs_d = BKE_nlastrip_copy(bmain, cs, use_same_action, flag);
BLI_addtail(&strip_d->strips, cs_d); BLI_addtail(&strip_d->strips, cs_d);
} }
@ -205,8 +210,11 @@ NlaStrip *BKE_nlastrip_copy(Main *bmain, NlaStrip *strip, const bool use_same_ac
return strip_d; return strip_d;
} }
/* Copy NLA Track */ /**
NlaTrack *BKE_nlatrack_copy(Main *bmain, NlaTrack *nlt, const bool use_same_actions) * Copy a single NLA Track.
* \param flag Control ID pointers management, see LIB_ID_CREATE_.../LIB_ID_COPY_... flags in BKE_library.h
*/
NlaTrack *BKE_nlatrack_copy(Main *bmain, NlaTrack *nlt, const bool use_same_actions, const int flag)
{ {
NlaStrip *strip, *strip_d; NlaStrip *strip, *strip_d;
NlaTrack *nlt_d; NlaTrack *nlt_d;
@ -223,7 +231,7 @@ NlaTrack *BKE_nlatrack_copy(Main *bmain, NlaTrack *nlt, const bool use_same_acti
BLI_listbase_clear(&nlt_d->strips); BLI_listbase_clear(&nlt_d->strips);
for (strip = nlt->strips.first; strip; strip = strip->next) { for (strip = nlt->strips.first; strip; strip = strip->next) {
strip_d = BKE_nlastrip_copy(bmain, strip, use_same_actions); strip_d = BKE_nlastrip_copy(bmain, strip, use_same_actions, flag);
BLI_addtail(&nlt_d->strips, strip_d); BLI_addtail(&nlt_d->strips, strip_d);
} }
@ -231,8 +239,11 @@ NlaTrack *BKE_nlatrack_copy(Main *bmain, NlaTrack *nlt, const bool use_same_acti
return nlt_d; return nlt_d;
} }
/* Copy all NLA data */ /**
void BKE_nla_tracks_copy(Main *bmain, ListBase *dst, ListBase *src) * Copy all NLA data.
* \param flag Control ID pointers management, see LIB_ID_CREATE_.../LIB_ID_COPY_... flags in BKE_library.h
*/
void BKE_nla_tracks_copy(Main *bmain, ListBase *dst, ListBase *src, const int flag)
{ {
NlaTrack *nlt, *nlt_d; NlaTrack *nlt, *nlt_d;
@ -247,7 +258,7 @@ void BKE_nla_tracks_copy(Main *bmain, ListBase *dst, ListBase *src)
for (nlt = src->first; nlt; nlt = nlt->next) { for (nlt = src->first; nlt; nlt = nlt->next) {
/* make a copy, and add the copy to the destination list */ /* make a copy, and add the copy to the destination list */
// XXX: we need to fix this sometime // XXX: we need to fix this sometime
nlt_d = BKE_nlatrack_copy(bmain, nlt, true); nlt_d = BKE_nlatrack_copy(bmain, nlt, true, flag);
BLI_addtail(dst, nlt_d); BLI_addtail(dst, nlt_d);
} }
} }

@ -712,7 +712,7 @@ static void deg_update_copy_on_write_animation(const Depsgraph *depsgraph,
__func__, __func__,
id_node->id_orig->name, id_node->id_orig->name,
id_node->id_cow); id_node->id_cow);
BKE_animdata_copy_id(NULL, id_node->id_cow, id_node->id_orig, false, false); BKE_animdata_copy_id(NULL, id_node->id_cow, id_node->id_orig, LIB_ID_CREATE_NO_USER_REFCOUNT);
RemapCallbackUserData user_data = {NULL}; RemapCallbackUserData user_data = {NULL};
user_data.depsgraph = depsgraph; user_data.depsgraph = depsgraph;
BKE_library_foreach_ID_link(NULL, BKE_library_foreach_ID_link(NULL,

@ -373,7 +373,7 @@ int join_armature_exec(bContext *C, wmOperator *op)
if (ob_iter->adt) { if (ob_iter->adt) {
if (ob_active->adt == NULL) { if (ob_active->adt == NULL) {
/* no animdata, so just use a copy of the whole thing */ /* no animdata, so just use a copy of the whole thing */
ob_active->adt = BKE_animdata_copy(bmain, ob_iter->adt, false, true); ob_active->adt = BKE_animdata_copy(bmain, ob_iter->adt, 0);
} }
else { else {
/* merge in data - we'll fix the drivers manually */ /* merge in data - we'll fix the drivers manually */
@ -384,7 +384,7 @@ int join_armature_exec(bContext *C, wmOperator *op)
if (curarm->adt) { if (curarm->adt) {
if (arm->adt == NULL) { if (arm->adt == NULL) {
/* no animdata, so just use a copy of the whole thing */ /* no animdata, so just use a copy of the whole thing */
arm->adt = BKE_animdata_copy(bmain, curarm->adt, false, true); arm->adt = BKE_animdata_copy(bmain, curarm->adt, 0);
} }
else { else {
/* merge in data - we'll fix the drivers manually */ /* merge in data - we'll fix the drivers manually */

@ -2122,7 +2122,7 @@ int ED_gpencil_join_objects_exec(bContext *C, wmOperator *op)
if (ob_iter->adt) { if (ob_iter->adt) {
if (ob_active->adt == NULL) { if (ob_active->adt == NULL) {
/* no animdata, so just use a copy of the whole thing */ /* no animdata, so just use a copy of the whole thing */
ob_active->adt = BKE_animdata_copy(bmain, ob_iter->adt, false, true); ob_active->adt = BKE_animdata_copy(bmain, ob_iter->adt, 0);
} }
else { else {
/* merge in data - we'll fix the drivers manually */ /* merge in data - we'll fix the drivers manually */
@ -2133,7 +2133,7 @@ int ED_gpencil_join_objects_exec(bContext *C, wmOperator *op)
if (gpd_src->adt) { if (gpd_src->adt) {
if (gpd_dst->adt == NULL) { if (gpd_dst->adt == NULL) {
/* no animdata, so just use a copy of the whole thing */ /* no animdata, so just use a copy of the whole thing */
gpd_dst->adt = BKE_animdata_copy(bmain, gpd_src->adt, false, true); gpd_dst->adt = BKE_animdata_copy(bmain, gpd_src->adt, 0);
} }
else { else {
/* merge in data - we'll fix the drivers manually */ /* merge in data - we'll fix the drivers manually */

@ -1473,13 +1473,13 @@ static int make_links_data_exec(bContext *C, wmOperator *op)
DEG_id_tag_update(&ob_dst->id, OB_RECALC_DATA); DEG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
break; break;
case MAKE_LINKS_ANIMDATA: case MAKE_LINKS_ANIMDATA:
BKE_animdata_copy_id(bmain, (ID *)ob_dst, (ID *)ob_src, false, true); BKE_animdata_copy_id(bmain, (ID *)ob_dst, (ID *)ob_src, 0);
if (ob_dst->data && ob_src->data) { if (ob_dst->data && ob_src->data) {
if (ID_IS_LINKED(obdata_id)) { if (ID_IS_LINKED(obdata_id)) {
is_lib = true; is_lib = true;
break; break;
} }
BKE_animdata_copy_id(bmain, (ID *)ob_dst->data, (ID *)ob_src->data, false, true); BKE_animdata_copy_id(bmain, (ID *)ob_dst->data, (ID *)ob_src->data, 0);
} }
DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
break; break;

@ -1057,7 +1057,7 @@ static int nlaedit_duplicate_exec(bContext *C, wmOperator *op)
/* if selected, split the strip at its midpoint */ /* if selected, split the strip at its midpoint */
if (strip->flag & NLASTRIP_FLAG_SELECT) { if (strip->flag & NLASTRIP_FLAG_SELECT) {
/* make a copy (assume that this is possible) */ /* make a copy (assume that this is possible) */
nstrip = BKE_nlastrip_copy(ac.bmain, strip, linked); nstrip = BKE_nlastrip_copy(ac.bmain, strip, linked, 0);
/* in case there's no space in the track above, or we haven't got a reference to it yet, try adding */ /* in case there's no space in the track above, or we haven't got a reference to it yet, try adding */
if (BKE_nlatrack_add_strip(nlt->next, nstrip) == 0) { if (BKE_nlatrack_add_strip(nlt->next, nstrip) == 0) {
@ -1242,7 +1242,7 @@ static void nlaedit_split_strip_actclip(Main *bmain, AnimData *adt, NlaTrack *nl
/* make a copy (assume that this is possible) and append /* make a copy (assume that this is possible) and append
* it immediately after the current strip * it immediately after the current strip
*/ */
nstrip = BKE_nlastrip_copy(bmain, strip, true); nstrip = BKE_nlastrip_copy(bmain, strip, true, 0);
BLI_insertlinkafter(&nlt->strips, strip, nstrip); BLI_insertlinkafter(&nlt->strips, strip, nstrip);
/* set the endpoint of the first strip and the start of the new strip /* set the endpoint of the first strip and the start of the new strip

@ -603,7 +603,7 @@ bool rna_AnimaData_override_apply(
if (adt_dst == NULL && adt_src != NULL) { if (adt_dst == NULL && adt_src != NULL) {
/* Copy anim data from reference into final local ID. */ /* Copy anim data from reference into final local ID. */
BKE_animdata_copy_id(NULL, ptr_dst->id.data, ptr_src->id.data, false, true); BKE_animdata_copy_id(NULL, ptr_dst->id.data, ptr_src->id.data, 0);
return true; return true;
} }
else if (adt_dst != NULL && adt_src == NULL) { else if (adt_dst != NULL && adt_src == NULL) {