fork download
  1. import bpy
  2. import bmesh
  3. import mathutils
  4. import math
  5.  
  6. def debug_print(message):
  7. """调试信息输出"""
  8. print(f"[DEBUG] {message}")
  9.  
  10. def clear_scene():
  11. """安全清理场景"""
  12. debug_print("开始清理场景...")
  13. try:
  14. # 切换到对象模式
  15. if bpy.context.object and bpy.context.object.mode != 'OBJECT':
  16. bpy.ops.object.mode_set(mode='OBJECT')
  17.  
  18. # 选择所有对象并删除
  19. bpy.ops.object.select_all(action='SELECT')
  20. bpy.ops.object.delete(use_global=False)
  21.  
  22. # 清理数据块
  23. for block in bpy.data.meshes:
  24. if block.users == 0:
  25. bpy.data.meshes.remove(block)
  26.  
  27. for block in bpy.data.materials:
  28. if block.users == 0:
  29. bpy.data.materials.remove(block)
  30.  
  31. debug_print("场景清理完成")
  32. return True
  33. except Exception as e:
  34. debug_print(f"清理场景失败: {e}")
  35. return False
  36.  
  37. def create_simple_material(name, color, metallic=0.0, roughness=0.5):
  38. """创建简化材质"""
  39. try:
  40. material = bpy.data.materials.new(name)
  41. material.use_nodes = True
  42. nodes = material.node_tree.nodes
  43.  
  44. # 清除默认节点
  45. nodes.clear()
  46.  
  47. # 创建BSDF和输出节点
  48. bsdf = nodes.new(type='ShaderNodeBsdfPrincipled')
  49. output = nodes.new(type='ShaderNodeOutputMaterial')
  50.  
  51. # 设置材质属性
  52. bsdf.inputs['Base Color'].default_value = (*color, 1.0)
  53. bsdf.inputs['Metallic'].default_value = metallic
  54. bsdf.inputs['Roughness'].default_value = roughness
  55.  
  56. # 连接节点
  57. material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
  58.  
  59. debug_print(f"材质 '{name}' 创建成功")
  60. return material
  61. except Exception as e:
  62. debug_print(f"创建材质失败: {e}")
  63. return None
  64.  
  65. def create_main_building():
  66. """创建主教学楼 - 简化版"""
  67. debug_print("创建主教学楼...")
  68. try:
  69. bpy.ops.mesh.primitive_cube_add(location=(0, 0, 12))
  70. building = bpy.context.object
  71. building.scale = (30, 10, 12) # 缩小尺寸以便观察
  72. building.name = "Main_Building"
  73.  
  74. # 添加简单材质
  75. material = create_simple_material("Building_Mat", (0.8, 0.9, 1.0))
  76. if material:
  77. building.data.materials.append(material)
  78.  
  79. debug_print("主教学楼创建成功")
  80. return building
  81. except Exception as e:
  82. debug_print(f"创建主教学楼失败: {e}")
  83. return None
  84.  
  85. def create_tech_dome():
  86. """创建科技中心 - 简化版"""
  87. debug_print("创建科技中心...")
  88. try:
  89. bpy.ops.mesh.primitive_uv_sphere_add(location=(40, 0, 7.5), radius=12)
  90. dome = bpy.context.object
  91. dome.scale = (1, 1, 0.3)
  92. dome.name = "Tech_Dome"
  93.  
  94. # 添加金属材质
  95. material = create_simple_material("Dome_Mat", (0.3, 0.3, 0.4), metallic=0.8)
  96. if material:
  97. dome.data.materials.append(material)
  98.  
  99. debug_print("科技中心创建成功")
  100. return dome
  101. except Exception as e:
  102. debug_print(f"创建科技中心失败: {e}")
  103. return None
  104.  
  105. def create_simple_fence():
  106. """创建简化围墙"""
  107. debug_print("创建围墙系统...")
  108. fences = []
  109.  
  110. try:
  111. # 只创建基础的围墙
  112. fence_locations = [
  113. (0, 50, 1.5), # 北
  114. (0, -50, 1.5), # 南
  115. (50, 0, 1.5), # 东
  116. (-50, 0, 1.5) # 西
  117. ]
  118.  
  119. for i, loc in enumerate(fence_locations):
  120. bpy.ops.mesh.primitive_cube_add(location=loc)
  121. fence = bpy.context.object
  122. fence.scale = (25, 1.5, 1.5)
  123. fence.name = f"Fence_{i+1}"
  124. fences.append(fence)
  125.  
  126. # 添加材质
  127. material = create_simple_material("Fence_Mat", (0.5, 0.5, 0.5))
  128. if material:
  129. fence.data.materials.append(material)
  130.  
  131. debug_print("围墙系统创建成功")
  132. return fences
  133. except Exception as e:
  134. debug_print(f"创建围墙失败: {e}")
  135. return []
  136.  
  137. def create_ground():
  138. """创建地面"""
  139. debug_print("创建地面...")
  140. try:
  141. bpy.ops.mesh.primitive_plane_add(size=200, location=(0, 0, 0))
  142. ground = bpy.context.object
  143. ground.name = "Ground"
  144.  
  145. # 添加地面材质
  146. material = create_simple_material("Ground_Mat", (0.2, 0.6, 0.3), roughness=0.9)
  147. if material:
  148. ground.data.materials.append(material)
  149.  
  150. debug_print("地面创建成功")
  151. return ground
  152. except Exception as e:
  153. debug_print(f"创建地面失败: {e}")
  154. return None
  155.  
  156. def setup_camera():
  157. """设置相机"""
  158. debug_print("设置相机...")
  159. try:
  160. bpy.ops.object.camera_add(location=(80, -80, 60))
  161. camera = bpy.context.object
  162. camera.rotation_euler = (math.radians(60), 0, math.radians(45))
  163. bpy.context.scene.camera = camera
  164. debug_print("相机设置成功")
  165. return camera
  166. except Exception as e:
  167. debug_print(f"设置相机失败: {e}")
  168. return None
  169.  
  170. def setup_lighting():
  171. """设置基础照明"""
  172. debug_print("设置照明...")
  173. try:
  174. # 添加太阳光
  175. bpy.ops.object.light_add(type='SUN', location=(50, -50, 50))
  176. sun = bpy.context.object
  177. sun.data.energy = 3
  178. sun.name = "Sun_Light"
  179.  
  180. debug_print("照明设置成功")
  181. return sun
  182. except Exception as e:
  183. debug_print(f"设置照明失败: {e}")
  184. return None
  185.  
  186. def main():
  187. """主执行函数"""
  188. debug_print("=== 开始创建深圳中学3D模型 ===")
  189.  
  190. # 步骤1: 清理场景
  191. if not clear_scene():
  192. debug_print("无法继续,场景清理失败")
  193. return
  194.  
  195. # 步骤2: 创建地面
  196. ground = create_ground()
  197. if not ground:
  198. debug_print("地面创建失败,但继续执行...")
  199.  
  200. # 步骤3: 创建主建筑
  201. building = create_main_building()
  202. if not building:
  203. debug_print("主建筑创建失败,但继续执行...")
  204.  
  205. # 步骤4: 创建科技中心
  206. dome = create_tech_dome()
  207. if not dome:
  208. debug_print("科技中心创建失败,但继续执行...")
  209.  
  210. # 步骤5: 创建围墙
  211. fences = create_simple_fence()
  212. if not fences:
  213. debug_print("围墙创建失败,但继续执行...")
  214.  
  215. # 步骤6: 设置相机和照明
  216. camera = setup_camera()
  217. lights = setup_lighting()
  218.  
  219. # 最终状态报告
  220. debug_print("=== 模型创建完成 ===")
  221. objects_created = len([obj for obj in [ground, building, dome, camera, lights] if obj is not None])
  222. debug_print(f"成功创建对象: {objects_created}/6")
  223.  
  224. if objects_created > 0:
  225. debug_print("✅ 基础模型创建成功!")
  226. debug_print("💡 提示: 可以在3D视图中查看模型,使用鼠标中键旋转视角")
  227. else:
  228. debug_print("❌ 所有对象创建失败,请检查错误信息")
  229.  
  230. # 运行脚本
  231. if __name__ == "__main__":
  232. main()
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: near line 1: near "import": syntax error