36fc6a00dad2f6e8e2ffb1b216171e368df32feb
[blender-addons-contrib.git] / add_test_scene_elements / add_light_template.py
1 # gpl: author rebellion
2
3 '''
4 bl_info = {
5     "name": "Light Template",
6     "author": "Rebellion",
7     "version": (1, 0),
8     "blender": (2, 7, 0),
9     "location": "View3D > Add > Lights Template",
10     "description": "Adds  a light/camera template to your scene",
11     "warning": "",
12     "wiki_url": "",
13     "category": "Camera"}
14 '''
15
16 import bpy
17 from bpy.types import Operator
18 from bpy.props import BoolProperty
19 from bpy_extras.object_utils import AddObjectHelper, object_data_add
20 from mathutils import Vector
21
22
23 def add_lamps(self, context):
24
25     if self.bKeyLight:
26         keyLight = bpy.data.lamps.new(name="Key_Light", type="SPOT")
27         ob = bpy.data.objects.new("Key_Light", keyLight)
28         constraint = ob.constraints.new(type='COPY_LOCATION')
29         constraint.use_offset = True
30         constraint.owner_space = 'LOCAL'
31         constraint.target = self.camera
32         constraint = ob.constraints.new(type='TRACK_TO')
33         constraint.target = self.target
34         constraint.track_axis = 'TRACK_NEGATIVE_Z'
35         constraint.up_axis = 'UP_X'
36         constraint.owner_space = 'LOCAL'
37         bpy.context.scene.objects.link(ob)
38         ob.rotation_euler[2] = -0.785398
39
40     if self.bFillLight:
41         fillLight = bpy.data.lamps.new(name="Fill_Light", type="SPOT")
42         ob = bpy.data.objects.new("Fill_Light", fillLight)
43         constraint = ob.constraints.new(type='COPY_LOCATION')
44         constraint.use_offset = True
45         constraint.owner_space = 'LOCAL'
46         constraint.target = self.camera
47         constraint = ob.constraints.new(type='TRACK_TO')
48         constraint.target = self.target
49         constraint.track_axis = 'TRACK_NEGATIVE_Z'
50         constraint.up_axis = 'UP_X'
51         constraint.owner_space = 'LOCAL'
52         bpy.context.scene.objects.link(ob)
53         ob.rotation_euler[2] = 0.785398
54         ob.data.energy = 0.3
55
56     if self.bBackLight:
57         backLight = bpy.data.lamps.new(name="Back_Light", type="SPOT")
58         ob = bpy.data.objects.new("Back_Light", backLight)
59         constraint = ob.constraints.new(type='COPY_LOCATION')
60         constraint.use_offset = True
61         constraint.owner_space = 'LOCAL'
62         constraint.target = self.camera
63         constraint = ob.constraints.new(type='TRACK_TO')
64         constraint.target = self.target
65         constraint.track_axis = 'TRACK_NEGATIVE_Z'
66         constraint.up_axis = 'UP_X'
67         constraint.owner_space = 'LOCAL'
68         bpy.context.scene.objects.link(ob)
69         ob.rotation_euler[2] = 3.14159
70         ob.data.energy = 0.2
71
72     if self.camera_constraint:
73         constraint = self.camera.constraints.new(type='TRACK_TO')
74         constraint.target = self.target
75         constraint.track_axis = 'TRACK_NEGATIVE_Z'
76         constraint.up_axis = 'UP_Y'
77
78
79 class OBJECT_OT_add_light_template(Operator):
80     """Add light template"""
81     bl_idname = "object.add_light_template"
82     bl_label = "Add Light Template"
83     bl_options = {'REGISTER', 'UNDO'}
84
85     camera = None
86     target = None
87
88     bKeyLight = BoolProperty(name="Key Light", default=True)
89     bFillLight = BoolProperty(name="Fill Light")
90     bBackLight = BoolProperty(name="Back Light")
91
92     camera_constraint = BoolProperty(name="Camera Constraint")
93
94     def execute(self, context):
95
96         objects = bpy.context.selected_objects
97
98         if len(objects) == 2:
99             for ob in objects:
100                 if ob.type == 'CAMERA':
101                     self.camera = ob
102                 else:
103                     self.target = ob
104
105         elif len(objects) == 1:
106             if objects[0].type == 'CAMERA':
107                 self.camera = objects[0]
108                 bpy.ops.object.empty_add()
109                 self.target = context.active_object
110             else:
111                 self.camera = context.scene.camera
112                 self.target = context.active_object
113         elif len(objects) == 0:
114             bpy.ops.object.empty_add()
115             self.target = context.active_object
116             self.camera = context.scene.camera
117
118         add_lamps(self, context)
119         return {'FINISHED'}