Blender-Animation
Überblick
Erstelle und steuere Animationen in Blender mit Python. Keyframe Objekt- und Knochentransformationen, baue Armaturen mit IK/FK-Rigs auf, animiere Shape Keys für Gesichtsbewegungen, bearbeite F-Curves für die Timing-Kontrolle, schichte Aktionen im NLA-Editor und verbinde Treiber für ausdrucksbasierte Automatisierung – alles über das Terminal scriptbar.
Anleitungen
1. Eigenschaften von Objekten keyframen
import bpy
obj = bpy.data.objects["Cube"]
# Keyframe Position an mehreren Frames
for loc, frame in [((0,0,0), 1), ((5,0,3), 30), ((5,4,0), 60)]:
obj.location = loc
obj.keyframe_insert(data_path="location", frame=frame)
# Funktioniert auch für rotation_euler, scale, einzelne Achsen (index=2 für Z), benutzerdefinierte Eigenschaften
obj.rotation_euler = (0, 0, 3.14159)
obj.keyframe_insert(data_path="rotation_euler", frame=60)
obj.scale = (2, 2, 2)
obj.keyframe_insert(data_path="scale", frame=60)
obj["intensity"] = 1.0 # benutzerdefinierte Eigenschaft
obj.keyframe_insert(data_path='["intensity"]', frame=30)
bpy.context.scene.frame_start, bpy.context.scene.frame_end = 1, 60
2. F-Curve-Interpolation und Modifikatoren steuern
import bpy
action = bpy.data.objects["Cube"].animation_data.action
for fcurve in action.fcurves:
for kp in fcurve.keyframe_points:
kp.interpolation = 'BEZIER' # CONSTANT, LINEAR, BEZIER, SINE, EXPO, BOUNCE, ELASTIC
kp.easing = 'EASE_IN_OUT' # AUTO, EASE_IN, EASE_OUT, EASE_IN_OUT
# Cycles-Modifikator, um die Animation zu loopen
mod = fcurve.modifiers.new(type='CYCLES')
mod.mode_before = 'REPEAT' # NONE, REPEAT, REPEAT_OFFSET, MIRROR
mod.mode_after = 'REPEAT'
# Rauschen zu einem bestimmten Kanal hinzufügen
z_curve = action.fcurves.find("location", index=2) # Z-Position
if z_curve:
noise = z_curve.modifiers.new(type='NOISE')
noise.strength, noise.scale = 0.3, 5.0
3. Armaturen und Knochen erstellen
import bpy
arm_data = bpy.data.armatures.new("Rig")
arm_obj = bpy.data.objects.new("Rig", arm_data)
bpy.context.collection.objects.link(arm_obj)
bpy.context.view_layer.objects.active = arm_obj
bpy.ops.object.mode_set(mode='EDIT')
# (Name, Kopf, Ende, Elternknochen, verbunden)
bone_defs = [
("Spine", (0,0,1.0), (0,0,1.4), None, False),
("Chest", (0,0,1.4), (0,0,1.8), "Spine", False),
("UpperArm.L", (0.2,0,1.7), (0.5,0,1.4), "Chest", False),
("Forearm.L", (0.5,0,1.4), (0.8,0,1.1), "UpperArm.L", True),
("Thigh.L", (0.1,0,1.0), (0.1,0,0.5), "Spine", False),
("Shin.L", (0.1,0,0.5), (0.1,0,0.05),"Thigh.L", True),
]
for name, head, tail, parent, connected in bone_defs:
b = arm_data.edit_bones.new(name)
b.head, b.tail = head, tail
if parent:
b.parent = arm_data.edit_bones[parent]
b.use_connect = connected
bpy.ops.object.mode_set(mode='OBJECT')
4. Constraints an Knochen einrichten
import bpy
arm_obj = bpy.data.objects["Rig"]
bpy.context.view_layer.objects.active = arm_obj
bpy.ops.object.mode_set(mode='POSE')
# IK – verwende ein Empty als Ziel, chain_count begrenzt betroffene Knochen
ik = arm_obj.pose.bones["Forearm.L"].constraints.new('IK')
ik.target = bpy.data.objects["IK_Target"]
ik.pole_target = bpy.data.objects["IK_Pole"]
ik.chain_count = 2
# Rotation kopieren – spiegelt die Rotation eines anderen Objekts
cr = arm_obj.pose.bones["Chest"].constraints.new('COPY_ROTATION')
cr.target = bpy.data.objects["ChestCtrl"]
# Andere Typen: LIMIT_ROTATION, DAMPED_TRACK, STRETCH_TO, COPY_LOCATION, TRACK_TO
bpy.ops.object.mode_set(mode='OBJECT')
5. Knochenposen keyframen
import bpy, math
arm_obj = bpy.data.objects["Rig"]
bpy.context.view_layer.objects.active = arm_obj
bpy.ops.object.mode_set(mode='POSE')
if not arm_obj.animation_data:
arm_obj.animation_data_create()
action = bpy.data.actions.new("WalkCycle")
arm_obj.animation_data.action = action
# Keyframe den Oberschenkelschwung: zurück → neutral → nach vorne
thigh = arm_obj.pose.bones["Thigh.L"]
for angle, frame in [(-30, 1), (0, 13), (30, 25)]:
thigh.rotation_euler = (math.radians(angle), 0, 0)
thigh.keyframe_insert(data_path="rotation_euler", frame=frame)
bpy.ops.object.mode_set(mode='OBJECT')
6. Shape Keys erstellen und animieren
import bpy
obj = bpy.data.objects["Head"]
mesh = obj.data
if not mesh.shape_keys: # Basis-Key ist zuerst erforderlich
obj.shape_key_add(name="Basis", from_mix=False)
smile = obj.shape_key_add(name="Lächeln", from_mix=False)
for i, vert in enumerate(smile.data):
if i in [42, 43, 56, 57]: # Ecken des Mundes
vert.co.z += 0.02
# Animieren des Shape-Key-Werts (0.0 → 1.0 → 0.0)
sk = mesh.shape_keys.key_blocks["Lächeln"]
for val, frame in [(0.0, 1), (1.0, 15), (0.0, 30)]:
sk.value = val
sk.keyframe_insert(data_path="value", frame=frame)
7. Aktionen mit dem NLA-Editor überlagern
import bpy
arm_obj = bpy.data.objects["Rig"]
if not arm_obj.animation_data:
arm_obj.animation_data_create()
# Aktionen als NLA-Streifen auf separaten Tracks ablegen
track1 = arm_obj.animation_data.nla_tracks.new()
track1.name = "Gehen"
strip1 = track1.strips.new("Gehen", start=1, action=bpy.data.actions["WalkCycle"])
strip1.repeat = 4 # 4 Mal loopen
strip1.blend_type = 'REPLACE' # REPLACE, COMBINE, ADD, SUBTRACT, MULTIPLY
track2 = arm_obj.animation_data.nla_tracks.new()
track2.name = "Winken"
strip2 = track2.strips.new("Winken", start=25, action=bpy.data.actions["WaveHand"])
strip2.blend_type = 'COMBINE' # über das Gehen mischen
strip2.influence = 0.8 # teilweise Mischung
arm_obj.animation_data.action = None # aktive Aktion löschen, damit NLA übernimmt
8. Treiber für ausdrucksbasierte Animationen hinzufügen
import bpy
driver = bpy.data.objects["Cube"].driver_add("location", 2).driver # Z-Position antreiben
driver.type = 'SCRIPTED'
driver.expression = "sin(frame * 0.1) * 2"
# Eine Variable hinzufügen, die die Transformation eines anderen Objekts liest
var = driver.variables.new()
var.name = "ctrl"
var.type = 'TRANSFORMS'
var.targets[0].id = bpy.data.objects["Controller"]
var.targets[0].transform_type = 'LOC_X'
var.targets[0].transform_space = 'WORLD_SPACE'
driver.expression = "ctrl * 3" # funktioniert auch bei Shape Keys via sk.driver_add("value")
Beispiele
Beispiel 1: Springender Ball mit Stauchung und Streckung
Benutzeranfrage: „Einen Ball animieren, der mit Stauchung und Streckung springt“
import bpy
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0, 3))
ball = bpy.context.active_object
ball.name = "BouncingBall"
keyframes = [ # (Frame, z_pos, scale_x, scale_y, scale_z)
(1, 3.0, 1.0, 1.0, 1.0), (12, 0.5, 1.0, 1.0, 1.0), # Fall
(15, 0.3, 1.3, 1.3, 0.6), (18, 0.5, 0.85, 0.85, 1.2), # Stauchung + Streckung
(30, 2.2, 1.0, 1.0, 1.0), (42, 0.5, 1.0, 1.0, 1.0), # Höhepunkt + zweiter Fall
(45, 0.3, 1.2, 1.2, 0.7), (55, 1.5, 1.0, 1.0, 1.0), # kleinerer Sprung
(62, 0.5, 1.0, 1.0, 1.0), # zur Ruhe kommen
]
for frame, z, sx, sy, sz in keyframes:
ball.location = (0, 0, z)
ball.keyframe_insert(data_path="location", frame=frame)
ball.scale = (sx, sy, sz)
ball.keyframe_insert(data_path="scale", frame=frame)
for fc in ball.animation_data.action.fcurves:
for kp in fc.keyframe_points:
kp.interpolation = 'BEZIER'
bpy.context.scene.frame_end = 62
bpy.ops.wm.save_as_mainfile(filepath="/tmp/bouncing_ball.blend")
Beispiel 2: Arm-Rig mit IK, der nach einem Objekt greift
Benutzeranfrage: „Einen einfachen Arm-Rig mit IK erstellen und animieren, der nach einem Würfel greift“
import bpy
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
arm_data = bpy.data.armatures.new("ArmRig")
arm_obj = bpy.data.objects.new("ArmRig", arm_data)
bpy.context.collection.objects.link(arm_obj)
bpy.context.view_layer.objects.active = arm_obj
bpy.ops.object.mode_set(mode='EDIT')
prev = None
for name, head, tail in [("UpperArm",(0,0,1.5),(0.6,0,1.5)),
("Forearm",(0.6,0,1.5),(1.2,0,1.5)),
("Hand",(1.2,0,1.5),(1.4,0,1.5))]:
b = arm_data.edit_bones.new(name)
b.head, b.tail = head, tail
if prev: b.parent, b.use_connect = prev, True
prev = b
bpy.ops.object.mode_set(mode='OBJECT')
# IK-Ziel + Constraint
bpy.ops.object.empty_add(type='PLAIN_AXES', location=(1.2, 0, 1.5))
ik_target = bpy.context.active_object
ik_target.name = "IK_Hand"
bpy.context.view_layer.objects.active = arm_obj
bpy.ops.object.mode_set(mode='POSE')
ik = arm_obj.pose.bones["Hand"].constraints.new('IK')
ik.target, ik.chain_count = ik_target, 3
bpy.ops.object.mode_set(mode='OBJECT')
# IK-Ziel animieren, das nach einem Würfel greift
bpy.ops.mesh.primitive_cube_add(size=0.3, location=(1.5, 0.5, 1.0))
for loc, frame in [((1.2,0,1.5),1), ((1.5,0.5,1.0),30), ((1.5,0.5,1.3),50)]:
ik_target.location = loc
ik_target.keyframe_insert(data_path="location", frame=frame)
for fc in ik_target.animation_data.action.fcurves:
for kp in fc.keyframe_points:
kp.interpolation, kp.easing = 'BEZIER', 'EASE_IN_OUT'
bpy.context.scene.frame_end = 50
bpy.ops.wm.save_as_mainfile(filepath="/tmp/arm_grab.blend")
Richtlinien
data_pathmuss exakt mit dem RNA-Pfad übereinstimmen:"location","rotation_euler","scale",'["custom_prop"]'.- Armatur-Workflow: Bearbeitungsmodus für Knochen (
edit_bones), Posemodus für Constraints und Keyframes (pose.bones). - Verwende für IK Empty-Objekte als Ziele.
chain_count=0löst die gesamte Kette bis zur Wurzel. - Shape Keys benötigen zuerst einen „Basis“-Key. Animiere über
key_blocks["Name"].value(0.0 bis 1.0). - Interpolation:
CONSTANTfür Halten/Schritt,LINEARfür mechanisch,BEZIERmit Easing für organische Bewegung. - Setze
animation_data.action = None, damit NLA-Streifen die Animation steuern – eine aktive Aktion überschreibt NLA. - Bei Gehzyklen keyframe einen Schritt und verwende einen Cycles-F-Curve-Modifikator oder NLA-Wiederholung zum Loopen.
- Bakee Constraints/Treiber mit
bpy.ops.nla.bake()vor dem Export – andere Software kann sie nicht lesen.