Fix T52833: OBJ triangulate doesn't match viewport
[blender-addons.git] / add_advanced_objects_menu / trilighting.py
blobe0068e66d267b2a120464b354d396675e0c2e526
1 # gpl: author Daniel Schalla
3 import bpy
4 from bpy.types import Operator
5 from bpy.props import (
6 EnumProperty,
7 FloatProperty,
8 IntProperty,
10 from math import (
11 sin, cos,
12 radians,
13 sqrt,
17 class TriLighting(Operator):
18 bl_idname = "object.trilighting"
19 bl_label = "Tri-Lighting Creator"
20 bl_description = ("Add 3 Point Lighting to Selected / Active Object\n"
21 "Needs an existing Active Object")
22 bl_options = {'REGISTER', 'UNDO'}
24 height = FloatProperty(
25 name="Height",
26 default=5
28 distance = FloatProperty(
29 name="Distance",
30 default=5,
31 min=0.1,
32 subtype="DISTANCE"
34 energy = IntProperty(
35 name="Base Energy",
36 default=3,
37 min=1
39 contrast = IntProperty(
40 name="Contrast",
41 default=50,
42 min=-100, max=100,
43 subtype="PERCENTAGE"
45 leftangle = IntProperty(
46 name="Left Angle",
47 default=26,
48 min=1, max=90,
49 subtype="ANGLE"
51 rightangle = IntProperty(
52 name="Right Angle",
53 default=45,
54 min=1, max=90,
55 subtype="ANGLE"
57 backangle = IntProperty(
58 name="Back Angle",
59 default=235,
60 min=90, max=270,
61 subtype="ANGLE"
63 Light_Type_List = [
64 ('POINT', "Point", "Point Light"),
65 ('SUN', "Sun", "Sun Light"),
66 ('SPOT', "Spot", "Spot Light"),
67 ('HEMI', "Hemi", "Hemi Light"),
68 ('AREA', "Area", "Area Light")
70 primarytype = EnumProperty(
71 attr='tl_type',
72 name="Key Type",
73 description="Choose the types of Key Lights you would like",
74 items=Light_Type_List,
75 default='HEMI'
77 secondarytype = EnumProperty(
78 attr='tl_type',
79 name="Fill + Back Type",
80 description="Choose the types of secondary Lights you would like",
81 items=Light_Type_List,
82 default="POINT"
85 @classmethod
86 def poll(cls, context):
87 return context.active_object is not None
89 def draw(self, context):
90 layout = self.layout
92 layout.label("Position:")
93 col = layout.column(align=True)
94 col.prop(self, "height")
95 col.prop(self, "distance")
97 layout.label("Light:")
98 col = layout.column(align=True)
99 col.prop(self, "energy")
100 col.prop(self, "contrast")
102 layout.label("Orientation:")
103 col = layout.column(align=True)
104 col.prop(self, "leftangle")
105 col.prop(self, "rightangle")
106 col.prop(self, "backangle")
108 col = layout.column()
109 col.label("Key Light Type:")
110 col.prop(self, "primarytype", text="")
111 col.label("Fill + Back Type:")
112 col.prop(self, "secondarytype", text="")
114 def execute(self, context):
115 try:
116 scene = context.scene
117 view = context.space_data
118 if view.type == 'VIEW_3D' and not view.lock_camera_and_layers:
119 camera = view.camera
120 else:
121 camera = scene.camera
123 if (camera is None):
124 cam_data = bpy.data.cameras.new(name='Camera')
125 cam_obj = bpy.data.objects.new(name='Camera', object_data=cam_data)
126 scene.objects.link(cam_obj)
127 scene.camera = cam_obj
128 bpy.ops.view3d.camera_to_view()
129 camera = cam_obj
130 bpy.ops.view3d.viewnumpad(type='TOP')
132 obj = bpy.context.scene.objects.active
134 # Calculate Energy for each Lamp
135 if(self.contrast > 0):
136 keyEnergy = self.energy
137 backEnergy = (self.energy / 100) * abs(self.contrast)
138 fillEnergy = (self.energy / 100) * abs(self.contrast)
139 else:
140 keyEnergy = (self.energy / 100) * abs(self.contrast)
141 backEnergy = self.energy
142 fillEnergy = self.energy
144 # Calculate Direction for each Lamp
146 # Calculate current Distance and get Delta
147 obj_position = obj.location
148 cam_position = camera.location
150 delta_position = cam_position - obj_position
151 vector_length = sqrt(
152 (pow(delta_position.x, 2) +
153 pow(delta_position.y, 2) +
154 pow(delta_position.z, 2))
156 if not vector_length:
157 # division by zero most likely
158 self.report({'WARNING'},
159 "Operation Cancelled. No viable object in the scene")
161 return {'CANCELLED'}
163 single_vector = (1 / vector_length) * delta_position
165 # Calc back position
166 singleback_vector = single_vector.copy()
167 singleback_vector.x = cos(radians(self.backangle)) * single_vector.x + \
168 (-sin(radians(self.backangle)) * single_vector.y)
170 singleback_vector.y = sin(radians(self.backangle)) * single_vector.x + \
171 (cos(radians(self.backangle)) * single_vector.y)
173 backx = obj_position.x + self.distance * singleback_vector.x
174 backy = obj_position.y + self.distance * singleback_vector.y
176 backData = bpy.data.lamps.new(name="TriLamp-Back", type=self.secondarytype)
177 backData.energy = backEnergy
179 backLamp = bpy.data.objects.new(name="TriLamp-Back", object_data=backData)
180 scene.objects.link(backLamp)
181 backLamp.location = (backx, backy, self.height)
183 trackToBack = backLamp.constraints.new(type="TRACK_TO")
184 trackToBack.target = obj
185 trackToBack.track_axis = "TRACK_NEGATIVE_Z"
186 trackToBack.up_axis = "UP_Y"
188 # Calc right position
189 singleright_vector = single_vector.copy()
190 singleright_vector.x = cos(radians(self.rightangle)) * single_vector.x + \
191 (-sin(radians(self.rightangle)) * single_vector.y)
193 singleright_vector.y = sin(radians(self.rightangle)) * single_vector.x + \
194 (cos(radians(self.rightangle)) * single_vector.y)
196 rightx = obj_position.x + self.distance * singleright_vector.x
197 righty = obj_position.y + self.distance * singleright_vector.y
199 rightData = bpy.data.lamps.new(name="TriLamp-Fill", type=self.secondarytype)
200 rightData.energy = fillEnergy
201 rightLamp = bpy.data.objects.new(name="TriLamp-Fill", object_data=rightData)
202 scene.objects.link(rightLamp)
203 rightLamp.location = (rightx, righty, self.height)
204 trackToRight = rightLamp.constraints.new(type="TRACK_TO")
205 trackToRight.target = obj
206 trackToRight.track_axis = "TRACK_NEGATIVE_Z"
207 trackToRight.up_axis = "UP_Y"
209 # Calc left position
210 singleleft_vector = single_vector.copy()
211 singleleft_vector.x = cos(radians(-self.leftangle)) * single_vector.x + \
212 (-sin(radians(-self.leftangle)) * single_vector.y)
213 singleleft_vector.y = sin(radians(-self.leftangle)) * single_vector.x + \
214 (cos(radians(-self.leftangle)) * single_vector.y)
215 leftx = obj_position.x + self.distance * singleleft_vector.x
216 lefty = obj_position.y + self.distance * singleleft_vector.y
218 leftData = bpy.data.lamps.new(name="TriLamp-Key", type=self.primarytype)
219 leftData.energy = keyEnergy
221 leftLamp = bpy.data.objects.new(name="TriLamp-Key", object_data=leftData)
222 scene.objects.link(leftLamp)
223 leftLamp.location = (leftx, lefty, self.height)
224 trackToLeft = leftLamp.constraints.new(type="TRACK_TO")
225 trackToLeft.target = obj
226 trackToLeft.track_axis = "TRACK_NEGATIVE_Z"
227 trackToLeft.up_axis = "UP_Y"
229 except Exception as e:
230 self.report({'WARNING'},
231 "Some operations could not be performed (See Console for more info)")
233 print("\n[Add Advanced Objects]\nOperator: "
234 "object.trilighting\nError: {}".format(e))
236 return {'CANCELLED'}
238 return {'FINISHED'}