Cleanup: move swapping objects in a collection to a utility function

Including this logic inline complicated D17016 & was noted as a TODO.
This commit is contained in:
Campbell Barton 2023-02-01 14:18:32 +11:00
parent 345dded146
commit e515f81318
3 changed files with 31 additions and 14 deletions

View File

@ -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
*

View File

@ -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.

View File

@ -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;
}