diff --git a/source/blender/editors/object/object_bake_simulation.cc b/source/blender/editors/object/object_bake_simulation.cc index b45a2b81030..46239fbe9b9 100644 --- a/source/blender/editors/object/object_bake_simulation.cc +++ b/source/blender/editors/object/object_bake_simulation.cc @@ -390,6 +390,57 @@ static void reset_old_bake_cache(NodeBakeRequest &request) } } +static void try_delete_bake( + bContext *C, Object &object, NodesModifierData &nmd, const int bake_id, ReportList *reports) +{ + Main *bmain = CTX_data_main(C); + if (!nmd.runtime->cache) { + return; + } + bake::ModifierCache &modifier_cache = *nmd.runtime->cache; + std::lock_guard lock{modifier_cache.mutex}; + if (auto *node_cache = modifier_cache.simulation_cache_by_id.lookup_ptr(bake_id)) { + (*node_cache)->reset(); + } + else if (auto *node_cache = modifier_cache.bake_cache_by_id.lookup_ptr(bake_id)) { + (*node_cache)->reset(); + } + NodesModifierBake *bake = nmd.find_bake(bake_id); + if (!bake) { + return; + } + clear_data_block_references(*bake); + + const std::optional bake_path = bake::get_node_bake_path( + *bmain, object, nmd, bake_id); + if (!bake_path) { + return; + } + const char *meta_dir = bake_path->meta_dir.c_str(); + if (BLI_exists(meta_dir)) { + if (BLI_delete(meta_dir, true, true)) { + BKE_reportf(reports, RPT_ERROR, "Failed to remove metadata directory %s", meta_dir); + } + } + const char *blobs_dir = bake_path->blobs_dir.c_str(); + if (BLI_exists(blobs_dir)) { + if (BLI_delete(blobs_dir, true, true)) { + BKE_reportf(reports, RPT_ERROR, "Failed to remove blobs directory %s", blobs_dir); + } + } + if (bake_path->bake_dir.has_value()) { + const char *zone_bake_dir = bake_path->bake_dir->c_str(); + /* Try to delete zone bake directory if it is empty. */ + BLI_delete(zone_bake_dir, true, false); + } + if (const std::optional modifier_bake_dir = bake::get_modifier_bake_path( + *bmain, object, nmd)) + { + /* Try to delete modifier bake directory if it is empty. */ + BLI_delete(modifier_bake_dir->c_str(), true, false); + } +} + enum class BakeRequestsMode { /** * Bake all requests before returning from the function. @@ -411,6 +462,7 @@ static int start_bake_job(bContext *C, if (NodesModifierBake *bake = request.nmd->find_bake(request.bake_id)) { clear_data_block_references(*bake); } + try_delete_bake(C, *request.object, *request.nmd, request.bake_id, op->reports); } BakeGeometryNodesJob *job = MEM_new(__func__); @@ -698,57 +750,6 @@ static int bake_simulation_modal(bContext *C, wmOperator * /*op*/, const wmEvent return OPERATOR_PASS_THROUGH; } -static void try_delete_bake( - bContext *C, Object &object, NodesModifierData &nmd, const int bake_id, ReportList *reports) -{ - Main *bmain = CTX_data_main(C); - if (!nmd.runtime->cache) { - return; - } - bake::ModifierCache &modifier_cache = *nmd.runtime->cache; - std::lock_guard lock{modifier_cache.mutex}; - if (auto *node_cache = modifier_cache.simulation_cache_by_id.lookup_ptr(bake_id)) { - (*node_cache)->reset(); - } - else if (auto *node_cache = modifier_cache.bake_cache_by_id.lookup_ptr(bake_id)) { - (*node_cache)->reset(); - } - NodesModifierBake *bake = nmd.find_bake(bake_id); - if (!bake) { - return; - } - clear_data_block_references(*bake); - - const std::optional bake_path = bake::get_node_bake_path( - *bmain, object, nmd, bake_id); - if (!bake_path) { - return; - } - const char *meta_dir = bake_path->meta_dir.c_str(); - if (BLI_exists(meta_dir)) { - if (BLI_delete(meta_dir, true, true)) { - BKE_reportf(reports, RPT_ERROR, "Failed to remove metadata directory %s", meta_dir); - } - } - const char *blobs_dir = bake_path->blobs_dir.c_str(); - if (BLI_exists(blobs_dir)) { - if (BLI_delete(blobs_dir, true, true)) { - BKE_reportf(reports, RPT_ERROR, "Failed to remove blobs directory %s", blobs_dir); - } - } - if (bake_path->bake_dir.has_value()) { - const char *zone_bake_dir = bake_path->bake_dir->c_str(); - /* Try to delete zone bake directory if it is empty. */ - BLI_delete(zone_bake_dir, true, false); - } - if (const std::optional modifier_bake_dir = bake::get_modifier_bake_path( - *bmain, object, nmd)) - { - /* Try to delete modifier bake directory if it is empty. */ - BLI_delete(modifier_bake_dir->c_str(), true, false); - } -} - static int delete_baked_simulation_exec(bContext *C, wmOperator *op) { Vector objects;