2023年7月7日金曜日

20230708 sat 立方体 球体 表面 内部 ランダム分布

 


# 新しいBlenderファイルを作成
bpy.ops.wm.read_factory_settings(use_empty=True)
bpy.ops.object.select_all(action='DESELECT')

# オブジェクトを削除
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()

# カメラの設定
bpy.ops.object.camera_add(location=(0, -5, 2))
bpy.context.scene.camera = bpy.context.object

# ライトの設定
bpy.ops.object.light_add(type='SUN', location=(0, 0, 5))

# レンダリング設定
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.device = 'GPU'

# レンダリング実行
bpy.ops.render.render(write_still=True)






blender python

半径 0.01 の球体 100個で

1辺2の正方形内に ランダムに配置



import bpy

import random

from mathutils import Vector


# Clear existing objects

bpy.ops.object.select_all(action='DESELECT')

bpy.ops.object.select_by_type(type='MESH')

bpy.ops.object.delete()


# Create spheres

for _ in range(1000):

    x = random.uniform(-1, 1)

    y = random.uniform(-1, 1)

    z = random.uniform(-1, 1)

    location = Vector((x, y, z)).normalized() * 0.99

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



Certainly! Here's the continuation:

After executing the code, you should see the 1000 spheres created in the Blender viewport. Each sphere will have a radius of 0.01 and be randomly positioned within a unit sphere of radius 0.99.

You can adjust the parameters in the code to customize the behavior. For example, you can change the number of spheres by modifying the range in the for loop (for _ in range(1000):), or you can change the radius of the spheres by modifying the radius parameter in the bpy.ops.mesh.primitive_uv_sphere_add() function.

If you want to further manipulate or modify the spheres after creating them, you can access them through the Blender API. For example, you can assign them to variables or perform operations on them using the bpy.data.objects collection.

Feel free to experiment with the code and adapt it to your specific needs. Let me know if there's anything else I can help you with!




# 球内部空間に配置

import bpy
import random
from mathutils import Vector

# 新しいBlenderファイルを作成
bpy.ops.wm.read_factory_settings(use_empty=True)
bpy.ops.object.select_all(action='DESELECT')

# オブジェクトを削除
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()

# 1000個の球体を作成
for _ in range(100):
    x = random.uniform(-1, 1)
    y = random.uniform(-1, 1)
    z = random.uniform(-1, 1)
    location = Vector((x, y, z)).normalized() * random.uniform(0.05, 0.99)
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.05, location=location)

# カメラの設定
bpy.ops.object.camera_add(location=(0, -5, 2))
bpy.context.scene.camera = bpy.context.object

# ライトの設定
bpy.ops.object.light_add(type='SUN', location=(0, 0, 5))

# レンダリング設定
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.device = 'GPU'

# レンダリング実行
bpy.ops.render.render(write_still=True)


このスクリプトでは、球体の内部空間にランダムに配置するために、位置の計算部分を変更しました。locationの計算では、ランダムな位置のベクトルを正規化し、0.05から0.99の範囲で乗算することで、内部空間に球体を配置しています。



コレクションを作る

名前は

”立方体 内部 blue1000"
”立方体 内部 red100"
”立方体 表面 blue1000"
”立方体 表面 red100"
”球体 内部 blue1000"
”球体 内部 red100"
”球体 表面 blue1000"
”球体 表面 red100"

import bpy

# 立方体内部の青いオブジェクト用コレクションの作成
cube_inner_blue_collection = bpy.data.collections.new("立方体 内部 blue1000")
bpy.context.scene.collection.children.link(cube_inner_blue_collection)

# 立方体内部の赤いオブジェクト用コレクションの作成
cube_inner_red_collection = bpy.data.collections.new("立方体 内部 red100")
bpy.context.scene.collection.children.link(cube_inner_red_collection)

# 立方体表面の青いオブジェクト用コレクションの作成
cube_surface_blue_collection = bpy.data.collections.new("立方体 表面 blue1000")
bpy.context.scene.collection.children.link(cube_surface_blue_collection)

# 立方体表面の赤いオブジェクト用コレクションの作成
cube_surface_red_collection = bpy.data.collections.new("立方体 表面 red100")
bpy.context.scene.collection.children.link(cube_surface_red_collection)

