2023年4月6日木曜日

 




import bpy

import math

import mathutils

from mathutils import Vector


# 目標位置を指定する

target_location = Vector((30, 30, 30))

zion_start = Vector((0, 0, 0))


# フレーム数を指定する

last_frame = 300


# 移動する距離を指定する

distance_per_frame = 0.1


# オブジェクト名を指定する

zion_object_name = "√3移動 球体"


# オブジェクトを作成する関数

def create_object(start, end, radius, name):

    # 座標変換を作成

    direction = end - start

    length = direction.length

    direction.normalize()

    mat_loc = mathutils.Matrix.Translation(start)


    # CylinderMeshに頂点、辺、面を追加

    mesh = bpy.data.meshes.new(name + "_Mesh")

    obj = bpy.data.objects.new(name, mesh)


    n_segments = 32  # 円周の分割数

    angle_per_segment = 2 * math.pi / n_segments


    verts = []

    for i in range(n_segments):

        angle = i * angle_per_segment

        x = radius * math.cos(angle)

        y = radius * math.sin(angle)

        verts.append((x, y, 0))

        verts.append((x, y, length))


    edges = []

    faces = []

    for i in range(n_segments):

        j = (i + 1) % n_segments

        edges.append((i, i + n_segments))

        faces.append((i, j, j + n_segments, i + n_segments))


    # CylinderMeshに頂点、辺、面をセット

    mesh.from_pydata(verts, edges, faces)

    mesh.update()


    # オブジェクトの座標変換を適用

    z_axis = mathutils.Vector((0, 0, 1))

    rotation = z_axis.rotation_difference(direction).to_matrix()

    mat_rot = rotation.to_4x4()

    mat_scale = mathutils.Matrix.Scale(1, 4, (start - end).normalized())

    obj.matrix_world = mat_loc @ mat_rot @ mat_scale


    return obj


# オブジェクトを作成

cylinder = create_object(zion_start, target_location, 1, zion_object_name)


# オブジェクトをシーンに追加

scene = bpy.context.scene

scene.collection.objects.link(cylinder)


# ビューポートを更新

bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)


# フレームを変更しながらオブジェクトを移動する

for frame in range(last_frame):

    location = cylinder.location

    direction = target_location - location

    if direction.length > distance_per_frame:

        location += direction.normalized() * distance_per_frame

        cylinder.location = location


    # フレームを進める

    scene.frame_set(frame)


    # ビューポートを更新する

    bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)








# 目標位置を指定する

target_location = Vector((30, 30, 30))


# フレーム数を指定する

last_frame = 300


# 移動する距離を指定する

distance_per_frame = 0.1




改良中 y=-30 中心 円周への球体36個

できた y= -30 中心で z=0平面移動  import bpy import math zion_collection_name = "線路レール 観察者" # コレクションを作成する col = bpy.data.collections.new(zio...