2023年4月8日土曜日

幻想 Z軸 球体 20230409 sun カメラ追跡 付き


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 * distance / chousei_kijyun if distance > 0 else 0 for distance in 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)







import bpy

# フレーム数を変更するための関数
def set_frame_range(start_frame, end_frame):
    bpy.context.scene.frame_start = start_frame
    bpy.context.scene.frame_end = end_frame

# メイン関数
def main():
    # フレームレンジを設定する
    set_frame_range(1, 640)

# スクリプトを実行する
if __name__ == "__main__":
    main()





以下は 失敗した


はい、カメラの追跡の点線を消すことができます。点線を非表示にするには、下記のスクリプトを実行してください。

css
bpy.context.scene.objects['Camera'].show_axis = False

import bpy
bpy.context.scene.objects['Camera'].show_axis = False


また、カメラの追跡をon/offに切り替えるには、下記のスクリプトを実行してください。

css
bpy.context.scene.objects['Camera'].constraints['Track To'].mute = True # カメラの追跡をoffにする bpy.context.scene.objects['Camera'].constraints['Track To'].mute = False # カメラの追跡をonにする

bpy.context.scene.objects['Camera'].constraints['Track To'].mute = True  # カメラの追跡をoffにする
bpy.context.scene.objects['Camera'].constraints['Track To'].mute = False  # カメラの追跡をonにする



ただし、これらのスクリプトを実行するには、カメラがすでに作成されている必要があります。カメラがない場合は、以下のスクリプトを使用してカメラを作成することができます。

csharp
bpy.ops.object.camera_add(location=(0, 0, 50)) # 原点にカメラを作成する bpy.context.scene.camera = bpy.context.object # カメラをアクティブカメラにする



このスクリプトでは、原点のカメラを作成していないため、カメラが存在しない状態になっています。そのため、球体を追跡しているわけではありません。

もし、カメラを作成して追跡するようにしたい場合は、以下のようにスクリプトを修正することができます。


import bpy from mathutils import Vector import math # 目標位置を指定する target_location = Vector((-30, 0, 0)) # フレーム数を指定する last_frame = 600 # 球体の数を指定する num_spheres = 5 # 移動開始位置から目標位置までの距離を計算する start_location = Vector((0, 0, target_location[2])) zion_distance = (start_location - target_location).length # 速度を計算する speed = zion_distance / (last_frame / num_spheres) # スフィアの作成 for i in range(num_spheres): center = Vector((0, 0, target_location[2] + i * (target_location[2] - start_location[2]) / (num_spheres - 1))) bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, location=center) # アニメーションの作成 for frame in range(last_frame + 1): for index, obj in enumerate(bpy.data.objects): if obj.name.startswith("Sphere"): distance = (target_location - obj.location).length if distance > 0.01: # 目標位置にまだ到達していない場合 direction = (target_location - obj.location).normalized() obj.location += direction * speed obj.keyframe_insert(data_path="location", frame=frame) # カメラの位置を設定する camera_location = Vector((0, 0, 50)) bpy.context.scene.camera.location = camera_location bpy.context.scene.camera.rotation_euler = Vector((0, 0, 0)) bpy.context.scene.camera.keyframe_insert(data_path="location", frame=frame) bpy.context.scene.camera.keyframe_insert(data_path="rotation_euler", frame=frame)





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

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