Anim: Fix NLA Strip Action assignment

When creating a new NLA strip for an action, as well as when setting
`strip.action` via RNA, use the generic action-assignment code. This
ensures that the slot selection follows the same logic as other Action
assignments.

If the generic slot selection doesn't find a suitable slot, and there is
a single slot on that Action of a suitable ID type, always assign it.
This is to support the following scenario:

- Python script creates an Action and adds F-Curves via the legacy API.
- This creates a slot 'XXSlot'.
- The script creates multiple NLA strips for that Action.
- The desired result is that these strips get the same Slot assigned as
  well.

The generic code doesn't work for this, because:

- The first strip assignment would see the slot `XXSlot` (`XX`
  indicating "not bound to any ID type yet"). Because that slot has
  never been used, it will be assigned (which is good). This assignment
  would change its name to, for example, `OBSlot`.

- The second strip assignment would not see a 'virgin' slot, and thus
  not auto-select `OBSlot`. This behaviour makes sense when assigning
  Actions in the Action editor (assigning an Action that already
  animates 'Cube' to 'Suzanne' should not assign the 'OBCube' slot to
  Suzanne), but for the NLA I feel that it could be a bit more
  'enthousiastic' in auto-picking a slot to support the above case.

This is preparation for the removal of the 'Slotted Actions'
experimental flag, and getting the new code to run as compatibly as
possible with the legacy code.

The return value of `animrig::nla::assign_action()` has changed a bit.
It used to indicate whether a slot was auto-selected; it now indicates
whether the Action assignment was successful. Whether a slot was
assigned or not can be seen at `strip.action_slot`.

Pull Request: https://projects.blender.org/blender/blender/pulls/128892
This commit is contained in:
Sybren A. Stüvel
2024-10-11 10:38:24 +02:00
parent ee3aea4caf
commit 76bddb41df
5 changed files with 135 additions and 51 deletions

View File

@@ -13,6 +13,16 @@ import sys
import unittest
def enable_experimental_animation_baklava():
bpy.context.preferences.view.show_developer_ui = True
bpy.context.preferences.experimental.use_animation_baklava = True
def disable_experimental_animation_baklava():
bpy.context.preferences.view.show_developer_ui = False
bpy.context.preferences.experimental.use_animation_baklava = False
class AbstractNlaStripTest(unittest.TestCase):
""" Sets up a series of strips in one NLA track. """
@@ -113,6 +123,55 @@ class NlaStripBoundaryTest(AbstractNlaStripTest):
self.assertFrameValue(4.1, 1.0)
class NLAStripActionSlotSelectionTest(AbstractNlaStripTest):
def setUp(self):
enable_experimental_animation_baklava()
return super().setUp()
def tearDown(self) -> None:
disable_experimental_animation_baklava()
return super().tearDown()
def test_two_strips_for_same_action(self):
action = bpy.data.actions.new("StripAction")
action.slots.new()
self.assertTrue(action.is_action_layered)
self.assertEqual(1, len(action.slots))
track = self.nla_tracks.new()
self.assertEqual('UNSPECIFIED', action.slots[0].id_root)
strip1 = track.strips.new("name", 1, action)
self.assertEqual(action.slots[0], strip1.action_slot)
self.assertEqual('OBJECT', action.slots[0].id_root, "Slot should have been rooted to object")
strip2 = track.strips.new("name", 10, action)
self.assertEqual(action.slots[0], strip2.action_slot)
def test_switch_action_via_assignment(self):
action1 = bpy.data.actions.new("StripAction 1")
action1.slots.new()
self.assertTrue(action1.is_action_layered)
self.assertEqual(1, len(action1.slots))
action2 = bpy.data.actions.new("StripAction 2")
action2.slots.new()
self.assertTrue(action2.is_action_layered)
self.assertEqual(1, len(action2.slots))
track = self.nla_tracks.new()
strip = track.strips.new("name", 1, action1)
self.assertEqual(action1.slots[0], strip.action_slot)
self.assertEqual('OBJECT', action1.slots[0].id_root, "Slot of Action 1 should have been rooted to object")
strip.action = action2
self.assertEqual(action2.slots[0], strip.action_slot)
self.assertEqual('OBJECT', action2.slots[0].id_root, "Slot of Action 2 should have been rooted to object")
if __name__ == "__main__":
# Drop all arguments before "--", or everything if the delimiter is absent. Keep the executable path.
unittest.main(argv=sys.argv[:1] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []))