From e72b86d3cba8c7366bee2e92162f3b07bf367f3d Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Wed, 1 Jun 2022 12:44:11 +0200 Subject: Fix T98469: Crash trying to add an object to a linked collection that is linked to multiple scenes. Crash happened because code could not find a valid base in current scene after adding the object, added some checks for that. Root of the issue was wrong assumptions in `BKE_object_add` logic, which would pick the first valid ancestor collection in case initially selected collection was not editable. In some case, this could pick a collection not instanced in the current scene's view layer, leading to not getting a valid base for the newly added object. Addressed this by adding a new variant of `BKE_collection_object_add`, `BKE_collection_viewlayer_object_add`, which ensures final collection is in given viewlayer. --- source/blender/blenkernel/intern/object.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'source/blender/blenkernel/intern/object.cc') diff --git a/source/blender/blenkernel/intern/object.cc b/source/blender/blenkernel/intern/object.cc index 2a25d73ed87..0bc092bec4f 100644 --- a/source/blender/blenkernel/intern/object.cc +++ b/source/blender/blenkernel/intern/object.cc @@ -2271,10 +2271,14 @@ Object *BKE_object_add(Main *bmain, ViewLayer *view_layer, int type, const char Object *ob = object_add_common(bmain, view_layer, type, name); LayerCollection *layer_collection = BKE_layer_collection_get_active(view_layer); - BKE_collection_object_add(bmain, layer_collection->collection, ob); + BKE_collection_viewlayer_object_add(bmain, view_layer, layer_collection->collection, ob); + /* Note: There is no way to be sure that #BKE_collection_viewlayer_object_add will actually + * manage to find a valid collection in given `view_layer` to add the new object to. */ Base *base = BKE_view_layer_base_find(view_layer, ob); - BKE_view_layer_base_select_and_set_active(view_layer, base); + if (base != nullptr) { + BKE_view_layer_base_select_and_set_active(view_layer, base); + } return ob; } -- cgit v1.2.3