【问题标题】:Getting array of Keyframe points in Blender在 Blender 中获取关键帧点数组
【发布时间】:2016-04-16 04:59:01
【问题描述】:

您好,我正在编写一个脚本,但它并没有像我希望的那样运行 这是我目前得到的

import bpy
def Key_Frame_Points(): #Gets the key-frame values as an array.

    fcurves = bpy.context.active_object.animation_data.action.fcurves

    for curve in fcurves:

        keyframePoints = fcurves[4].keyframe_points # selects Action channel's axis / attribute

        for keyframe in keyframePoints:

            print('KEY FRAME POINTS ARE  @T ',keyframe.co[0])         
            KEYFRAME_POINTS_ARRAY = keyframe.co[0]

    print(KEYFRAME_POINTS_ARRAY)

Key_Frame_Points()

当我运行它时,它会按照我的意愿打印出所选对象上的所有关键帧。但问题是我不能出于某种原因将值打印到变量中。如果您运行它并检查 System concole。它的行为很奇怪。就像它打印出关键帧对象的值一样。但是当我要求它以数组的形式获取这些值时,它只是打印出最后一帧。

这是它的简要外观

【问题讨论】:

    标签: python-3.x blender blender-2.67


    【解决方案1】:

    我认为您想要做的是将每个 keyframe.co[1] 添加到一个数组中,这意味着您要使用 KEYFRAME_POINTS_ARRAY.append(keyframe.co[1]) 并且要使其正常工作,您需要使用 KEYFRAME_POINTS_ARRAY = [] 将其定义为循环外的空数组

    请注意,keyframe.co[0] 是键控帧,而keyframe.co[1] 是该帧的键控值。

    另外值得注意的是,您正在循环 fcurves 但不使用每条曲线。

    for curve in fcurves:
        keyframePoints = fcurves[4].keyframe_points
    

    通过在这里使用fcurves[4],您每次都在读取相同的fcurve,您可能打算使用keyframePoints = curve.keyframe_points

    所以我希望你想要 -

    import bpy
    
    def Key_Frame_Points(): #Gets the key-frame values as an array.
        KEYFRAME_POINTS_ARRAY = []
        fcurves = bpy.context.active_object.animation_data.action.fcurves
    
        for curve in fcurves:
            keyframePoints = curve.keyframe_points
            for keyframe in keyframePoints:
                print('KEY FRAME POINTS ARE frame:{} value:{}'.format(keyframe.co[0],keyframe.co[1]))
                KEYFRAME_POINTS_ARRAY.append(keyframe.co[1])
        return KEYFRAME_POINTS_ARRAY
    
    print(Key_Frame_Points())
    

    您可能也有兴趣使用fcurves.find(data_path) 按路径查找特定曲线。

    还有fcurve.evaluate(frame) 可以为您提供任何帧的曲线值,而不仅仅是键控值。

    【讨论】:

    • 谢谢,但这就是我要做的。 ts 现在打印出我想要的关键帧点作为数组:P。在 KEYFRAME_POINTS_ARRAY.append(keyframe.co[0]) 中添加了一个附加并删除了 fcurves 中的 for 曲线:并将 KEYFRAME_POINTS_ARRAY 作为正确的数组打印出来。
    • 如果我的数据路径是.....rotation_quaternion,为什么co[1] 是单个值?
    • @KamilSzot 每个 fcurve 都保存一个值的键控数据。对于rotation_quaternion,您会发现四个fcurve,每个具有相同的数据路径,但array_index 值不同,quat 中的每个值对应一个。 This answer 应该有助于解释它。
    猜你喜欢
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    相关资源
    最近更新 更多