0

我目前正在使用 blender 3.0,但在运行某些渲染时遇到问题(blender 2.93 也会发生这种情况)。
我的团队有一个场景,我们通过 python 生成一些对象(40-50)(它们是某个集合中对象的副本),然后我们将其渲染并保存到一个文件中。每次渲染后,我们都会运行一些清理代码以避免内存问题(我们需要大约 10k 次渲染)。要执行清理代码并准备下一次渲染,一开始大约需要 0.05 秒,但在 1.5/2k 次渲染之后需要超过 5 分钟(随着渲染次数的增加,我们会在当前渲染和下一次渲染之间冻结更长的时间) )。重新启动搅拌机“将冻结持续时间”重置为 0 秒。
整体代码如下:

for idx in range(number_of_renders):
    camera.location = Vector(random, random, random)
    camera_target.location = Vector(random, random, random)
    ... # randomly set hide_viewport and hide_render to true/false to change overall brightness of all lights
    spawned_objects = populate_scene(settings)
    bpy.ops.render.render(write_still=True)
    metrics = generate_metrics(settings) # generate some metrics about facing objects
    save_metrics(metrics) # save the metrics in a .txt file
    cleanup(spawned_objects)

我们用来创建副本的代码:

templates = [template.name for template in bpy.data.collections.get("Templates").objects]
... # set random image to all templates textures
source_object = bpy.data.objects[random.choice(templates)] # get a random object from templates
product = source_object.copy()
product.data = source_object.data.copy()
product.location.x/y/z = ...
spawned_objects.append(product)
if facing_object: # if it is a facing object we add it into the facing collection (we measure some metrics later on)
    facing_collection.objects.link(product)
else:
    background_collection.objects.link(product)

我们用来清理生成对象的代码:

def cleanup(spawned_objects):
    # Deselect all eventually selected objects
    bpy.ops.object.select_all(action='DESELECT')

    # Select only the spawned objects
    for obj in spawned_objects:
        obj.select_set(True)

    # Delete spawned objects
    bpy.ops.object.delete()

    # Cleanup
    for block in bpy.data.meshes:
        if block.users == 0:
            bpy.data.meshes.remove(block)

    for block in bpy.data.materials:
        if block.users == 0:
            bpy.data.materials.remove(block)

    for block in bpy.data.textures:
        if block.users == 0:
            bpy.data.textures.remove(block)

    for block in bpy.data.images:
        if block.users == 0:
            bpy.data.images.remove(block)

我们目前正在通过运行该程序 10 次来“解决”这个问题,每次都以 1k 渲染为上限,然后我们将它们合并到一个目录中。关于我们为什么会冻结或为什么我们制作的渲染越多它变得越长的任何想法?

4

0 回答 0