Blender rendering using Python gives black images but works on Blender Software

Hi All, I’m working on rendering some scenes using Blender and running into some issues. I’m using .blend files of Open-source animation movies like Charge and Sintel which has multiple .blend files like anim.blend, lighting.blend, etc, for one scene.

When I tried to render using anim.blend it is giving just a blank screen and when I append lighting.blend to anim.blend the rendered scene looks fine. This works in Blender Software but when I run it using a Python script it just gives a blank screen.

Here is the code:


import bpy
import time
import random
import os
import sys
from contextlib import contextmanager

@contextmanager
def stdout_redirected(to=os.devnull):
    '''
    import os

    with stdout_redirected(to=filename):
        print("from Python")
        os.system("echo non-Python applications are also supported")
    '''
    fd = sys.stdout.fileno()

    ##### assert that Python and C stdio write using the same file descriptor
    ####assert libc.fileno(ctypes.c_void_p.in_dll(libc, "stdout")) == fd == 1

    def _redirect_stdout(to):
        sys.stdout.close() # + implicit flush()
        os.dup2(to.fileno(), fd) # fd writes to 'to' file
        sys.stdout = os.fdopen(fd, 'w') # Python writes to fd

    with os.fdopen(os.dup(fd), 'w') as old_stdout:
        with open(to, 'w') as file:
            _redirect_stdout(to=file)
        try:
            yield # allow code to be run with the redirected stdout
        finally:
            _redirect_stdout(to=old_stdout) # restore stdout.
                                            # buffering and flags such as
                                            # CLOEXEC may be different


def set_scene_props():
    """ Set scene properties """

    scene = bpy.context.scene
    scene.render.resolution_percentage = 25
    # scene.frame_step = 5


def time_seed():
    seed = time.time()
    print(f"seed: {seed}")
    random.seed(seed)
    bpy.context.window_manager.clipboard = str(seed)
    return seed


def setup_scene():
    bpy.data.scenes["010_0050.anim"].render.filepath = "out_append_no_composite/####.png"
    bpy.ops.wm.append(
        filepath="010_0050.lighting.blend",
        directory="/scratch/rr3937/charge/010_0050/010_0050.lighting.blend\\Collection\\",
        filename="010_0050.lighting"
    )

    seed = 0
    random.seed(seed) if seed else time_seed()
    set_scene_props()

def enable_gpus(device_type, use_cpus=False):
    # # <----- To render on GPU ----->
    bpy.context.scene.render.use_sequencer = False

    bpy.context.scene.render.use_stamp = False
    bpy.context.scene.render.use_stamp_note = False

    preferences = bpy.context.preferences
    cycles_preferences = preferences.addons["cycles"].preferences
    cycles_preferences.refresh_devices()
    cuda_devices = cycles_preferences.devices[0]

    if device_type == "CUDA":
        devices = cuda_devices
    elif device_type == "OPENCL":
        devices = opencl_devices
    else:
        raise RuntimeError("Unsupported device type")

    activated_gpus = []
    devices = [devices]

    for device in devices:
        print("Device properties: {}".format(dir(device)))
        print("Device: {}".format(device.name))
        print("Device type: {}".format(device.type))
        if device.type == "CPU":
            device.use = use_cpus
        else:
            device.use = True
            activated_gpus.append(device.name)

    cycles_preferences.compute_device_type = device_type
    bpy.context.scene.cycles.device = "GPU"

    return activated_gpus


def main():
    # rendering on GPU
    activated_gpu_list = enable_gpus("CUDA")
    print("Activated GPUs: {}".format(activated_gpu_list))

    # Change the frame per second(FPS) of the scene and update the time stretching
    context = setup_scene()
    with stdout_redirected():
        bpy.ops.render.render(animation=True)

main()

Can you please tell me what am I missing in this script because it works fine on the software? Any help is appreciated.

I am not an expert in the code, but:
In setup_scene method I can see file settings, and I’ve got a question:
Have you tried to change path, directory and so on?
image

I have felt like it can’t find the lighting.blend. You can print theirs’s values in therminal to check if it found it.
You can make backup files, and than try to change those settings.

1 Like

Thank you. I was able to solve the issue.