Skip to content

Instantly share code, notes, and snippets.

@IgnetStudio
Created March 19, 2025 20:41
Show Gist options
  • Save IgnetStudio/72b386021de488ef30d79953f9d00cb1 to your computer and use it in GitHub Desktop.
Save IgnetStudio/72b386021de488ef30d79953f9d00cb1 to your computer and use it in GitHub Desktop.
3d experiment
import bpy
# Select the object to animate (make sure the object is selected in Blender)
obj = bpy.context.active_object
# Check if the object exists
if obj is None:
print("Error: No object selected.")
else:
# Create animation data for the object if it doesn't exist
if obj.animation_data is None:
obj.animation_data_create()
# Create a new animation action
action = bpy.data.actions.new(name="MyAnimation")
obj.animation_data.action = action
# --- Position animation (Z-axis) ---
fcu_z = action.fcurves.new(data_path="location", index=2)
fcu_z.keyframe_points.add(3)
keyframe1 = fcu_z.keyframe_points[0]
keyframe1.co = 1, 0.0
keyframe1.interpolation = 'LINEAR'
keyframe2 = fcu_z.keyframe_points[1]
keyframe2.co = 50, 5.0
keyframe2.interpolation = 'LINEAR'
keyframe3 = fcu_z.keyframe_points[2]
keyframe3.co = 100, 0.0
keyframe3.interpolation = 'LINEAR'
# --- Rotation animation (Z-axis) ---
# We use "rotation_euler" because we want to control the rotation in degrees/radians.
fcu_rot_z = action.fcurves.new(data_path="rotation_euler", index=2)
fcu_rot_z.keyframe_points.add(3)
# Frame 1: Rotation 0 degrees (initial)
keyframe_rot_1 = fcu_rot_z.keyframe_points[0]
keyframe_rot_1.co = 1, 0.0 # 0 radians = 0 degrees
keyframe_rot_1.interpolation = 'LINEAR'
# Frame 50: Rotation 180 degrees
keyframe_rot_2 = fcu_rot_z.keyframe_points[1]
keyframe_rot_2.co = 50, 3.14159 # π radians = 180 degrees
keyframe_rot_2.interpolation = 'LINEAR'
# Frame 100: Rotation 360 degrees (back to initial)
keyframe_rot_3 = fcu_rot_z.keyframe_points[2]
keyframe_rot_3.co = 100, 6.28318 # 2π radians = 360 degrees (or 0)
keyframe_rot_3.interpolation = 'LINEAR'
# Refresh the 3D view
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
area.tag_redraw()
print("Animation created.")
@mic-jan
Copy link

mic-jan commented Mar 19, 2025

Użycie

  1. Zaznacz obiekt w Blenderze
  2. Przejdź na panel "Scripting"
  3. Wklej kod
  4. Uruchom "Run Script", żeby przypisać skrypt animacji do obiektu
  5. Można przetestować działanie animacji na panelu Layout > playback

Ta konkretna animacja trwa 100 klatek
1-50: ruch po osi Z do góry (aż współrzędna Z=5.0) i jednoczesny obrót wokół osi Z o 180 stopni
50-100: ruch w dół i dalsze rotacja do wykonania pełnego obrotu

@IgnetStudio
Copy link
Author

This solution works in 3x version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment