Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created June 1, 2023 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CGArtPython/83a324c9938216c91df746c15179e992 to your computer and use it in GitHub Desktop.
Save CGArtPython/83a324c9938216c91df746c15179e992 to your computer and use it in GitHub Desktop.
Beginner Blender Python tutorial code for a cube location animation (link to video: https://www.youtube.com/watch?v=nmJqIaSZlRs&lc=Ugw4q26HfFYOa0zv1Wh4AaABAg)
# give Python access to Blender's functionality
import bpy
# add a cube into the scene
bpy.ops.mesh.primitive_cube_add()
# get a reference to the currently active object
cube = bpy.context.active_object
# insert keyframe at frame one
start_frame = 1
cube.keyframe_insert("location", frame=start_frame)
# change the location of the cube on the z-axis
cube.location.z = 5
# insert keyframe at the last frame
end_frame = 180
cube.keyframe_insert("location", frame=end_frame)
@stat74
Copy link

stat74 commented Dec 3, 2023

This was helpful. I'm going to refactor this to remove the hard-coded font object name, but here's an example in which I move the z position of some words from z = 0.4 to z = 0.54 across 50 frames:

def move_letter_behind():
font_object = bpy.data.objects["Font Object.027"]
bpy.context.view_layer.objects.active = font_object
font_object.location.z = 0.4
font_object.keyframe_insert("location", frame=1)
font_object.location.z = 0.54
font_object.keyframe_insert("location", frame=50)

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