fork download
  1. 深圳中学高中园 - Blender建筑生成代码
  2.  
  3. 基于"深圳中学高中园"的概念,我将创建一个现代化校园建筑的Blender生成代码:
  4.  
  5. ```python
  6. import bpy
  7. import bmesh
  8. import mathutils
  9. import math
  10. import random
  11.  
  12. def clear_scene():
  13. """清理场景"""
  14. bpy.ops.object.select_all(action='SELECT')
  15. bpy.ops.object.delete(use_global=False)
  16.  
  17. def create_modern_school_campus():
  18. """创建现代化校园建筑群"""
  19.  
  20. # 主教学楼 - 现代化设计
  21. print("创建主教学楼...")
  22. bpy.ops.mesh.primitive_cube_add(location=(0, 0, 15))
  23. main_building = bpy.context.object
  24. main_building.scale = (40, 12, 15)
  25. main_building.name = "主教学楼"
  26.  
  27. # 添加现代建筑材质
  28. main_mat = create_modern_facade_material("主楼外墙", (0.9, 0.9, 0.95))
  29. main_building.data.materials.append(main_mat)
  30.  
  31. # 添加窗户细节
  32. add_building_windows(main_building, rows=6, columns=8)
  33.  
  34. # 科技楼 - 弧形设计
  35. print("创建科技楼...")
  36. create_tech_building((30, 25, 8))
  37.  
  38. # 实验楼 - 模块化设计
  39. print("创建实验楼...")
  40. create_lab_building((-25, 20, 6))
  41.  
  42. # 行政楼
  43. print("创建行政楼...")
  44. bpy.ops.mesh.primitive_cube_add(location=(-30, -20, 8))
  45. admin_building = bpy.context.object
  46. admin_building.scale = (10, 8, 8)
  47. admin_building.name = "行政楼"
  48.  
  49. # 宿舍楼群
  50. print("创建宿舍楼群...")
  51. create_dormitory_complex((20, -25))
  52.  
  53. # 图书馆 - 标志性建筑
  54. print("创建图书馆...")
  55. create_library_building((-15, -30, 10))
  56.  
  57. # 体育场馆
  58. print("创建体育场馆...")
  59. create_sports_facilities((0, 40))
  60.  
  61. # 连廊系统
  62. print("创建连廊系统...")
  63. create_walkway_system()
  64.  
  65. # 校园景观
  66. print("创建校园景观...")
  67. create_campus_landscape()
  68.  
  69. def create_modern_facade_material(name, color):
  70. """创建现代建筑外墙材质"""
  71. material = bpy.data.materials.new(name)
  72. material.use_nodes = True
  73. nodes = material.node_tree.nodes
  74. nodes.clear()
  75.  
  76. # 主BSDF节点
  77. bsdf = nodes.new('ShaderNodeBsdfPrincipled')
  78. output = nodes.new('ShaderNodeOutputMaterial')
  79.  
  80. # 设置现代建筑材质
  81. bsdf.inputs['Base Color'].default_value = (*color, 1.0)
  82. bsdf.inputs['Metallic'].default_value = 0.1
  83. bsdf.inputs['Roughness'].default_value = 0.3
  84. bsdf.inputs['Specular'].default_value = 0.5
  85.  
  86. # 添加玻璃效果
  87. bsdf.inputs['Transmission'].default_value = 0.2
  88.  
  89. material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
  90.  
  91. return material
  92.  
  93. def add_building_windows(building, rows=5, columns=10):
  94. """为建筑添加窗户细节"""
  95. building_dimensions = building.dimensions
  96. window_size = (1.5, 0.2, 1.0) # 窗户尺寸
  97.  
  98. for i in range(rows):
  99. for j in range(columns):
  100. # 计算窗户位置(建筑表面)
  101. x_pos = (j - columns/2 + 0.5) * (building_dimensions.x / columns)
  102. z_pos = (i + 0.5) * (building_dimensions.z / (rows + 1))
  103.  
  104. # 前后两面都添加窗户
  105. for y_multiplier in [-1, 1]:
  106. y_pos = y_multiplier * (building_dimensions.y / 2 + 0.1)
  107.  
  108. bpy.ops.mesh.primitive_cube_add(
  109. location=(x_pos, y_pos, z_pos)
  110. )
  111. window = bpy.context.object
  112. window.scale = window_size
  113. window.name = f"窗户_{i}_{j}"
  114.  
  115. # 窗户材质
  116. window_mat = bpy.data.materials.new("窗户材质")
  117. window_mat.use_nodes = True
  118. nodes = window_mat.node_tree.nodes
  119. nodes.clear()
  120.  
  121. bsdf = nodes.new('ShaderNodeBsdfPrincipled')
  122. output = nodes.new('ShaderNodeOutputMaterial')
  123.  
  124. # 蓝色玻璃效果
  125. bsdf.inputs['Base Color'].default_value = (0.6, 0.7, 0.9, 1.0)
  126. bsdf.inputs['Metallic'].default_value = 0.0
  127. bsdf.inputs['Roughness'].default_value = 0.1
  128. bsdf.inputs['Transmission'].default_value = 0.8
  129.  
  130. window_mat.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
  131. window.data.materials.append(window_mat)
  132.  
  133. def create_tech_building(location):
  134. """创建科技楼 - 弧形现代设计"""
  135. # 主楼体
  136. bpy.ops.mesh.primitive_cube_add(location=location)
  137. tech_building = bpy.context.object
  138. tech_building.scale = (8, 8, 12)
  139. tech_building.name = "科技楼"
  140.  
  141. # 弧形顶部结构
  142. bpy.ops.mesh.primitive_cylinder_add(
  143. location=(location[0], location[1], location[2] + 12),
  144. radius=6, depth=2
  145. )
  146. dome = bpy.context.object
  147. dome.name = "科技楼穹顶"
  148.  
  149. # 科技感材质
  150. tech_mat = create_tech_material()
  151. tech_building.data.materials.append(tech_mat)
  152. dome.data.materials.append(tech_mat)
  153.  
  154. def create_tech_material():
  155. """创建科技感材质"""
  156. material = bpy.data.materials.new("科技材质")
  157. material.use_nodes = True
  158. nodes = material.node_tree.nodes
  159. nodes.clear()
  160.  
  161. bsdf = nodes.new('ShaderNodeBsdfPrincipled')
  162. output = nodes.new('ShaderNodeOutputMaterial')
  163.  
  164. # 银色科技感
  165. bsdf.inputs['Base Color'].default_value = (0.7, 0.7, 0.8, 1.0)
  166. bsdf.inputs['Metallic'].default_value = 0.8
  167. bsdf.inputs['Roughness'].default_value = 0.2
  168.  
  169. material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
  170.  
  171. return material
  172.  
  173. def create_lab_building(location):
  174. """创建实验楼 - 模块化设计"""
  175. # 创建多个模块化建筑体
  176. modules = [
  177. ((-3, 0, 0), (6, 8, 10)),
  178. ((3, 0, 0), (6, 8, 8)),
  179. ((0, 8, 0), (12, 6, 6))
  180. ]
  181.  
  182. for i, (mod_location, mod_scale) in enumerate(modules):
  183. bpy.ops.mesh.primitive_cube_add(
  184. location=(
  185. location[0] + mod_location[0],
  186. location[1] + mod_location[1],
  187. location[2] + mod_location[2]
  188. )
  189. )
  190. module = bpy.context.object
  191. module.scale = mod_scale
  192. module.name = f"实验楼模块_{i+1}"
  193.  
  194. # 实验楼材质
  195. lab_mat = create_lab_material()
  196. module.data.materials.append(lab_mat)
  197.  
  198. def create_lab_material():
  199. """创建实验楼材质"""
  200. material = bpy.data.materials.new("实验楼材质")
  201. material.use_nodes = True
  202. nodes = material.node_tree.nodes
  203. nodes.clear()
  204.  
  205. bsdf = nodes.new('ShaderNodeBsdfPrincipled')
  206. output = nodes.new('ShaderNodeOutputMaterial')
  207.  
  208. # 浅灰色调
  209. bsdf.inputs['Base Color'].default_value = (0.8, 0.85, 0.9, 1.0)
  210. bsdf.inputs['Roughness'].default_value = 0.4
  211.  
  212. material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
  213.  
  214. return material
  215.  
  216. def create_dormitory_complex(location):
  217. """创建宿舍楼群"""
  218. # 创建多栋宿舍楼
  219. for i in range(3):
  220. for j in range(2):
  221. x_pos = location[0] + i * 15
  222. y_pos = location[1] + j * 20
  223.  
  224. bpy.ops.mesh.primitive_cube_add(location=(x_pos, y_pos, 8))
  225. dorm = bpy.context.object
  226. dorm.scale = (5, 8, 8)
  227. dorm.name = f"宿舍楼_{i+1}_{j+1}"
  228.  
  229. # 宿舍楼材质
  230. dorm_mat = create_dormitory_material()
  231. dorm.data.materials.append(dorm_mat)
  232.  
  233. # 添加阳台细节
  234. add_balconies(dorm)
  235.  
  236. def create_dormitory_material():
  237. """创建宿舍楼材质"""
  238. material = bpy.data.materials.new("宿舍楼材质")
  239. material.use_nodes = True
  240. nodes = material.node_tree.nodes
  241. nodes.clear()
  242.  
  243. bsdf = nodes.new('ShaderNodeBsdfPrincipled')
  244. output = nodes.new('ShaderNodeOutputMaterial')
  245.  
  246. # 暖色调
  247. bsdf.inputs['Base Color'].default_value = (0.95, 0.92, 0.85, 1.0)
  248. bsdf.inputs['Roughness'].default_value = 0.6
  249.  
  250. material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
  251.  
  252. return material
  253.  
  254. def add_balconies(building):
  255. """为建筑添加阳台"""
  256. building_dimensions = building.dimensions
  257.  
  258. for floor in range(4): # 4层阳台
  259. z_pos = 2 + floor * 2
  260.  
  261. bpy.ops.mesh.primitive_cube_add(
  262. location=(0, building_dimensions.y/2 + 0.5, z_pos)
  263. )
  264. balcony = bpy.context.object
  265. balcony.scale = (building_dimensions.x/2 - 0.2, 1, 0.3)
  266. balcony.name = f"{building.name}_阳台_{floor+1}"
  267.  
  268. def create_library_building(location):
  269. """创建图书馆 - 标志性建筑"""
  270. # 主楼体
  271. bpy.ops.mesh.primitive_cube_add(location=location)
  272. library = bpy.context.object
  273. library.scale = (12, 15, 6)
  274. library.name = "图书馆"
  275.  
  276. # 特色屋顶
  277. bpy.ops.mesh.primitive_cone_add(
  278. location=(location[0], location[1], location[2] + 8),
  279. radius1=8, radius2=4, depth=4
  280. )
  281. roof = bpy.context.object
  282. roof.name = "图书馆屋顶"
  283.  
  284. # 图书馆材质
  285. library_mat = create_library_material()
  286. library.data.materials.append(library_mat)
  287. roof.data.materials.append(library_mat)
  288.  
  289. def create_library_material():
  290. """创建图书馆材质"""
  291. material = bpy.data.materials.new("图书馆材质")
  292. material.use_nodes = True
  293. nodes = material.node_tree.nodes
  294. nodes.clear()
  295.  
  296. bsdf = nodes.new('ShaderNodeBsdfPrincipled')
  297. output = nodes.new('ShaderNodeOutputMaterial')
  298.  
  299. # 深色稳重色调
  300. bsdf.inputs['Base Color'].default_value = (0.3, 0.35, 0.4, 1.0)
  301. bsdf.inputs['Roughness'].default_value = 0.7
  302.  
  303. material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
  304.  
  305. return material
  306.  
  307. def create_sports_facilities(location):
  308. """创建体育设施"""
  309. # 操场
  310. bpy.ops.mesh.primitive_cylinder_add(
  311. location=(location[0], location[1], 0.1),
  312. radius=15, depth=0.2
  313. )
  314. field = bpy.context.object
  315. field.name = "操场"
  316.  
  317. # 操场材质 - 绿色
  318. field_mat = bpy.data.materials.new("操场材质")
  319. field_mat.use_nodes = True
  320. field_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (0.2, 0.6, 0.3, 1.0)
  321. field.data.materials.append(field_mat)
  322.  
  323. # 体育馆
  324. bpy.ops.mesh.primitive_cube_add(location=(location[0] + 20, location[1], 5))
  325. gym = bpy.context.object
  326. gym.scale = (10, 15, 5)
  327. gym.name = "体育馆"
  328.  
  329. def create_walkway_system():
  330. """创建校园连廊系统"""
  331. # 连接主要建筑的连廊
  332. connections = [
  333. ((0, 6, 5), (25, 20, 5)), # 主楼到科技楼
  334. ((0, -6, 5), (-25, 15, 5)), # 主楼到实验楼
  335. ((0, -6, 5), (-15, -25, 5)) # 主楼到图书馆
  336. ]
  337.  
  338. for start, end in connections:
  339. # 计算连廊方向和长度
  340. direction = mathutils.Vector(end) - mathutils.Vector(start)
  341. length = direction.length
  342. center = (mathutils.Vector(start) + mathutils.Vector(end)) / 2
  343.  
  344. bpy.ops.mesh.primitive_cube_add(location=center)
  345. walkway = bpy.context.object
  346. walkway.scale = (2, length/2, 1)
  347.  
  348. # 旋转到正确方向
  349. walkway.rotation_euler.z = math.atan2(direction.y, direction.x)
  350.  
  351. # 连廊材质
  352. walkway_mat = bpy.data.materials.new("连廊材质")
  353. walkway_mat.use_nodes = True
  354. walkway_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (0.9, 0.9, 0.9, 1.0)
  355. walkway.data.materials.append(walkway_mat)
  356.  
  357. def create_campus_landscape():
  358. """创建校园景观"""
  359. # 地面
  360. bpy.ops.mesh.primitive_plane_add(size=200, location=(0, 0, 0))
  361. ground = bpy.context.object
  362. ground.name = "校园地面"
  363.  
  364. ground_mat = bpy.data.materials.new("地面材质")
  365. ground_mat.use_nodes = True
  366. ground_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (0.3, 0.5, 0.2, 1.0)
  367. ground.data.materials.append(ground_mat)
  368.  
  369. # 添加树木
  370. for i in range(30):
  371. x = random.uniform(-80, 80)
  372. y = random.uniform(-80, 80)
  373.  
  374. # 避开建筑区域
  375. if abs(x) < 50 and abs(y) < 50:
  376. continue
  377.  
  378. bpy.ops.mesh.primitive_cone_add(
  379. location=(x, y, 2),
  380. radius1=1.5, radius2=0, depth=4
  381. )
  382. tree = bpy.context.object
  383. tree.name = f"树木_{i+1}"
  384.  
  385. # 树木材质
  386. tree_mat = bpy.data.materials.new("树木材质")
  387. tree_mat.use_nodes = True
  388. tree_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (0.1, 0.4, 0.1, 1.0)
  389. tree.data.materials.append(tree_mat)
  390.  
  391. def setup_camera_and_lighting():
  392. """设置相机和照明"""
  393. # 相机
  394. bpy.ops.object.camera_add(location=(100, -100, 80))
  395. camera = bpy.context.object
  396. camera.rotation_euler = (math.radians(60), 0, math.radians(45))
  397. bpy.context.scene.camera = camera
  398.  
  399. # 照明
  400. bpy.ops.object.light_add(type='SUN', location=(50, -50, 50))
  401. sun = bpy.context.object
  402. sun.data.energy = 3
  403.  
  404. bpy.ops.object.light_add(type='AREA', location=(0, 0, 30))
  405. area_light = bpy.context.object
  406. area_light.data.energy = 200
  407.  
  408. def main():
  409. """主函数"""
  410. print("开始创建深圳中学高中园...")
  411.  
  412. # 清理场景
  413. clear_scene()
  414.  
  415. # 创建校园建筑
  416. create_modern_school_campus()
  417.  
  418. # 设置相机和照明
  419. setup_camera_and_lighting()
  420.  
  421. print("深圳中学高中园创建完成!")
  422. print("包含设施:")
  423. print("- 现代化主教学楼")
  424. print("- 科技楼与实验楼")
  425. print("- 宿舍楼群")
  426. print("- 图书馆")
  427. print("- 体育场馆")
  428. print("- 校园连廊系统")
  429. print("- 绿化景观")
  430.  
  431. # 运行脚本
  432. if __name__ == "__main__":
  433. main()
Success #stdin #stdout #stderr 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 1: near "深圳中学高中园": syntax error