fork download
  1. import bpy
  2. import mathutils
  3. import os
  4.  
  5. def setup_high_quality_render():
  6. """设置高质量渲染"""
  7. # 设置渲染引擎为Cycles
  8. bpy.context.scene.render.engine = 'CYCLES'
  9.  
  10. # 设置设备为GPU(如果可用)
  11. bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'CUDA'
  12. bpy.context.scene.cycles.device = 'GPU'
  13.  
  14. # 启用所有可用的GPU
  15. for device in bpy.context.preferences.addons['cycles'].preferences.devices:
  16. device.use = True
  17.  
  18. # 设置采样
  19. bpy.context.scene.cycles.samples = 256
  20. bpy.context.scene.cycles.preview_samples = 128
  21.  
  22. # 设置分辨率
  23. bpy.context.scene.render.resolution_x = 1920
  24. bpy.context.scene.render.resolution_y = 1080
  25. bpy.context.scene.render.resolution_percentage = 100
  26.  
  27. # 设置输出格式
  28. bpy.context.scene.render.image_settings.file_format = 'PNG'
  29. bpy.context.scene.render.image_settings.color_mode = 'RGBA'
  30. bpy.context.scene.render.image_settings.compression = 100
  31.  
  32. def create_advanced_materials():
  33. """创建高级材质"""
  34. # 玻璃材质
  35. glass_mat = bpy.data.materials.new("Advanced_Glass")
  36. glass_mat.use_nodes = True
  37. nodes = glass_mat.node_tree.nodes
  38. nodes.clear()
  39.  
  40. # 创建玻璃材质节点
  41. output = nodes.new(type='ShaderNodeOutputMaterial')
  42. glass_bsdf = nodes.new(type='ShaderNodeBsdfGlass')
  43. mix_shader = nodes.new(type='ShaderNodeMixShader')
  44. transparent = nodes.new(type='ShaderNodeBsdfTransparent')
  45.  
  46. # 设置玻璃参数
  47. glass_bsdf.inputs['IOR'].default_value = 1.45
  48. glass_bsdf.inputs['Roughness'].default_value = 0.02
  49. glass_bsdf.inputs['Color'].default_value = (0.9, 0.95, 1.0, 1.0)
  50.  
  51. # 连接节点
  52. links = glass_mat.node_tree.links
  53. links.new(transparent.outputs['BSDF'], mix_shader.inputs[1])
  54. links.new(glass_bsdf.outputs['BSDF'], mix_shader.inputs[2])
  55. links.new(mix_shader.outputs['Shader'], output.inputs['Surface'])
  56.  
  57. return glass_mat
  58.  
  59. def setup_lighting():
  60. """设置专业照明"""
  61. # 清除现有灯光
  62. bpy.ops.object.select_all(action='DESELECT')
  63. for obj in bpy.data.objects:
  64. if obj.type == 'LIGHT':
  65. obj.select_set(True)
  66. bpy.ops.object.delete()
  67.  
  68. # 添加HDRI环境光
  69. bpy.context.scene.world.use_nodes = True
  70. world_nodes = bpy.context.scene.world.node_tree.nodes
  71. world_links = bpy.context.scene.world.node_tree.links
  72.  
  73. # 清除默认节点
  74. world_nodes.clear()
  75.  
  76. # 添加环境纹理节点
  77. env_texture = world_nodes.new(type='ShaderNodeTexEnvironment')
  78. background = world_nodes.new(type='ShaderNodeBackground')
  79. output = world_nodes.new(type='ShaderNodeOutputWorld')
  80.  
  81. # 设置HDRI(这里使用内置的)
  82. env_texture.image = bpy.data.images.new("HDRI", 1024, 1024)
  83.  
  84. world_links.new(env_texture.outputs['Color'], background.inputs['Color'])
  85. world_links.new(background.outputs['Background'], output.inputs['Surface'])
  86.  
  87. # 添加主要灯光
  88. bpy.ops.object.light_add(type='SUN', location=(50, -50, 100))
  89. sun = bpy.context.object
  90. sun.data.energy = 3
  91.  
  92. # 添加填充光
  93. bpy.ops.object.light_add(type='AREA', location=(-30, 30, 50))
  94. fill_light = bpy.context.object
  95. fill_light.data.energy = 200
  96. fill_light.data.size = 10
  97.  
  98. def render_school_model():
  99. """渲染学校模型"""
  100. # 设置渲染
  101. setup_high_quality_render()
  102. setup_lighting()
  103.  
  104. # 设置相机
  105. bpy.ops.object.camera_add(location=(150, -150, 80))
  106. camera = bpy.context.object
  107. camera.rotation_euler = (mathutils.Euler((math.radians(60), 0, math.radians(45)), 'XYZ'))
  108. bpy.context.scene.camera = camera
  109.  
  110. # 设置输出路径
  111. output_path = os.path.join(os.path.expanduser("~"), "shenzhen_school_render.png")
  112. bpy.context.scene.render.filepath = output_path
  113.  
  114. # 开始渲染
  115. print("开始渲染...")
  116. bpy.ops.render.render(write_still=True)
  117. print(f"渲染完成!图像保存至: {output_path}")
  118.  
  119. # 运行渲染(需要先有3D模型)
  120. # render_school_model()
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