From 874cf52c10d73911cfec709d9b0202ac690aab6f Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Tue, 6 Oct 2020 17:43:12 +0200 Subject: Refactor: Remove `BKE_XXX_localize()`, in favor of using regular ID copying code. Besides the NodeTree case (which remains unchanged), the localize code is only used in one place (to generate previews of shading data-blocks). This commit introduces a new `LIB_ID_CREATE_LOCAL` option for ID creation/copying, which essentially implements the behavior of the removed `BKE_XXX_localize()` functions into regular mainstream ID copy code. When this option is set: - new ID is tagged with `LIB_TAG_LOCALIZED`; - Some ID copying callbacks have specific behaviors, mainly the root nodetree of shading IDs gets duplicated with specialized `ntreeLocalize()` function. Note that I would not consider getting rid of `ntreeLocalize` for now, this function is recursive, which should ideally never happen within ID management copying code (this introduces all kind of complications). No behavioral change expected from this commit. --- source/blender/blenkernel/intern/material.c | 52 ++++++++--------------------- 1 file changed, 13 insertions(+), 39 deletions(-) (limited to 'source/blender/blenkernel/intern/material.c') diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c index d8c0b5d6dce..8c09cb551a1 100644 --- a/source/blender/blenkernel/intern/material.c +++ b/source/blender/blenkernel/intern/material.c @@ -100,12 +100,20 @@ static void material_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const Material *material_dst = (Material *)id_dst; const Material *material_src = (const Material *)id_src; + const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0; /* We always need allocation of our private ID data. */ const int flag_private_id_data = flag & ~LIB_ID_CREATE_NO_ALLOCATE; - if (material_src->nodetree) { - BKE_id_copy_ex( - bmain, (ID *)material_src->nodetree, (ID **)&material_dst->nodetree, flag_private_id_data); + if (material_src->nodetree != NULL) { + if (is_localized) { + material_dst->nodetree = ntreeLocalize(material_src->nodetree); + } + else { + BKE_id_copy_ex(bmain, + (ID *)material_src->nodetree, + (ID **)&material_dst->nodetree, + flag_private_id_data); + } } if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) { @@ -116,7 +124,8 @@ static void material_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const } if (material_src->texpaintslot != NULL) { - material_dst->texpaintslot = MEM_dupallocN(material_src->texpaintslot); + /* TODO: Think we can also skip copying this data in the more generic `NO_MAIN` case? */ + material_dst->texpaintslot = is_localized ? NULL : MEM_dupallocN(material_src->texpaintslot); } if (material_src->gp_style != NULL) { @@ -315,41 +324,6 @@ Material *BKE_material_copy(Main *bmain, const Material *ma) return ma_copy; } -/* XXX (see above) material copy without adding to main dbase */ -Material *BKE_material_localize(Material *ma) -{ - /* TODO(bastien): Replace with something like: - * - * Material *ma_copy; - * BKE_id_copy_ex(bmain, &ma->id, (ID **)&ma_copy, - * LIB_ID_COPY_NO_MAIN | LIB_ID_COPY_NO_PREVIEW | LIB_ID_COPY_NO_USER_REFCOUNT, - * false); - * return ma_copy; - * - * NOTE: Only possible once nested node trees are fully converted to that too. */ - - Material *man = BKE_libblock_copy_for_localize(&ma->id); - - man->texpaintslot = NULL; - man->preview = NULL; - - if (ma->nodetree != NULL) { - man->nodetree = ntreeLocalize(ma->nodetree); - } - - if (ma->gp_style != NULL) { - man->gp_style = MEM_dupallocN(ma->gp_style); - } - - BLI_listbase_clear(&man->gpumaterial); - - /* TODO Duplicate Engine Settings and set runtime to NULL */ - - man->id.tag |= LIB_TAG_LOCALIZED; - - return man; -} - Material ***BKE_object_material_array_p(Object *ob) { if (ob->type == OB_MESH) { -- cgit v1.2.3