import bpy
import math
zion_collection_name = "Real ball"
# コレクションを作成する
col = bpy.data.collections.new(zion_collection_name)
bpy.context.scene.collection.children.link(col)
import bpy
import math
from mathutils import Vector
# Set target location
target_location = Vector((0, 0, 60))
# Set initial locations
initial_locations = [Vector((-30, 0, 0)), Vector((30, 0, 0))]
# Set sphere radius and segments
sphere_radius = 2
sphere_segments = 32
# 初期位置との距離から速度を計算する
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 * (chousei_kijyun/ distance) if distance > 0 else 0 for distance in distances]
# Create sphere objects and set locations
for i, initial_location in enumerate(initial_locations):
bpy.ops.mesh.primitive_uv_sphere_add(radius=sphere_radius, segments=sphere_segments)
sphere = bpy.context.object
sphere.location = initial_location
if i == 0:
sphere.name = "Real ball x=-30"
else:
sphere.name = "Real ball x=+30"
# Set animation keyframes
last_frame = 1000
for frame in range(last_frame+1):
distance = (target_location - sphere.location).length
if distance > 0.01:
direction = (target_location - sphere.location).normalized()
sphere.location += direction * min(speeds[i], distance)
sphere.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)