Python에서 선택한 스트립의 모든 키 프레임을 얻는 방법은 무엇입니까?

Aug 19 2020

VSE에 이미지 스트립이 있고 불투명도에 대해 2 개의 키 프레임이 있다고 가정합니다. 파이썬을 통해 각각의 값을 어떻게 편집 할 수 있습니까?

답변

6 Leander Aug 19 2020 at 17:02

블렌더의 데이터에 액세스하려면 bpy 모듈을 가져옵니다.

import bpy

컨텍스트 방법

키 프레임을 방금 만들고 텍스트 편집기에서 단순히 스크립팅하는 경우 컨텍스트별로 개체를 조회 할 수 있습니다. 스트립에 두 개의 키 프레임이 있고 시퀀서에서 선택 (활성)되어 있는지 확인해야합니다.

컨텍스트별로 활성 장면을 가져옵니다.

scene = bpy.context.scene

컨텍스트별로 활성 시퀀서 스트립을 가져옵니다.

strip = scene.sequence_editor.active_strip

data_path이름과 속성으로 활성 스트립에 연결된 fcurve를 찾습니다 . ( 을 사용하여 data_path를 더 우아하게 구성하는 방법에 대한 batFINGER의 답변 을 참조하십시오 strip.path_from_id("blend_alpha").)

data_path = 'sequence_editor.sequences_all["' + strip.name + '"].blend_alpha'
fcrv = scene.animation_data.action.fcurves.find(data_path)

keyframe_pointsfcurve에 저장된 값에 일부 값을 지정하십시오 .

for i, y in [[0, 0.0], [1, 1.0]]:
    fcrv.keyframe_points[i].co.y = y
    fcrv.keyframe_points[i].handle_left.y = y
    fcrv.keyframe_points[i].handle_right.y = y

시퀀서를 강제로 새로 고칩니다.

bpy.ops.sequencer.refresh_all()

데이터 방법

애드온에서이 기능을 사용하려고하거나 활성 스트립이 선택되었는지 확실하지 않거나 키 프레임이 전혀 존재하지 않는 경우 데이터를 검증해야합니다.

github에서 코드 스 니펫을 봅니다.

전제 조건이 누락 된 경우 중단 할 수있는 메서드의 기능을 캡슐화합니다. 필수 매개 변수는 다음과 같습니다.

  • 장면의 이름

  • 시퀀서 스트립의 이름

  • 기존 키 프레임 값을 덮어 쓰는 데 사용되는 값

    def modify_strip_keyframes (scene_name, strip_name, keyframe_values ​​= [1.0, 0.0]) :

이름으로 장면을 가져 오지만 존재하지 않으면 반환합니다.

    scene = bpy.data.scenes.get(scene_name)
    if scene == None:
        print("Scene not found.")
        return

이 있는지 확인 animation_data하고 sequence_editor. 키 프레임이 없거나 스트립이없는 경우 이러한 항목은입니다 None. (의 속성을 호출 None하면 스크립트가 중단됩니다.)

    if (scene.animation_data == None or scene.sequence_editor == None):
        print("No strips with keyframes.")
        return
    

이름으로 스트립을 얻고 그것의 blend_alpha(불투명도) 속성 과 관련된 fcurve를 얻습니다 .

    strip = scene.sequence_editor.sequences.get(strip_name)
    if strip == None:
        print("Strip not found.")
        return
    
    data_path = 'sequence_editor.sequences_all["' + strip_name + '"].blend_alpha'
    fcrv = scene.animation_data.action.fcurves.find(data_path)
    
    if fcrv == None:
        print("No opacity keyframes found.")
        return

keyframe_points값이 제공된만큼 fcurve에 많은 것이 있는지 확인하십시오 . 그런 다음 점을 반복하고 새 값을 keyframe_point좌표에 할당합니다 co.

    if len(fcrv.keyframe_points) != len(keyframe_values):
        print("The strip has " + str(len(fcrv.keyframe_points)) +
            " keys, but " + str(len(keyframe_values)) + " values were supplied.")
        return
    
    for i in range(len(fcrv.keyframe_points)):
        key = fcrv.keyframe_points[i]
        key.co.y = keyframe_values[i]
        key.handle_left.y = keyframe_values[i]
        key.handle_right.y = keyframe_values[i]
        key.handle_left.x = key.co.x
        key.handle_right.x = key.co.x
    

함수를 실행하려면 함수를 호출하고 시퀀서를 새로 고쳐 변경 사항을 시각화하십시오.

modify_strip_keyframes("Scene", "cat", keyframe_values = [1, 0.5])
bpy.ops.sequencer.refresh_all()
6 batFINGER Aug 19 2020 at 17:17

데이터 경로를 기반으로 fcurve 찾기

그래프 편집기로 전환하고 가시적 fcurves를 보는 제안은 그래프 편집기 설정에 따라 다릅니다.

대신 키 프레임 된 데이터 경로를 만들고 작업 fcurves에서 검색합니다.

VSE 스트립 애니메이션은 장면 개체에 속합니다.

아래 스크립트

  • 활성 스트립을 가져옵니다

  • ID 개체 인 장면, "Foo"라는 활성 영화 스트립의 예에서 활성 스트립의 경로를 찾습니다.

     'sequence_editor.sequences_all["Foo"].blend_alpha'
    
  • 장면 액션 fcurve 컬렉션 내에서 검색합니다.

참고 : None시퀀스 편집기, 활성 스트립, 애니메이션 데이터 및 작업을 포함하여 위의 많은 또는 모든 속성이 값 을 가질 수 있습니다 . 각각에 대해 테스트해야합니다.

import bpy
from bpy import context

scene = context.scene
seq = scene.sequence_editor
active_strip = seq.active_strip
datapath = active_strip.path_from_id("blend_alpha")
action = scene.animation_data.action

fc = action.fcurves.find(datapath)
1 Noname Aug 19 2020 at 16:52
#Assuming you are at VSE with your strip selected
context.area.type = 'GRAPH_EDITOR'
for fcurve in context.visible_fcurves:
    for keyframe in fcurve.keyframe_points:
        #Do w/e you want with the keyframe
        pass
#We switch back to VSE
context.area.type = 'SEQUENCE_EDITOR'

내 오해는 그래프> 키 프레임이었습니다. 현실은 그래프> FCurves> 키 프레임입니다.