import bpy
from math import radians
# 初始化参数
light_power = 200 # 灯光功率为200W
light_distance = 5 # 距离物体的默认高度
light_data_name = 'three_point_lighting' # 主灯光数据名称
light_suffixes = ['_key', '_fill', '_back'] # 按键光、补光、背光的后缀
def create_three_point_lights(context, obj, number_of_lights=3, power=light_power, distance=light_distance):
assert number_of_lights == 3, "目前只支持3个灯光的配置"
# 计算灯光位置
rotations = [radians(120), radians(0), radians(-120)]
for i in range(number_of_lights):
# 创建灯光数据
light_data = bpy.data.lights.new(name=light_data_name + light_suffixes[i], type='AREA')
light_data.energy = power
# 创建灯光对象
light_object = bpy.data.objects.new(name=obj.name + light_suffixes[i], object_data=light_data)
light_object.location = obj.location
# 设置灯光蝶形(area_shape = 'RECTANGLE')
light_object.data.shape = 'RECTANGLE'
light_object.data.size = 1 # 调整蝶形大小
light_object.data.size_y = 2 # 灯光的Y轴长度
# 链接灯光对象到场景
context.collection.objects.link(light_object)
# 设置灯光方向和位置
light_object.location.z += distance # 设置灯光的高度
light_object.rotation_euler = (radians(45), 0, rotations[i]) # 设置灯光的角度
# 设置约束,使灯光跟随物体
track_to = light_object.constraints.new(type='TRACK_TO')
track_to.target = obj
track_to.track_axis = 'TRACK_NEGATIVE_Z'
track_to.up_axis = 'UP_Y'
# 主函数
def main(context):
obj = context.object
if obj: # 检查是否有选中的物体
create_three_point_lights(context, obj)
# 运行主函数
if bpy.context.area.type == 'VIEW_3D':
main(bpy.context)