2024年4月20日土曜日

How to split actions in Blender

One of the ways to further utilize animation assets in Blender is to split actions. Here is a step-by-step guide on how to split actions.


Step 1: Baking the Animation

First, bake the animation in Blender. This involves solidifying the animation data and making any necessary adjustments such as removing root motion. For detailed instructions, please refer to this link.


Step 2: Preparing a Text File for Action Splitting

Prepare a text file in the following format to split actions:

WalkForward:10-40

WalkRight:50-80

If the format of the animation names in the text file differs (e.g., "10-40 WalkForward"), please adjust the format using the script provided at this link.


Step 3: Using the ActionMaking.py Script

Open the ActionMaking.py script from Blender’s text editor. Set the file_path to the path of the text file prepared in Step 2, and set the original_action_name to the name of the action you want to split.


Step 4: Running the Script

Select the target armature and run the Run Script. This will appropriately split the specified action based on the text file, making each action available as a separate item.


ActionMaking.py


import bpy


# Function to load animation names and frame ranges from a text file

def load_animation_data(file_path):

    with open(file_path, 'r') as file:

        animation_data = [line.strip().split(':') for line in file if line.strip()]

    return [(data[0], tuple(map(int, data[1].split('-')))) for data in animation_data]


# Function to duplicate the specified action and set up each copy as an independent animation clip

def duplicate_and_setup_actions(obj, original_action_name, animation_data):

    original_action = bpy.data.actions.get(original_action_name)

    if not original_action:

        print("Original action not found:", original_action_name)

        return


    for anim_name, (start_frame, end_frame) in animation_data:

        # Duplicate the action and assign a new name

        new_action = original_action.copy()

        new_action.name = anim_name


        # Trim the action's keyframes to the specified frame range

        for fcurve in new_action.fcurves:

            keyframe_points = [kp for kp in fcurve.keyframe_points if start_frame <= kp.co.x <= end_frame]

            fcurve.keyframe_points.clear()

            for kp in keyframe_points:

                fcurve.keyframe_points.insert(frame=kp.co.x - start_frame, value=kp.co.y)


        # Optionally set the new action to the object (for demonstration, not always necessary)

        obj.animation_data.action = new_action


# Main execution block

if __name__ == "__main__":

    file_path = "C:\BlenderAsset\AnimationNames1.txt"  # Set the file path appropriately

    obj = bpy.context.object  # Get the current object

    original_action_name = "Action"  # Name of the original action to duplicate


    # Load animation data from the file

    animation_data = load_animation_data(file_path)

    # Duplicate and set up actions

    duplicate_and_setup_actions(obj, original_action_name, animation_data)


0 件のコメント:

コメントを投稿