三角形
球体中心 三角形 重心
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)
# 三角形の法線方向を計算して、球体を移動させる
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
# 三角形の重心を計算する
tri_center = mathutils.Vector((0, 0, 0))
for p in point_locs:
tri_center += mathutils.Vector(p)
tri_center /= 3
# 三角形の重心を中心に半径3の球体を作成
bpy.ops.mesh.primitive_uv_sphere_add(radius=3, location=tri_center, enter_editmode=False)
三角形と 円 バラバラ 角度タイプ
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)
# 三角形の重心を計算して、半径3の円を作成
tri_centroid = (tri_verts[0] + tri_verts[1] + tri_verts[2]) / 3
bpy.ops.curve.primitive_bezier_circle_add(radius=3, 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 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)
# 三角形の法線方向を計算して、球体を移動させる
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