Skip to content

Instantly share code, notes, and snippets.

@santolucito
Created November 28, 2023 19:06
Show Gist options
  • Save santolucito/aaaf7865f09a9b917d3db1a0f1bfe6c6 to your computer and use it in GitHub Desktop.
Save santolucito/aaaf7865f09a9b917d3db1a0f1bfe6c6 to your computer and use it in GitHub Desktop.
blender bpy example with keyframes
import bpy
#delete all objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
#delete all materials
for m in bpy.data.materials:
bpy.data.materials.remove(m)
#create a new material
bpy.data.materials.new(name="myMaterial")
#set the color of the material
bpy.data.materials['myMaterial'].diffuse_color = (1, 0, 0, 1)
#make 5 cubes in a row
for i in range(5):
#make one cube
bpy.ops.mesh.primitive_cube_add(size=2, location=(i*3, 0, 0), scale=(1, 1, 1))
#give the cube the material we just created
bpy.context.object.data.materials.append(bpy.data.materials["myMaterial"]) #add the material to the object
#make a camera
bpy.ops.object.camera_add(location=(5,5,15))
cam = bpy.context.active_object
#set the camera to point at the cube
bpy.ops.object.constraint_add(type='TRACK_TO')
bpy.context.object.constraints["Track To"].target = bpy.data.objects["Cube"]
#make a light
bpy.ops.object.light_add(type='AREA', radius=1, align='WORLD', location=(5, 0, 5), scale=(1, 1, 1))
light = bpy.context.active_object
#turn up the power on the light
bpy.context.object.data.energy = 100
#add keyframes for the camera
cam.keyframe_insert("location", frame=1)
cam.location.x = -12
cam.location.z = 5
cam.keyframe_insert("location", frame=50)
#add keyframes for the light
light.keyframe_insert("location", frame=1)
light.location.x = 2
light.keyframe_insert("location", frame=50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment