動いた
import bpy
from mathutils import Vector
# Set sphere radius
sphere_radius = 1
# Set target location
target_location = Vector((-30, 0, 0))
# Set initial location and movement direction for sphere
initial_location = Vector((0, 0, 0))
movement_direction = (target_location - initial_location).normalized()
# Create sphere and set location
bpy.ops.mesh.primitive_uv_sphere_add(radius=sphere_radius)
sphere = bpy.context.object
sphere.name = "Sphere"
sphere.location = initial_location
# Animate sphere
num_frames = 1000
movement_speed = 0.1
for frame in range(num_frames):
distance = (target_location - sphere.location).length
if distance > 0.01:
direction = movement_direction
sphere.location += direction * min(movement_speed, distance)
sphere.keyframe_insert(data_path="location", frame=frame+1)
いか 失敗
import bpy
from mathutils import Vector
# Set number of spheres
num_spheres = 11
# Set sphere radius
sphere_radius = 1
# Set target location
target_location = Vector((0, 0, 0))
# Set initial locations and movement direction for spheres
initial_locations = []
movement_directions = []
for i in range(num_spheres):
x = -30 + i*(60/(num_spheres-1))
initial_location = Vector((x, 0, 0))
movement_direction = (target_location - initial_location).normalized()
initial_locations.append(initial_location)
movement_directions.append(movement_direction)
# Create spheres and set locations
for i, initial_location in enumerate(initial_locations):
bpy.ops.mesh.primitive_uv_sphere_add(radius=sphere_radius)
sphere = bpy.context.object
sphere.name = f"Sphere_{i+1}"
sphere.location = initial_location
# Animate spheres
num_frames = 1000
for i, sphere in enumerate(bpy.data.objects):
if sphere.name.startswith("Sphere_"):
movement_speed = 0.1
for frame in range(num_frames):
distance = (target_location - sphere.location).length
if distance > 0.01:
direction = movement_directions[i]
sphere.location += direction * min(movement_speed, distance)
sphere.keyframe_insert(data_path="location", frame=frame+1)
以下は 参考
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((-60, 0, 0)), Vector((60, 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 Left ball"
else:
sphere.name = "Real Right ball"
# 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)