[Bf-blender-cvs] [d7cf7d40482] master: Link/Append: Move main linking code (from WM) to use new instantiation code in BKE.

Bastien Montagne noreply at git.blender.org
Mon Nov 22 17:11:37 CET 2021


Commit: d7cf7d4048289d49463a3ef8b9b2172d3c4d44b6
Author: Bastien Montagne
Date:   Tue Nov 9 15:56:29 2021 +0100
Branches: master
https://developer.blender.org/rBd7cf7d4048289d49463a3ef8b9b2172d3c4d44b6

Link/Append: Move main linking code (from WM) to use new instantiation code in BKE.

This removes the last main usage of BLO's instantiation code.

Also required some limited refactoring of BKE_blendfile_link_append's
instantiation to make it more modular, and usable by both linking and
appending code paths.

NOTE: This should be a non-behavioral change commit.

Part of T91414: Unify link/append between WM operators and BPY context
manager API, and cleanup usages of `BKE_library_make_local`.

===================================================================

M	source/blender/blenkernel/intern/blendfile_link_append.c

===================================================================

diff --git a/source/blender/blenkernel/intern/blendfile_link_append.c b/source/blender/blenkernel/intern/blendfile_link_append.c
index 57781172738..f0d41e91340 100644
--- a/source/blender/blenkernel/intern/blendfile_link_append.c
+++ b/source/blender/blenkernel/intern/blendfile_link_append.c
@@ -288,9 +288,21 @@ ID *BKE_blendfile_link_append_context_item_newid_get(
 /* -------------------------------------------------------------------- */
 /** \name Library link/append helper functions.
  *
- *  FIXME: Deduplicate code with similar one in readfile.c
  * \{ */
 
+/* Struct gathering all required data to handle instantiation of loose data-blocks. */
+typedef struct LooseDataInstantiateContext {
+  BlendfileLinkAppendContext *lapp_context;
+
+  /* The collection in which to add loose collections/objects. */
+  Collection *active_collection;
+
+  Main *bmain;
+  Scene *scene;
+  ViewLayer *view_layer;
+  const View3D *v3d;
+} LooseDataInstantiateContext;
+
 static bool object_in_any_scene(Main *bmain, Object *ob)
 {
   LISTBASE_FOREACH (Scene *, sce, &bmain->scenes) {
@@ -320,8 +332,15 @@ static bool object_in_any_collection(Main *bmain, Object *ob)
   return false;
 }
 
-static ID *append_loose_data_instantiate_process_check(BlendfileLinkAppendContextItem *item)
+static ID *loose_data_instantiate_process_check(LooseDataInstantiateContext *instantiate_context,
+                                                BlendfileLinkAppendContextItem *item)
 {
+  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
+  /* In linking case, we always want to handle instantiation. */
+  if (lapp_context->flag & FILE_LINK) {
+    return item->new_id;
+  }
+
   /* We consider that if we either kept it linked, or re-used already local data, instantiation
    * status of those should not be modified. */
   if (!ELEM(item->action, LINK_APPEND_ACT_COPY_LOCAL, LINK_APPEND_ACT_MAKE_LOCAL)) {
@@ -348,89 +367,128 @@ static ID *append_loose_data_instantiate_process_check(BlendfileLinkAppendContex
   return id;
 }
 
-static void append_loose_data_instantiate_ensure_active_collection(
-    BlendfileLinkAppendContext *lapp_context,
-    Main *bmain,
-    Scene *scene,
-    ViewLayer *view_layer,
-    Collection **r_active_collection)
+static void loose_data_instantiate_ensure_active_collection(
+    LooseDataInstantiateContext *instantiate_context)
 {
+
+  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
+  Main *bmain = instantiate_context->bmain;
+  Scene *scene = instantiate_context->scene;
+  ViewLayer *view_layer = instantiate_context->view_layer;
+
   /* Find or add collection as needed. */
-  if (*r_active_collection == NULL) {
+  if (instantiate_context->active_collection == NULL) {
     if (lapp_context->flag & FILE_ACTIVE_COLLECTION) {
       LayerCollection *lc = BKE_layer_collection_get_active(view_layer);
-      *r_active_collection = lc->collection;
+      instantiate_context->active_collection = lc->collection;
     }
     else {
-      *r_active_collection = BKE_collection_add(
-          bmain, scene->master_collection, DATA_("Appended Data"));
+      if (lapp_context->flag & FILE_LINK) {
+        instantiate_context->active_collection = BKE_collection_add(
+            bmain, scene->master_collection, DATA_("Linked Data"));
+      }
+      else {
+        instantiate_context->active_collection = BKE_collection_add(
+            bmain, scene->master_collection, DATA_("Appended Data"));
+      }
     }
   }
 }
 
-/* TODO: De-duplicate this code with the one in readfile.c, think we need some utils code for that
- * in BKE. */
-static void append_loose_data_instantiate(BlendfileLinkAppendContext *lapp_context,
-                                          Main *bmain,
-                                          Scene *scene,
-                                          ViewLayer *view_layer,
-                                          const View3D *v3d)
+static void loose_data_instantiate_object_base_instance_init(Main *bmain,
+                                                             Collection *collection,
+                                                             Object *ob,
+                                                             ViewLayer *view_layer,
+                                                             const View3D *v3d,
+                                                             const int flag,
+                                                             bool set_active)
 {
-  if (scene == NULL) {
-    /* In some cases, like the asset drag&drop e.g., the caller code manages instantiation itself.
-     */
-    return;
+  /* Auto-select and appending. */
+  if ((flag & FILE_AUTOSELECT) && ((flag & FILE_LINK) == 0)) {
+    /* While in general the object should not be manipulated,
+     * when the user requests the object to be selected, ensure it's visible and selectable. */
+    ob->visibility_flag &= ~(OB_HIDE_VIEWPORT | OB_HIDE_SELECT);
   }
 
-  LinkNode *itemlink;
-  Collection *active_collection = NULL;
-  const bool do_obdata = (lapp_context->flag & BLO_LIBLINK_OBDATA_INSTANCE) != 0;
+  BKE_collection_object_add(bmain, collection, ob);
 
-  /* Do NOT make base active here! screws up GUI stuff,
-   * if you want it do it at the editor level. */
-  const bool object_set_active = false;
+  Base *base = BKE_view_layer_base_find(view_layer, ob);
+
+  if (v3d != NULL) {
+    base->local_view_bits |= v3d->local_view_uuid;
+  }
+
+  if (flag & FILE_AUTOSELECT) {
+    /* All objects that use #FILE_AUTOSELECT must be selectable (unless linking data). */
+    BLI_assert((base->flag & BASE_SELECTABLE) || (flag & FILE_LINK));
+    if (base->flag & BASE_SELECTABLE) {
+      base->flag |= BASE_SELECTED;
+    }
+  }
+
+  if (set_active) {
+    view_layer->basact = base;
+  }
+
+  BKE_scene_object_base_flag_sync_from_base(base);
+}
+
+/* Tag obdata that actually need to be instantiated (those referenced by an object do not, since
+ * the object will be instantiated instaed if needed. */
+static void loose_data_instantiate_obdata_preprocess(
+    LooseDataInstantiateContext *instantiate_context)
+{
+  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
+  LinkNode *itemlink;
 
   /* First pass on obdata to enable their instantiation by default, then do a second pass on
    * objects to clear it for any obdata already in use. */
-  if (do_obdata) {
-    for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
-      BlendfileLinkAppendContextItem *item = itemlink->link;
-      ID *id = append_loose_data_instantiate_process_check(item);
-      if (id == NULL) {
-        continue;
-      }
-      const ID_Type idcode = GS(id->name);
-      if (!OB_DATA_SUPPORT_ID(idcode)) {
-        continue;
-      }
+  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
+    BlendfileLinkAppendContextItem *item = itemlink->link;
+    ID *id = loose_data_instantiate_process_check(instantiate_context, item);
+    if (id == NULL) {
+      continue;
+    }
+    const ID_Type idcode = GS(id->name);
+    if (!OB_DATA_SUPPORT_ID(idcode)) {
+      continue;
+    }
 
-      id->tag |= LIB_TAG_DOIT;
+    id->tag |= LIB_TAG_DOIT;
+  }
+  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
+    BlendfileLinkAppendContextItem *item = itemlink->link;
+    ID *id = item->new_id;
+    if (id == NULL || GS(id->name) != ID_OB) {
+      continue;
     }
-    for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
-      BlendfileLinkAppendContextItem *item = itemlink->link;
-      ID *id = item->new_id;
-      if (id == NULL || GS(id->name) != ID_OB) {
-        continue;
-      }
 
-      Object *ob = (Object *)id;
-      Object *new_ob = (Object *)id->newid;
-      if (ob->data != NULL) {
-        ((ID *)(ob->data))->tag &= ~LIB_TAG_DOIT;
-      }
-      if (new_ob != NULL && new_ob->data != NULL) {
-        ((ID *)(new_ob->data))->tag &= ~LIB_TAG_DOIT;
-      }
+    Object *ob = (Object *)id;
+    Object *new_ob = (Object *)id->newid;
+    if (ob->data != NULL) {
+      ((ID *)(ob->data))->tag &= ~LIB_TAG_DOIT;
+    }
+    if (new_ob != NULL && new_ob->data != NULL) {
+      ((ID *)(new_ob->data))->tag &= ~LIB_TAG_DOIT;
     }
   }
+}
 
-  /* First do collections, then objects, then obdata. */
+static void loose_data_instantiate_collection_process(
+    LooseDataInstantiateContext *instantiate_context)
+{
+  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
+  Main *bmain = instantiate_context->bmain;
+  Scene *scene = instantiate_context->scene;
+  ViewLayer *view_layer = instantiate_context->view_layer;
+  const View3D *v3d = instantiate_context->v3d;
 
   /* NOTE: For collections we only view_layer-instantiate duplicated collections that have
    * non-instantiated objects in them. */
+  LinkNode *itemlink;
   for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
     BlendfileLinkAppendContextItem *item = itemlink->link;
-    ID *id = append_loose_data_instantiate_process_check(item);
+    ID *id = loose_data_instantiate_process_check(instantiate_context, item);
     if (id == NULL || GS(id->name) != ID_GR) {
       continue;
     }
@@ -456,55 +514,70 @@ static void append_loose_data_instantiate(BlendfileLinkAppendContext *lapp_conte
         }
       }
     }
-    if (do_add_collection) {
-      append_loose_data_instantiate_ensure_active_collection(
-          lapp_context, bmain, scene, view_layer, &active_collection);
-
-      /* In case user requested instantiation of collections as empties, we do so for the one they
-       * explicitly selected (originally directly linked IDs). */
-      if ((lapp_context->flag & BLO_LIBLINK_COLLECTION_INSTANCE) != 0 &&
-          (item->tag & LINK_APPEND_TAG_INDIRECT) == 0) {
-        /* BKE_object_add(...) messes with the selection. */
-        Object *ob = BKE_object_add_only_object(bmain, OB_EMPTY, collection->id.name + 2);
-        ob->type = OB_EMPTY;
-        ob->empty_drawsize = U.collection_instance_empty_size;
-
-        const bool set_selected = (lapp_context->flag & FILE_AUTOSELECT) != 0;
-     

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list