承知しました。最初の円錐を最後に非表示にし、追加の円錐を作成しない形にスクリプトを修正します。以下が更新されたスクリプトです:
python
import bpy
import math
from mathutils import Vector, Euler, Matrix
内容
正方形平面上に11x11の円錐を配置
正方形と円錐を別々のサブコレクションに配置
最初の円錐を最後に非表示にする
正方形の位置・回転に合わせて円錐を配置
各円錐が指定位置を向くように調整
サイズ変化と透明度変化の方向を選択可能に
コレクション、サブコレクション名にパラメータを追加
円錐の向きを反転するオプション追加
AI Assistant
パラメータ
main_collection_name = "ConeGrid_{grid_size}x{grid_size}"
square_collection_name = "BasePlane_Size{size}Pos{pos}"
cone_collection_name = "Cones_Size{size}Pos{pos}"
cone_base_name = "Cone {x} {y}"
square_name = "BasePlane"
grid_size = 11 # 11x11のグリッド
cone_radius = 0.2
cone_height = 0.5
vertices = 32
square_size = 10 # 正方形の一辺の長さ
square_center = (0, 10, 0) # 正方形の中心位置
square_rotation = (90, 0, 0) # 正方形の回転(度数法)
target_point = (0, 0, 0) # 円錐が向く目標点
start_color = (0.0, 0.0, 1.0, 1.0) # 開始色: 青 (R, G, B, A)
end_color = (1.0, 0.0, 0.0, 1.0) # 終了色: 赤 (R, G, B, A)
alpha_start = 1.0
alpha_end = 0.3
size_start = 0.0 # 円錐のサイズ変化なし(開始サイズ)
size_end = 0.0 # 円錐のサイズ変化なし(終了サイズ)
reverse_size = False
reverse_alpha = False
reverse_cones = True # 円錐の向きを反転するスイッチ
スクリプト本体
def create_square(name, size, center, rotation, collection):
verts = [(-size/2, -size/2, 0), (size/2, -size/2, 0), (size/2, size/2, 0), (-size/2, size/2, 0)]
faces = [(0, 1, 2, 3)]
mesh = bpy.data.meshes.new(name)
mesh.from_pydata(verts, [], faces)
mesh.update()
obj = bpy.data.objects.new(name, mesh)
collection.objects.link(obj)
obj.location = center
obj.rotation_euler = Euler([math.radians(angle) for angle in rotation])
return obj
def create_cone(name, radius, height, vertices, collection):
bpy.ops.mesh.primitive_cone_add(
radius1=radius,
depth=height,
vertices=vertices,
enter_editmode=False,
location=(0, 0, 0)
)
cone = bpy.context.active_object
cone.name = name
bpy.data.collections[collection.name].objects.link(cone)
bpy.context.collection.objects.unlink(cone)
return cone
def lerp(a, b, t):
return a + (b - a) * t
def lerp_color(color1, color2, t):
return tuple(lerp(a, b, t) for a, b in , color2))
メイン処理
main_collection_name = main_collection_name.format(grid_size=grid_size)
if main_collection_name not in bpy.data.collections:
main_collection = bpy.data.collections.new(main_collection_name)
bpy.context.scene.collection.children.link(main_collection)
else:
main_collection = bpy.data.collections[main_collection_name]
サブコレクション名の設定
pos_str = f"{square_center[0]}{square_center[1]} {square_center[2]}"
square_collection_name = square_collection_name.format(size=square_size, pos=pos_str)
cone_collection_name = cone_collection_name.format(size=square_size, pos=pos_str)
正方形のサブコレクション作成
if square_collection_name not in main_collection.children:
square_collection = bpy.data.collections.new(square_collection_name)
main_collection.children.link(square_collection)
else:
square_collection = main_collection.children[square_collection_name]
円錐のサブコレクション作成
if cone_collection_name not in main_collection.children:
cone_collection = bpy.data.collections.new(cone_collection_name)
main_collection.children.link(cone_collection)
else:
cone_collection = main_collection.children[cone_collection_name]
正方形の作成
square = create_square(square_name, square_size, square_center, square_rotation, square_collection)
正方形の変換行列を取得
square_matrix = square.matrix_world
円錐の配置
first_cone = None
step = square_size / (grid_size - 1)
for i in range(grid_size):
for j in range(grid_size):
local_x = -square_size/2 + i * step
local_y = -square_size/2 + j * step
local_z = 0
code Copy code
# ローカル座標をグローバル座標に変換
cone_bottom_center = square_matrix @ Vector((local_x, local_y, local_z))
t = (i * grid_size + j) / (grid_size * grid_size - 1)
# サイズの計算(サイズ変化なし)
current_size = 1.0
# 透明度の計算
if reverse_alpha:
current_alpha = lerp(alpha_end, alpha_start, t)
else:
current_alpha = lerp(alpha_start, alpha_end, t)
# 色の計算
current_color = lerp_color(start_color, end_color, t)
# 円錐の作成
cone_name = cone_base_name.format(x=i, y=j)
cone = create_cone(cone_name, cone_radius * current_size, cone_height * current_size, vertices, cone_collection)
# 最初の円錐を記録
if i == 0 and j == 0:
first_cone = cone
# 円錐の底面中心を設定
cone.location = cone_bottom_center
# 円錐の頂点が目標点を向くように位置を調整
direction = Vector(target_point) - cone_bottom_center
direction.normalize()
# 円錐の向きを反転する場合
if reverse_cones:
direction = -direction
# 円錐を目標点に向けて回転
rot_quat = direction.to_track_quat('-Z', 'Y')
cone.rotation_euler = rot_quat.to_euler()
# マテリアルの設定
material = bpy.data.materials.new(name=f"ConeMaterial_{i}_{j}")
material.use_nodes = True
material.blend_method = 'BLEND'
nodes = material.node_tree.nodes
principled = nodes["Principled BSDF"]
<URL>puts["Base Color"].default_value = current_color
<URL>puts["Alpha"].default_value = current_alpha
cone.data.materials.append(material)
最初の円錐を非表示にする
if first_cone:
first_cone.hide_viewport = True
first_cone.hide_render = True
print("Script completed successfully.")
このスクリプトでは以下の変更を行いました:
最初の円錐(i=0, j=0の位置にある円錐)を first_cone 変数に記録します。
すべての円錐の作成が終わった後、first_cone が存在する場合(None でない場合)、その円錐を非表示にします。
first_cone.hide_viewport = True でビューポートでの表示を無効にします。
first_cone.hide_render = True でレンダリング時の表示も無効にします。
。
これにより、最初の円錐が作成され、他のすべての円錐と同じように配置されますが、最後に非表示になります。追加の円錐は作成されません。この方法では、最初の円錐のデータは保持されたまま、単に表示されないようになります。
Share
Export
Related
他のプログラミング言語でも同様の方法があるのか
円錐を非表示にするためのPythonのコード例は
円錐を非表示にする際の注意点は
円錐を非表示にすることで性能が向上するのか