You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blender脚本collection.all_objects遍历渲染无法覆盖全部集合物体问题

Blender集合遍历渲染仅输出部分文件问题分析与修复

问题诱因

  • 核心原因:Blender 中 collection.all_objects 属于内置的 bpy_prop_collection 动态集合类型,并非 Python 原生列表。遍历过程中如果修改集合内对象的属性(如本案例中修改 hide_render 状态),会触发 Blender 内部数据更新,导致迭代器异常提前终止,循环仅执行2次就退出,因此仅生成2个渲染文件。仅打印对象名称的场景下没有修改任何对象属性,迭代器不会被干扰,因此可以正常输出4个对象名。
  • 次要诱因:若封装的 Render_Scene 函数使用异步渲染接口,未等待单帧渲染完成就执行 obj1.hide_render = True 逻辑,也会导致部分对象渲染时已被隐藏,无法生成有效文件。

修复方案

  1. 遍历前先将 collection.all_objects 转换为 Python 原生列表,固定待遍历的对象列表,避免后续属性修改影响迭代过程。
  2. 确保渲染逻辑为同步执行,单帧渲染完成后再修改对象的隐藏状态。

修正后的代码示例:

import bpy

# 初始化所有网格对象为渲染隐藏状态
for obj in bpy.data.objects:
    if obj.type == "MESH":
        obj.hide_render = True

count = 0
collection1 = bpy.data.collections["Category1"]
# 关键:将动态集合转为原生列表,固定遍历对象
target_objects = list(collection1.all_objects)

for obj1 in target_objects:
    # 过滤非网格对象,避免异常
    if obj1.type != "MESH":
        continue
    obj1.hide_render = False
    # 若使用Blender原生渲染,需添加 write_still=True 保证同步渲染并写入文件
    # 示例原生渲染写法:
    # bpy.context.scene.render.filepath = f"//render_output/{count}.png"
    # bpy.ops.render.render(write_still=True)
    Render_Scene(str(count))
    obj1.hide_render = True
    count += 1

如果修改后仍有异常,可排查 Render_Scene 内部逻辑,确认输出路径是否每次更新、是否存在同名文件覆盖的问题。

内容的提问来源于stack exchange,提问作者Theodor Chichirita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 23:45:01