Sub topic 15
Building a Lighting Setup with Python
Assume you can navigate Blender, use the Properties and Shader Editors, and render a simple scene in Cycles or EEVEE. This chapter uses Blender’s bpy API, Python’s interface to Blender data and operations.
Python is useful when a lighting setup must be repeatable. Instead of manually creating and positioning every lamp, a script can create the same lights, world nodes, and render settings each time.
Creating an Area Light
An area light emits light from a rectangular surface. Its energy controls power in watts, while size controls the apparent source size. A larger source produces softer shadows.
import bpy
# Create the light data and the object that uses it.
light_data = bpy.data.lights.new(name="Key", type="AREA")
light_data.energy = 1000.0
light_data.size = 2.0
light_data.color = (1.0, 0.95, 0.9)
key = bpy.data.objects.new(name="Key", object_data=light_data)
bpy.context.collection.objects.link(key)
key.location = (4.0, -4.0, 5.0)The light data stores properties such as power and size. The object stores the light’s location and rotation. This separation is important when several objects use the same light data.
Aiming the Light at a Subject
A TRACK_TO constraint continuously rotates an object toward a target. Lights emit along their local negative Z axis, so the constraint uses TRACK_NEGATIVE_Z.
target = bpy.data.objects.get("Subject")
if target is not None:
constraint = key.constraints.new(type="TRACK_TO")
constraint.target = target
constraint.track_axis = "TRACK_NEGATIVE_Z"
constraint.up_axis = "UP_Y"If the target does not exist, the script skips the constraint instead of raising an error. Naming the subject consistently makes scripted setups easier to reuse.
Building an HDRI World
An HDRI is a high-dynamic-range image that contains both visible environment detail and strong lighting information. The following setup loads an HDRI relative to the current .blend file and rotates it around the vertical axis.
import bpy
import math
scene = bpy.context.scene
world = scene.world or bpy.data.worlds.new("World")
scene.world = world
world.use_nodes = True
nodes = world.node_tree.nodes
links = world.node_tree.links
nodes.clear()
output = nodes.new("ShaderNodeOutputWorld")
background = nodes.new("ShaderNodeBackground")
environment = nodes.new("ShaderNodeTexEnvironment")
mapping = nodes.new("ShaderNodeMapping")
coordinates = nodes.new("ShaderNodeTexCoord")
environment.image = bpy.data.images.load("//hdri/studio.exr")
background.inputs["Strength"].default_value = 0.8
mapping.inputs["Rotation"].default_value[2] = math.radians(90)
links.new(coordinates.outputs["Generated"], mapping.inputs["Vector"])
links.new(mapping.outputs["Vector"], environment.inputs["Vector"])
links.new(environment.outputs["Color"], background.inputs["Color"])
links.new(background.outputs["Background"], output.inputs["Surface"])The // prefix makes the image path relative to the .blend file. Save the file before running the script, and keep the HDRI in the referenced folder.
Render Settings and Reuse
You can finish the setup by selecting Cycles and enabling denoising:
scene.render.engine = "CYCLES"
scene.cycles.samples = 128
scene.cycles.use_denoising = True
scene.view_settings.exposure = 0.0Adjust exposure after balancing lamp power and HDRI strength. Avoid using exposure to hide a badly placed or excessively bright light.
Exercise
How would you make the HDRI contribute reflections and ambient light without appearing in the camera background?
Show answer
Set scene.render.film_transparent = True, then render with transparency enabled. The HDRI can still illuminate the scene, but the background becomes transparent.