[Bf-blender-cvs] [a51455918cb] master: Cleanup: de-duplicate code for instancing objects when linking

Campbell Barton noreply at git.blender.org
Fri Nov 6 05:02:26 CET 2020


Commit: a51455918cb9289224d56fa0e756c6abc801dc32
Author: Campbell Barton
Date:   Fri Nov 6 15:00:07 2020 +1100
Branches: master
https://developer.blender.org/rBa51455918cb9289224d56fa0e756c6abc801dc32

Cleanup: de-duplicate code for instancing objects when linking

Ref D8843

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

M	source/blender/blenloader/intern/readfile.c

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

diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 080e22c68b9..855d320a63d 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -7069,6 +7069,31 @@ static bool object_in_any_collection(Main *bmain, Object *ob)
   return false;
 }
 
+/**
+ * Shared operations to perform on the object's base after adding it to the scene.
+ */
+static void object_base_instance_init(
+    Object *ob, bool set_selected, bool set_active, ViewLayer *view_layer, const View3D *v3d)
+{
+  Base *base = BKE_view_layer_base_find(view_layer, ob);
+
+  if (v3d != NULL) {
+    base->local_view_bits |= v3d->local_view_uuid;
+  }
+
+  if (set_selected) {
+    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);
+}
+
 static void add_loose_objects_to_scene(Main *mainvar,
                                        Main *bmain,
                                        Scene *scene,
@@ -7114,19 +7139,12 @@ static void add_loose_objects_to_scene(Main *mainvar,
         ob->mode = OB_MODE_OBJECT;
 
         BKE_collection_object_add(bmain, active_collection, ob);
-        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) && (base->flag & BASE_SELECTABLE)) {
-          /* Do NOT make base active here! screws up GUI stuff,
-           * if you want it do it at the editor level. */
-          base->flag |= BASE_SELECTED;
-        }
-
-        BKE_scene_object_base_flag_sync_from_base(base);
+        const bool set_selected = (flag & FILE_AUTOSELECT) != 0;
+        /* Do NOT make base active here! screws up GUI stuff,
+         * if you want it do it at the editor level. */
+        const bool set_active = false;
+        object_base_instance_init(ob, set_selected, set_active, view_layer, v3d);
 
         ob->id.tag &= ~LIB_TAG_INDIRECT;
         ob->id.flag &= ~LIB_INDIRECT_WEAK_LINK;
@@ -7172,19 +7190,12 @@ static void add_loose_object_data_to_scene(Main *mainvar,
         BKE_object_materials_test(bmain, ob, ob->data);
 
         BKE_collection_object_add(bmain, active_collection, ob);
-        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) && (base->flag & BASE_SELECTABLE)) {
-          /* Do NOT make base active here! screws up GUI stuff,
-           * if you want it do it at the editor level. */
-          base->flag |= BASE_SELECTED;
-        }
-
-        BKE_scene_object_base_flag_sync_from_base(base);
+        const bool set_selected = (flag & FILE_AUTOSELECT) != 0;
+        /* Do NOT make base active here! screws up GUI stuff,
+         * if you want it do it at the editor level. */
+        bool set_active = false;
+        object_base_instance_init(ob, set_selected, set_active, view_layer, v3d);
 
         copy_v3_v3(ob->loc, scene->cursor.location);
       }
@@ -7218,23 +7229,15 @@ static void add_collections_to_scene(Main *mainvar,
       ob->empty_drawsize = U.collection_instance_empty_size;
 
       BKE_collection_object_add(bmain, active_collection, ob);
-      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) && (base->flag & BASE_SELECTABLE)) {
-        base->flag |= BASE_SELECTED;
-      }
+      const bool set_selected = (flag & FILE_AUTOSELECT) != 0;
+      /* TODO: why is it OK to make this active here but not in other situations?
+       * See other callers of #object_base_instance_init */
+      const bool set_active = set_selected;
+      object_base_instance_init(ob, set_selected, set_active, view_layer, v3d);
 
-      BKE_scene_object_base_flag_sync_from_base(base);
       DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
 
-      if (flag & FILE_AUTOSELECT) {
-        view_layer->basact = base;
-      }
-
       /* Assign the collection. */
       ob->instance_collection = collection;
       id_us_plus(&collection->id);



More information about the Bf-blender-cvs mailing list