How can you add multiple action clips to a single NLA track in Blender 2.9 using a Python script?
I'm trying to loop through a file with rows of:
frame_number=X, object_name=Y, animation=Z
e.g.
frame_number=1235, object_name=31, animation=action_off
I'd like to loop through these rows and do the following:
- Select the
object_name
at theframe_number
- Select
PianoKeyAnimations
NLATrack.- If
action_on
addKeyPress
animation. Ifaction_off
addKeyRelease
animation.- Deselect
PianoKeyAnimation
NLATrack,KeyPress
/KeyRelease
animations andobject_name
in the Scene Collection.
The NLA action clips/strips I've set up are:
- KeyPress: Duration of 1 frame
- KeyRelease: Duration 1 frame
Here's the portion of the script that I'm struggling with:
# Select Nlatrack based on name
def nlaselect(trackName):
ob = C.object
ad = ob.animation_data
if ad:
for i, track in enumerate(ad.nla_tracks):
# Find NLA track
track.select = track.name.startswith(trackName)
# make active track if in pos 0
if track.select and not i:
ad.nla_tracks.active = track
all_tracks = {DATA_HERE}
current_area = bpy.context.area.ui_type
bpy.context.area.ui_type = 'NLA_EDITOR'
current_frame = 0
frames_delay = 240 # Delay animation by 4 seconds in 60 fps render
for row in all_tracks:
current_frame += row.frame_number
C.view_layer.objects.active =
D.collections['INSTRUMENT'].all_objects[str(row.object)]
C.object.select_set(True)
bpy.context.area.ui_type = 'NLA_EDITOR'
C.object.animation_data.nla_tracks.active = None
C.scene.frame_set( current_frame + frames_delay )
if row.animation == 'action_on':
nlaselect('PianoKeyAnimations')
bpy.ops.nla.actionclip_add(action='KeyPress')
if row.animation == 'action_off':
nlaselect('PianoKeyAnimations')
bpy.ops.nla.actionclip_add(action='KeyRelease')
bpy.context.area.ui_type = current_area
C.object.select_set(False)
This seems to be creating multiple NLATracks with the selected objects.
See screenshots:
Has anyone got any advice on how to solve this? Thanks!