見出し画像

blenderでビル群を作成するコード

import bpy
import random

def create_building(location, width, depth, height):
    bpy.ops.mesh.primitive_cube_add(size=1, enter_editmode=False, align='WORLD', location=location)
    building = bpy.context.active_object
    building.scale = (width / 2, depth / 2, height / 2)
    return building

def create_city(blocks_x, blocks_y, block_width, block_depth, min_height, max_height):
    for x in range(blocks_x):
        for y in range(blocks_y):
            width = random.uniform(block_width * 0.5, block_width)
            depth = random.uniform(block_depth * 0.5, block_depth)
            height = random.uniform(min_height, max_height)
            
            location_x = x * block_width + block_width / 2
            location_y = y * block_depth + block_depth / 2
            location = (location_x, location_y, height / 2)
            
            create_building(location, width, depth, height)

# パラメータを設定
blocks_x = 5
blocks_y = 5
block_width = 4
block_depth = 4
min_height = 3
max_height = 20

# 既存のオブジェクトをすべて削除
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

# 都市を生成
create_city(blocks_x, blocks_y, block_width, block_depth, min_height, max_height)

この記事が気に入ったらサポートをしてみませんか?