2
$\begingroup$

I've looked all over this stack for other people's code bits but nothing has worked so far.

I want to run a bpy script. I have the following function which I call at the end of a for loop, after I change the scenes geometry, as I desire.

def render_image_indexed(output_dir, out_type, step, ani_length):
    string_order= int(np.ceil(np.log10(ani_length+1)))
    index_order = int(np.ceil(np.log10(step+1)))
    out_string = ""
    
    for i in range(string_order-index_order):
        out_string += "0"
    if (step != 0):
        out_string += str(int(step))
    bpy.context.scene.render.filepath = output_dir+out_string
    bpy.ops.render.render(write_still = True)

I have the following set within the bpy script:

#Render settings
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.device = 'GPU'

Which I hoped would solve the problem, despite having the same settings set within the Blender GUI. I know there is a _cycles package I can import with a _cycles.render(), but I can't find any documentation for this function on the internet. Hopefully you guys have a fish to give, because I'm having trouble catching my own.

Ideally this solution would not involve command line options, though I will welcome one if I must.

$\endgroup$
1
  • 1
    $\begingroup$ Do you have multiple scenes in the file? You would need to set the value for each scene, as in this answer But I don't understand why you want to change the settings from the user preferences. More importantly, what's the behavior you're getting that's different from the behavior you want? $\endgroup$ Apr 6, 2022 at 16:03

1 Answer 1

2
$\begingroup$

I was able to get this to work. For anyone who wants to copy and paste a solution allows one to disbatch a single frame at a time to render, on GPU, here is the code that gets you there:

### Performance utilities

def render_image_indexed(output_dir, out_type, step, ani_length):
    string_order= int(np.ceil(np.log10(ani_length+1)))
    index_order = int(np.ceil(np.log10(step+1)))
    out_string = ""
    
    for i in range(string_order-index_order):
        out_string += "0"
    if (step != 0):
        out_string += str(int(step))
    bpy.context.scene.render.filepath = output_dir+out_string
    bpy.ops.render.render(write_still = True)

def clear_all_data():
    for obj in bpy.context.scene.objects:
        if obj.type == 'MESH':
            obj.select_set(True)
        else:
            obj.select_set(False)
        bpy.ops.object.delete()
    
    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)
    
    for block in bpy.data.curves:
        if block.users == 0:
            bpy.data.curves.remove(block)

###
        
if __name__ == "__main__":
    out_dir = "/mnt/<YourHardDrive>/<YourFolderLocation>/"
    pattern = ".png"
    
    #Technical
    # get_devices() to let Blender detects GPU device
    bpy.context.preferences.addons["cycles"].preferences.get_devices()
    print(bpy.context.preferences.addons["cycles"].preferences.compute_device_type)
    for d in bpy.context.preferences.addons["cycles"].preferences.devices:
        d["use"] = 1 # Using all devices, include GPU and CPU
        print(d["name"], d["use"])
    
    #clear all objects
    for obj in bpy.data.objects:
        bpy.data.objects.remove(obj,do_unlink=True)
    clear_all_data()
    #Render settings
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.cycles.device = 'GPU'
    for frame in range(<NUMFRAMES>):
        <YOUR CODE HERE>
        render_image_indexed(out_dir, pattern, frame, num_frames)

Remember to link your camera to your scene. Adding camera to scene

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .