1 # gpl: author Daniel Schalla
4 from bpy
.types
import Operator
5 from bpy
.props
import (
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(
28 distance
= FloatProperty(
39 contrast
= IntProperty(
45 leftangle
= IntProperty(
51 rightangle
= IntProperty(
57 backangle
= IntProperty(
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(
73 description
="Choose the types of Key Lights you would like",
74 items
=Light_Type_List
,
77 secondarytype
= EnumProperty(
79 name
="Fill + Back Type",
80 description
="Choose the types of secondary Lights you would like",
81 items
=Light_Type_List
,
86 def poll(cls
, context
):
87 return context
.active_object
is not None
89 def draw(self
, context
):
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
):
116 scene
= context
.scene
117 view
= context
.space_data
118 if view
.type == 'VIEW_3D' and not view
.lock_camera_and_layers
:
121 camera
= scene
.camera
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()
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
)
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")
163 single_vector
= (1 / vector_length
) * delta_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"
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
))