Skip to content

Instantly share code, notes, and snippets.

@behreajj
Created May 25, 2021 13:25
Show Gist options
  • Save behreajj/243a639d6d819649846927bdca67490c to your computer and use it in GitHub Desktop.
Save behreajj/243a639d6d819649846927bdca67490c to your computer and use it in GitHub Desktop.
Constraint Setup
import bpy
from mathutils import Vector
cam_data = bpy.data.cameras.new("Camera")
cam_data.type = 'ORTHO'
cam_data.ortho_scale = 4.0
cam_obj = bpy.data.objects.new(cam_data.name, cam_data)
cam_obj.location = (10.0, 10.0, 5.0)
bpy.context.collection.objects.link(cam_obj)
empty_obj = bpy.data.objects.new("Cam.Target", None)
empty_obj.location = (0.0, 0.0, 0.0)
bpy.context.collection.objects.link(empty_obj)
look_at_constraint = cam_obj.constraints.new("TRACK_TO")
look_at_constraint.target = empty_obj
area_0 = bpy.data.lights.new("Area.000", 'AREA')
area_1 = bpy.data.lights.new("Area.001", 'AREA')
area_2 = bpy.data.lights.new("Area.002", 'AREA')
lights = [area_0, area_1, area_2]
locs = [
Vector((2.0, -2.0, 1.0)),
Vector((2.0, 2.0, 1.0)),
cam_obj.location + Vector((0.25, -0.25, 1.5))
]
sizes = [1.0, 1.0, 2.0]
energies = [20.0, 20.0, 40.0]
# For lights, colors should have 3 entries, not 4,
# as they do not contain alpha.
clrs = [
(0.75, 0.0, 0.5),
(0.1, 0.5, 1.0),
(1.0, 0.875, 0.825)
]
i_range = range(0, len(lights), 1)
for i in i_range:
light_data = lights[i]
light_data.energy = energies[i]
light_data.color = clrs[i]
light_data.size = sizes[i]
light_obj = bpy.data.objects.new(light_data.name, light_data)
light_obj.location = locs[i]
bpy.context.collection.objects.link(light_obj)
look_at_constraint = light_obj.constraints.new("TRACK_TO")
look_at_constraint.target = empty_obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment