深圳中学高中园 - Blender建筑生成代码
基于"深圳中学高中园"的概念,我将创建一个现代化校园建筑的Blender生成代码:
```python
import bpy
import bmesh
import mathutils
import math
import random
def clear_scene():
"""清理场景"""
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
def create_modern_school_campus():
"""创建现代化校园建筑群"""
# 主教学楼 - 现代化设计
print("创建主教学楼...")
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 15))
main_building = bpy.context.object
main_building.scale = (40, 12, 15)
main_building.name = "主教学楼"
# 添加现代建筑材质
main_mat = create_modern_facade_material("主楼外墙", (0.9, 0.9, 0.95))
main_building.data.materials.append(main_mat)
# 添加窗户细节
add_building_windows(main_building, rows=6, columns=8)
# 科技楼 - 弧形设计
print("创建科技楼...")
create_tech_building((30, 25, 8))
# 实验楼 - 模块化设计
print("创建实验楼...")
create_lab_building((-25, 20, 6))
# 行政楼
print("创建行政楼...")
bpy.ops.mesh.primitive_cube_add(location=(-30, -20, 8))
admin_building = bpy.context.object
admin_building.scale = (10, 8, 8)
admin_building.name = "行政楼"
# 宿舍楼群
print("创建宿舍楼群...")
create_dormitory_complex((20, -25))
# 图书馆 - 标志性建筑
print("创建图书馆...")
create_library_building((-15, -30, 10))
# 体育场馆
print("创建体育场馆...")
create_sports_facilities((0, 40))
# 连廊系统
print("创建连廊系统...")
create_walkway_system()
# 校园景观
print("创建校园景观...")
create_campus_landscape()
def create_modern_facade_material(name, color):
"""创建现代建筑外墙材质"""
material = bpy.data.materials.new(name)
material.use_nodes = True
nodes = material.node_tree.nodes
nodes.clear()
# 主BSDF节点
bsdf = nodes.new('ShaderNodeBsdfPrincipled')
output = nodes.new('ShaderNodeOutputMaterial')
# 设置现代建筑材质
bsdf.inputs['Base Color'].default_value = (*color, 1.0)
bsdf.inputs['Metallic'].default_value = 0.1
bsdf.inputs['Roughness'].default_value = 0.3
bsdf.inputs['Specular'].default_value = 0.5
# 添加玻璃效果
bsdf.inputs['Transmission'].default_value = 0.2
material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
return material
def add_building_windows(building, rows=5, columns=10):
"""为建筑添加窗户细节"""
building_dimensions = building.dimensions
window_size = (1.5, 0.2, 1.0) # 窗户尺寸
for i in range(rows):
for j in range(columns):
# 计算窗户位置(建筑表面)
x_pos = (j - columns/2 + 0.5) * (building_dimensions.x / columns)
z_pos = (i + 0.5) * (building_dimensions.z / (rows + 1))
# 前后两面都添加窗户
for y_multiplier in [-1, 1]:
y_pos = y_multiplier * (building_dimensions.y / 2 + 0.1)
bpy.ops.mesh.primitive_cube_add(
location=(x_pos, y_pos, z_pos)
)
window = bpy.context.object
window.scale = window_size
window.name = f"窗户_{i}_{j}"
# 窗户材质
window_mat = bpy.data.materials.new("窗户材质")
window_mat.use_nodes = True
nodes = window_mat.node_tree.nodes
nodes.clear()
bsdf = nodes.new('ShaderNodeBsdfPrincipled')
output = nodes.new('ShaderNodeOutputMaterial')
# 蓝色玻璃效果
bsdf.inputs['Base Color'].default_value = (0.6, 0.7, 0.9, 1.0)
bsdf.inputs['Metallic'].default_value = 0.0
bsdf.inputs['Roughness'].default_value = 0.1
bsdf.inputs['Transmission'].default_value = 0.8
window_mat.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
window.data.materials.append(window_mat)
def create_tech_building(location):
"""创建科技楼 - 弧形现代设计"""
# 主楼体
bpy.ops.mesh.primitive_cube_add(location=location)
tech_building = bpy.context.object
tech_building.scale = (8, 8, 12)
tech_building.name = "科技楼"
# 弧形顶部结构
bpy.ops.mesh.primitive_cylinder_add(
location=(location[0], location[1], location[2] + 12),
radius=6, depth=2
)
dome = bpy.context.object
dome.name = "科技楼穹顶"
# 科技感材质
tech_mat = create_tech_material()
tech_building.data.materials.append(tech_mat)
dome.data.materials.append(tech_mat)
def create_tech_material():
"""创建科技感材质"""
material = bpy.data.materials.new("科技材质")
material.use_nodes = True
nodes = material.node_tree.nodes
nodes.clear()
bsdf = nodes.new('ShaderNodeBsdfPrincipled')
output = nodes.new('ShaderNodeOutputMaterial')
# 银色科技感
bsdf.inputs['Base Color'].default_value = (0.7, 0.7, 0.8, 1.0)
bsdf.inputs['Metallic'].default_value = 0.8
bsdf.inputs['Roughness'].default_value = 0.2
material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
return material
def create_lab_building(location):
"""创建实验楼 - 模块化设计"""
# 创建多个模块化建筑体
modules = [
((-3, 0, 0), (6, 8, 10)),
((3, 0, 0), (6, 8, 8)),
((0, 8, 0), (12, 6, 6))
]
for i, (mod_location, mod_scale) in enumerate(modules):
bpy.ops.mesh.primitive_cube_add(
location=(
location[0] + mod_location[0],
location[1] + mod_location[1],
location[2] + mod_location[2]
)
)
module = bpy.context.object
module.scale = mod_scale
module.name = f"实验楼模块_{i+1}"
# 实验楼材质
lab_mat = create_lab_material()
module.data.materials.append(lab_mat)
def create_lab_material():
"""创建实验楼材质"""
material = bpy.data.materials.new("实验楼材质")
material.use_nodes = True
nodes = material.node_tree.nodes
nodes.clear()
bsdf = nodes.new('ShaderNodeBsdfPrincipled')
output = nodes.new('ShaderNodeOutputMaterial')
# 浅灰色调
bsdf.inputs['Base Color'].default_value = (0.8, 0.85, 0.9, 1.0)
bsdf.inputs['Roughness'].default_value = 0.4
material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
return material
def create_dormitory_complex(location):
"""创建宿舍楼群"""
# 创建多栋宿舍楼
for i in range(3):
for j in range(2):
x_pos = location[0] + i * 15
y_pos = location[1] + j * 20
bpy.ops.mesh.primitive_cube_add(location=(x_pos, y_pos, 8))
dorm = bpy.context.object
dorm.scale = (5, 8, 8)
dorm.name = f"宿舍楼_{i+1}_{j+1}"
# 宿舍楼材质
dorm_mat = create_dormitory_material()
dorm.data.materials.append(dorm_mat)
# 添加阳台细节
add_balconies(dorm)
def create_dormitory_material():
"""创建宿舍楼材质"""
material = bpy.data.materials.new("宿舍楼材质")
material.use_nodes = True
nodes = material.node_tree.nodes
nodes.clear()
bsdf = nodes.new('ShaderNodeBsdfPrincipled')
output = nodes.new('ShaderNodeOutputMaterial')
# 暖色调
bsdf.inputs['Base Color'].default_value = (0.95, 0.92, 0.85, 1.0)
bsdf.inputs['Roughness'].default_value = 0.6
material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
return material
def add_balconies(building):
"""为建筑添加阳台"""
building_dimensions = building.dimensions
for floor in range
(4): # 4层阳台
bpy.ops.mesh.primitive_cube_add(
location=(0, building_dimensions.y/2 + 0.5, z_pos)
)
balcony = bpy.context.object
balcony.scale = (building_dimensions.x/2 - 0.2, 1, 0.3)
balcony.name = f"{building.name}_阳台_{floor+1}"
def create_library_building(location):
"""创建图书馆 - 标志性建筑"""
# 主楼体
bpy.ops.mesh.primitive_cube_add(location=location)
library = bpy.context.object
library.scale = (12, 15, 6)
library.name = "图书馆"
# 特色屋顶
bpy.ops.mesh.primitive_cone_add(
location=(location[0], location[1], location[2] + 8),
radius1=8, radius2=4, depth=4
)
roof = bpy.context.object
roof.name = "图书馆屋顶"
# 图书馆材质
library_mat = create_library_material()
library.data.materials.append(library_mat)
roof.data.materials.append(library_mat)
def create_library_material():
"""创建图书馆材质"""
material = bpy.data.materials.new("图书馆材质")
material.use_nodes = True
nodes = material.node_tree.nodes
nodes.clear()
bsdf = nodes.new('ShaderNodeBsdfPrincipled')
output = nodes.new('ShaderNodeOutputMaterial')
# 深色稳重色调
bsdf.inputs['Base Color'].default_value = (0.3, 0.35, 0.4, 1.0)
bsdf.inputs['Roughness'].default_value = 0.7
material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
return material
def create_sports_facilities(location):
"""创建体育设施"""
# 操场
bpy.ops.mesh.primitive_cylinder_add(
location=(location[0], location[1], 0.1),
radius=15, depth=0.2
)
field = bpy.context.object
field.name = "操场"
# 操场材质 - 绿色
field_mat = bpy.data.materials.new("操场材质")
field_mat.use_nodes = True
field_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (0.2, 0.6, 0.3, 1.0)
field.data.materials.append(field_mat)
# 体育馆
bpy.ops.mesh.primitive_cube_add(location=(location[0] + 20, location[1], 5))
gym = bpy.context.object
gym.scale = (10, 15, 5)
gym.name = "体育馆"
def create_walkway_system():
"""创建校园连廊系统"""
# 连接主要建筑的连廊
connections = [
((0, 6, 5), (25, 20, 5)), # 主楼到科技楼
((0, -6, 5), (-25, 15, 5)), # 主楼到实验楼
((0, -6, 5), (-15, -25, 5)) # 主楼到图书馆
]
for start, end in connections:
# 计算连廊方向和长度
direction = mathutils.Vector(end) - mathutils.Vector(start)
length = direction.length
center = (mathutils.Vector(start) + mathutils.Vector(end)) / 2
bpy.ops.mesh.primitive_cube_add(location=center)
walkway = bpy.context.object
walkway.scale = (2, length/2, 1)
# 旋转到正确方向
walkway.
rotation_euler.
z = math.
atan2(direction.
y, direction.
x)
# 连廊材质
walkway_mat = bpy.data.materials.new("连廊材质")
walkway_mat.use_nodes = True
walkway_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (0.9, 0.9, 0.9, 1.0)
walkway.data.materials.append(walkway_mat)
def create_campus_landscape():
"""创建校园景观"""
# 地面
bpy.ops.mesh.primitive_plane_add(size=200, location=(0, 0, 0))
ground = bpy.context.object
ground.name = "校园地面"
ground_mat = bpy.data.materials.new("地面材质")
ground_mat.use_nodes = True
ground_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (0.3, 0.5, 0.2, 1.0)
ground.data.materials.append(ground_mat)
# 添加树木
for i in range(30):
x = random.uniform(-80, 80)
y = random.uniform(-80, 80)
# 避开建筑区域
if abs(x
) < 50 and
abs(y
) < 50: continue
bpy.ops.mesh.primitive_cone_add(
location=(x, y, 2),
radius1=1.5, radius2=0, depth=4
)
tree = bpy.context.object
tree.name = f"树木_{i+1}"
# 树木材质
tree_mat = bpy.data.materials.new("树木材质")
tree_mat.use_nodes = True
tree_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (0.1, 0.4, 0.1, 1.0)
tree.data.materials.append(tree_mat)
def setup_camera_and_lighting():
"""设置相机和照明"""
# 相机
bpy.ops.object.camera_add(location=(100, -100, 80))
camera = bpy.context.object
camera.rotation_euler = (math.radians(60), 0, math.radians(45))
bpy.context.scene.camera = camera
# 照明
bpy.ops.object.light_add(type='SUN', location=(50, -50, 50))
sun = bpy.context.object
sun.data.energy = 3
bpy.ops.object.light_add(type='AREA', location=(0, 0, 30))
area_light = bpy.context.object
area_light.data.energy = 200
def main():
"""主函数"""
print("开始创建深圳中学高中园...")
# 清理场景
clear_scene()
# 创建校园建筑
create_modern_school_campus()
# 设置相机和照明
setup_camera_and_lighting()
print("深圳中学高中园创建完成!")
print("包含设施:")
print("- 现代化主教学楼")
print("- 科技楼与实验楼")
print("- 宿舍楼群")
print("- 图书馆")
print("- 体育场馆")
print("- 校园连廊系统")
print("- 绿化景观")
# 运行脚本
if __name__ == "__main__":
main()