Docs: Add Action Slot example code to PyAPI docs

Add some example code for working with Action slots.

Reference: https://projects.blender.org/blender/blender-manual/issues/105297
Pull Request: https://projects.blender.org/blender/blender/pulls/151510
This commit is contained in:
Nick Alberelli 2026-01-09 16:32:49 +01:00 • committed by Sybren A. Stüvel
parent 1d676f42d8
commit d11b9878c4
4 changed files with 77 additions and 0 deletions

View file

@ -0,0 +1,28 @@
"""
Action Slots organize animation data within an action. Each action has slots with specific animation
data. An animated data-block specifies an action and a slot, determining the animation data it uses.
See the `Blender Manual <https://docs.blender.org/manual/en/5.1/animation/actions.html#action-slots>`_
for how Action Slots are used, or the `technical documentation <https://developer.blender.org/docs/features/animation/>`_
for details on the animation system's architecture.
Create & Access an Action Slot
++++++++++++++++++++++++++++++
To get started with Action Slots, you can easily create them by inserting a keyframe on an object. When you do this,
Blender automatically creates an Action & Slot for that data-block.
"""
import bpy
# Assume Suzanne mesh is present in the scene.
suzanne = bpy.data.objects["Suzanne"]
# Create animation data and an action for Suzanne:
# Slot will be automatically created.
suzanne.keyframe_insert("location", index=0)
# Action slots can be accessed like this:
action = suzanne.animation_data.action
for slot in action.slots:
print(f"Slot Identifier {slot.identifier!r} "
f"with name {slot.name_display!r} "
f"targets ID type {slot.target_id_type!r}")

View file

@ -0,0 +1,21 @@
"""
Manually Create an Action Slot
++++++++++++++++++++++++++++++
If required you can also manually create Action Slots on an Action. Note the ``target_id_type``
that matches the data-block type. Identifiers start with a prefix based on the ID type,
e.g. "OB" for objects, followed by the name. There can be identifiers like ``OBSuzanne``
and ``MESuzanne`` and the name (``Suzanne``) can be shared between them. This is intentional,
so that the slots and the datablocks can have the same name.
"""
# Actions creation.
action = bpy.data.actions.new("SuzanneAction")
# Creation of slots requires an ID type and a name.
slot = action.slots.new(id_type='OBJECT', name="Suzanne")
print(f"slot type={slot.target_id_type!r} "
f"name={slot.name_display!r} "
f"identifier={slot.identifier!r}")
# Output:
# slot type=OBJECT name=Suzanne identifier=OBSuzanne

View file

@ -0,0 +1,15 @@
"""
Explicitly Assigning Action Slots
+++++++++++++++++++++++++++++++++
An action slot is compatible with a data-block if the slot's ``target_id_type`` matches the data-block's type.
If there are multiple slots on the Action, and you want to just pick the first one that's
compatible, use the following code. ``anim_data.action_suitable_slots`` can be used `after` the
Action has been assigned; it is a list of action slots of that Action, but only the ones that
are actually compatible with the owner of anim_data (in this case, Suzanne).
"""
# If there are multiple slots on the Action, pick the first one that's compatible
anim_data = suzanne.animation_data_create()
anim_data.action = action
assert anim_data.action_suitable_slots, "expecting at least one suitable slot"
anim_data.action_slot = anim_data.action_suitable_slots[0]

View file

@ -0,0 +1,13 @@
"""
Finding Action Slot Users
+++++++++++++++++++++++++
To return a list of the data-blocks that are animated by a specific slot of an Action, use the ``users()`` method of the ActionSlot.
"""
# Iterate through all actions in the Blender data.
print("Action & slot users:")
for action in bpy.data.actions:
for slot in action.slots:
# Return the data-blocks that are animated by this slot of this action
users = slot.users()
print(f"{action.name:20} slot={slot.identifier:12s} users: {users}")