Anim: make it easier to convert from legacy to current Action API
The changes:
1. Add `group_name` to the `channelbag.fcurves.new()` and
`action.fcurve_ensure_for_datablock()` RNA functions.
2. Add `anim_utils.action_ensure_channelbag_for_slot(action, slot)`.
3. Add `channelbag.fcurves.ensure()` RNA function.
This makes it possible to replace this legacy code:
```py
fcurve = action.fcurves.new("location", index=2, action_group="Name")
```
with this code:
```py
channelbag = action_ensure_channelbag_for_slot(action, action_slot)
fcurve = channelbag.fcurves.new("location", index=2, group_name="Name")
```
or replace this legacy code:
```py
fcurve = action.fcurves.find("location", index=2, action_group="Name")
if not fcurve:
fcurve = action.fcurves.new("location", index=2, action_group="Name")
```
with this code:
```py
channelbag = action_ensure_channelbag_for_slot(action, action_slot)
fcurve = channelbag.fcurves.ensure("location", index=2, group_name="Name")
```
Note that the parameter name is different (`action_group` became
`group_name`). This clarifies that this is the name of the group, and
not a reference to the group itself.
This is part of #146586
Pull Request: https://projects.blender.org/blender/blender/pulls/146977
This commit is contained in:
@@ -607,6 +607,21 @@ class ChannelbagsTest(unittest.TestCase):
|
||||
self.assertEqual([channelbag], list(self.strip.channelbags))
|
||||
self.assertEqual(self.slot, channelbag.slot)
|
||||
|
||||
def test_ensure_fcurve(self):
|
||||
channelbag = self.strip.channelbag(self.slot, ensure=True)
|
||||
self.assertEqual([], channelbag.fcurves[:])
|
||||
|
||||
fcurve_1 = channelbag.fcurves.ensure("location", index=2, group_name="Name")
|
||||
self.assertEqual("location", fcurve_1.data_path)
|
||||
self.assertEqual(2, fcurve_1.array_index)
|
||||
self.assertEqual("Name", fcurve_1.group.name)
|
||||
self.assertIn("Name", channelbag.groups)
|
||||
self.assertEqual([fcurve_1], channelbag.fcurves[:])
|
||||
|
||||
fcurve_2 = channelbag.fcurves.ensure("location", index=2, group_name="Name")
|
||||
self.assertEqual(fcurve_1, fcurve_2)
|
||||
self.assertEqual([fcurve_1], channelbag.fcurves[:])
|
||||
|
||||
def test_create_remove_fcurves(self):
|
||||
channelbag = self.strip.channelbags.new(self.slot)
|
||||
|
||||
@@ -988,6 +1003,24 @@ class ConvenienceFunctionsTest(unittest.TestCase):
|
||||
channelbag = self.action.layers[0].strips[0].channelbags[0]
|
||||
self.assertEqual(fcurve, channelbag.fcurves[0])
|
||||
|
||||
def test_fcurve_ensure_for_datablock_group_name(self) -> None:
|
||||
# Assign the Action to the Cube.
|
||||
ob_cube = bpy.data.objects["Cube"]
|
||||
adt = ob_cube.animation_data_create()
|
||||
adt.action = self.action
|
||||
|
||||
with self.assertRaises(IndexError):
|
||||
self.action.layers[0].strips[0].channelbags[0]
|
||||
|
||||
fcurve = self.action.fcurve_ensure_for_datablock(ob_cube, "location", index=2, group_name="grúpa")
|
||||
|
||||
channelbag = self.action.layers[0].strips[0].channelbags[0]
|
||||
self.assertEqual(fcurve, channelbag.fcurves[0])
|
||||
|
||||
# Check that the group was also created correctly.
|
||||
self.assertIn("grúpa", channelbag.groups)
|
||||
self.assertIn(fcurve, channelbag.groups["grúpa"].channels[:])
|
||||
|
||||
|
||||
def main():
|
||||
global args
|
||||
|
||||
Reference in New Issue
Block a user