import bpy
import mathutils
import os
def setup_high_quality_render():
"""设置高质量渲染"""
# 设置渲染引擎为Cycles
bpy.context.scene.render.engine = 'CYCLES'
# 设置设备为GPU(如果可用)
bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'CUDA'
bpy.context.scene.cycles.device = 'GPU'
# 启用所有可用的GPU
for device in bpy.context.preferences.addons['cycles'].preferences.devices:
device.use = True
# 设置采样
bpy.context.scene.cycles.samples = 256
bpy.context.scene.cycles.preview_samples = 128
# 设置分辨率
bpy.context.scene.render.resolution_x = 1920
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.resolution_percentage = 100
# 设置输出格式
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.image_settings.color_mode = 'RGBA'
bpy.context.scene.render.image_settings.compression = 100
def create_advanced_materials():
"""创建高级材质"""
# 玻璃材质
glass_mat = bpy.data.materials.new("Advanced_Glass")
glass_mat.use_nodes = True
nodes = glass_mat.node_tree.nodes
nodes.clear()
# 创建玻璃材质节点
output = nodes.new(type='ShaderNodeOutputMaterial')
glass_bsdf = nodes.new(type='ShaderNodeBsdfGlass')
mix_shader = nodes.new(type='ShaderNodeMixShader')
transparent = nodes.new(type='ShaderNodeBsdfTransparent')
# 设置玻璃参数
glass_bsdf.inputs['IOR'].default_value = 1.45
glass_bsdf.inputs['Roughness'].default_value = 0.02
glass_bsdf.inputs['Color'].default_value = (0.9, 0.95, 1.0, 1.0)
# 连接节点
links = glass_mat.node_tree.links
links.new(transparent.outputs['BSDF'], mix_shader.inputs[1])
links.new(glass_bsdf.outputs['BSDF'], mix_shader.inputs[2])
links.new(mix_shader.outputs['Shader'], output.inputs['Surface'])
return glass_mat
def setup_lighting():
"""设置专业照明"""
# 清除现有灯光
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.data.objects:
if obj.type == 'LIGHT':
obj.select_set(True)
bpy.ops.object.delete()
# 添加HDRI环境光
bpy.context.scene.world.use_nodes = True
world_nodes = bpy.context.scene.world.node_tree.nodes
world_links = bpy.context.scene.world.node_tree.links
# 清除默认节点
world_nodes.clear()
# 添加环境纹理节点
env_texture = world_nodes.new(type='ShaderNodeTexEnvironment')
background = world_nodes.new(type='ShaderNodeBackground')
output = world_nodes.new(type='ShaderNodeOutputWorld')
# 设置HDRI(这里使用内置的)
env_texture.image = bpy.data.images.new("HDRI", 1024, 1024)
world_links.new(env_texture.outputs['Color'], background.inputs['Color'])
world_links.new(background.outputs['Background'], output.inputs['Surface'])
# 添加主要灯光
bpy.ops.object.light_add(type='SUN', location=(50, -50, 100))
sun = bpy.context.object
sun.data.energy = 3
# 添加填充光
bpy.ops.object.light_add(type='AREA', location=(-30, 30, 50))
fill_light = bpy.context.object
fill_light.data.energy = 200
fill_light.data.size = 10
def render_school_model():
"""渲染学校模型"""
# 设置渲染
setup_high_quality_render()
setup_lighting()
# 设置相机
bpy.ops.object.camera_add(location=(150, -150, 80))
camera = bpy.context.object
camera.rotation_euler = (mathutils.Euler((math.radians(60), 0, math.radians(45)), 'XYZ'))
bpy.context.scene.camera = camera
# 设置输出路径
output_path = os.path.join(os.path.expanduser("~"), "shenzhen_school_render.png")
bpy.context.scene.render.filepath = output_path
# 开始渲染
print("开始渲染...")
bpy.ops.render.render(write_still=True)
print(f"渲染完成!图像保存至: {output_path}")
# 运行渲染(需要先有3D模型)
# render_school_model()