Fix: Grease Pencil: Wrong layer attributes after reorder

Reordering layer nodes by drag-drop, move up/down, add new etc. swaps
layer attributes with wrong layers. This is due to mistake in map
`new_by_old_map`. Values of this map is used as indices in src array.
And keys are indices of dst array. Expected behavior is to copy attribute from
old position (`layer_i_old`) of src array to new position (`layer_i_new`) of
dst array. See: `array_utils::gather`.

Noticed during !141772.

Pull Request: https://projects.blender.org/blender/blender/pulls/141935
This commit is contained in:
Pratik Borhade
2025-07-18 16:30:41 +02:00
committed by Falk David
parent 20eaa1b1b0
commit ef89c75382
2 changed files with 14 additions and 1 deletions

View File

@@ -3903,7 +3903,7 @@ static void reorder_layer_data(GreasePencil &grease_pencil,
const bke::greasepencil::Layer *layer = layers[layer_i_new];
BLI_assert(old_layer_index_by_layer.contains(layer));
const int layer_i_old = old_layer_index_by_layer.pop(layer);
new_by_old_map[layer_i_old] = layer_i_new;
new_by_old_map[layer_i_new] = layer_i_old;
}
BLI_assert(old_layer_index_by_layer.is_empty());

View File

@@ -24,12 +24,17 @@ class TestGreasePencil(unittest.TestCase):
class TestGreasePencilLayers(unittest.TestCase):
tint_factors = [0.3, 0.6, 0.9]
def setUp(self):
self.gp = bpy.data.grease_pencils_v3.new("test_grease_pencil")
self.gp.layers.new("test_layer01")
self.gp.layers.new("test_layer02")
self.gp.layers.new("test_layer03")
for i, layer in enumerate(self.gp.layers):
layer.tint_factor = self.tint_factors[i]
def tearDown(self):
bpy.data.grease_pencils_v3.remove(self.gp)
del self.gp
@@ -69,6 +74,14 @@ class TestGreasePencilLayers(unittest.TestCase):
self.assertEqual(self.gp.layers[1].name, "test_layer01")
self.assertEqual(self.gp.layers[2].name, "test_layer03")
def test_grease_pencil_layers_attribute_reorder(self):
layer = self.gp.layers[0]
self.gp.layers.move_top(layer)
# Check layer attribute
self.assertEqual(round(self.gp.layers[0].tint_factor, 1), self.tint_factors[1])
self.assertEqual(round(self.gp.layers[1].tint_factor, 1), self.tint_factors[2])
self.assertEqual(round(self.gp.layers[2].tint_factor, 1), self.tint_factors[0])
class TestGreasePencilFrame(unittest.TestCase):
def setUp(self):