外心
https://manabitimes.jp/math/628
import bpy
import mathutils
import math
import random
# 大きな球体の半径と位置
big_sphere_radius = 2.0
big_sphere_location = (0, 0, 0)
# 大きな球体を作成
bpy.ops.mesh.primitive_uv_sphere_add(radius=big_sphere_radius, location=big_sphere_location, enter_editmode=False)
big_sphere = bpy.context.object
# ランダムな3点を選択し、それぞれに半径0.1の球体を作成
point_locs = []
for i in range(3):
# ランダムな点を選択
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]
point_loc = (x, y, z)
point_locs.append(point_loc)
# 選択された点を中心に半径0.1の球体を作成
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.1, location=point_loc, enter_editmode=False)
ball = bpy.context.object
# 3つの円柱を作成する
for i in range(3):
p1 = mathutils.Vector(point_locs[i])
p2 = mathutils.Vector(point_locs[(i+1)%3])
cylinder_loc = (p1+p2)/2.0
cylinder_radius = 0.05
cylinder_height = (p2-p1).length
cylinder_orientation = p2-p1
bpy.ops.mesh.primitive_cylinder_add(radius=cylinder_radius, depth=cylinder_height, location=cylinder_loc, enter_editmode=False)
cylinder = bpy.context.object
cylinder.rotation_mode = 'QUATERNION'
cylinder.rotation_quaternion = cylinder_orientation.to_track_quat('-Z', 'Y')
# 大きな球体を選択して、水色で透明度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')
import bpy
import mathutils
import math
import random
# 大きな球体の半径と位置
big_sphere_radius = 2.0
big_sphere_location = (0, 0, 0)
# 大きな球体を作成
bpy.ops.mesh.primitive_uv_sphere_add(radius=big_sphere_radius, location=big_sphere_location, enter_editmode=False)
big_sphere = bpy.context.object
# ランダムな3点を選択し、それぞれに半径0.1の球体を作成
point_locs = []
for i in range(3):
# ランダムな点を選択
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]
point_loc = (x, y, z)
point_locs.append(point_loc)
# 選択された点を中心に半径0.1の球体を作成
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.1, location=point_loc, enter_editmode=False)
ball = bpy.context.object
# 大きな球体の法線方向とランダムなベクトルを計算して、球体を回転させる
normal = ball.location - big_sphere.location
axis = mathutils.Vector((random.uniform(-1, 1), random.uniform(-1, 1), random.uniform(-1, 1))).cross(normal)
angle = normal.angle(mathutils.Vector((0, 0, 1)))
ball.rotation_euler = axis.to_track_quat('-Z', 'Y').to_euler()
ball.rotation_euler.rotate_axis('Z', angle)
# 球体を大きな球体の子オブジェクトに設定する
ball.parent = big_sphere
# 三角形を作成
tri_verts = [mathutils.Vector(p) for p in point_locs]
tri_faces = [(0, 1, 2)]
tri_mesh = bpy.data.meshes.new('triangle_mesh')
tri_obj = bpy.data.objects.new('triangle_object', tri_mesh)
bpy.context.scene.collection.objects.link(tri_obj)
tri_mesh.from_pydata(tri_verts, [], tri_faces)
この三角形の外心を中心とする
円と
その半径の2倍の円を作る
オブジェクト名も付けて
# 三角形の重心を計算して、半径3の円を作成
tri_centroid = (tri_verts[0] + tri_verts[1] + tri_verts[2]) / 3
bpy.ops.curve.primitive_bezier_circle_add(radius=3*zion_ookisa, enter_editmode=False, location=tri_centroid)
circle = bpy.context.object
# 三角形の法線方向を計算して、球体を移動させる
tri_normal = tri_obj.matrix_world.to_3x3() @ tri_mesh.polygons[0].normal
move_vec = tri_normal.normalized() * 0.05
for ball in big_sphere.children:
ball.location += move_vec
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 = 3
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}"
# ランダムに3つの球体を選び、選択状態にする
random_balls = [obj for obj in bpy.data.objects if obj.name.startswith(random_ball_name)]
selected_spheres = random.sample(random_balls, 3)
# 選択した球体をショッキングピンク色にする
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
# 選ばれた3つの球体の間に、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の色を付ける
import bpy
# アニメーションのフレーム数
frame_count = 600
# フレームごとに球体を回転させる
for i in range(frame_count):
angle = i / frame_count * 2 * bpy.app.mathutils.pi
for obj in bpy.data.objects:
if obj.type == 'MESH' and obj.name != big_sphere_name:
obj.location = obj.location.rotate(bpy.app.mathutils.Euler((0, 0, angle), 'XYZ'))
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 = 12
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つの球体を選び、赤色に変更
selected_spheres = sorted(random.sample(range(random_ball_count), 2))
for index, sphere in enumerate(bpy.data.objects):
if sphere.name.startswith(random_ball_name):
if index in selected_spheres:
sphere.color = (1, 0, 0, 1)
sphere.name = f"{segment_ball_name}{index}"
else:
sphere.color = (0, 0, 1, 0.5)
# 選ばれた2つの球体の間に、9つの小さな球体を作成
x1 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[0]}"].location[0]
y1 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[0]}"].location[1]
z1 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[0]}"].location[2]
x2 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[1]}"].location[0]
y2 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[1]}"].location[1]
z2 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[1]}"].location[2]
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}"
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 = 12
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つの球体を選び、赤色に変更
selected_spheres = random.sample(range(random_ball_count), 2)
for index, sphere in enumerate(bpy.data.objects):
if sphere.name.startswith(random_ball_name):
if index in selected_spheres:
sphere.color = (1, 0, 0, 1)
sphere.name = f"{segment_ball_name}{index}"
else:
sphere.hide_render = True
# 選ばれた2つの球体の間に、9つの小さな球体を作成
for i in range(segment_ball_count):
x1 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[0]}"].location[0]
y1 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[0]}"].location[1]
z1 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[0]}"].location[2]
x2 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[1]}"].location[0]
y2 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[1]}"].location[1]
z2 = bpy.data.objects[f"{segment_ball_name}{selected_spheres[1]}"].location[2]
x = x1 + (x2 - x1) / (segment_ball_count + 1) * (i + 1)
y = y1 + (y2 - y1) / (segment_ball_count + 1) * (i + 1)
z = z1 + (z2 - z1) / (segment_ball_count + 1) * (i + 1)
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}"
import math
import random
import bpy
zion_bigspere_radius = 3.0
zion_bigsphere_location = (0, 0, 0)
zion_bigsphere_name = "大球体"
zion_random_ball = 12
zion_random_ball_radius = 0.1
zion_senbun_ball_name = "線分9小球"
bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_radius, location= zion_center_sphere, enter_editmode=False)
for i in range(zion_random_ball):
theta = random.uniform(0, 2 * math.pi)
phi = random.uniform(0, math.pi)
x = zion_radius * math.sin(phi) * math.cos(theta) + zion_center_sphere[0]
y = zion_radius * math.sin(phi) * math.sin(theta) + zion_center_sphere[1]
z = zion_radius * math.cos(phi) + zion_center_sphere[2]
loc = (x, y, z)
bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_random_ball_radius, location=loc, enter_editmode=False)
selected_spheres = random.sample(range(zion_random_ball), 2)
for index, sphere in enumerate(bpy.data.objects):
if index in selected_spheres:
sphere.color = (1, 0, 0, 1)
sphere.name = f"{zion_big_sphere}{index}"
else:
sphere.hide_render = True
for i in range(9):
x1 = bpy.data.objects[f"{zion_big_sphere}{selected_spheres[0]}"].location[0]
y1 = bpy.data.objects[f"{zion_big_sphere}{selected_spheres[0]}"].location[1]
z1 = bpy.data.objects[f"{zion_big_sphere}{selected_spheres[0]}"].location[2]
x2 = bpy.data.objects[f"{zion_big_sphere}{selected_spheres[1]}"].location[0]
y2 = bpy.data.objects[f"{zion_big_sphere}{selected_spheres[1]}"].location[1]
z2 = bpy.data.objects[f"{zion_big_sphere}{selected_spheres[1]}"].location[2]
x = x1 + (x2 - x1) / 9 * (i + 1)
y = y1 + (y2 - y1) / 9 * (i + 1)
z = z1 + (z2 - z1) / 9 * (i + 1)
loc = (x, y, z)
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.05, location=loc, enter_editmode=False)
bpy.context.object.name = f"{zion_senbun}{i}"
(radius=0.05 のオブジェクト名を 先頭に zion_9senbun_name = "9小球体”
半径3の球体を zion_big_spere_name = "9小球体”
import math
import random
import bpy
zion_big_sphere_radius = 3.0
zion_big_sphere_location = (0, 0, 0)
zion_middle_sphere_name = "表面 球体"
zion_random_ball = 12
zion_random_ball_radius = 0.1
zion_segment_ball_name = "線分9小球"
# 大きな球体を作成
bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_big_sphere_radius, location=zion_big_sphere_location, enter_editmode=False)
# ランダムな小さな球体を作成
for i in range(zion_random_ball):
theta = random.uniform(0, 2 * math.pi)
phi = random.uniform(0, math.pi)
x = zion_big_sphere_radius * math.sin(phi) * math.cos(theta) + zion_big_sphere_location[0]
y = zion_big_sphere_radius * math.sin(phi) * math.sin(theta) + zion_big_sphere_location[1]
z = zion_big_sphere_radius * math.cos(phi) + zion_big_sphere_location[2]
loc = (x, y, z)
bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_random_ball_radius, location=loc, enter_editmode=False)
# ランダムに2つの球体を選び、赤色に変更
selected_spheres = random.sample(range(zion_random_ball), 2)
for index, sphere in enumerate(bpy.data.objects):
if index in selected_spheres:
sphere.color = (1, 0, 0, 1)
sphere.name = f"{zion_segment_ball_name}{index}"
else:
sphere.hide_render = True
# 選ばれた2つの球体の間に、9つの小さな球体を作成
for i in range(9):
x1 = bpy.data.objects[f"{zion_segment_ball_name}{selected_spheres[0]}"].location[0]
y1 = bpy.data.objects[f"{zion_segment_ball_name}{selected_spheres[0]}"].location[1]
z1 = bpy.data.objects[f"{zion_segment_ball_name}{selected_spheres[0]}"].location[2]
x2 = bpy.data.objects[f"{zion_segment_ball_name}{selected_spheres[1]}"].location[0]
y2 = bpy.data.objects[f"{zion_segment_ball_name}{selected_spheres[1]}"].location[1]
z2 = bpy.data.objects[f"{zion_segment_ball_name}{selected_spheres[1]}"].location[2]
x = x1 + (x2 - x1) / 9 * (i + 1)
y = y1 + (y2 - y1) / 9 * (i + 1)
z = z1 + (z2 - z1) / 9 * (i + 1)
loc = (x, y, z)
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.05, location=loc, enter_editmode=False)
bpy.context.object.name = f"{zion_segment_ball_name}{i}"
import bpy
from math import radians
# 定義:数値調整
zion_radius = 3.0
zion_center_sphere = (0, 0, 0)
zion_kaiten_frame = 600
zion_name = "表面回転球体"
# 球体を作成
bpy.ops.mesh.primitive_uv_sphere_add(radius=zion_radius, location=zion_center_sphere)
# オブジェクト名を変更
bpy.context.object.name = zion_name
# 回転アニメーションを設定
bpy.context.scene.frame_end = zion_kaiten_frame
rotation_degrees = 360.0 / zion_kaiten_frame
for i in range(zion_kaiten_frame):
bpy.context.scene.frame_set(i)
bpy.context.object.rotation_euler[2] = radians(rotation_degrees * i)
bpy.ops.anim.keyframe_insert(type='Rotation')