2023年4月25日火曜日

20230426 円柱 三角形 ランダム 球体表面










外心

https://manabitimes.jp/math/628



20230426ccc 球体表面に 12個の球体













import math
import random
import bpy

big_sphere_radius = 3.0
big_sphere_location = (0, 0, 0)
big_sphere_name = "Big Sphere"

random_ball_count = 2
random_ball_radius = 0.1
random_ball_name = "random_ball"

segment_ball_count = 9
segment_ball_radius = 0.05
segment_ball_name = "senbun_ball"

# 大きな球体を作成
bpy.ops.mesh.primitive_uv_sphere_add(radius=big_sphere_radius, location=big_sphere_location, enter_editmode=False)
bpy.context.object.name = big_sphere_name

# ランダムな小さな球体を作成
for i in range(random_ball_count):
    theta = random.uniform(0, 2 * math.pi)
    phi = random.uniform(0, math.pi)
    x = big_sphere_radius * math.sin(phi) * math.cos(theta) + big_sphere_location[0]
    y = big_sphere_radius * math.sin(phi) * math.sin(theta) + big_sphere_location[1]
    z = big_sphere_radius * math.cos(phi) + big_sphere_location[2]
    loc = (x, y, z)
    bpy.ops.mesh.primitive_uv_sphere_add(radius=random_ball_radius, location=loc, enter_editmode=False)
    bpy.context.object.name = f"{random_ball_name}{i}"

# ランダムに2つの球体を選び、選択状態にする
random_balls = [obj for obj in bpy.data.objects if obj.name.startswith(random_ball_name)]
selected_spheres = random.sample(random_balls, 2)

# 選択した球体をショッキングピンク色にする
pink_material = bpy.data.materials.new(name="Pink")
pink_material.diffuse_color = (1, 0, 1, 1)
for obj in selected_spheres:
    obj.active_material = pink_material

# 選ばれた2つの球体の間に、9つの小さな球体を作成
x1, y1, z1 = selected_spheres[0].location
x2, y2, z2 = selected_spheres[1].location

for i in range(segment_ball_count):
    t = (i + 1) / (segment_ball_count + 1)
    x = (1 - t) * x1 + t * x2
    y = (1 - t) * y1 + t * y2
    z = (1 - t) * z1 + t * z2
    loc = (x, y, z)
    bpy.ops.mesh.primitive_uv_sphere_add(radius=segment_ball_radius, location=loc, enter_editmode=False)
    bpy.context.object.name = f"{segment_ball_name}{i}"
    bpy.context.object.active_material = bpy.data.materials.new(f"Material_{i}")
    bpy.context.object.active_material.diffuse_color = (1, 0, 0, 1)  # 赤色

# 球体の選択を解除
bpy.ops.object.select_all(action='DESELECT')

# 大きな球体を選択して、水色で透明度0.1の色を付ける
big_sphere = bpy.data.objects[big_sphere_name]
big_sphere.select_set(True)
blue_material = bpy.data.materials.new(name="Blue")
blue_material.diffuse_color = (0, 0.5, 1, 0.05)
for obj in bpy.data.objects:
    if obj.type == 'MESH' and obj.name == big_sphere_name:
        obj.active_material = blue_material

# 球体の選択を解除
bpy.ops.object.select_all(action='DESELECT')





20230426bbb 球体表面に 36個の球体 

 







import bpy

import math


# 大きい球体の半径

zion_big_sphere_radius = 6.0


# 小さい球体の半径

zion_small_sphere_radius = 0.1


# 大きい球体の回転数

frame_count = 600


# 小さい球体の数

zion_small_sphere_count = 360


# 小さい球体の名前

zion_small_sphere_name = "SmallSphere"


# 大きい球体の回転方向(時計回り)

zion_big_sphere_rotation_direction = -1


# 大きい球体を作成

bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_big_sphere_radius, location=(0, 0, 0))

zion_big_sphere = bpy.context.object

zion_big_sphere.name = "BigSphere"


# 小さい球体を作成

for i in range(zion_small_sphere_count):

    theta = i * math.pi / 180

    phi = i * math.pi / 180

    x = zion_big_sphere_radius * math.sin(theta) * math.cos(phi)

    y = zion_big_sphere_radius * math.sin(theta) * math.sin(phi)

    z = zion_big_sphere_radius * math.cos(theta) 


    bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_small_sphere_radius, location=(x, y, z))

    small_sphere = bpy.context.object

    small_sphere.name = zion_small_sphere_name + "." + str(i)


# アニメーションのキーフレームを生成する

for i in range(frame_count):

    # 大きい球体を回転させる

    zion_big_sphere = bpy.data.objects.get("BigSphere")

    if zion_big_sphere:

        zion_big_sphere.rotation_euler[2] += zion_big_sphere_rotation_direction * 2 * math.pi / frame_count

        zion_big_sphere.keyframe_insert(data_path="rotation_euler", frame=i)


        # 小さい球体も同じ回転を加える

        for j in range(zion_small_sphere_count):

            small_sphere_name = zion_small_sphere_name + "." + str(j)

            small_sphere = bpy.data.objects.get(small_sphere_name)

            if small_sphere:

                small_sphere.rotation_euler[2] += zion_big_sphere_rotation_direction * 2 * math.pi / frame_count

                small_sphere.keyframe_insert(data_path="rotation_euler", frame=i)


                # 小さい球体の回転速度を大きい球体に合わせる

                small_sphere.rotation_euler[2] -= j * 2 * math.pi / zion_small_sphere_count

                small_sphere.keyframe_insert(data_path="rotation_euler", frame=i)



20230426aaa 球体表面に 36個の球体  





# ランダム配置 球体表面に36個 球体



import bpy

import math

import random


# 大きい球体の情報

zion_big_sphere_name = "Center Sphere"  # 大きい球体の名前

zion_big_sphere_radius = 2.0  # 大きい球体の半径

zion_big_sphere_location = (0, 0, 0)  # 大きい球体の位置

zion_big_sphere_rotation_speed = 600  # 1回転するのに要するフレーム数

zion_big_sphere_rotation_direction = 1  # 回転方向(1: 反時計回り、-1: 時計回り)


# 小さい球体の情報

zion_small_sphere_name = "Small Sphere"  # 小さい球体の名前

zion_small_sphere_count = 36  # 小さい球体の数

zion_small_sphere_radius = 0.1  # 小さい球体の半径


# 大きい球体を作成

bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_big_sphere_radius, location=zion_big_sphere_location)


# 大きい球体を変数に格納

zion_big_sphere = bpy.context.object

zion_big_sphere.name = zion_big_sphere_name


# 小さい球体を作成

for i in range(zion_small_sphere_count):

    # 球面座標を計算

    theta = random.uniform(0, 2 * math.pi)

    phi = random.uniform(0, math.pi)

    x = zion_big_sphere_radius * math.sin(phi) * math.cos(theta)

    y = zion_big_sphere_radius * math.sin(phi) * math.sin(theta)

    z = zion_big_sphere_radius * math.cos(phi)

    location = (x, y, z)


    # 小さい球体を作成

    bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_small_sphere_radius, location=location)


    # 小さい球体を大きい球体の子オブジェクトにする

    zion_small_sphere = bpy.context.object

    zion_small_sphere.name = zion_small_sphere_name + "." + str(i)

    zion_small_sphere.parent = zion_big_sphere


# アニメーションを作成

frame_count = zion_big_sphere_rotation_speed

for i in range(frame_count):

    # 大きい球体を回転させる

    zion_big_sphere.rotation_euler.rotate_axis("Z", zion_big_sphere_rotation_direction * 2 * math.pi / frame_count)


    # 小さい球体を大きい球体の表面に沿って移動させる

    for j in range(zion_small_sphere_count):

        small_sphere_name = zion_small_sphere_name + "." + str(j)

        small_sphere = bpy.data.objects[small_sphere_name]

        sphere_phi = math.acos(small_sphere.location[2] / zion_big_sphere_radius)

        sphere_theta = math.atan2(small_sphere.location[1], small_sphere.location[0]) + zion