Skip to content

Instantly share code, notes, and snippets.

@remino
Last active November 18, 2023 04:51
Show Gist options
  • Save remino/5859428ae059fc8b70b97070a6cd38db to your computer and use it in GitHub Desktop.
Save remino/5859428ae059fc8b70b97070a6cd38db to your computer and use it in GitHub Desktop.
Script to create a three-light set-up scene in Blender
# threelights.blender.py
# By Rémino Rem <remino.net>
# 2023-11-18
#
# This creates a three-light scene set-up in Blender. Works in Blender 4.
#
# Best run in a blank file, as it will destroy anything in the scene.
import bpy
import mathutils
# Clear existing objects except camera
for obj in bpy.context.scene.objects:
if obj.type != 'CAMERA':
obj.select_set(True)
else:
obj.select_set(False)
bpy.ops.object.delete()
# Create a cube
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 1))
cube = bpy.context.object
# Function to create a light
def create_light(name, type, location, energy, rotation):
bpy.ops.object.light_add(type=type, radius=1, align='WORLD', location=location)
light = bpy.context.object
light.data.energy = energy
light.name = name
light.rotation_euler = rotation
# Create Key Light, positioned and angled towards the subject
key_light_location = (5, -5, 5)
key_light_direction = mathutils.Vector(cube.location) - mathutils.Vector(key_light_location)
key_light_rotation = key_light_direction.to_track_quat('-Z', 'X').to_euler()
create_light('Key Light', 'AREA', key_light_location, 1000, key_light_rotation)
# Create Fill Light
create_light('Fill Light', 'AREA', (5, 5, 5), 300, (-0.785398, 0, -0.785398))
# Create Back Light, positioned to the left from the camera's POV and pointing forward and left
create_light('Back Light', 'AREA', (-5, -5, 5), 500, (0.785398, -0.261799, 0))
# Position and rotate the camera to face the cube
camera_location = (10, 0, 5) # Position between Key and Fill lights
if not any(obj.type == 'CAMERA' for obj in bpy.context.scene.objects):
bpy.ops.object.camera_add(location=camera_location)
camera = bpy.context.object
else:
camera = next(obj for obj in bpy.context.scene.objects if obj.type == 'CAMERA')
camera.location = camera_location
# Calculate the direction vector from the camera to the cube
direction = mathutils.Vector(cube.location) - mathutils.Vector(camera.location)
# Point the camera's '-Z' and use its 'Y' as up
rot_quat = direction.to_track_quat('-Z', 'Y')
camera.rotation_euler = rot_quat.to_euler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment