void BKE_libblock_free_us(Main *bmain, void *idv) /* test users */ { ID *id = idv; id_us_min(id); /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding groups when deleting an object. * Since only 'user_one' usage of objects is groups, and only 'real user' usage of objects is scenes, * removing that 'user_one' tag when there is no more real (scene) users of an object ensures it gets * fully unlinked. * Otherwise, there is no real way to get rid of an object anymore - better handling of this is TODO. */ if ((GS(id->name) == ID_OB) && (id->us == 1)) { id_us_clear_real(id); } if (id->us == 0) { BKE_libblock_unlink(bmain, id, false, false); BKE_libblock_free(bmain, id); } }
/** * Execute the 'data' part of the remapping (that is, all ID pointers from other ID datablocks). * * Behavior differs depending on whether given \a id is NULL or not: * - \a id NULL: \a old_id must be non-NULL, \a new_id may be NULL (unlinking \a old_id) or not * (remapping \a old_id to \a new_id). The whole \a bmain database is checked, and all pointers to \a old_id * are remapped to \a new_id. * - \a id is non-NULL: * + If \a old_id is NULL, \a new_id must also be NULL, and all ID pointers from \a id are cleared (i.e. \a id * does not references any other datablock anymore). * + If \a old_id is non-NULL, behavior is as with a NULL \a id, but only within given \a id. * * \param bmain: the Main data storage to operate on (must never be NULL). * \param id: the datablock to operate on (can be NULL, in which case we operate over all IDs from given bmain). * \param old_id: the datablock to dereference (may be NULL if \a id is non-NULL). * \param new_id: the new datablock to replace \a old_id references with (may be NULL). * \param r_id_remap_data: if non-NULL, the IDRemap struct to use (uselful to retrieve info about remapping process). */ ATTR_NONNULL(1) static void libblock_remap_data( Main *bmain, ID *id, ID *old_id, ID *new_id, const short remap_flags, IDRemap *r_id_remap_data) { IDRemap id_remap_data; ListBase *lb_array[MAX_LIBARRAY]; int i; if (r_id_remap_data == NULL) { r_id_remap_data = &id_remap_data; } r_id_remap_data->bmain = bmain; r_id_remap_data->old_id = old_id; r_id_remap_data->new_id = new_id; r_id_remap_data->id = NULL; r_id_remap_data->flag = remap_flags; r_id_remap_data->status = 0; r_id_remap_data->skipped_direct = 0; r_id_remap_data->skipped_indirect = 0; r_id_remap_data->skipped_refcounted = 0; if (id) { #ifdef DEBUG_PRINT printf("\tchecking id %s (%p, %p)\n", id->name, id, id->lib); #endif r_id_remap_data->id = id; libblock_remap_data_preprocess(r_id_remap_data); BKE_library_foreach_ID_link(id, foreach_libblock_remap_callback, (void *)r_id_remap_data, IDWALK_NOP); } else { i = set_listbasepointers(bmain, lb_array); /* Note that this is a very 'bruteforce' approach, maybe we could use some depsgraph to only process * objects actually using given old_id... sounds rather unlikely currently, though, so this will do for now. */ while (i--) { ID *id_curr = lb_array[i]->first; if (!id_curr || !BKE_library_idtype_can_use_idtype(GS(id_curr->name), GS(old_id->name))) { continue; } for (; id_curr; id_curr = id_curr->next) { /* Note that we cannot skip indirect usages of old_id here (if requested), we still need to check it for * the user count handling... * XXX No more true (except for debug usage of those skipping counters). */ r_id_remap_data->id = id_curr; libblock_remap_data_preprocess(r_id_remap_data); BKE_library_foreach_ID_link( id_curr, foreach_libblock_remap_callback, (void *)r_id_remap_data, IDWALK_NOP); } } } if (old_id && GS(old_id->name) == ID_OB) { BKE_sca_logic_links_remap(bmain, (Object *)old_id, (Object *)new_id); } /* XXX We may not want to always 'transfer' fakeuser from old to new id... Think for now it's desired behavior * though, we can always add an option (flag) to control this later if needed. */ if (old_id && (old_id->flag & LIB_FAKEUSER)) { id_fake_user_clear(old_id); id_fake_user_set(new_id); } id_us_clear_real(old_id); if (new_id && (new_id->tag & LIB_TAG_INDIRECT) && (r_id_remap_data->status & ID_REMAP_IS_LINKED_DIRECT)) { new_id->tag &= ~LIB_TAG_INDIRECT; new_id->tag |= LIB_TAG_EXTERN; } #ifdef DEBUG_PRINT printf("%s: %d occurences skipped (%d direct and %d indirect ones)\n", __func__, r_id_remap_data->skipped_direct + r_id_remap_data->skipped_indirect, r_id_remap_data->skipped_direct, r_id_remap_data->skipped_indirect); #endif }
/** * Replace all references in given Main to \a old_id by \a new_id * (if \a new_id is NULL, it unlinks \a old_id). */ void BKE_libblock_remap_locked( Main *bmain, void *old_idv, void *new_idv, const short remap_flags) { IDRemap id_remap_data; ID *old_id = old_idv; ID *new_id = new_idv; int skipped_direct, skipped_refcounted; BLI_assert(old_id != NULL); BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name)); BLI_assert(old_id != new_id); libblock_remap_data(bmain, NULL, old_id, new_id, remap_flags, &id_remap_data); if (free_notifier_reference_cb) { free_notifier_reference_cb(old_id); } /* We assume editors do not hold references to their IDs... This is false in some cases * (Image is especially tricky here), editors' code is to handle refcount (id->us) itself then. */ if (remap_editor_id_reference_cb) { remap_editor_id_reference_cb(old_id, new_id); } skipped_direct = id_remap_data.skipped_direct; skipped_refcounted = id_remap_data.skipped_refcounted; /* If old_id was used by some ugly 'user_one' stuff (like Image or Clip editors...), and user count has actually * been incremented for that, we have to decrease once more its user count... unless we had to skip * some 'user_one' cases. */ if ((old_id->tag & LIB_TAG_EXTRAUSER_SET) && !(id_remap_data.status & ID_REMAP_IS_USER_ONE_SKIPPED)) { id_us_clear_real(old_id); } if (old_id->us - skipped_refcounted < 0) { printf("Error in remapping process from '%s' (%p) to '%s' (%p): " "wrong user count in old ID after process (summing up to %d)\n", old_id->name, old_id, new_id ? new_id->name : "<NULL>", new_id, old_id->us - skipped_refcounted); BLI_assert(0); } if (skipped_direct == 0) { /* old_id is assumed to not be used directly anymore... */ if (old_id->lib && (old_id->tag & LIB_TAG_EXTERN)) { old_id->tag &= ~LIB_TAG_EXTERN; old_id->tag |= LIB_TAG_INDIRECT; } } /* Some after-process updates. * This is a bit ugly, but cannot see a way to avoid it. Maybe we should do a per-ID callback for this instead? */ switch (GS(old_id->name)) { case ID_OB: libblock_remap_data_postprocess_object_update(bmain, (Object *)old_id, (Object *)new_id); break; case ID_GR: if (!new_id) { /* Only affects us in case group was unlinked. */ for (Scene *sce = bmain->scene.first; sce; sce = sce->id.next) { libblock_remap_data_postprocess_group_scene_unlink(bmain, sce, old_id); } } break; case ID_ME: case ID_CU: case ID_MB: if (new_id) { /* Only affects us in case obdata was relinked (changed). */ for (Object *ob = bmain->object.first; ob; ob = ob->id.next) { libblock_remap_data_postprocess_obdata_relink(bmain, ob, new_id); } } break; default: break; } /* Node trees may virtually use any kind of data-block... */ /* XXX Yuck!!!! nodetree update can do pretty much any thing when talking about py nodes, * including creating new data-blocks (see T50385), so we need to unlock main here. :( * Why can't we have re-entrent locks? */ BKE_main_unlock(bmain); libblock_remap_data_postprocess_nodetree_update(bmain, new_id); BKE_main_lock(bmain); /* Full rebuild of DAG! */ DAG_relations_tag_update(bmain); }