为不透明度和音量编写关键帧插入脚本

Scripting the Keyframe Inserts for Opacity and Volume

在 Blender 2.74 下的 Video Sequence Editor 中添加、修剪和排列我的家庭视频记录。我的下一个目标是使用脚本自动淡入和淡出每个音频和视频序列。

目前我的脚本循环遍历所有序列并检查类型。如果序列类型是电影或图像,则不透明度将被设置关键帧,如果序列是声音,则音量将被设置关键帧。目前我的脚本还可以找到每个序列的开始和结束帧,以及计算和跳转到淡入淡出应该 start/end 的帧的能力。但是,要使用脚本对 Graph Editor 内的不透明度和体积进行关键帧设置,似乎不可能。

根据 Blender 2.73.8 API,似乎可以使用 bpy.ops.graph.keyframe_insert(type='ALL') 编写关键帧脚本,但似乎无法对不透明度和音量进行关键帧设置使用脚本。

有人可以告诉我如何使用脚本设置不透明度和体积的关键帧吗?

import bpy



# ----------------------------------------
# main
# ----------------------------------------

scene = bpy.context.scene
scene.frame_current = 0

queue = scene.sequence_editor.sequences

print("Frames Per Second [ ", scene.render.fps, " ].")
bpy.ops.sequencer.select_all(0)

for i in queue:
    itemLead = i.frame_start
    itemBack = itemLead + i.frame_final_duration

    print("Lead [ ", itemLead, " ] Tail [ ", itemBack, " ].")
    itemType = i.type

    if itemType == "MOVIE":
        i.select = 1

        scene.frame_current = itemLead
        i.blend_alpha = 0.0

        ##bpy.ops.graph.keyframe_insert(type="blend_alpha")

        print("Movie mode.")
        i.select = 0

        continue

    if itemType == "SOUND":
        i.select = 1
        print("Sound mode.")
        i.select = 0

        continue

    if itemType == "IMAGE":
        i.select = 1
        print("Image mode.")
        i.select = 0

        continue

    print("Skipped [ ", itemType, " ].")

要使用 python 添加关键帧,您需要告诉 属性(条带)的所有者 insert a keyframe 其属性之一(不透明度)

scene = bpy.context.scene
queue = scene.sequence_editor.sequences

queue[0].blend_alpha = 0.0
queue[0].keyframe_insert('blend_alpha', frame=1)

queue[0].blend_alpha = 1.0
queue[0].keyframe_insert('blend_alpha', frame=10)

您可能还注意到,您可以为关键帧指定帧,这样您就不需要调整当前帧。如果你确实想改变当前帧,最好使用 scene.frame_set().

请参阅下面我的代码的最终版本:

import bpy



# ----------------------------------------
# main
# ----------------------------------------

scene = bpy.context.scene
queue = scene.sequence_editor.sequences
depth = scene.render.fps * 1.8

for i in queue:

    itemType = i.type
    itemLead = i.frame_offset_start + i.frame_start
    itemHind = itemLead + i.frame_final_duration

    if itemType == "MOVIE":

        i.blend_alpha = 0.0
        i.keyframe_insert(data_path="blend_alpha", frame=itemLead)

        i.blend_alpha = 1.0
        i.keyframe_insert(data_path="blend_alpha", frame=itemLead + depth)

        i.blend_alpha = 1.0
        i.keyframe_insert(data_path="blend_alpha", frame=itemHind - depth)

        i.blend_alpha = 0.0
        i.keyframe_insert(data_path="blend_alpha", frame=itemHind)

        continue

    if itemType == "SOUND":

        i.volume = 0.0
        i.keyframe_insert(data_path="volume", frame=itemLead)

        i.volume = 1.0
        i.keyframe_insert(data_path="volume", frame=itemLead + depth)

        i.volume = 1.0
        i.keyframe_insert(data_path="volume", frame=itemHind - depth)

        i.volume = 0.0
        i.keyframe_insert(data_path="volume", frame=itemHind)

        continue

    if itemType == "IMAGE":

        i.blend_alpha = 0.0
        i.keyframe_insert(data_path="blend_alpha", frame=itemLead)

        i.blend_alpha = 1.0
        i.keyframe_insert(data_path="blend_alpha", frame=itemLead + depth)

        i.blend_alpha = 1.0
        i.keyframe_insert(data_path="blend_alpha", frame=itemHind - depth)

        i.blend_alpha = 0.0
        i.keyframe_insert(data_path="blend_alpha", frame=itemHind)

        continue