import bpy
import bmesh
import mathutils
import math
def debug_print(message):
"""调试信息输出"""
print(f"[DEBUG] {message}")
def clear_scene():
"""安全清理场景"""
debug_print("开始清理场景...")
try:
# 切换到对象模式
if bpy.context.object and bpy.context.object.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
# 选择所有对象并删除
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
# 清理数据块
for block in bpy.data.meshes:
if block.users == 0:
for block in bpy.data.materials:
if block.users == 0:
bpy.
data.
materials.
remove(block
)
debug_print("场景清理完成")
return True
except Exception as e:
debug_print(f"清理场景失败: {e}")
return False
def create_simple_material(name, color, metallic=0.0, roughness=0.5):
"""创建简化材质"""
try:
material = bpy.data.materials.new(name)
material.use_nodes = True
nodes = material.node_tree.nodes
# 清除默认节点
nodes.clear()
# 创建BSDF和输出节点
bsdf = nodes.new(type='ShaderNodeBsdfPrincipled')
output = nodes.new(type='ShaderNodeOutputMaterial')
# 设置材质属性
bsdf.inputs['Base Color'].default_value = (*color, 1.0)
bsdf.inputs['Metallic'].default_value = metallic
bsdf.inputs['Roughness'].default_value = roughness
# 连接节点
material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
debug_print(f"材质 '{name}' 创建成功")
return material
except Exception as e:
debug_print(f"创建材质失败: {e}")
return None
def create_main_building():
"""创建主教学楼 - 简化版"""
debug_print("创建主教学楼...")
try:
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 12))
building = bpy.context.object
building.scale = (30, 10, 12) # 缩小尺寸以便观察
building.name = "Main_Building"
# 添加简单材质
material = create_simple_material("Building_Mat", (0.8, 0.9, 1.0))
if material:
building.data.materials.append(material)
debug_print("主教学楼创建成功")
return building
except Exception as e:
debug_print(f"创建主教学楼失败: {e}")
return None
def create_tech_dome():
"""创建科技中心 - 简化版"""
debug_print("创建科技中心...")
try:
bpy.ops.mesh.primitive_uv_sphere_add(location=(40, 0, 7.5), radius=12)
dome = bpy.context.object
dome.scale = (1, 1, 0.3)
dome.name = "Tech_Dome"
# 添加金属材质
material = create_simple_material("Dome_Mat", (0.3, 0.3, 0.4), metallic=0.8)
if material:
dome.data.materials.append(material)
debug_print("科技中心创建成功")
return dome
except Exception as e:
debug_print(f"创建科技中心失败: {e}")
return None
def create_simple_fence():
"""创建简化围墙"""
debug_print("创建围墙系统...")
fences = []
try:
# 只创建基础的围墙
fence_locations = [
(0, 50, 1.5), # 北
(0, -50, 1.5), # 南
(50, 0, 1.5), # 东
(-50, 0, 1.5) # 西
]
for i, loc in enumerate(fence_locations):
bpy.ops.mesh.primitive_cube_add(location=loc)
fence = bpy.context.object
fence.scale = (25, 1.5, 1.5)
fence.name = f"Fence_{i+1}"
fences.append(fence)
# 添加材质
material = create_simple_material("Fence_Mat", (0.5, 0.5, 0.5))
if material:
fence.data.materials.append(material)
debug_print("围墙系统创建成功")
return fences
except Exception as e:
debug_print(f"创建围墙失败: {e}")
return []
def create_ground():
"""创建地面"""
debug_print("创建地面...")
try:
bpy.ops.mesh.primitive_plane_add(size=200, location=(0, 0, 0))
ground = bpy.context.object
ground.name = "Ground"
# 添加地面材质
material = create_simple_material("Ground_Mat", (0.2, 0.6, 0.3), roughness=0.9)
if material:
ground.data.materials.append(material)
debug_print("地面创建成功")
return ground
except Exception as e:
debug_print(f"创建地面失败: {e}")
return None
def setup_camera():
"""设置相机"""
debug_print("设置相机...")
try:
bpy.ops.object.camera_add(location=(80, -80, 60))
camera = bpy.context.object
camera.rotation_euler = (math.radians(60), 0, math.radians(45))
bpy.context.scene.camera = camera
debug_print("相机设置成功")
return camera
except Exception as e:
debug_print(f"设置相机失败: {e}")
return None
def setup_lighting():
"""设置基础照明"""
debug_print("设置照明...")
try:
# 添加太阳光
bpy.ops.object.light_add(type='SUN', location=(50, -50, 50))
sun = bpy.context.object
sun.data.energy = 3
sun.name = "Sun_Light"
debug_print("照明设置成功")
return sun
except Exception as e:
debug_print(f"设置照明失败: {e}")
return None
def main():
"""主执行函数"""
debug_print("=== 开始创建深圳中学3D模型 ===")
# 步骤1: 清理场景
if not clear_scene():
debug_print("无法继续,场景清理失败")
return
# 步骤2: 创建地面
ground = create_ground()
if not ground:
debug_print("地面创建失败,但继续执行...")
# 步骤3: 创建主建筑
building = create_main_building()
if not building:
debug_print("主建筑创建失败,但继续执行...")
# 步骤4: 创建科技中心
dome = create_tech_dome()
if not dome:
debug_print("科技中心创建失败,但继续执行...")
# 步骤5: 创建围墙
fences = create_simple_fence()
if not fences:
debug_print("围墙创建失败,但继续执行...")
# 步骤6: 设置相机和照明
camera = setup_camera()
lights = setup_lighting()
# 最终状态报告
debug_print("=== 模型创建完成 ===")
objects_created = len([obj for obj in [ground, building, dome, camera, lights] if obj is not None])
debug_print(f"成功创建对象: {objects_created}/6")
if objects_created > 0:
debug_print("✅ 基础模型创建成功!")
debug_print("💡 提示: 可以在3D视图中查看模型,使用鼠标中键旋转视角")
else:
debug_print("❌ 所有对象创建失败,请检查错误信息")
# 运行脚本
if __name__ == "__main__":
main()