title: Blender 2.9 + Python script – Need help adding multiple action clips / action strips to an NLA track
body: <p>How can you add multiple action clips to a single NLA track in Blender 2.9 using a Python script?</p>
  <p>I'm trying to loop through a file with rows of:</p>
  <pre><code>frame_number=X, object_name=Y, animation=Z
  </code></pre>
  <p>e.g.</p>
  <pre><code>frame_number=1235, object_name=31, animation=action_off
  </code></pre>
  <p>I'd like to loop through these rows and do the following:</p>
  <blockquote>
  <ol>
  <li>Select the <code>object_name</code> at the <code>frame_number</code></li>
  <li>Select <code>PianoKeyAnimations</code> NLATrack.</li>
  <li>If <code>action_on</code> add <code>KeyPress</code> animation. If <code>action_off</code> add <code>KeyRelease</code> animation.</li>
  <li>Deselect <code>PianoKeyAnimation</code> NLATrack, <code>KeyPress</code> / <code>KeyRelease</code> animations and <code>object_name</code> in the Scene Collection.</li>
  </ol>
  </blockquote>
  <p>The NLA action clips/strips I've set up are:</p>
  <pre><code>- KeyPress: Duration of 1 frame
  - KeyRelease: Duration 1 frame
  </code></pre>
  <p>Here's the portion of the script that I'm struggling with:</p>
  <pre><code># 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)
  </code></pre>
  <p>This seems to be creating multiple NLATracks with the selected objects.</p>
  <p>See screenshots:</p>
  <p><a href="https://i.stack.imgur.com/TyiUM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TyiUM.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/kWKqm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kWKqm.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/xwqzE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xwqzE.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/fxCjx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fxCjx.png" alt="enter image description here" /></a></p>
  <p>Has anyone got any advice on how to solve this? Thanks!</p>