diff --git a/source/blender/blenkernel/BKE_collection.h b/source/blender/blenkernel/BKE_collection.h index dd7866d83e5..61f3e675391 100644 --- a/source/blender/blenkernel/BKE_collection.h +++ b/source/blender/blenkernel/BKE_collection.h @@ -153,6 +153,14 @@ bool BKE_collection_object_remove(struct Main *bmain, struct Collection *collection, struct Object *object, bool free_us); +/** + * Replace one object with another in a collection (managing user counts). + */ +bool BKE_collection_object_replace(struct Main *bmain, + struct Collection *collection, + struct Object *ob_old, + struct Object *ob_new); + /** * Move object from a collection into another * diff --git a/source/blender/blenkernel/intern/collection.c b/source/blender/blenkernel/intern/collection.c index 8b430546644..a28c623057f 100644 --- a/source/blender/blenkernel/intern/collection.c +++ b/source/blender/blenkernel/intern/collection.c @@ -1214,6 +1214,28 @@ bool BKE_collection_object_remove(Main *bmain, return true; } +bool BKE_collection_object_replace(Main *bmain, + Collection *collection, + Object *ob_old, + Object *ob_new) +{ + CollectionObject *cob = BLI_findptr( + &collection->gobject, ob_old, offsetof(CollectionObject, ob)); + if (cob == NULL) { + return false; + } + + id_us_min(&cob->ob->id); + cob->ob = ob_new; + id_us_plus(&cob->ob->id); + + if (BKE_collection_is_in_scene(collection)) { + BKE_main_collection_sync(bmain); + } + + return true; +} + /** * Remove object from all collections of scene * \param collection_skip: Don't remove base from this collection. diff --git a/source/blender/makesrna/intern/rna_collection.c b/source/blender/makesrna/intern/rna_collection.c index 4fbefb68d6b..74e1f74d486 100644 --- a/source/blender/makesrna/intern/rna_collection.c +++ b/source/blender/makesrna/intern/rna_collection.c @@ -193,24 +193,11 @@ static bool rna_Collection_objects_override_apply(Main *bmain, return true; } - CollectionObject *cob_dst = BLI_findptr( - &coll_dst->gobject, ob_dst, offsetof(CollectionObject, ob)); - - if (cob_dst == NULL) { + if (!BKE_collection_object_replace(bmain, coll_dst, ob_dst, ob_src)) { BLI_assert_msg(0, "Could not find destination object in destination collection!"); return false; } - /* XXX TODO: We most certainly rather want to have a 'swap object pointer in collection' - * util in BKE_collection. This is only temp quick dirty test! */ - id_us_min(&cob_dst->ob->id); - cob_dst->ob = ob_src; - id_us_plus(&cob_dst->ob->id); - - if (BKE_collection_is_in_scene(coll_dst)) { - BKE_main_collection_sync(bmain); - } - RNA_property_update_main(bmain, NULL, ptr_dst, prop_dst); return true; }