Skip to main content
 首页 » 编程设计

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

2025年05月04日16kenshinobiy

在 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, " ].") 

请您参考如下方法:

要使用 Python 添加关键帧,您需要将属性( strip )的所有者告知 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() .