Currently my script uses this below to change the active animation action on the active object.

for a in bpy.data.actions:
    action = obj.animation_data.action
    obj.animation_data.action = bpy.data.actions.get("Another_Action")

How would I alter this to work with all selected objects instead of just the active object? My script also creates it's own custom prop which it adds to any armatures created by it. Essentially I need to first check if the object is selected/active and then check if "my_prop" exists within said object. This will keep my script only operating on armatures it's previously created (they'll have the proper bone names etc) and allow it to operate on all selected objects. If you can only help figure out how to run on selected objects without the custom prop in mind, that helps too. Thank you

import bpy

your_action = bpy.data.actions["Another_Action"]

for ob in bpy.context.selected_objects:
    # Skip non-armature objects
    if ob.type != 'ARMATURE': continue

    # Skip objects without your custom prop
    # NOTE: if the custom prop is on the armature,
    # not the object, use `not in ob.data` instead.
    if "Custom Prop Name" not in ob: continue

    # Create the animation data if it doesn't exist
    if not ob.animation_data:

    # Set the active action
    ob.animation_data.action = your_action
  • $\begingroup$ +1 for the helpful comments in the code $\endgroup$
    – Chris
    Nov 29 '21 at 3:00
  • $\begingroup$ Thank you, this looks like it will work and will try it asap. $\endgroup$ Nov 30 '21 at 5:18
  • $\begingroup$ I assume you meant bpy.context.selected_objects not bpy.data.selected_objects. I tried changing it to that and I still seem to get told there isn't an action/none type error. I have also created a panel with an action list so you can see the actions without opening the action editor. For some reason when I try to use selected_objects the action doesn't become active or "in the slot" unless I click on on an item/action in the action list but if you just are trying to change it for one armature and only one selected using "if bpy.context.active_object in bpy.data.objects" it updates properly $\endgroup$ Nov 30 '21 at 6:40
  • $\begingroup$ I did mean context.selected_objects! I didn't really follow the rest of your comment sorry. $\endgroup$
    – scurest
    Nov 30 '21 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.