2023年7月9日日曜日

20230710 mon 球体で 数直線

 




RGBAのカラーサンプル

https://ywork2020.com/content/tips-rgba-color-sample.html


RGBAの各成分を255の範囲から1.0の範囲に変換するためには、次のような変換を行います。

  1. RGBAの各成分の値を0から255の範囲に持つ整数として取得します。
  2. 各成分の値を255で除算します。
  3. 得られた結果を小数点以下2桁まで丸めます。

以下にPythonのコード例を示します。






blender python で スクリプト 書く


円柱を3つ作る

円柱長さ 10

円柱半径 0.01

円柱中心位置 Bleu  0,0,+1

円柱中心位置 Green  0,0,0

円柱中心位置 Red  0,0,-1



# 3本の円柱 

import bpy

import math


# 円柱を作成する関数

def create_cylinder(center_point, index):

    bpy.ops.mesh.primitive_cylinder_add(

        align='WORLD',

        location=center_point,

        depth=10,

        rotation=(0, math.pi/2, 0),  # y軸を回転軸にして90度回転

        radius=0.01

    )


    # 作成した円柱に連番の名前を付ける

    obj = bpy.context.object

    obj.name = "Cylinder{}".format(index)


# 円柱1

center_point_1 = (0, 0, -1)

create_cylinder(center_point_1, 1)


# 円柱2

center_point_2 = (0, 0, 0)

create_cylinder(center_point_2, 2)


# 円柱3

center_point_3 = (0, 0, 1)

create_cylinder(center_point_3, 3)





#色付き 円柱 3つ

import bpy

import math


# 円柱を作成する関数

def create_cylinder(center_point, index, color):

    bpy.ops.mesh.primitive_cylinder_add(

        align='WORLD',

        location=center_point,

        depth=10,

        rotation=(0, math.pi/2, 0),  # y軸を回転軸にして90度回転

        radius=0.01

    )


    # 作成した円柱に連番の名前を付ける

    obj = bpy.context.object

    obj.name = "Cylinder{}".format(index)


    # マテリアルを追加して色を設定

    mat = bpy.data.materials.new("Material{}".format(index))

    mat.use_nodes = True

    obj.data.materials.append(mat)

    

    # ノードエディタで色を設定

    node_tree = mat.node_tree

    principled_bsdf = node_tree.nodes.get("Principled BSDF")

    if principled_bsdf:

        principled_bsdf.inputs[0].default_value = color


# 円柱1

center_point_1 = (0, 0, -1)

color_1 = (200/255, 0.0, 0.0, 0.8)  # 赤色 (RGBA)

create_cylinder(center_point_1, 1, color_1)


# 円柱2

center_point_2 = (0, 0, 0)

color_2 = (0.0, 1.0, 0.0, 1.0)  # 緑色 (RGBA)

create_cylinder(center_point_2, 2, color_2)


# 円柱3

center_point_3 = (0, 0, 1)

color_3 = (0.0, 0.0, 1.0, 1.0)  # 青色 (RGBA)

create_cylinder(center_point_3, 3, color_3)




21個の球体を作る

球体半径 0.02

名前を付ける 連番で Green水平

(-1,0,0) と(+1,0,0) の間に均等配置





# Green水平 球体21個

import bpy


# 球体を作成する関数

def create_sphere(location, index, color):

    bpy.ops.mesh.primitive_uv_sphere_add(

        radius=0.03,

        location=location

    )


    # 作成した球体に連番の名前を付ける

    obj = bpy.context.object

    obj.name = "Green水平{}".format(index)


    # マテリアルを追加して色を設定

    mat = bpy.data.materials.new("Material{}".format(index))

    mat.use_nodes = True

    obj.data.materials.append(mat)


    # ノードエディタで色を設定

    node_tree = mat.node_tree

    principled_bsdf = node_tree.nodes.get("Principled BSDF")

    if principled_bsdf:

        principled_bsdf.inputs[0].default_value = color


