import bpy
import math
from mathutils import Vector
# Set target location
target_location = Vector((0, 0, 60))
# Set initial locations
initial_locations = [Vector((0, 0, 0)), Vector((0, 0, -30))]
# Set torus properties
torus_major_radius = 60
torus_minor_radius = 1
# 初期位置との距離から速度を計算する
chousei_kijyun = 60
distances = [math.sqrt(sum([(a - b) ** 2 for a, b in zip(loc, target_location)])) for loc in initial_locations]
speeds = [0.1 * distance / chousei_kijyun if distance > 0 else 0 for distance in distances]
# Create torus objects and set locations
for i, initial_location in enumerate(initial_locations):
bpy.ops.mesh.primitive_torus_add(major_radius=torus_major_radius, minor_radius=torus_minor_radius)
torus = bpy.context.object
torus.location = initial_location
torus = bpy.context.object
if i == 0:
torus.name = "torus 上面 z=0位置"
else:
torus.name = "torus 追いつく"
# Set animation keyframes
last_frame = 600
for frame in range(last_frame+1):
distance = (target_location - torus.location).length
if distance > 0.01:
direction = (target_location - torus.location).normalized()
torus.location += direction * min(speeds[i], distance)
torus.keyframe_insert(data_path="location", frame=frame)
# Function to set the number of frames
def set_frame_range(start_frame, end_frame):
bpy.context.scene.frame_start = start_frame
bpy.context.scene.frame_end = end_frame
# Example: set the number of frames to 1000
set_frame_range(1, 1000)