import bpy
from mathutils import Vector

# 設定可能な変数
zion_object_name = "逆さ円錐 600上昇"  # 円錐オブジェクトの名前
radius = 60  # 円錐の半径
height = 60  # 円錐の高さ
distance_per_frame = 0.1  # 円錐の移動距離
animation_frames = 600  # アニメーションの再生時間(フレーム数)
wait_frames = 40  # アニメーション再生完了後の待機時間(フレーム数)

# 円錐を作成する
def create_cone():
    bpy.ops.mesh.primitive_cone_add(radius1=0, radius2=radius, depth=height)
    cone = bpy.context.object
    cone.name = zion_object_name
    cone.location = Vector((0, 0, -height/2))  # 初期位置を設定する
    cone.active_material = bpy.data.materials.new(name="zion_material")
    cone.active_material.use_nodes = True
    node_tree = cone.active_material.node_tree
    nodes = node_tree.nodes
    links = node_tree.links
    diffuse = nodes["Principled BSDF"]
    diffuse.inputs['Base Color'].default_value = (0.2, 0.8, 0.5, 0.3)
    return cone

cone = create_cone()

# アニメーションを再生するたびに呼び出される関数
def animate_cone(scene):
    global cone

    # 現在のフレーム数から円錐の位置を計算する
    z = scene.frame_current * distance_per_frame
    if z <= height:
        cone.location = Vector((0, 0, -height/2 + z))
    else:
        cone.location = Vector((0, 0, height/2))

    # アニメーション再生完了後は一定フレーム数待機する
    if scene.frame_current >= animation_frames:
        if (scene.frame_current - animation_frames) < wait_frames:
            return
        else:
            # 現在の円錐を削除して、新しい円錐を作成する
            bpy.data.objects.remove(cone, do_unlink=True)
            cone = create_cone()
            # フレーム数を初期化する
            scene.frame_set(0)
            return

# フレーム更新のコールバック関数を登録する
bpy.app.handlers.frame_change_pre.append(animate_cone)

# アニメーションの再生時間を設定する
bpy.context.scene.frame_end = animation_frames + wait_frames