Created
May 18, 2025 15:13
-
-
Save ertugrulcetin/f41512611d74de20b44bd2f394f44576 to your computer and use it in GitHub Desktop.
Blender Script: Convert Character Animation to In-Place (Zero Root Bone Translation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
def make_animation_in_place(anim_name="rotate"): | |
""" | |
Modifies the given animation to be in-place by setting | |
X, Y, and Z location keyframe values to 0. | |
""" | |
action = bpy.data.actions.get(anim_name) | |
if not action: | |
print(f"Animation '{anim_name}' not found.") | |
return | |
print(f"Processing animation: {action.name}") | |
# We are interested in location F-Curves. | |
# For an object, these are typically: | |
# - 'location' with array_index 0 (X) | |
# - 'location' with array_index 1 (Y) | |
# - 'location' with array_index 2 (Z) | |
# Or, if applied to bones in pose mode: | |
# - 'pose.bones["BoneName"].location' with array_index 0 (X), 1 (Y), 2 (Z) | |
fcurves_modified_count = 0 | |
for fcurve in action.fcurves: | |
# Check for object-level location | |
if fcurve.data_path == "location" and fcurve.array_index in [0, 1, 2]: | |
print(f" Modifying F-Curve: {fcurve.data_path}, Index: {fcurve.array_index}") | |
for keyframe in fcurve.keyframe_points: | |
keyframe.co.y = 0.0 # Set value to 0 | |
keyframe.handle_left.y = 0.0 | |
keyframe.handle_right.y = 0.0 | |
fcurves_modified_count += 1 | |
# Check for bone-level location (common for armatures) | |
elif ".location" in fcurve.data_path and fcurve.array_index in [0, 1, 2]: | |
# Make sure it's a pose bone location, not something else | |
if fcurve.data_path.startswith("pose.bones[") and fcurve.data_path.endswith("].location"): | |
print(f" Modifying F-Curve: {fcurve.data_path}, Index: {fcurve.array_index}") | |
for keyframe in fcurve.keyframe_points: | |
keyframe.co.y = 0.0 # Set value to 0 | |
keyframe.handle_left.y = 0.0 | |
keyframe.handle_right.y = 0.0 | |
fcurves_modified_count += 1 | |
if fcurves_modified_count > 0: | |
print(f"Successfully modified {fcurves_modified_count} F-Curves for in-place animation.") | |
# It's good practice to update the view | |
for area in bpy.context.screen.areas: | |
if area.type == 'GRAPH_EDITOR': | |
area.tag_redraw() | |
elif area.type == 'DOPESHEET_EDITOR': | |
area.tag_redraw() | |
else: | |
print(f"No location F-Curves found or modified for animation '{anim_name}'. Ensure the animation has location keyframes.") | |
# --- How to use --- | |
# 1. Open Blender. | |
# 2. Go to the "Scripting" tab. | |
# 3. Click "+ New" to create a new text block. | |
# 4. Paste this script into the text editor. | |
# 5. Make sure the object with the "rotate" animation is selected, | |
# OR that the animation is available in bpy.data.actions. | |
# 6. Change anim_name="rotate" if your animation has a different name. | |
# 7. Click "Run Script" (the play button). | |
# Example of how to run it, REPLACE WITH YOUR ANIMATION NAME: | |
make_animation_in_place(anim_name="rotate") | |
print("Script finished.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment