4
$\begingroup$

I'm trying to write a script that pushes all the actions into NLA for the selected active object or a rig. But I'm using blender through the command prompt so I can't use code that has UI like

bpy.context.area.type = 'DOPESHEET_EDITOR' or bpy.context.space_data.ui_mode = 'ACTION'

I use this code but it only pushes one action. Can I modify it to push all the actions? I just start learning python for blender. Thanks

for ob in bpy.context.scene.objects:
if ob.animation_data is not None:
    action = ob.animation_data.action
    if action is not None:
        track = ob.animation_data.nla_tracks.new()
        track.strips.new(action.name, action.frame_range[0], action)
        ob.animation_data.action = None
$\endgroup$
1
  • $\begingroup$ The short answer is yes, if you do have multiple actions, you can add them to track strips without needing to use bpy.context. I'll try to write up an example when I have time later today. $\endgroup$ Feb 23, 2022 at 18:38

1 Answer 1

3
$\begingroup$

The thing is that you can only have one action in an object's animation_data so you can't iterate over any part of the object to find separate actions.

To get around this, you need to stash/push the actions to the object's animation_data.nla_tracks as you create them. Here's an example that gives an already selected object two actions and puts each action in a separate track. The stash function is from Context override for action stashing on devtalk.

Once you've created and "stashed" the actions in the nla_tracks, you can manipulate the tracks using direct manipulation to set up the tracks however you want them.

import bpy

object = bpy.context.active_object

# Function taken from https://devtalk.blender.org/t/context-override-for-action-stashing/7559
def stash(obj, action, track_name, start_frame):
    # Simulate stash :
    # * add a track
    # * add an action on track
    # * lock & mute the track
    # * remove active action from object
    tracks = obj.animation_data.nla_tracks
    new_track = tracks.new(prev=None)
    new_track.name = track_name
    strip = new_track.strips.new(action.name, start_frame, action)
    new_track.lock = True
    new_track.mute = True
    obj.animation_data.action = None

# Create an action
object.location = (0,0,0)
object.keyframe_insert("location", frame = 1)
object.location = (0,3,0)
object.keyframe_insert("location", frame = 13)
object.animation_data.action.name = "move me"
stash(object, object.animation_data.action, 'move track', 1)

# Create another action
object.rotation_euler = (0,0,0)
object.keyframe_insert("rotation_euler", frame = 1)
object.rotation_euler = (0,1,0)
object.keyframe_insert("rotation_euler", frame = 13)
object.animation_data.action.name = "turn me"
stash(object, object.animation_data.action, 'turn track', 1)

Obviously, instead of using stash you can use the technique from inside it to arrange the tracks how you want them. The key is that you have to do this for each action as soon as it is created.

Here is what the NLA editor looks like after I've run this script with a Monkey as the default object. Notice that the script stashes the actions but does not enable them. That's something else you have to do as you desire.

View of NLA editor once the script has run

$\endgroup$
5
  • 1
    $\begingroup$ Wow thanks Marty I'll try this right away $\endgroup$ Feb 24, 2022 at 1:12
  • 1
    $\begingroup$ Thank you it works like a charm! You're a genius !! In my case, I already have the actions so I just replace the create action into choose action with this code # object.animation_data.action = bpy.data.actions['walk'] # Thanks again $\endgroup$ Feb 24, 2022 at 1:35
  • $\begingroup$ @Indrapsukarmadji You're welcome. Since you're apparently a new user to Blender StackExchange, you might want to read What should I do when someone answers my question? and perhaps accept my answer. $\endgroup$ Feb 24, 2022 at 14:34
  • $\begingroup$ Yes, noted sorry $\endgroup$ Feb 26, 2022 at 1:04
  • $\begingroup$ @Indrapsukarmadji nothing for you to apologize for. StackExchange is different than other forums and it takes a bit of getting used to. I look forward to seeing what you accomplish. $\endgroup$ Feb 26, 2022 at 1:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .