1

如何使用 Python 脚本将多个动作剪辑添加到 Blender 2.9 中的单个 NLA 轨道?

我正在尝试遍历包含以下行的文件:

frame_number=X, object_name=Y, animation=Z

例如

frame_number=1235, object_name=31, animation=action_off

我想遍历这些行并执行以下操作:

  1. 选择object_nameframe_number
  2. 选择PianoKeyAnimationsNLATrack。
  3. 如果action_on添加KeyPress动画。如果action_off添加KeyRelease动画。
  4. 取消选择PianoKeyAnimationNLATrack,KeyPress/KeyRelease动画和object_name场景集合。

我设置的 NLA 动作剪辑/条带是:

- KeyPress: Duration of 1 frame
- KeyRelease: Duration 1 frame

这是我正在努力解决的脚本部分:

# 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)

这似乎是用选定的对象创建多个 NLATrack。

见截图:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

有没有人对如何解决这个问题有任何建议?谢谢!

4

0 回答 0