LibOverride: Stash away 'leftover' objects/collections from auto-resync process.

Instead of storing those in scne's master collection, which is fairly
annoying, we now add them to a (hidden) specific collection. Easy to
ignore, or check and cleanup.
This commit is contained in:
Bastien Montagne 2021-03-19 17:31:39 +01:00
parent a9e64d8613
commit a00249dd22
3 changed files with 49 additions and 9 deletions

View File

@ -42,6 +42,7 @@
extern "C" {
#endif
struct Collection;
struct ID;
struct IDOverrideLibrary;
struct IDOverrideLibraryProperty;
@ -81,6 +82,7 @@ bool BKE_lib_override_library_resync(struct Main *bmain,
struct Scene *scene,
struct ViewLayer *view_layer,
struct ID *id_root,
struct Collection *override_resync_residual_storage,
const bool do_hierarchy_enforce);
void BKE_lib_override_library_main_resync(struct Main *bmain,
struct Scene *scene,

View File

@ -663,6 +663,7 @@ static void lib_override_library_create_post_process(Main *bmain,
ViewLayer *view_layer,
ID *id_root,
ID *id_reference,
Collection *residual_storage,
const bool is_resync)
{
BKE_main_collection_sync(bmain);
@ -718,7 +719,12 @@ static void lib_override_library_create_post_process(Main *bmain,
Object *ob_new = (Object *)id_root->newid;
if (!lib_override_library_create_post_process_object_is_instantiated(
view_layer, ob_new, is_resync)) {
BKE_collection_object_add_from(bmain, scene, (Object *)id_root, ob_new);
if (is_resync && residual_storage != NULL) {
BKE_collection_object_add(bmain, residual_storage, ob_new);
}
else {
BKE_collection_object_add_from(bmain, scene, (Object *)id_root, ob_new);
}
}
break;
}
@ -734,7 +740,7 @@ static void lib_override_library_create_post_process(Main *bmain,
BLI_assert(ob_new->id.override_library != NULL &&
ob_new->id.override_library->reference == &ob->id);
Collection *default_instantiating_collection = NULL;
Collection *default_instantiating_collection = residual_storage;
if (!lib_override_library_create_post_process_object_is_instantiated(
view_layer, ob_new, is_resync)) {
if (default_instantiating_collection == NULL) {
@ -799,7 +805,8 @@ bool BKE_lib_override_library_create(
return success;
}
lib_override_library_create_post_process(bmain, scene, view_layer, id_root, id_reference, false);
lib_override_library_create_post_process(
bmain, scene, view_layer, id_root, id_reference, NULL, false);
/* Cleanup. */
BKE_main_id_clear_newpoins(bmain);
@ -858,8 +865,12 @@ bool BKE_lib_override_library_proxy_convert(Main *bmain,
* \param id_root: The root liboverride ID to resync from.
* \return true if override was successfully resynced.
*/
bool BKE_lib_override_library_resync(
Main *bmain, Scene *scene, ViewLayer *view_layer, ID *id_root, const bool do_hierarchy_enforce)
bool BKE_lib_override_library_resync(Main *bmain,
Scene *scene,
ViewLayer *view_layer,
ID *id_root,
Collection *override_resync_residual_storage,
const bool do_hierarchy_enforce)
{
BLI_assert(ID_IS_OVERRIDE_LIBRARY_REAL(id_root));
BLI_assert(!ID_IS_LINKED(id_root));
@ -1070,8 +1081,13 @@ bool BKE_lib_override_library_resync(
* since we already relinked old root override collection to new resync'ed one above. So this
* call is not expected to instantiate this new resync'ed collection anywhere, just to ensure
* that we do not have any stray objects. */
lib_override_library_create_post_process(
bmain, scene, view_layer, id_root_reference, id_root, true);
lib_override_library_create_post_process(bmain,
scene,
view_layer,
id_root_reference,
id_root,
override_resync_residual_storage,
true);
/* Cleanup. */
BLI_ghash_free(linkedref_to_old_override, NULL, NULL);
@ -1098,6 +1114,23 @@ bool BKE_lib_override_library_resync(
*/
void BKE_lib_override_library_main_resync(Main *bmain, Scene *scene, ViewLayer *view_layer)
{
/* We use a specific collection to gather/store all 'orphaned' override collections and objects
* generated by resyncprocess. This avoids putting them in scene's master collection. */
#define OVERRIDE_RESYNC_RESIDUAL_STORAGE_NAME "OVERRIDE_RESYNC_LEFTOVERS"
Collection *override_resync_residual_storage = BLI_findstring(
&bmain->collections, OVERRIDE_RESYNC_RESIDUAL_STORAGE_NAME, offsetof(ID, name) + 2);
if (override_resync_residual_storage != NULL &&
override_resync_residual_storage->id.lib != NULL) {
override_resync_residual_storage = NULL;
}
if (override_resync_residual_storage == NULL) {
override_resync_residual_storage = BKE_collection_add(
bmain, scene->master_collection, OVERRIDE_RESYNC_RESIDUAL_STORAGE_NAME);
/* Hide the collection from viewport and render. */
override_resync_residual_storage->flag |= COLLECTION_RESTRICT_VIEWPORT |
COLLECTION_RESTRICT_RENDER;
}
BKE_main_relations_create(bmain, 0);
BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
@ -1187,7 +1220,8 @@ void BKE_lib_override_library_main_resync(Main *bmain, Scene *scene, ViewLayer *
}
do_continue = true;
CLOG_INFO(&LOG, 2, "Resyncing %s...", id->name);
const bool success = BKE_lib_override_library_resync(bmain, scene, view_layer, id, false);
const bool success = BKE_lib_override_library_resync(
bmain, scene, view_layer, id, override_resync_residual_storage, false);
CLOG_INFO(&LOG, 2, "\tSuccess: %d", success);
break;
}
@ -1198,6 +1232,10 @@ void BKE_lib_override_library_main_resync(Main *bmain, Scene *scene, ViewLayer *
}
FOREACH_MAIN_LISTBASE_END;
}
if (BKE_collection_is_empty(override_resync_residual_storage)) {
BKE_collection_delete(bmain, override_resync_residual_storage, true);
}
}
/**

View File

@ -925,7 +925,7 @@ static void id_override_library_resync_fn(bContext *C,
}
BKE_lib_override_library_resync(
bmain, scene, CTX_data_view_layer(C), id_root, do_hierarchy_enforce);
bmain, scene, CTX_data_view_layer(C), id_root, NULL, do_hierarchy_enforce);
WM_event_add_notifier(C, NC_WINDOW, NULL);
}