1
$\begingroup$

My final goal is to make a script that can render a collection of meshes, one at a time, from various angles. This problem has been discussed multiple times, such as Blender script import model and render it.

However, all these approaches use the blender renderer, which requires a lighting setup. I just want to observe the geometry, so for me lighting is not really important. Viewport shading is perfect, but this requires an opengl context to generate from python, so I'm stuck with the renderer.

The question is, how could I setup the lighting in python such that it mimics the viewport shading? Or, what would be easy lighting setup that would produce a similar effect.

Just to make it clear, for my purposes lighting effects are not important, I am just trying to observe the geometry.

$\endgroup$

1 Answer 1

2
$\begingroup$

You can use the Workbench engine for that as it has simple to control lighting settings

The Workbench engine does not use the lights of the scene. The lighting conditions that will be used can be set in the Lighting panel.

Look at this script to get an idea how to configure the render settings:

import bpy

scene = bpy.context.scene

# set the render engine to workbench
scene.render.engine = 'BLENDER_WORKBENCH'

# get the workbench display settings
area = [ area for area in bpy.context.screen.areas if area.type == 'VIEW_3D' ][0]
for space in area.spaces:
    if space.type == 'VIEW_3D':
        display = scene.display
        break

# loop through all cameras
for camera in [ obj for obj in scene.objects if obj.type == 'CAMERA' ]:
    scene.camera = camera
    
    # set output path
    scene.render.filepath = '<your/path/to/output/folder/>' + camera.name

    # showoff: change render settings per camera
    if camera.name == 'Camera0':
        display.shading.light = 'FLAT'
        display.shading.show_cavity = False
        display.shading.show_object_outline = False
        display.shading.object_outline_color = (0, 0, 0)
        display.shading.color_type = 'MATERIAL'

    if camera.name == 'Camera1':
        display.shading.light = 'MATCAP'
        display.shading.show_cavity = True
        display.shading.show_object_outline = True
        display.shading.object_outline_color = (0.673848, 0.232197, 0)
        display.shading.color_type = 'OBJECT'

    if camera.name == 'Camera2':
        display.shading.light = 'STUDIO'
        display.shading.show_cavity = False
        display.shading.show_object_outline = True
        display.shading.object_outline_color = (1, 0, 0)
        display.shading.color_type = 'RANDOM'

    # render
    #bpy.ops.render.opengl() # for reference, this will not work in background mode
    bpy.ops.render.render(write_still=True)

The script can be run directly in Blender or in background mode per commandline (-b flag). For my test scene this gives me this result:

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you! It was exactly what I needed. $\endgroup$
    – Paul92
    Sep 25, 2023 at 18:33
  • $\begingroup$ Great, happy blending! $\endgroup$
    – taiyo
    Sep 25, 2023 at 21:23

You must log in to answer this question.

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