Anim: ensure correct type prefix when setting Slot.identifier

Previously it was possible to make the type prefix of a Slot's identifier get
out-of-sync with its actual target ID type, by setting the identifier via
Python.

This PR changes `Slot.identifier` assignment to ensure that the type prefix is
set to match `target_id_type`. This now makes it impossible for the identifier
prefix and `target_id_type` to get out of sync, since this API previously was
the only way to accomplish that.

When the prefix that the user attempts to set doesn't match the `target_id_type`
of the Slot, a warning is issued telling the user about the mismatch and that
the identifier has been set with the correct prefix instead.

Pull Request: https://projects.blender.org/blender/blender/pulls/133983
This commit is contained in:
Nathan Vegdahl
2025-02-04 15:46:22 +01:00
committed by Nathan Vegdahl
parent 0be16983eb
commit f84197f0b9
5 changed files with 85 additions and 17 deletions

View File

@@ -191,6 +191,33 @@ class ActionSlotAssignmentTest(unittest.TestCase):
"After assignment, the ID type should remain UNSPECIFIED when the Action is linked.")
self.assertEqual("XXLegacy Slot", slot.identifier)
def test_slot_identifier_writing(self):
"""Test writing to the identifier of a slot."""
action = self._load_legacy_action(link=False)
slot_1 = action.slots[0]
slot_2 = action.slots.new('OBJECT', "Slot")
self.assertEqual("XXLegacy Slot", slot_1.identifier)
self.assertEqual('UNSPECIFIED', slot_1.target_id_type)
self.assertEqual("OBSlot", slot_2.identifier)
self.assertEqual('OBJECT', slot_2.target_id_type)
# Assigning identifier with same type prefix should work.
slot_1.identifier = "XXCoolerSlot"
slot_2.identifier = "OBCoolerSlot"
self.assertEqual("XXCoolerSlot", slot_1.identifier)
self.assertEqual("OBCoolerSlot", slot_2.identifier)
# Assigning identifier with different type prefix should still set the
# name part, but leave the type prefix untouched so that it stays
# consistent with the actual target ID type of the slot.
slot_1.identifier = "MAEvenCoolerSlot"
slot_2.identifier = "MAEvenCoolerSlot"
self.assertEqual("XXEvenCoolerSlot", slot_1.identifier)
self.assertEqual("OBEvenCoolerSlot", slot_2.identifier)
def test_untyped_slot_target_id_writing(self):
"""Test writing to the target id type of an untyped slot."""