import bpy
import math
zion_collection_name = "速度一定 Sphere Z軸"
# コレクションを作成する
col = bpy.data.collections.new(zion_collection_name)
bpy.context.scene.collection.children.link(col)
一応の完成
import bpy
import math
from mathutils import Vector
# 目標位置を指定する
target_location = Vector((-30, 0, 0))
# 初期位置のリスト
initial_locations = [
Vector((0, 0, 30)),
Vector((0, 0, 15)),
Vector((0, 0, 0)),
Vector((0, 0, -15)),
Vector((0, 0, -30))
]
# 初期位置との距離から速度を計算する
chousei_kijyun = 30
distances = [math.sqrt(sum([(a - b) ** 2 for a, b in zip(loc, target_location)])) for loc in initial_locations]
speeds = [0.1 for _ in range(len(distances))]
# 球体の作成と初期位置の設定
bpy.ops.object.empty_add(type='PLAIN_AXES')
empty = bpy.context.object
for index, loc in enumerate(initial_locations):
sphere = bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=loc)
obj = bpy.context.object
obj.name = f"Sphere {index+1}"
obj.parent = empty
# アニメーションの作成
last_frame = 600
for frame in range(last_frame+1):
for obj in empty.children:
if obj.name.startswith("Sphere"):
distance = (target_location - obj.location).length
if distance > 0.01:
direction = (target_location - obj.location).normalized()
obj.location += direction * min(speeds[int(obj.name[-1])-1], distance)
obj.keyframe_insert(data_path="location", frame=frame)