import bpy
from mathutils import Vector
from math import sqrt
# Create first sphere
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, enter_editmode=False, location=(-30, 0, 0))
obj1 = bpy.context.active_object
# Animate first sphere
frames = 40 * bpy.context.scene.render.fps
for i in range(frames):
if obj1.location[0] < 0:
obj1.location = ((60 / frames) * i - 30, 0, 0)
else:
obj1.location = (30, 0, 0)
obj1.keyframe_insert(data_path="location", frame=i)
# Create second sphere
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, enter_editmode=False, location=(30, 0, 0))
obj2 = bpy.context.active_object
# Animate second sphere
for i in range(frames):
if obj2.location[0] > 0:
obj2.location = (30 - (60 / frames) * i, 0, 0)
else:
obj2.location = (-30, 0, 0)
obj2.keyframe_insert(data_path="location", frame=i)
# Create third sphere
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, enter_editmode=False, location=(0, 0, -10))
obj3 = bpy.context.active_object
# Animate third sphere
for i in range(frames):
# Check for collision between obj1 and obj2
if sqrt(sum((obj1.location - obj2.location)**2)) < obj1.dimensions[0]:
obj3.location = Vector((0, 0, 10))
else:
obj3.location = Vector((0, 0, -10))
obj3.keyframe_insert(data_path="location", frame=i)
bbb