0xZohar commited on
Commit
f5175a7
·
verified ·
1 Parent(s): 1c36423

Upload code/cube3d/render/render_bricks_safe.py with huggingface_hub

Browse files
code/cube3d/render/render_bricks_safe.py CHANGED
@@ -212,30 +212,16 @@ try:
212
  scene.camera = bpy.context.active_object
213
 
214
  scene.render.engine = 'CYCLES'
215
- scene.cycles.samples = 512
216
- scene.cycles.device = 'GPU'
217
  scene.render.image_settings.file_format = 'PNG'
218
  scene.render.filepath = "{output_file}"
219
  scene.render.use_overwrite = True
220
 
221
- if sys.platform == 'darwin':
222
- compute_device_type = 'METAL'
223
- else:
224
- compute_device_type = 'CUDA'
225
-
226
- print("配置GPU...")
227
- bpy.context.preferences.addons['cycles'].preferences.compute_device_type = compute_device_type
228
- bpy.context.preferences.addons['cycles'].preferences.get_devices()
229
- gpu_used = False
230
- for device in bpy.context.preferences.addons['cycles'].preferences.devices:
231
- if device['name'].startswith(('NVIDIA', 'Apple')):
232
- device['use'] = 1
233
- gpu_used = True
234
- print("启用GPU: " + device['name'])
235
- else:
236
- device['use'] = 0
237
- if not gpu_used:
238
- print("警告:未找到可用GPU,使用CPU渲染")
239
 
240
  print("开始渲染...")
241
  result = bpy.ops.render.render(write_still=True)
@@ -299,19 +285,23 @@ except Exception as e:
299
  except Exception as test_e:
300
  print(f"Blender测试失败: {test_e}")
301
 
302
- # 在 Linux 上使用 xvfb-run 以支持无头渲染
303
  if platform.system() == 'Linux' and os.path.exists('/usr/bin/xvfb-run'):
304
  cmd = [
305
- 'xvfb-run', '-a', '-s', '-screen 0 1024x768x24',
306
- BLENDER_PATH, '--background',
 
 
 
307
  '--python', script_path
308
  ]
309
- print("使用 xvfb-run 进行无头渲染")
310
  else:
311
  cmd = [
312
  BLENDER_PATH, '--background',
313
  '--python', script_path
314
  ]
 
315
 
316
  print(f"执行命令: {' '.join(cmd)}")
317
  print(f"脚本路径: {script_path}")
 
212
  scene.camera = bpy.context.active_object
213
 
214
  scene.render.engine = 'CYCLES'
215
+ scene.cycles.samples = 64 # 降低采样数加快 CPU 渲染
216
+ scene.cycles.device = 'CPU' # 强制 CPU 渲染(云平台更稳定)
217
  scene.render.image_settings.file_format = 'PNG'
218
  scene.render.filepath = "{output_file}"
219
  scene.render.use_overwrite = True
220
 
221
+ print("ℹ️ 使用 CPU 渲染(samples=64,云平台优化配置)")
222
+
223
+ # GPU 配置已禁用(HuggingFace Spaces 不支持图形 GPU)
224
+ # 如需 GPU 渲染,请部署到支持 CUDA 图形能力的平台
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
  print("开始渲染...")
227
  result = bpy.ops.render.render(write_still=True)
 
285
  except Exception as test_e:
286
  print(f"Blender测试失败: {test_e}")
287
 
288
+ # 在 Linux 上使用 xvfb-run 以支持无头渲染(含 GLX 扩展)
289
  if platform.system() == 'Linux' and os.path.exists('/usr/bin/xvfb-run'):
290
  cmd = [
291
+ 'xvfb-run',
292
+ '-a', # 自动选择显示编号
293
+ '--server-args', '-screen 0 1024x768x24 -ac +extension GLX +extension RENDER +render -noreset',
294
+ BLENDER_PATH,
295
+ '--background',
296
  '--python', script_path
297
  ]
298
+ print("使用 xvfb-run 进行无头渲染(GLX 扩展已启用)")
299
  else:
300
  cmd = [
301
  BLENDER_PATH, '--background',
302
  '--python', script_path
303
  ]
304
+ print("⚠️ 直接运行 Blender(无 xvfb)")
305
 
306
  print(f"执行命令: {' '.join(cmd)}")
307
  print(f"脚本路径: {script_path}")