# 21個の球体を作成

start_pos = (-1.0, 0.0, 0.0)  # 配置の開始位置

distance = 0.1  # 球体同士の間隔

color = (0.0, 1.0, 0.0, 1.0)  # 緑色 (RGBA)


for i in range(21):

    x = start_pos[0] + (i * distance)

    location = (x, start_pos[1], start_pos[2])

    create_sphere(location, i+1, color)



21個の青い球体を作る

球体半径 0.03 

(0,0,1) と(+2,0,1) の間に均等配置




作った 球体21個を複製し 移動する

(1,0,1) と(+2,0,1) の間に均等配置

名前を付ける 連番で Blue+1水平


作った 球体21個を複製し 移動する

(1,0,1) と(+2,0,1) の間に均等配置

名前を付ける 連番で Blue time+1水平





3つのコレクションを作り 3つに分けて 名前は B原点 B水平移動 B時間移動


import bpy


# コレクションを作成する関数

def create_collection(name):

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

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


# コレクションを作成

create_collection("B原点")

create_collection("B水平移動")

create_collection("B時間移動")





コレクションだけを作り 名前は 

R原点 

R水平移動 

R時間移動

G時間移動

G原点


import bpy


# コレクションを作成する関数

def create_collection(name):

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

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


# コレクションの作成

create_collection("R原点")

create_collection("R水平移動")

create_collection("R時間移動")

create_collection("G時間移動")

create_collection("G原点")




20230710 散布図 

 




BlenderのPythonスクリプトを使用して、50個のランダムなXY座標を持つ点の散布図を作成












ChatGPTでGPT-4のCode Interpreterを触ってみる by Yuichiro Minato | blueqat

https://blueqat.com/yuichiro_minato2/45612470-6574-41d1-8940-469669924e12




# 50個のランダムなXY座標を持つ点の散布図

import bpy

import random


# 点の数を指定します

point_count = 50


# ランダムなXY座標を生成します

points = [(random.uniform(-10, 10), random.uniform(-10, 10)) for _ in range(point_count)]


# 点オブジェクトを作成します

for point in points:

    bpy.ops.object.empty_add(location=(point[0], point[1], 0))


# レンダリング設定を調整します

bpy.context.scene.render.engine = 'CYCLES'

bpy.context.scene.cycles.device = 'GPU'


# カメラを設定します

bpy.ops.object.camera_add(location=(0, 0, 20))

bpy.context.scene.camera = bpy.context.object


# ランダムな色のマテリアルを作成します

for obj in bpy.data.objects:

    if obj.type == 'EMPTY':

        mat = bpy.data.materials.new(name='PointMaterial')

        mat.use_nodes = True

        obj.data.materials.append(mat)

        mat.node_tree.nodes.new(type='ShaderNodeRGB')

        mat.node_tree.nodes["RGB"].outputs[0].default_value = (random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1))


# レンダリングを実行します

bpy.ops.render.render(write_still=True)





2023年7月8日土曜日

20320709  正式版 原点 正面窓 Y軸 90度回転




# 原点 正面窓 Y軸 90度回転

import bpy

import math


radius_list = [1, math.sqrt(2)/2, math.sqrt(3)/2, math.sqrt(2), math.sqrt(3), 2]

height = 0.005



for radius in radius_list:

    bpy.ops.mesh.primitive_cylinder_add(

        radius=radius,

        depth=height,

        location=(0, 0, 0)

    )


    obj = bpy.context.active_object

    bpy.context.view_layer.objects.active = obj

    obj.select_set(True)


    obj.name = f"原点 窓{radius}"


    bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='BOUNDS')


    bpy.ops.transform.rotate(value=math.radians(90), orient_axis='Y')








コレクションだけを作る

名前は

”単位円 円周トーラス"
”原点 円柱"
”窓面 円柱"
”実物 円柱"

”原点 数学 XY 円柱円板"
”原点 正面窓 YZ 円柱円板"
”原点 土台 ZX 円柱円板"

”窓面 数学 XY 円柱円板"
”窓面 正面窓 YZ 円柱円板"
”窓面 土台 ZX 円柱円板"

”実物 数学 XY 円柱円板"
”実物 正面窓 YZ 円柱円板"
”実物 土台 ZX 円柱円板"



あああああ



20230709 天球 三角測量 円柱で薄い円板 作る 

 


円柱で円板を複数作る


半径1


半径 √2/2


半径 √3/2


半径√2


半径√3


半径 2


中心   1,0,0








#原点側面 円柱 薄い円板

import bpy

import math



# 円柱の半径と高さのリスト

radius_list = [1, math.sqrt(2)/2, math.sqrt(3)/2, math.sqrt(2), math.sqrt(3), 2]

height = 0.005


# 色と透明度の設定

color = (0.5, 0, 0, 0.3)  # 薄い赤色 (RGBA形式)


# 側面のコレクションを作成

for radius in radius_list:

    # 円柱を作成

    bpy.ops.mesh.primitive_cylinder_add(

        radius=radius,

        depth=height,

        location=(0, 0, 0)

    )

    

    # オブジェクトを選択

    obj = bpy.context.active_object

    bpy.context.view_layer.objects.active = obj

    obj.select_set(True)

    

    # 名前を設定

    obj.name = f"原点側面 {radius}"

    



#原点窓 正面 円柱 薄い円板

import bpy

import math



# 円柱の半径と高さのリスト

radius_list = [1, math.sqrt(2)/2, math.sqrt(3)/2, math.sqrt(2), math.sqrt(3), 2]

height = 0.005




# 側面のコレクションを作成

for radius in radius_list:

    # 円柱を作成

    bpy.ops.mesh.primitive_cylinder_add(

        radius=radius,

        depth=height,

        location=(0, 0, 0)

    )

    

    # オブジェクトを選択

    obj = bpy.context.active_object

    bpy.context.view_layer.objects.active = obj

    obj.select_set(True)

    

    # 名前を設定

    obj.name = f"原点窓 正面 {radius}"

    




# 窓 側面 XY平面 円柱円板

import bpy

import math



# 円柱の半径と高さのリスト

radius_list = [1, math.sqrt(2)/2, math.sqrt(3)/2, math.sqrt(2), math.sqrt(3), 2]

height = 0.005


# 色と透明度の設定

color = (0.5, 0, 0, 0.3)  # 薄い赤色 (RGBA形式)


# 側面のコレクションを作成

for radius in radius_list:

    # 円柱を作成

    bpy.ops.mesh.primitive_cylinder_add(

        radius=radius,

        depth=height,

        location=(1, 0, 0)

    )

    

    # オブジェクトを選択

    obj = bpy.context.active_object

    bpy.context.view_layer.objects.active = obj

    obj.select_set(True)

    

    # 名前を設定

    obj.name = f"”窓 側面 {radius}"

    



# 窓 正面 YZ平面 円柱円板

import bpy

import math



# 円柱の半径と高さのリスト

radius_list = [1, math.sqrt(2)/2, math.sqrt(3)/2, math.sqrt(2), math.sqrt(3), 2]

height = 0.005


# 色と透明度の設定

color = (0.5, 0, 0, 0.3)  # 薄い赤色 (RGBA形式)


# 側面のコレクションを作成

for radius in radius_list:

    # 円柱を作成

    bpy.ops.mesh.primitive_cylinder_add(

        radius=radius,

        depth=height,

        location=(1, 0, 0)

    )

    

    # オブジェクトを選択

    obj = bpy.context.active_object

    bpy.context.view_layer.objects.active = obj

    obj.select_set(True)

    

    # 名前を設定

    obj.name = f"”窓 正面 {radius}"

    








ああああ

20230709 sun 天球 三角測量

 







blender python 

半径1のトーラスを作る

オブジェクトに名前を付ける


マイナー半径 0.05

トーラスの中心位置 000




# トーラス 半径1円周


import bpy


# トーラスを作成

bpy.ops.mesh.primitive_torus_add(

    align='WORLD',

    location=(0, 0, 0),

    rotation=(0, 0, 0),

    major_radius=1,

    minor_radius=0.01,

    major_segments=48,

    minor_segments=24

)


# 作成したオブジェクトに名前を付ける

obj = bpy.context.active_object

obj.name = "MyTorus"




追加のトーラスを作る

x軸を回転軸にして 90度回転





# xz 平面の 単位円 円周トーラス

import bpy

import math


# トーラスを作成

bpy.ops.mesh.primitive_torus_add(

    align='WORLD',

    location=(0, 0, 0),

    rotation=(0, 0, 0),

    major_radius=1,

    minor_radius=0.01,

    major_segments=48,

    minor_segments=24

)


# 作成したオブジェクトに名前を付ける

obj = bpy.context.active_object

obj.name = "MyTorus"


# 追加のトーラスを作成

bpy.ops.object.duplicate(linked=False)


# 回転を設定

rotation_rad = math.radians(90)  # 90度をラジアンに変換

bpy.context.active_object.rotation_euler = (rotation_rad, 0, 0)




太さ 0.05 で 円柱を 2つ作る

円柱長さ 1


円柱中心軸の中心は 1,1,0 と 1,0,0 の中間

円柱中心軸の中心は 1,-1,0 と 1,0,0 の中間位置


x軸を回転軸にして 90度回転




# 被写体 円柱

import bpy

import math


# 円柱を作成する関数

def create_cylinder(center_point, end_point, index):

    # 中心点と終点の座標から方向ベクトルと長さを計算

    direction = [end_point[i] - center_point[i] for i in range(3)]

    length = sum([i**2 for i in direction])**0.5


    # 円柱を作成

    bpy.ops.mesh.primitive_cylinder_add(

        align='WORLD',

        location=(center_point[0] + direction[0] / 2, center_point[1] + direction[1] / 2, center_point[2] + direction[2] / 2),

        depth=length,

        rotation=(math.pi/2, 0, 0),  # x軸を回転軸にして90度回転

        radius=0.015

    )


    # 作成した円柱に連番の名前を付ける

    obj = bpy.context.active_object

    obj.name = "Cylinder{}".format(index)


# 円柱1の中心軸の中間位置の座標

center_point_1 = ((1 + 1) / 2, (1 + 0) / 2, 0)

end_point_1 = (1, 0, 0)


# 円柱1を作成

create_cylinder(center_point_1, end_point_1, 1)


# 円柱2の中心軸の中間位置の座標

center_point_2 = ((1 + 1) / 2, (-1 + 0) / 2, 0)

end_point_2 = (1, 0, 0)


# 円柱2を作成

create_cylinder(center_point_2, end_point_2, 2)






円柱で円板を複数作る

半径1

半径 √2/2

半径 √3/2

半径√2

半径√3

半径 2

中心   1,0,0


色を塗れるように

マテリアルをrgba で設定






コレクションを作る

名前は

”単位円 円周トーラス"
”原点 円柱"
”窓面 円柱"
”実物 円柱"

”原点 数学 XY 円柱円板"
”原点 正面窓 YZ 円柱円板"
”原点 土台 ZX 円柱円板"

”窓面 数学 XY 円柱円板"
”窓面 正面窓 YZ 円柱円板"
”窓面 土台 ZX 円柱円板"

”実物 数学 XY 円柱円板"
”実物 正面窓 YZ 円柱円板"
”実物 土台 ZX 円柱円板"






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






import bpy

”単位円 円周トーラス"
cube_inner_blue_collection = bpy.data.collections.new(”単位円 円周トーラス")
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)









ああああ






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"