# 球体内部の青いオブジェクト用コレクションの作成
sphere_inner_blue_collection = bpy.data.collections.new("球体 内部 blue1000")
bpy.context.scene.collection.children.link(sphere_inner_blue_collection)

# 球体内部の赤いオブジェクト用コレクションの作成
sphere_inner_red_collection = bpy.data.collections.new("球体 内部 red100")
bpy.context.scene.collection.children.link(sphere_inner_red_collection)

# 球体表面の青いオブジェクト用コレクションの作成
sphere_surface_blue_collection = bpy.data.collections.new("球体 表面 blue1000")
bpy.context.scene.collection.children.link(sphere_surface_blue_collection)

# 球体表面の赤いオブジェクト用コレクションの作成
sphere_surface_red_collection = bpy.data.collections.new("球体 表面 red100")
bpy.context.scene.collection.children.link(sphere_surface_red_collection)







# 立方体 内部空間に配置

import bpy
import random

# 立方体のサイズ
cube_size = 2.0

# オブジェクトの数
object_count = 1000

# オブジェクトをランダムに配置
for _ in range(object_count):
    # ランダムな位置を生成
    x = random.uniform(-cube_size/2, cube_size/2)
    y = random.uniform(-cube_size/2, cube_size/2)
    z = random.uniform(-cube_size/2, cube_size/2)

    # 立方体内にオブジェクトを配置
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.05, location=(x, y, z))



# 立方体 表面に配置

import bpy
import random


# 立方体のサイズ
cube_size = 2.0

# オブジェクトの数
object_count = 100

# 立方体の表面にオブジェクトをランダムに配置
for _ in range(object_count):
    # ランダムな面を選択
    face_index = random.randint(0, 5)
    
    # 面の中心の位置を計算
    if face_index == 0:  # 前面
        x = random.uniform(-cube_size / 2, cube_size / 2)
        y = random.uniform(-cube_size / 2, cube_size / 2)
        z = cube_size / 2
    elif face_index == 1:  # 後面
        x = random.uniform(-cube_size / 2, cube_size / 2)
        y = random.uniform(-cube_size / 2, cube_size / 2)
        z = -cube_size / 2
    elif face_index == 2:  # 上面
        x = random.uniform(-cube_size / 2, cube_size / 2)
        y = cube_size / 2
        z = random.uniform(-cube_size / 2, cube_size / 2)
    elif face_index == 3:  # 底面
        x = random.uniform(-cube_size / 2, cube_size / 2)
        y = -cube_size / 2
        z = random.uniform(-cube_size / 2, cube_size / 2)
    elif face_index == 4:  # 左側面
        x = -cube_size / 2
        y = random.uniform(-cube_size / 2, cube_size / 2)
        z = random.uniform(-cube_size / 2, cube_size / 2)
    elif face_index == 5:  # 右側面
        x = cube_size / 2
        y = random.uniform(-cube_size / 2, cube_size / 2)
        z = random.uniform(-cube_size / 2, cube_size / 2)

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




# 球体 表面に 配置

import bpy
import random
from math import pi, cos, sin


# 球体の半径とオブジェクトの数
sphere_radius = 1.0
object_count = 100

# オブジェクトを球体表面にランダムに配置
for _ in range(object_count):
    # 球面座標系でのランダムな角度を生成
    theta = random.uniform(0, 2 * pi)
    phi = random.uniform(0, pi)

    # 球面座標からデカルト座標への変換
    x = sphere_radius * sin(phi) * cos(theta)
    y = sphere_radius * sin(phi) * sin(theta)
    z = sphere_radius * cos(phi)

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




このスクリプトでは、球体の半径とオブジェクトの数を指定し、球面座標系でランダムな角度を生成してからデカルト座標に変換します。それにより、球体表面上にランダムにオブジェクトを配置します。




# 半径1の球体 内部空間に分布

import bpy
import random
from mathutils import Vector

# 球体の半径とオブジェクトの数
sphere_radius = 1.0
object_count = 100

# オブジェクトを球体内部にランダムに配置
for _ in range(object_count):
    # ランダムな3Dベクトルを生成
    random_vector = Vector((random.uniform(-1, 1), random.uniform(-1, 1), random.uniform(-1, 1)))
    
    # ランダムな3Dベクトルを正規化して球体内に配置
    location = random_vector.normalized() * random.uniform(0, sphere_radius)
    
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.05, location=location)



aaaa

a

ああああああああああああああああああああああ





blender pytohn で


0,0,0

1,1,0を

対角線とする正方形をつくる


This updated code will create a square with vertices at (1, 1, 0), (1, -1, 0), (-1, -1, 0), and (-1, 1, 0).


import bpy

from mathutils import Matrix


# Create the vertices of the square

vertices = [

    (1, 1, 0),

    (1, -1, 0),

    (-1, -1, 0),

    (-1, 1, 0)

]


# Create the edges of the square

edges = [

    (0, 1),

    (1, 2),

    (2, 3),

    (3, 0)

]


# Create a new mesh and object

mesh = bpy.data.meshes.new("SquareMesh")

obj = bpy.data.objects.new("Square", mesh)


# Link the object to the scene

scene = bpy.context.scene

scene.collection.objects.link(obj)


# Create the mesh data

mesh.from_pydata(vertices, edges, [])


# Set the diagonal length to 1

diagonal_length = 1

scale_factor = diagonal_length / (2 ** 0.5)

matrix = Matrix.Scale(scale_factor, 3, (0, 0, 0))

obj.matrix_world = matrix


# Select the object and enter edit mode

bpy.context.view_layer.objects.active = obj

bpy.ops.object.mode_set(mode="EDIT")

bpy.ops.mesh.select_all(action="SELECT")


# Scale the object to set the diagonal length to 1

bpy.ops.transform.resize(value=(diagonal_length, diagonal_length, 1))


# Exit edit mode

bpy.ops.object.mode_set(mode="OBJECT")




マテリアルにして

表と裏の色を


表 濃い緑

裏 薄い緑


オブジェクトに 名前を使える ”正方形2x2x2"




2023年7月5日水曜日

20230706 円柱線路 追加

 a

















コレクション名 ”線路”

だけを 作る



import bpy


# Collection name

collection_name = "線路"


# Create the collection if it doesn't exist

if collection_name not in bpy.data.collections:

    collection = bpy.data.collections.new(collection_name)

    bpy.context.scene.collection.children.link(collection)

else:

    collection = bpy.data.collections[collection_name]









blender python で 円柱を作る


円柱の中心 y=0

円柱の中心軸が x軸に平行

円柱の半径 0.05

円柱の長さ 20


名前を付ける "線路レール_y”

y は 円柱の中心の y成分 数値


y軸を 回転軸に 90度回転させる




import bpy


# パラメータ

center_y = 0.0  # 円柱の中心のy座標

radius = 0.05  # 円柱の半径

length = 20.0  # 円柱の長さ

name = "線路レール_y"  # オブジェクト名


# 円柱を作成

bpy.ops.mesh.primitive_cylinder_add(

    radius=radius,

    depth=length,

    location=(0, center_y, 0),

    rotation=(0, 0, 0)

)


# 作成したオブジェクトの名前を変更

obj = bpy.context.active_object

obj.name = name


# y軸を中心に90度回転

bpy.ops.object.select_all(action='DESELECT')

obj.select_set(True)

bpy.context.view_layer.objects.active = obj

bpy.ops.transform.rotate(value=1.5708, orient_axis='Y')


# オブジェクトのマテリアルを作成

material = bpy.data.materials.new(name="Cylinder Material")

material.diffuse_color = (0.8, 0.2, 0.2, 1.0)  # RGB色を設定


# オブジェクトにマテリアルを割り当て

obj.data.materials.append(material)


# 作成したオブジェクトを選択解除

obj.select_set(False)








円柱を4つ 作って


円柱の中心 y=0 で作る

円柱の中心 y=2ー√3 で作る

円柱の中心 y=√3 で作る

円柱の中心 y=ー√3 で作る

すべて

y軸を回転軸に 90度回転


import bpy

import math


# パラメータ

radius = 0.05  # 円柱の半径

length = 20.0  # 円柱の長さ

names = ["Cylinder1", "Cylinder2", "Cylinder3", "Cylinder4"]  # オブジェクト名


center_positions = [0.0, 2 - math.sqrt(3), math.sqrt(3), -math.sqrt(3)]  # 中心位置のy座標


for i, center_y in enumerate(center_positions):

    # 円柱を作成

    bpy.ops.mesh.primitive_cylinder_add(

        radius=radius,

        depth=length,

        location=(0, center_y, 0),

        rotation=(0, 0, 0)

    )


    # 作成したオブジェクトの名前を変更

    obj = bpy.context.active_object

    obj.name = names[i]


    # y軸を中心に90度回転

    bpy.ops.object.select_all(action='DESELECT')

    obj.select_set(True)

    bpy.context.view_layer.objects.active = obj

    bpy.ops.transform.rotate(value=1.5708, orient_axis='Y')


    # オブジェクトのマテリアルを作成

    material = bpy.data.materials.new(name="Cylinder Material")

    material.diffuse_color = (0.8, 0.2, 0.2, 1.0)  # RGB色を設定


    # オブジェクトにマテリアルを割り当て

    obj.data.materials.append(material)


    # 作成したオブジェクトを選択解除

    obj.select_set(False)







ああああああああああああああ



blender python で

トーラスを複数 作る

連番で 名前を付ける


名前の付け方は 

トーラスの中心位置 x,y,zなら ”torus_x,y,z"

x,y,zは 小数点1桁まで 使う

 

トーラスのマイナー半径  0.02


トーラスの中心 

0,-√3,0

0,-2√3,0

0,-10,0



指定した

トーラス中心から

-1,0,0

までの距離を求めて

radius_zionad と定義



トーラスの半径を

2*radius_zionad 


トーラスを描く



import bpy

import math


# パラメータ

minor_radius = 0.02  # トーラスのマイナー半径


center_positions = [(0, -math.sqrt(3), 0),

                    (0, -2 * math.sqrt(3), 0),

                    (0, -10, 0)]  # トーラスの中心位置


target_position = (-1, 0, 0)  # 目標位置

radius_zionad = math.sqrt((target_position[0] - center_positions[0][0]) ** 2 +

                          (target_position[1] - center_positions[0][1]) ** 2 +

                          (target_position[2] - center_positions[0][2]) ** 2)  # 中心から目標位置までの距離

torus_radius = 2 * radius_zionad  # トーラスの半径


# トーラスを作成

for i, center in enumerate(center_positions):

    # トーラスを作成

    bpy.ops.mesh.primitive_torus_add(

        align='WORLD',

        major_radius=torus_radius,

        minor_radius=minor_radius,

        location=center,

    )


    # 作成したオブジェクトの名前を設定

    obj = bpy.context.active_object

    name = f"torus_{center[0]:.1f},{center[1]:.1f},{center[2]:.1f}"

    obj.name = name





y= √3の 直線と

y=-√3を中心とする円周の交点に

半径 0.2 の球体を描く


円周の中心座標は (0,ー√3,0)

円周の半径4


import bpy

import math


# パラメータ

sphere_radius = 0.2  # 球体の半径

circle_radius = 4.0  # 円周の半径


# 円周の中心座標

circle_center = (0, -math.sqrt(3), 0)


# 円周と直線の交点のy座標

intersection_y = math.sqrt(3)


# 交点のx座標

intersection_x = [-2, 2]


# 交点を中心に球体を作成

for x in intersection_x:

    intersection_point = (x, intersection_y, 0)

    bpy.ops.mesh.primitive_uv_sphere_add(

        radius=sphere_radius,

        location=intersection_point

    )


    # 作成したオブジェクトの名前を設定

    obj = bpy.context.active_object

    obj.name = "Sphere_{}".format(x)


# 円周を作成

bpy.ops.mesh.primitive_circle_add(

    radius=circle_radius,

    location=circle_center

)




半径0.2の球体を作る

0,+√3、0


import bpy


# パラメータ

sphere_radius = 0.2  # 球体の半径

center_point = (0, 3 ** 0.5, 0)  # 球体の中心座標 (0, √3, 0)


# 球体を作成

bpy.ops.mesh.primitive_uv_sphere_add(

    radius=sphere_radius,

    location=center_point

)


# 作成したオブジェクトの名前を設定

obj = bpy.context.active_object

obj.name = "Sphere y=√3"







半径0.2の球体を作る

-1,+√3、0

1,+√3、0


import bpy


# パラメータ

sphere_radius = 0.2  # 球体の半径

center_points = [(-1, 3 ** 0.5, 0), (1, 3 ** 0.5, 0)]  # 球体の中心座標


# 球体を作成

for center_point in center_points:

    bpy.ops.mesh.primitive_uv_sphere_add(

        radius=sphere_radius,

        location=center_point

    )


    # 作成したオブジェクトの名前を設定

    obj = bpy.context.active_object

    obj.name = f"Sphere_{center_point[0]}_{center_point[1]}_{center_point[2]}"






aaaaa


2023年7月4日火曜日

20230705 長さ2の線路と いろいろ円周

aa



 


コレクション名 ”線路 円内”

を 作る



import bpy


# Create a new collection

collection_name = "線路 円内"

collection = bpy.data.collections.new(collection_name)


# Link the collection to the scene

scene = bpy.context.scene

scene.collection.children.link(collection)



blender python で


マークにする

半径0.2 の球体を 複数 作る

名前を連番で 作る


球体中心位置は


-1,0,0

0,0,0

+1,0,0


import bpy

from mathutils import Vector


def create_spheres():

    centers = [Vector((-1, 0, 0)), Vector((0, 0, 0)), Vector((1, 0, 0))]

    radius = 0.2


    for i, center in enumerate(centers):

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

        sphere = bpy.context.active_object

        sphere.name = f"Rail_{i+1}"


        # Customize the sphere's properties here

        # Example: Assign materials, set scale, etc.


    bpy.ops.object.mode_set(mode='OBJECT')


# Call the function to create the spheres

create_spheres()








blender python で

トーラスを複数 作る

連番で 名前を付ける


名前の付け方は 

トーラスの中心位置 x,y,zなら ”torus_x,y,z"

x,y,zは 小数点1桁まで 使う

 

トーラスのマイナー半径  0.02


トーラスの中心 

0,-√3,0

0,-2√3,0

0,-10,0



指定した

トーラス中心から

-1,0,0

までの距離を求めて

トーラスの半径にする


トーラスを描く



import bpy

import math


# パラメーター設定

minor_radius = 0.02

center_positions = [(0, -math.sqrt(3), 0), (0, -2*math.sqrt(3), 0), (0, -10, 0)]

passing_point = (-1, 0, 0)


# トーラスを作成し、連番の名前を付ける

for i, center in enumerate(center_positions):

    # 中心位置からの距離を計算して半径を求める

    distance = math.dist(center, passing_point)

    major_radius = distance


    # トーラスを作成

    bpy.ops.mesh.primitive_torus_add(

        align='WORLD',

        major_radius=major_radius,

        minor_radius=minor_radius,

        major_segments=48,

        minor_segments=12

    )

    torus = bpy.context.active_object


    # トーラスの位置と名前を設定

    torus.location = center

    torus_name = f"torus_{center[0]:.1f},{center[1]:.1f},{center[2]:.1f}"

    torus.name = torus_name




コレクション名 ”円周”

を 作る


import bpy


# Collection name

collection_name = "円周"


# Create the collection if it doesn't exist

if collection_name not in bpy.data.collections:

    collection = bpy.data.collections.new(collection_name)

    bpy.context.scene.collection.children.link(collection)

else:

    collection = bpy.data.collections[collection_name]


print(f"Collection '{collection_name}' created.")





球体を作る
球体の半径 0.2

名前は 連番で "円中心_y"

y軸位置の 小数点第1までの名にする


球体中心位置

0,-√3,0

0,-2√3,0

0,-10,0

0,2 - √3,0










コレクション名 ”円周 中心”

だけを 作る


import bpy


# Collection name

collection_name = "円周中心"


# Create the collection if it doesn't exist

if collection_name not in bpy.data.collections:

    collection = bpy.data.collections.new(collection_name)

    bpy.context.scene.collection.children.link(collection)

else:

    collection = bpy.data.collections[collection_name]


print(f"Collection '{collection_name}' created.")






import bpy
import math

# Parameters
sphere_radius = 0.2
center_positions = [(0, -math.sqrt(3), 0), (0, -2*math.sqrt(3), 0), (0, -10, 0), ]

# Create spheres at the center positions
for i, center in enumerate(center_positions):
    # Get the y-axis position with one decimal place
    y_position = round(center[1], 1)
    
    # Create sphere
    bpy.ops.mesh.primitive_uv_sphere_add(radius=sphere_radius, location=center)
    sphere = bpy.context.active_object

    # Set sphere name
    sphere_name = f"円中心_{y_position}"
    sphere.name = sphere_name
    
print("Spheres created.")











コレクション名 ”ドーム”

だけを 作る



import bpy


# Collection name

collection_name = "ドーム"


# Create the collection if it doesn't exist

if collection_name not in bpy.data.collections:

    collection = bpy.data.collections.new(collection_name)

    bpy.context.scene.collection.children.link(collection)

else:

    collection = bpy.data.collections[collection_name]


print(f"Collection '{collection_name}' created.")









球体を作る
球体の半径 0.2

名前は 連番で "ドーム_y"

y軸位置の 小数点第1までの名にする



球体中心位置は 以下で 計算する



blender python で

円周を複数 作る

連番で 名前を付ける



名前の付け方は 

円周の中心位置 x,y,zなら ”円周_x,y,z"

x,y,zは 小数点1桁まで 使う

 


円周の中心 

0,-√3,0

0,-2√3,0

0,-10,0





指定した

トーラス中心から

-2,0,0

までの距離を求めて

円周の半径にする


それぞれの円周の
(x,y,z)で

yが 最大位置を

球体の中心にして
球体を描く




import bpy
import math

# Define the center positions for the circular paths
center_positions = [(0, -math.sqrt(3), 0),
                    (0, -2 * math.sqrt(3), 0),
                    (0, -10, 0)]

# Define the target position for each circular path
target_position = (-1, 0, 0)

# Calculate the radius for each circular path
radii = [math.sqrt((target_position[0] - center[0]) ** 2 + (target_position[1] - center[1]) ** 2 + (target_position[2] - center[2]) ** 2) for center in center_positions]

# Create spheres on each circular path
for i, center in enumerate(center_positions):
    # Generate the name for the sphere
    name = "Circle_{},{},{}".format(round(center[0], 1), round(center[1], 1), round(center[2], 1))

    # Set the sphere's center position
    bpy.ops.mesh.primitive_uv_sphere_add(radius=radii[i], location=center)

    # Rename the sphere object
    sphere = bpy.context.object
    sphere.name = name









コレクション名 ”正面”

だけを 作る



import bpy


# Collection name

collection_name = "正面"


# Create the collection if it doesn't exist

if collection_name not in bpy.data.collections:

    collection = bpy.data.collections.new(collection_name)

    bpy.context.scene.collection.children.link(collection)

else:

    collection = bpy.data.collections[collection_name]


print(f"Collection '{collection_name}' created.")








球体を作る
球体の半径 0.2

名前は 連番で "正面_y"

y軸位置の 小数点第1までの名にする


0,2-(√3),0
0,(√13)-(2√3),0
0,(√101)-10,0



import bpy
import math

# 球体の半径
半径 = 0.2

# 位置のリスト
位置リスト = [
    (0, 2 - math.sqrt(3), 0),
    (0, math.sqrt(13) - (2 * math.sqrt(3)), 0),
    (0, math.sqrt(101) - 10, 0)
]

# 連番でオブジェクトを作成する
for i, 位置 in enumerate(位置リスト):
    # 名前の作成
    名前 = "正面_y{:.1f}".format(位置[1])

    # 球体を作成
    bpy.ops.mesh.primitive_uv_sphere_add(radius=半径, location=位置)
    球体 = bpy.context.active_object

    # 名前を設定
    球体.name = 名前









円柱を作る
円柱の中心 000
円柱の半径 0.1
円柱の長さ 20

-10,0,0 から  +10.0,0 に
円柱の中心軸の 両端が位置する

