"""Add one adjustable area light aimed at a point in the active scene."""
import bpy
from mathutils import Vector

# XYZ positions in Blender world units.
POSITION = (3.0, -4.0, 5.0)
TARGET = (0.0, 0.0, 1.0)
POWER_WATTS = 350.0
SIZE = 2.0

direction = Vector(TARGET) - Vector(POSITION)
if direction.length == 0 or POWER_WATTS < 0 or SIZE <= 0:
    raise ValueError("Use different positions for the light and target, and check power and size.")

scene = bpy.context.scene
key = next(
    (
        obj for obj in scene.objects
        if obj.type == "LIGHT" and obj.get("rp_area_key") and len(obj.users_scene) == 1
    ),
    None,
)
if key is None:
    light = bpy.data.lights.new("RP Area Key", type="AREA")
    key = bpy.data.objects.new("RP Area Key", light)
    scene.collection.objects.link(key)
    key["rp_area_key"] = True
elif key.data.users > 1:
    key.data = key.data.copy()

key.data.type = "AREA"
key.data.energy = POWER_WATTS
key.data.shape = "DISK"
key.data.size = SIZE
key.location = POSITION
key.rotation_mode = "QUATERNION"
key.rotation_quaternion = direction.to_track_quat("-Z", "Y")
print(f"Area light ready: {key.name}.")
