diff --git a/source/blender/blenkernel/intern/blendfile.cc b/source/blender/blenkernel/intern/blendfile.cc index 09a9f69b8b3..cf781bd24dc 100644 --- a/source/blender/blenkernel/intern/blendfile.cc +++ b/source/blender/blenkernel/intern/blendfile.cc @@ -16,6 +16,8 @@ #include "MEM_guardedalloc.h" +#include "DNA_brush_types.h" +#include "DNA_material_types.h" #include "DNA_scene_types.h" #include "DNA_screen_types.h" #include "DNA_space_types.h" @@ -510,6 +512,27 @@ static void reuse_editable_asset_bmain_data_for_blendfile(ReuseOldBMainData *reu FOREACH_MAIN_LISTBASE_ID_END; } +/** + * Grease pencil brushes may have a material pinned that is from the current file. Moving local + * scene data to a different #Main is tricky, so in that case, unpin the material. + */ +static void unpin_file_local_grease_pencil_brush_materials(const ReuseOldBMainData *reuse_data) +{ + ID *old_id_iter; + FOREACH_MAIN_LISTBASE_ID_BEGIN (&reuse_data->old_bmain->brushes, old_id_iter) { + const Brush *brush = reinterpret_cast(old_id_iter); + if (brush->gpencil_settings && brush->gpencil_settings->material && + /* Don't unpin if this material is linked, then it can be preserved for the new file. */ + !ID_IS_LINKED(&brush->gpencil_settings->material)) + { + /* Unpin material and clear pointer. */ + brush->gpencil_settings->flag &= ~GP_BRUSH_MATERIAL_PINNED; + brush->gpencil_settings->material = nullptr; + } + } + FOREACH_MAIN_LISTBASE_ID_END; +} + /** * Does a complete replacement of data in `new_bmain` by data from `old_bmain. Original new data * are moved to the `old_bmain`, and will be freed together with it. @@ -946,6 +969,7 @@ static void setup_app_data(bContext *C, BKE_main_idmap_destroy(reuse_data.id_map); if (!params->is_factory_settings && reuse_editable_asset_needed(&reuse_data)) { + unpin_file_local_grease_pencil_brush_materials(&reuse_data); /* Keep linked brush asset data, similar to UI data. Only does a known * subset know. Could do everything, but that risks dragging along more * scene data than we want. */