diff options
author | Bastien Montagne <bastien@blender.org> | 2020-10-07 15:27:33 +0300 |
---|---|---|
committer | Bastien Montagne <bastien@blender.org> | 2020-10-07 19:05:06 +0300 |
commit | 7c9131d11eb01a70db440fac7bb3f4b3833d544a (patch) | |
tree | c7c495566a3f111c8c495f1daa207292bab9c43a /source/blender/blenkernel | |
parent | 1f50beb9f28edd2fe54d97647222ad6ee5808c1c (diff) |
Refactor `BKE_id_copy_ex` to return the new ID pointer.
Note that possibility to pass the new ID pointer as parameter was kept,
as this is needed for some rather specific cases (like in depsgraph/COW,
when copying into already allocated memory).
Part of T71219.
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_lib_id.h | 5 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/anim_data.c | 4 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/displist.c | 7 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/gpencil_modifier.c | 5 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/hair.c | 3 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/lib_id.c | 55 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mesh.c | 3 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mesh_convert.c | 21 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/node.c | 9 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/particle_distribute.c | 4 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/particle_system.c | 5 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/pointcloud.c | 3 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/volume.cc | 3 |
13 files changed, 63 insertions, 64 deletions
diff --git a/source/blender/blenkernel/BKE_lib_id.h b/source/blender/blenkernel/BKE_lib_id.h index 33ccd20bcff..9b1e2c0d41b 100644 --- a/source/blender/blenkernel/BKE_lib_id.h +++ b/source/blender/blenkernel/BKE_lib_id.h @@ -238,7 +238,10 @@ bool id_single_user(struct bContext *C, struct PropertyRNA *prop); bool BKE_id_copy_is_allowed(const struct ID *id); bool BKE_id_copy(struct Main *bmain, const struct ID *id, struct ID **newid); -bool BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, const int flag); +struct ID *BKE_id_copy_ex(struct Main *bmain, + const struct ID *id, + struct ID **r_newid, + const int flag); struct ID *BKE_id_copy_for_duplicate(struct Main *bmain, struct ID *id, const uint duplicate_flags); diff --git a/source/blender/blenkernel/intern/anim_data.c b/source/blender/blenkernel/intern/anim_data.c index 7e00b1a6407..c677d976783 100644 --- a/source/blender/blenkernel/intern/anim_data.c +++ b/source/blender/blenkernel/intern/anim_data.c @@ -342,8 +342,8 @@ AnimData *BKE_animdata_copy(Main *bmain, AnimData *adt, const int flag) flag; BLI_assert(bmain != NULL); BLI_assert(dadt->action == NULL || dadt->action != dadt->tmpact); - BKE_id_copy_ex(bmain, (ID *)dadt->action, (ID **)&dadt->action, id_copy_flag); - BKE_id_copy_ex(bmain, (ID *)dadt->tmpact, (ID **)&dadt->tmpact, id_copy_flag); + dadt->action = (bAction *)BKE_id_copy_ex(bmain, (ID *)dadt->action, NULL, id_copy_flag); + dadt->tmpact = (bAction *)BKE_id_copy_ex(bmain, (ID *)dadt->tmpact, NULL, id_copy_flag); } else if (do_id_user) { id_us_plus((ID *)dadt->action); diff --git a/source/blender/blenkernel/intern/displist.c b/source/blender/blenkernel/intern/displist.c index 8fdd6ee69d9..c37da9a9365 100644 --- a/source/blender/blenkernel/intern/displist.c +++ b/source/blender/blenkernel/intern/displist.c @@ -1071,8 +1071,8 @@ static void curve_calc_modifiers_post(Depsgraph *depsgraph, if (modified) { if (vertCos) { - Mesh *temp_mesh; - BKE_id_copy_ex(NULL, &modified->id, (ID **)&temp_mesh, LIB_ID_COPY_LOCALIZE); + Mesh *temp_mesh = (Mesh *)BKE_id_copy_ex( + NULL, &modified->id, NULL, LIB_ID_COPY_LOCALIZE); BKE_id_free(NULL, modified); modified = temp_mesh; @@ -1115,8 +1115,7 @@ static void curve_calc_modifiers_post(Depsgraph *depsgraph, if (vertCos) { if (modified) { - Mesh *temp_mesh; - BKE_id_copy_ex(NULL, &modified->id, (ID **)&temp_mesh, LIB_ID_COPY_LOCALIZE); + Mesh *temp_mesh = (Mesh *)BKE_id_copy_ex(NULL, &modified->id, NULL, LIB_ID_COPY_LOCALIZE); BKE_id_free(NULL, modified); modified = temp_mesh; diff --git a/source/blender/blenkernel/intern/gpencil_modifier.c b/source/blender/blenkernel/intern/gpencil_modifier.c index 2eccdb872dd..d12e445fe99 100644 --- a/source/blender/blenkernel/intern/gpencil_modifier.c +++ b/source/blender/blenkernel/intern/gpencil_modifier.c @@ -660,10 +660,9 @@ static void gpencil_copy_activeframe_to_eval( static bGPdata *gpencil_copy_for_eval(bGPdata *gpd) { - int flags = LIB_ID_COPY_LOCALIZE; + const int flags = LIB_ID_COPY_LOCALIZE; - bGPdata *result; - BKE_id_copy_ex(NULL, &gpd->id, (ID **)&result, flags); + bGPdata *result = (bGPdata *)BKE_id_copy_ex(NULL, &gpd->id, NULL, flags); return result; } diff --git a/source/blender/blenkernel/intern/hair.c b/source/blender/blenkernel/intern/hair.c index b9b88dd062b..edbc13d8773 100644 --- a/source/blender/blenkernel/intern/hair.c +++ b/source/blender/blenkernel/intern/hair.c @@ -330,8 +330,7 @@ Hair *BKE_hair_copy_for_eval(Hair *hair_src, bool reference) flags |= LIB_ID_COPY_CD_REFERENCE; } - Hair *result; - BKE_id_copy_ex(NULL, &hair_src->id, (ID **)&result, flags); + Hair *result = (Hair *)BKE_id_copy_ex(NULL, &hair_src->id, NULL, flags); return result; } diff --git a/source/blender/blenkernel/intern/lib_id.c b/source/blender/blenkernel/intern/lib_id.c index fd127f5871f..f69bada0fe8 100644 --- a/source/blender/blenkernel/intern/lib_id.c +++ b/source/blender/blenkernel/intern/lib_id.c @@ -530,51 +530,55 @@ bool BKE_id_copy_is_allowed(const ID *id) /** * Generic entry point for copying a data-block (new API). * - * \note Copy is only affecting given data-block + * \note Copy is generally only affecting the given data-block * (no ID used by copied one will be affected, besides usercount). - * There is only one exception, if #LIB_ID_COPY_ACTIONS is defined, - * actions used by animdata will be duplicated. + * There are exceptions though: + * - Embedded IDs (root node trees and master collections) are always copied with their owner. + * - If #LIB_ID_COPY_ACTIONS is defined, actions used by animdata will be duplicated. + * - If #LIB_ID_COPY_SHAPEKEY is defined, shapekeys will be duplicated. + * - If #LIB_ID_CREATE_LOCAL is defined, root node trees will be deep-duplicated recursively. * * \note Usercount of new copy is always set to 1. * * \param bmain: Main database, may be NULL only if LIB_ID_CREATE_NO_MAIN is specified. * \param id: Source data-block. - * \param r_newid: Pointer to new (copied) ID pointer. - * \param flag: Set of copy options, see DNA_ID.h enum for details - * (leave to zero for default, full copy). - * \return False when copying that ID type is not supported, true otherwise. + * \param r_newid: Pointer to new (copied) ID pointer, may be NULL. Used to allow copying into + * already allocated memory. + * \param flag: Set of copy options, see DNA_ID.h enum for details (leave to zero for default, + * full copy). + * \return NULL when copying that ID type is not supported, the new copy otherwise. */ -bool BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag) +ID *BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag) { - BLI_assert(r_newid != NULL); + ID *newid = (r_newid != NULL) ? *r_newid : NULL; /* Make sure destination pointer is all good. */ if ((flag & LIB_ID_CREATE_NO_ALLOCATE) == 0) { - *r_newid = NULL; + newid = NULL; } else { - if (*r_newid != NULL) { + if (newid != NULL) { /* Allow some garbage non-initialized memory to go in, and clean it up here. */ const size_t size = BKE_libblock_get_alloc_info(GS(id->name), NULL); - memset(*r_newid, 0, size); + memset(newid, 0, size); } } /* Early output is source is NULL. */ if (id == NULL) { - return false; + return NULL; } const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id); if (idtype_info != NULL) { if ((idtype_info->flags & IDTYPE_FLAGS_NO_COPY) != 0) { - return false; + return NULL; } - BKE_libblock_copy_ex(bmain, id, r_newid, flag); + BKE_libblock_copy_ex(bmain, id, &newid, flag); if (idtype_info->copy_data != NULL) { - idtype_info->copy_data(bmain, *r_newid, id, flag); + idtype_info->copy_data(bmain, newid, id, flag); } } else { @@ -584,22 +588,26 @@ bool BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag) /* Update ID refcount, remap pointers to self in new ID. */ struct IDCopyLibManagementData data = { .id_src = id, - .id_dst = *r_newid, + .id_dst = newid, .flag = flag, }; - BKE_library_foreach_ID_link(bmain, *r_newid, id_copy_libmanagement_cb, &data, IDWALK_NOP); + BKE_library_foreach_ID_link(bmain, newid, id_copy_libmanagement_cb, &data, IDWALK_NOP); /* Do not make new copy local in case we are copying outside of main... * XXX TODO: is this behavior OK, or should we need own flag to control that? */ if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) { BLI_assert((flag & LIB_ID_COPY_KEEP_LIB) == 0); - lib_id_copy_ensure_local(bmain, id, *r_newid); + lib_id_copy_ensure_local(bmain, id, newid); } else { - (*r_newid)->lib = id->lib; + newid->lib = id->lib; } - return true; + if (r_newid != NULL) { + *r_newid = newid; + } + + return newid; } /** @@ -608,7 +616,7 @@ bool BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag) */ bool BKE_id_copy(Main *bmain, const ID *id, ID **newid) { - return BKE_id_copy_ex(bmain, id, newid, LIB_ID_COPY_DEFAULT); + return (BKE_id_copy_ex(bmain, id, newid, LIB_ID_COPY_DEFAULT) != NULL); } /** @@ -729,7 +737,8 @@ bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop) if (RNA_property_editable(ptr, prop)) { Main *bmain = CTX_data_main(C); /* copy animation actions too */ - if (BKE_id_copy_ex(bmain, id, &newid, LIB_ID_COPY_DEFAULT | LIB_ID_COPY_ACTIONS) && newid) { + newid = BKE_id_copy_ex(bmain, id, NULL, LIB_ID_COPY_DEFAULT | LIB_ID_COPY_ACTIONS); + if (newid != NULL) { /* us is 1 by convention with new IDs, but RNA_property_pointer_set * will also increment it, decrement it here. */ id_us_min(newid); diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c index fa87fef2b9a..d0ee4d53eb1 100644 --- a/source/blender/blenkernel/intern/mesh.c +++ b/source/blender/blenkernel/intern/mesh.c @@ -1013,8 +1013,7 @@ Mesh *BKE_mesh_copy_for_eval(struct Mesh *source, bool reference) flags |= LIB_ID_COPY_CD_REFERENCE; } - Mesh *result; - BKE_id_copy_ex(NULL, &source->id, (ID **)&result, flags); + Mesh *result = (Mesh *)BKE_id_copy_ex(NULL, &source->id, NULL, flags); return result; } diff --git a/source/blender/blenkernel/intern/mesh_convert.c b/source/blender/blenkernel/intern/mesh_convert.c index 557948ef3a4..c11f9961f67 100644 --- a/source/blender/blenkernel/intern/mesh_convert.c +++ b/source/blender/blenkernel/intern/mesh_convert.c @@ -1010,8 +1010,7 @@ static Object *object_for_curve_to_mesh_create(Object *object) Curve *curve = (Curve *)object->data; /* Create object itself. */ - Object *temp_object; - BKE_id_copy_ex(NULL, &object->id, (ID **)&temp_object, LIB_ID_COPY_LOCALIZE); + Object *temp_object = (Object *)BKE_id_copy_ex(NULL, &object->id, NULL, LIB_ID_COPY_LOCALIZE); /* Remove all modifiers, since we don't want them to be applied. */ BKE_object_free_modifiers(temp_object, LIB_ID_CREATE_NO_USER_REFCOUNT); @@ -1177,11 +1176,8 @@ static Mesh *mesh_new_from_mesh(Object *object, Mesh *mesh) * add the data to 'mesh' so future calls to this function don't need to re-convert the data. */ BKE_mesh_wrapper_ensure_mdata(mesh); - Mesh *mesh_result = NULL; - BKE_id_copy_ex(NULL, - &mesh->id, - (ID **)&mesh_result, - LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT); + Mesh *mesh_result = (Mesh *)BKE_id_copy_ex( + NULL, &mesh->id, NULL, LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT); /* NOTE: Materials should already be copied. */ /* Copy original mesh name. This is because edit meshes might not have one properly set name. */ BLI_strncpy(mesh_result->id.name, ((ID *)object->data)->name, sizeof(mesh_result->id.name)); @@ -1407,16 +1403,16 @@ Mesh *BKE_mesh_create_derived_for_modifier(struct Depsgraph *depsgraph, { Mesh *me = ob_eval->runtime.data_orig ? ob_eval->runtime.data_orig : ob_eval->data; const ModifierTypeInfo *mti = BKE_modifier_get_info(md_eval->type); - Mesh *result; + Mesh *result = NULL; KeyBlock *kb; ModifierEvalContext mectx = {depsgraph, ob_eval, MOD_APPLY_TO_BASE_MESH}; if (!(md_eval->mode & eModifierMode_Realtime)) { - return NULL; + return result; } if (mti->isDisabled && mti->isDisabled(scene, md_eval, 0)) { - return NULL; + return result; } if (build_shapekey_layers && me->key && @@ -1428,7 +1424,7 @@ Mesh *BKE_mesh_create_derived_for_modifier(struct Depsgraph *depsgraph, int numVerts; float(*deformedVerts)[3] = BKE_mesh_vert_coords_alloc(me, &numVerts); - BKE_id_copy_ex(NULL, &me->id, (ID **)&result, LIB_ID_COPY_LOCALIZE); + result = (Mesh *)BKE_id_copy_ex(NULL, &me->id, NULL, LIB_ID_COPY_LOCALIZE); mti->deformVerts(md_eval, &mectx, result, deformedVerts, numVerts); BKE_mesh_vert_coords_apply(result, deformedVerts); @@ -1439,8 +1435,7 @@ Mesh *BKE_mesh_create_derived_for_modifier(struct Depsgraph *depsgraph, MEM_freeN(deformedVerts); } else { - Mesh *mesh_temp; - BKE_id_copy_ex(NULL, &me->id, (ID **)&mesh_temp, LIB_ID_COPY_LOCALIZE); + Mesh *mesh_temp = (Mesh *)BKE_id_copy_ex(NULL, &me->id, NULL, LIB_ID_COPY_LOCALIZE); if (build_shapekey_layers) { add_shapekey_layers(mesh_temp, me); diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c index 8e168d09cc2..cf31b5338cb 100644 --- a/source/blender/blenkernel/intern/node.c +++ b/source/blender/blenkernel/intern/node.c @@ -2328,9 +2328,9 @@ bNodeTree *ntreeAddTree(Main *bmain, const char *name, const char *idname) bNodeTree *ntreeCopyTree_ex(const bNodeTree *ntree, Main *bmain, const bool do_id_user) { - bNodeTree *ntree_copy; const int flag = do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT | LIB_ID_CREATE_NO_MAIN; - BKE_id_copy_ex(bmain, (ID *)ntree, (ID **)&ntree_copy, flag); + + bNodeTree *ntree_copy = (bNodeTree *)BKE_id_copy_ex(bmain, (ID *)ntree, NULL, flag); return ntree_copy; } bNodeTree *ntreeCopyTree(Main *bmain, const bNodeTree *ntree) @@ -2981,9 +2981,8 @@ bNodeTree *ntreeLocalize(bNodeTree *ntree) /* Make full copy outside of Main database. * Note: previews are not copied here. */ - bNodeTree *ltree; - BKE_id_copy_ex( - NULL, &ntree->id, (ID **)<ree, (LIB_ID_COPY_LOCALIZE | LIB_ID_COPY_NO_ANIMDATA)); + bNodeTree *ltree = (bNodeTree *)BKE_id_copy_ex( + NULL, &ntree->id, NULL, (LIB_ID_COPY_LOCALIZE | LIB_ID_COPY_NO_ANIMDATA)); ltree->id.tag |= LIB_TAG_LOCALIZED; diff --git a/source/blender/blenkernel/intern/particle_distribute.c b/source/blender/blenkernel/intern/particle_distribute.c index fad93245a04..d5999196b60 100644 --- a/source/blender/blenkernel/intern/particle_distribute.c +++ b/source/blender/blenkernel/intern/particle_distribute.c @@ -942,7 +942,7 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, mesh = final_mesh; } else { - BKE_id_copy_ex(NULL, ob->data, (ID **)&mesh, LIB_ID_COPY_LOCALIZE); + mesh = (Mesh *)BKE_id_copy_ex(NULL, ob->data, NULL, LIB_ID_COPY_LOCALIZE); } BKE_mesh_tessface_ensure(mesh); @@ -990,7 +990,7 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, mesh = final_mesh; } else { - BKE_id_copy_ex(NULL, ob->data, (ID **)&mesh, LIB_ID_COPY_LOCALIZE); + mesh = (Mesh *)BKE_id_copy_ex(NULL, ob->data, NULL, LIB_ID_COPY_LOCALIZE); } BKE_mesh_tessface_ensure(mesh); diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c index a7e768a79c7..862ebf43913 100644 --- a/source/blender/blenkernel/intern/particle_system.c +++ b/source/blender/blenkernel/intern/particle_system.c @@ -4744,9 +4744,8 @@ static int hair_needs_recalc(ParticleSystem *psys) static ParticleSettings *particle_settings_localize(ParticleSettings *particle_settings) { - ParticleSettings *particle_settings_local; - BKE_id_copy_ex( - NULL, (ID *)&particle_settings->id, (ID **)&particle_settings_local, LIB_ID_COPY_LOCALIZE); + ParticleSettings *particle_settings_local = (ParticleSettings *)BKE_id_copy_ex( + NULL, (ID *)&particle_settings->id, NULL, LIB_ID_COPY_LOCALIZE); return particle_settings_local; } diff --git a/source/blender/blenkernel/intern/pointcloud.c b/source/blender/blenkernel/intern/pointcloud.c index 26f192d8625..32a09e0857f 100644 --- a/source/blender/blenkernel/intern/pointcloud.c +++ b/source/blender/blenkernel/intern/pointcloud.c @@ -309,8 +309,7 @@ PointCloud *BKE_pointcloud_copy_for_eval(struct PointCloud *pointcloud_src, bool flags |= LIB_ID_COPY_CD_REFERENCE; } - PointCloud *result; - BKE_id_copy_ex(NULL, &pointcloud_src->id, (ID **)&result, flags); + PointCloud *result = (PointCloud *)BKE_id_copy_ex(NULL, &pointcloud_src->id, NULL, flags); return result; } diff --git a/source/blender/blenkernel/intern/volume.cc b/source/blender/blenkernel/intern/volume.cc index 87c45753393..fbe6f3e075c 100644 --- a/source/blender/blenkernel/intern/volume.cc +++ b/source/blender/blenkernel/intern/volume.cc @@ -1319,8 +1319,7 @@ Volume *BKE_volume_copy_for_eval(Volume *volume_src, bool reference) flags |= LIB_ID_COPY_CD_REFERENCE; } - Volume *result; - BKE_id_copy_ex(NULL, &volume_src->id, (ID **)&result, flags); + Volume *result = (Volume *)BKE_id_copy_ex(NULL, &volume_src->id, NULL, flags); result->filepath[0] = '\0'; return result; |