Fix (unreported): Prevent crash when duplicating linked collections

If any of the selected collection entries in the outliner is a linked
collection, `outliner_collection_from_tree_element` can return null and
it will crash in `BKE_collection_child_find`. This patch handles this
case by skipping those entries and modified the warning message to show
how many selected entries have failed.

----

This is how the message looks like now:

<img width="746" alt="image.png" src="attachments/b91f2a64-7f2d-42d4-8657-7deaf00e90b6">

Pull Request: https://projects.blender.org/blender/blender/pulls/140847
This commit is contained in:
YimingWu
2025-07-03 03:51:56 +02:00
committed by YimingWu
parent 560e81075a
commit 8a3d773b16

View File

@@ -645,11 +645,16 @@ static wmOperatorStatus collection_duplicate_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
int failed_count = 0;
LISTBASE_FOREACH (LinkData *, link, &selected_collections.selected_array) {
TreeElement *te = static_cast<TreeElement *>(link->data);
Collection *collection = outliner_collection_from_tree_element(te);
Collection *parent = (te->parent) ? outliner_collection_from_tree_element(te->parent) :
nullptr;
if (!parent) {
failed_count += 1;
continue;
}
CollectionChild *child = BKE_collection_child_find(parent, collection);
/* We are allowed to duplicated linked collections (they will become local IDs then),
@@ -676,19 +681,21 @@ static wmOperatorStatus collection_duplicate_exec(bContext *C, wmOperator *op)
}
}
if (parent == nullptr) {
BKE_report(op->reports,
RPT_WARNING,
"Could not find a valid parent collection for the new duplicate, "
"it won't be linked to any view layer");
}
const eDupli_ID_Flags dupli_flags = (eDupli_ID_Flags)(USER_DUP_OBJECT |
(linked ? 0 : U.dupflag));
BKE_collection_duplicate(
bmain, parent, child, collection, dupli_flags, LIB_ID_DUPLICATE_IS_ROOT_ID);
}
if (failed_count != 0) {
BKE_reportf(op->reports,
RPT_WARNING,
"Unable to duplicate %d of the selected collections. "
"Could not find a valid parent collection for the new duplicate, "
"they won't be linked to any view layer",
failed_count);
}
BLI_freelistN(&selected_collections.selected_array);
DEG_relations_tag_update(bmain);
WM_main_add_notifier(NC_SCENE | ND_LAYER, CTX_data_scene(C));