3d6d880e8c66325b12602506a9e045454e82e31a
[blender-addons-contrib.git] / add_advanced_objects_menu / add_light_template.py
1 # gpl: author Rebellion
2
3 import bpy
4 from bpy.types import Operator
5 from bpy.props import BoolProperty
6
7
8 def add_lights(self, context):
9
10     if self.bKeyLight:
11         keyLight = bpy.data.lights.new(name="Key_Light", type="SPOT")
12         ob = bpy.data.objects.new("Key_Light", keyLight)
13         constraint = ob.constraints.new(type='COPY_LOCATION')
14         constraint.use_offset = True
15         constraint.owner_space = 'LOCAL'
16         constraint.target = self.camera
17         constraint = ob.constraints.new(type='TRACK_TO')
18         constraint.target = self.target
19         constraint.track_axis = 'TRACK_NEGATIVE_Z'
20         constraint.up_axis = 'UP_X'
21         constraint.owner_space = 'LOCAL'
22         bpy.context.collection.objects.link(ob)
23         ob.rotation_euler[2] = -0.785398
24
25     if self.bFillLight:
26         fillLight = bpy.data.lights.new(name="Fill_Light", type="SPOT")
27         ob = bpy.data.objects.new("Fill_Light", fillLight)
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.collection.objects.link(ob)
38         ob.rotation_euler[2] = 0.785398
39         ob.data.energy = 0.3
40
41     if self.bBackLight:
42         backLight = bpy.data.lights.new(name="Back_Light", type="SPOT")
43         ob = bpy.data.objects.new("Back_Light", backLight)
44         constraint = ob.constraints.new(type='COPY_LOCATION')
45         constraint.use_offset = True
46         constraint.owner_space = 'LOCAL'
47         constraint.target = self.camera
48         constraint = ob.constraints.new(type='TRACK_TO')
49         constraint.target = self.target
50         constraint.track_axis = 'TRACK_NEGATIVE_Z'
51         constraint.up_axis = 'UP_X'
52         constraint.owner_space = 'LOCAL'
53         bpy.context.collection.objects.link(ob)
54         ob.rotation_euler[2] = 3.14159
55         ob.data.energy = 0.2
56
57     if self.camera_constraint and self.camera is not None and \
58        self.camera.type == "CAMERA":
59
60         constraint = self.camera.constraints.new(type='TRACK_TO')
61         constraint.target = self.target
62         constraint.track_axis = 'TRACK_NEGATIVE_Z'
63         constraint.up_axis = 'UP_Y'
64
65
66 class OBJECT_OT_add_light_template(Operator):
67     bl_idname = "object.add_light_template"
68     bl_label = "Add Light Template"
69     bl_description = ("Add Key, Fill and Back Lights to the Scene\n"
70                       "Needs an existing Active Object")
71     bl_options = {'REGISTER', 'UNDO'}
72
73     camera = None
74     target = None
75
76     bKeyLight: BoolProperty(
77             name="Key Light",
78             description="Enable Key Light in the Scene",
79             default=True
80             )
81     bFillLight: BoolProperty(
82             name="Fill Light",
83             description="Enable Fill Light in the Scene",
84             default=True
85             )
86     bBackLight: BoolProperty(
87             name="Back Light",
88             description="Enable Back Light in the Scene",
89             default=True
90             )
91     camera_constraint: BoolProperty(
92             name="Camera Constraint",
93             description="Add a Constraint to the Camera Object",
94             default=False
95             )
96
97     @classmethod
98     def poll(cls, context):
99         return context.active_object is not None
100
101     def execute(self, context):
102         try:
103             objects = context.selected_objects
104
105             if len(objects) == 2:
106                 for ob in objects:
107                     if ob.type == 'CAMERA':
108                         self.camera = ob
109                     else:
110                         self.target = ob
111             elif len(objects) == 1:
112                 if objects[0].type == 'CAMERA':
113                     self.camera = objects[0]
114                     bpy.ops.object.empty_add()
115                     self.target = context.active_object
116                 else:
117                     self.camera = context.scene.camera
118                     self.target = context.active_object
119             elif len(objects) == 0:
120                 bpy.ops.object.empty_add()
121                 self.target = context.active_object
122                 self.camera = context.scene.camera
123
124             add_lights(self, context)
125
126         except Exception as e:
127             self.report({'WARNING'},
128                         "Some operations could not be performed (See Console for more info)")
129
130             print("\n[Add Advanced  Objects]\nOperator: "
131                   "object.add_light_template\nError: {}".format(e))
132
133         return {'FINISHED'}
134
135
136 def register():
137     bpy.utils.register_class(OBJECT_OT_add_light_template)
138
139
140 def unregister():
141     bpy.utils.unregister_class(OBJECT_OT_add_light_template)
142
143
144 if __name__ == "__main__":
145     register()