from time import sleep import bpy # Clear existing mesh objects bpy.ops.object.select_all(action='DESELECT') bpy.ops.object.select_by_type(type='MESH') bpy.ops.object.delete() # Delete the light bpy.ops.object.select_by_type(type='LIGHT') bpy.ops.object.delete() # Add a UV sphere bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0)) sphere = bpy.context.active_object # Set render engine to Cycles bpy.context.scene.render.engine = 'CYCLES' bpy.context.scene.cycles.samples = 256 bpy.context.scene.render.resolution_x = 1024 bpy.context.scene.render.resolution_y = 1024 bpy.context.scene.render.use_persistent_data = True bpy.context.scene.cycles.use_denoising = False # Enable GPU rendering if available bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'OPTIX' bpy.context.preferences.addons['cycles'].preferences.get_devices() # Ensure the correct device is used bpy.context.scene.cycles.device = 'GPU' # Set up a camera bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(0, -3, 1), rotation=(1.1, 0, 0)) camera = bpy.context.active_object bpy.context.scene.camera = camera # Set render output settings bpy.context.scene.render.image_settings.file_format = 'PNG' bpy.context.scene.render.filepath = "rendered_sphere.png" path_image0="abandoned_church.exr" image0 = bpy.data.images.load(path_image0) image0.pack() path_image1="abandoned_parking.exr" image1 = bpy.data.images.load(path_image1) image1.pack() # Set up the world world = bpy.data.worlds[0] world.use_nodes = True env_node = world.node_tree.nodes.new("ShaderNodeTexEnvironment") background_node = world.node_tree.nodes["Background"] world.node_tree.links.new(env_node.outputs["Color"], background_node.inputs["Color"]) env_node.image = image0 # # Save the file bpy.ops.wm.save_as_mainfile(filepath="memory_leak_repro.blend") # Render the scene for i in range(100): if i%2==0: env_node.image = image0 else: env_node.image = image1 # When commenting out this the leak does not trigger. This is enough to force an update of many items in the scene. bpy.context.scene.camera = camera bpy.ops.render.render(write_still=True) print("done") bpy.ops.wm.read_factory_settings(use_empty=True) sleep(10)