円柱を y軸を回転軸で 90度回転

名前を付ける "線路レールの円柱”


import bpy
import math

# 円柱の中心
中心 = (0, 0, 0)

# 円柱の半径
半径 = 0.1

# 円柱の長さ
長さ = 20

# 円柱の両端の位置
始点 = (-10.0, 0, 0)
終点 = (10.0, 0, 0)

# 円柱を作成
bpy.ops.mesh.primitive_cylinder_add(
    radius=半径,
    depth=長さ,
    location=中心,
    end_fill_type='NOTHING'
)

# 作成された円柱オブジェクトを取得
円柱 = bpy.context.active_object

# 円柱の両端を設定
円柱.location = (0, 0, 0)
円柱.rotation_euler = (0, math.radians(90), 0)
円柱.scale = (1, 1, 1)
bpy.ops.object.origin_set(type='ORIGIN_CENTER')

# 両端の位置を設定
円柱.data.vertices[0].co = 始点
円柱.data.vertices[1].co = 終点



名前を付ける "線路レールの円柱”

さらに もう1つ 円柱を作る

# 円柱の両端の位置
始点 = (-10.0, 2-√3, 0)
終点 = (10.0,  2-√3, 0)
名前を付ける "線路レール 2ー√3の円柱”



import bpy
import math

# 円柱の中心
中心 = (0, 2-3**0.5, 0)

# 円柱の半径
半径 = 0.1

# 円柱の長さ
長さ = 20

# 円柱の両端の位置
始点 = (-10.0,  2-3**0.5, 0)
終点 = (10.0,  2-3**0.5, 0)

# 円柱を作成
bpy.ops.mesh.primitive_cylinder_add(
    radius=半径,
    depth=長さ,
    location=中心,
    end_fill_type='NOTHING'
)

# 作成された円柱オブジェクトを取得
円柱 = bpy.context.active_object

# 円柱の両端を設定
円柱.location = (0, 0, 0)
円柱.rotation_euler = (0, math.radians(90), 0)
円柱.scale = (1, 1, 1)
bpy.ops.object.origin_set(type='ORIGIN_CENTER')

# 両端の位置を設定
円柱.data.vertices[0].co = 始点
円柱.data.vertices[1].co = 終点




20230703 元 20230508

 


配布 20230703 a001 元20230508 skybox  003  moto 20230422 bbb半球 半径2 ビデオ会議 006

https://drive.google.com/file/d/15Gyz2Lf5XSIZFNFbBFpj25gaZhtPs_Bz/view?usp=drive_link



















aaaa


2023年6月22日木曜日

20230623 列車慣性系の3枚重ね





import bpy

import math


# 長方形のサイズ

width = 2.0

height = 1.0


# 長方形の中心座標

center = (0.0, 0.0, 0.0)


# 球体の半径と個数

sphere_radius = 0.02

sphere_count = 80


# コレクションの作成と名前付け

collection_names = ["t=0_train", "t=-1_train", "t=+1_train"]


for collection_name in collection_names:

    if collection_name not in bpy.data.collections:

        collection = bpy.data.collections.new(collection_name)

        bpy.context.scene.collection.children.link(collection)


# 球体を配置する関数

def create_spheres(collection_name, z_position):

    collection = bpy.data.collections[collection_name]


    # 球体の作成と配置

    for i in range(sphere_count):

        angle = 2 * math.pi * i / sphere_count

        x = center[0] + (width/2 + sphere_radius) * math.cos(angle)

        y = center[1] + (height/2 + sphere_radius) * math.sin(angle)

        z = z_position


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


        # 最後に作成された球体オブジェクトを選択状態にする

        obj = bpy.context.object

        obj.name = f"t{z_position}_Sphere_{i+1}"

        obj.select_set(True)

        bpy.context.view_layer.objects.active = obj


        # 不要なリンクを削除

        bpy.context.collection.objects.unlink(obj)


        # 球体を対応するコレクションに追加

        collection.objects.link(obj)


# 球体を配置する関数を呼び出し

create_spheres("t=0_train", 0.0)

create_spheres("t=-1_train", -1.0)

create_spheres("t=+1_train", 1.0)