スキップしてメイン コンテンツに移動

x軸に2単位ごとに半径0.5の球を10個生成するPythonコード


import bpy
import mathutils

# Sphere properties
radius = 0.5

# Material properties
material_name = "Material"
color = (1, 0, 0)

# Loop through x-axis
for i in range(-9, 11, 2):
    x = i
    y = 0
    z = 0
    
    # Create sphere mesh
    bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(x, y, z))
    sphere = bpy.context.object
    
    # Create material slot
    if material_name not in bpy.data.materials:
        material = bpy.data.materials.new(name=material_name)
        material.diffuse_color = color
    else:
        material = bpy.data.materials[material_name]
    
    sphere.data.materials.append(material)





以下は、x軸に2単位ごとに半径0.5の球を11個生成するPythonコードです。





import bpy
import mathutils

# Sphere properties
radius = 0.5

# Material properties
material_name = "Material"
color = (1, 0, 0)

# Loop through x-axis
for i in range(-10, 11, 2):
    x = i
    y = 0
    z = 0
    
    # Create sphere mesh
    bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(x, y, z))
    sphere = bpy.context.object
    
    # Create material slot
    if material_name not in bpy.data.materials:
        material = bpy.data.materials.new(name=material_name)
        material.diffuse_color = color
    else:
        material = bpy.data.materials[material_name]
    
    sphere.data.materials.append(material)







修正後のコードに、x軸方向に1ずつ移動する命令を追加するには、球体オブジェクトの location プロパティにアクセスして、x の値を加算するだけです。下記のように修正してください。




import bpy

# Sphere properties
radius = 0.5

# Material properties
material_name = "Material"
color = (0, 0, 1)

# Create material
if material_name not in bpy.data.materials:
    material = bpy.data.materials.new(name=material_name)
    material.diffuse_color = color
else:
    material = bpy.data.materials[material_name]

# Loop through x-axis
for i in range(-10, 11, 2):
    x = i
    y = 0
    z = 0
    
    # Create sphere mesh
    bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(x, y, z))
    sphere = bpy.context.object
    
    # Assign material to sphere
    sphere.active_material = material
    
    # Move sphere along x-axis
    sphere.location.x += 1






togetter.com/t/c2022meetzionad
togetter.com/t/b2022meetzionad




twitter 新着検索 Dürer & 測距儀


aaa
bbb




ccc twitter zionadchat
twitter に追い出されたら 連絡先は Gettr https://gettr.com/user/zionadchat
twitter サブアカウント https://twitter.com/2022zionad

old page いいい
new page いいい

目次 2022の目次 単純トリック hatena zionadchat
いいいいいいいい