From 21d2856429433eb80b97e30b5e4b71f85d894e52 Mon Sep 17 00:00:00 2001 From: Andrew Gribben Date: Mon, 26 Oct 2026 18:36:37 +0000 Subject: [PATCH] Size painted segmentation from the filament count, not the color palette (#15998) multi_material_segmentation_by_painting sized its per-filament output from filament_colour, but apply_mm_segmentation reads one entry per filament_diameter. When the palette was shorter than the filament list, slicing an object painted with a filament that had no color read past the end and crashed. Size it from filament_diameter instead. Adds a regression test that paints one side of a cube with filament 2 while filament_colour has one entry. --- src/libslic3r/MultiMaterialSegmentation.cpp | 3 +- tests/fff_print/test_multifilament.cpp | 31 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/MultiMaterialSegmentation.cpp b/src/libslic3r/MultiMaterialSegmentation.cpp index 651d815144..79ae7e2d8a 100644 --- a/src/libslic3r/MultiMaterialSegmentation.cpp +++ b/src/libslic3r/MultiMaterialSegmentation.cpp @@ -2469,7 +2469,8 @@ std::vector> segmentation_by_painting(const PrintObject // Returns multi-material segmentation based on painting in multi-material segmentation gizmo std::vector> multi_material_segmentation_by_painting(const PrintObject &print_object, const std::function &throw_on_cancel_callback) { - const size_t num_facets_states = print_object.print()->config().filament_colour.size() + 1; + // apply_mm_segmentation() reads one entry per filament_diameter, and filament_colour can be shorter. + const size_t num_facets_states = print_object.print()->config().filament_diameter.size() + 1; const float max_width = float(print_object.config().mmu_segmented_region_max_width.value); const float interlocking_depth = float(print_object.config().mmu_segmented_region_interlocking_depth.value); const bool interlocking_beam = print_object.config().interlocking_beam.value; diff --git a/tests/fff_print/test_multifilament.cpp b/tests/fff_print/test_multifilament.cpp index 45e1bdef77..3125bf6757 100644 --- a/tests/fff_print/test_multifilament.cpp +++ b/tests/fff_print/test_multifilament.cpp @@ -7,6 +7,8 @@ #include #include "libslic3r/GCode/GCodeProcessor.hpp" #include "libslic3r/GCodeReader.hpp" +#include "libslic3r/TriangleSelector.hpp" +#include "libslic3r/MultiMaterialSegmentation.hpp" #include "test_helpers.hpp" #include "test_utils.hpp" @@ -54,6 +56,35 @@ static std::set tools_for_role(const std::string& gcode, const std::string& return tools; } +TEST_CASE("Painted filaments slice when the display palette is shorter than the filament list", "[MultiFilament][Regression]") +{ + auto config = multifilament_config(2, { + { "filament_colour", "#FF0000" }, + { "enable_prime_tower", "0" }, + { "gcode_comments", "1" } + }); + Print print; + Model model; + init_print({cube(10)}, print, model, config); + auto *volume = model.objects.front()->volumes.front(); + TriangleSelector selector(volume->mesh()); + // Paint one side with the second filament, leaving the rest on the first. + selector.set_facet(4, EnforcerBlockerType::Extruder2); + selector.set_facet(5, EnforcerBlockerType::Extruder2); + REQUIRE(volume->mmu_segmentation_facets.set(selector)); + print.apply(model, config); + PrintObject *object = print.objects_mutable().front(); + object->slice(); + const auto segmentation = multi_material_segmentation_by_painting(*object, [] {}); + REQUIRE_FALSE(segmentation.empty()); + bool second_filament_present = false; + for (const auto &layer : segmentation) { + REQUIRE(layer.size() == 2); + second_filament_present |= !layer[1].empty(); + } + REQUIRE(second_filament_present); +} + // X where the nozzle sits while each tagged _WAIT_FOR_TEMP_ON_WIPE_TOWER M109 blocks: // the nearest preceding G1 carrying an X (the park travel emitted just before the wait). static std::vector wait_park_xs(const std::string& gcode)