2023年4月12日水曜日

Copies keyframes of specific frame number of selected bones in pose mode to specified frame numbers.

 import bpy


# キーフレームの番号リスト

keyframe_numbers = [1, 2]  # このリストにコピー先のキーフレーム番号を追加してください。


# アクティブなオブジェクトがアーマチュアであることを確認

if bpy.context.object.type == 'ARMATURE':

    armature = bpy.context.object

    action = armature.animation_data.action


    # 選択中のボーンに対して処理を行う

    for bone in armature.pose.bones:

        if bone.bone.select:

            bone_path = f'pose.bones["{bone.name}"].'


            # キーフレーム10の各F-Curveを探索

            for fcurve in action.fcurves:

                if fcurve.data_path.startswith(bone_path):

                    keyframe_10 = None


                    # キーフレーム10の値を取得

                    for keyframe in fcurve.keyframe_points:

                        if keyframe.co[0] == 10:

                            keyframe_10 = keyframe

                            break


                    # キーフレーム10が見つかった場合

                    if keyframe_10 is not None:

                        # 与えられた番号のリストのキーフレームにコピー

                        for kf_number in keyframe_numbers:

                            fcurve.keyframe_points.insert(kf_number, keyframe_10.co[1])


else:

    print("Error: Active object is not an armature.")