为不透明度和体积编写关键帧插入的脚本

时间:2015-12-09 23:19:45

标签: python blender

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

目前我的脚本循环遍历所有序列并检查类型。如果序列类型是电影或图像,则不透明度将是关键帧,如果序列是声音,则要将音量设置为关键帧。目前我的脚本还能够找到每个序列的开始和结束帧,以及计算和跳转到应该开始/结束衰落的帧的能力。但是,要使用脚本来限制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, " ].")

2 个答案:

答案 0 :(得分:5)

要使用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()

答案 1 :(得分:1)

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

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