import bpy
import math
from mathutils import Vector
from math import radians
zion_collection_name = "cylinder "
# Create the collection
col = bpy.data.collections.new(zion_collection_name)
bpy.context.scene.collection.children.link(col)
# Set target location
target_location = Vector((0, 0, 60))
# Set initial locations
initial_locations = [ Vector((0, 0, 0))]
# Set cylinder properties
cylinder_radius = 1
cylinder_depth = 120
# Calculate speeds based on initial distances
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 cylinder objects and set locations
for i, initial_location in enumerate(initial_locations):
bpy.ops.mesh.primitive_cylinder_add(radius=cylinder_radius, depth=cylinder_depth)
cylinder = bpy.context.object
cylinder.location = initial_location
cylinder.name = "線路 rail"
# Rotate cylinder by 90 degrees around the y-axis
cylinder.rotation_euler.rotate_axis('Y', radians(90))
# Set animation keyframes
last_frame = 600
for frame in range(last_frame+1):
distance = (target_location - cylinder.location).length
if distance > 0.01:
direction = (target_location - cylinder.location).normalized()
cylinder.location += direction * min(speeds[i], distance)
cylinder.keyframe_insert(data_path="location", frame=frame)
# 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)