-
Notifications
You must be signed in to change notification settings - Fork 46
/
light_brush.py
504 lines (425 loc) · 21.6 KB
/
light_brush.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import bpy
from bpy_extras import view3d_utils
from math import *
from mathutils.geometry import intersect_line_sphere
from mathutils import Vector
from bpy.props import *
from . common import isFamily, findLightGrp, get_user_keymap_item
from . operators import LightOperator
def raycast_add_light(context, event, diff, add_light=False):
"""Run this function on left mouse, execute the ray cast"""
# get the context arguments
region = context.region
rv3d = context.region_data
coord = event.mouse_region_x, event.mouse_region_y
# get the ray from the viewport and mouse
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)
ray_target = ray_origin + view_vector
def visible_objects_and_duplis():
"""Loop over (object, matrix) pairs (mesh only)"""
for obj in context.visible_objects:
if isFamily(obj):
continue
if obj.type == 'MESH':
yield (obj, obj.matrix_world.copy())
if obj.instance_type != 'NONE':
depsgraph = context.depsgraph
for dup in depsgraph.object_instances:
obj_dupli = dup.object
if obj_dupli.type == 'MESH':
yield (obj_dupli, dup.matrix_world.copy())
def obj_ray_cast(obj, matrix):
"""Wrapper for ray casting that moves the ray into object space"""
# get the ray relative to the object
matrix_inv = matrix.inverted()
ray_origin_obj = matrix_inv @ ray_origin
ray_target_obj = matrix_inv @ ray_target
ray_direction_obj = ray_target_obj - ray_origin_obj
# cast the ray
success, location, normal, face_index = obj.ray_cast(ray_origin_obj, ray_direction_obj)
if success:
return location, normal, face_index
else:
return None, None, None
# cast rays and find the closest object
best_length_squared = -1.0
best_obj = None
normal = None
location = None
for obj, matrix in visible_objects_and_duplis():
if obj.type == 'MESH':
hit, hit_normal, face_index = obj_ray_cast(obj, matrix)
if hit is not None:
hit_world = matrix @ hit
length_squared = (hit_world - ray_origin).length_squared
if best_obj is None or length_squared < best_length_squared:
best_length_squared = length_squared
best_obj = obj
normal = hit_normal # local space
location = hit_world
if best_obj is None:
return False
# convert normal from local space to global
matrix = best_obj.matrix_world
matrix_new = matrix.to_3x3().inverted().transposed()
normal = matrix_new @ normal
normal.normalize()
print(normal, bpy.context.active_object)
if add_light:
bpy.ops.scene.add_leomoon_studio_light()
print(bpy.context.object)
#####
profile = findLightGrp(bpy.context.active_object).parent
handle = [ob for ob in profile.children if ob.name.startswith('LLS_HANDLE')][0]
light_handle = bpy.context.active_object.parent
actuator = light_handle.parent
position = intersect_line_sphere(
location - handle.location,
(normal if diff else view_vector.reflect(normal)) + location - handle.location,
Vector((0,0,0)),
light_handle.location.z,
False,
)[0]
if not position:
return False
# ctrl x
x,y,z = position
actuator.rotation_euler.x = atan2(x, -y) - handle.rotation_euler.z
# ctrl y
actuator.rotation_euler.y = copysign(Vector.angle(Vector((x,y,z)), Vector((x,y,0))), z)
# actuator.rotation_euler.y = copysign(Vector.angle(Vector((x,y,z)), Vector((x,y,0))) + handle.rotation_euler.x, z)
return True
def raycast(context, event, diff):
"""Run this function on left mouse, execute the ray cast"""
# get the context arguments
region = context.region
rv3d = context.region_data
coord = event.mouse_region_x, event.mouse_region_y
# get the ray from the viewport and mouse
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)
ray_target = ray_origin + view_vector
def visible_objects_and_duplis():
"""Loop over (object, matrix) pairs (mesh only)"""
for obj in context.visible_objects:
if isFamily(obj):
continue
if obj.type == 'MESH':
yield (obj, obj.matrix_world.copy())
if obj.instance_type != 'NONE':
depsgraph = context.depsgraph
for dup in depsgraph.object_instances:
obj_dupli = dup.object
if obj_dupli.type == 'MESH':
yield (obj_dupli, dup.matrix_world.copy())
def obj_ray_cast(obj, matrix):
"""Wrapper for ray casting that moves the ray into object space"""
# get the ray relative to the object
matrix_inv = matrix.inverted()
ray_origin_obj = matrix_inv @ ray_origin
ray_target_obj = matrix_inv @ ray_target
ray_direction_obj = ray_target_obj - ray_origin_obj
# cast the ray
success, location, normal, face_index = obj.ray_cast(ray_origin_obj, ray_direction_obj)
if success:
return location, normal, face_index
else:
return None, None, None
# cast rays and find the closest object
best_length_squared = -1.0
best_obj = None
normal = None
location = None
for obj, matrix in visible_objects_and_duplis():
if obj.type == 'MESH':
hit, hit_normal, face_index = obj_ray_cast(obj, matrix)
if hit is not None:
hit_world = matrix @ hit
length_squared = (hit_world - ray_origin).length_squared
if best_obj is None or length_squared < best_length_squared:
best_length_squared = length_squared
best_obj = obj
normal = hit_normal # local space
location = hit_world
if best_obj is None:
return {'RUNNING_MODAL'}
# convert normal from local space to global
matrix = best_obj.matrix_world
matrix_new = matrix.to_3x3().inverted().transposed()
normal = matrix_new @ normal
normal.normalize()
#####
profile = findLightGrp(context.active_object).parent
handle = [ob for ob in profile.children if ob.name.startswith('LLS_HANDLE')][0]
light_handle = context.active_object.parent
actuator = light_handle.parent
position = intersect_line_sphere(
location - handle.location,
(normal if diff else view_vector.reflect(normal)) + location - handle.location,
Vector((0,0,0)),
light_handle.location.z,
False,
)[0]
if not position:
return {'RUNNING_MODAL'}
# ctrl x
x,y,z = position
actuator.rotation_euler.x = atan2(x, -y) - handle.rotation_euler.z
# ctrl y
actuator.rotation_euler.y = copysign(Vector.angle(Vector((x,y,z)), Vector((x,y,0))), z)
# actuator.rotation_euler.y = copysign(Vector.angle(Vector((x,y,z)), Vector((x,y,0))) + handle.rotation_euler.x, z)
class LLSLightBrush(bpy.types.Operator, LightOperator):
"""Click on object to position light and reflection"""
bl_idname = "lls.light_brush"
bl_label = "Light Brush"
bl_options = {"UNDO"}
aux: BoolProperty(default=False) # is aux operator working
normal_type: BoolProperty(default=False)
def modal(self, context, event):
# print(event.type, event.value)
if self.aux:
if event.type in {'LEFTMOUSE', 'RIGHTMOUSE', 'ESC', 'RET', 'NUMPAD_ENTER'}:
self.aux = False
return {'RUNNING_MODAL'}
context.area.header_text_set(text=f"[LM] Select Face, [ESC/RM] Quit, [N] {'Reflection | <Normal>' if self.normal_type else '<Reflection> | Normal'}")
if event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE', 'Z', 'LEFT_SHIFT', 'LEFT_ALT', 'LEFT_CTRL'}:
# allow navigation
return {'PASS_THROUGH'}
elif event.type in {'RIGHTMOUSE', 'ESC', 'RET', 'NUMPAD_ENTER'}:
context.area.header_text_set(text=None)
return {'FINISHED'}
elif event.type == 'LEFTMOUSE':
if event.value == 'PRESS':
raycast(context, event, self.normal_type)
return {'RUNNING_MODAL'}
elif event.value == 'RELEASE':
return {'PASS_THROUGH'}
elif event.type == 'MOUSEMOVE':
if event.value == 'PRESS':
raycast(context, event, self.normal_type)
return {'PASS_THROUGH'}
elif event.type == 'N' and event.value == 'PRESS':
self.normal_type = not self.normal_type
#return {'PASS_THROUGH'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.space_data.type == 'VIEW_3D':
# set workspace tool to select
self.beginning_tool = context.workspace.tools.from_space_view3d_mode("OBJECT", create=False).idname
print(self.beginning_tool)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name='builtin.select_box')
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "Active space must be a View3d")
return {'CANCELLED'}
class OverrideContext:
pass
class OverrideEvent:
pass
key_released = False
class OT_LLSFast3DEdit(bpy.types.Operator, LightOperator):
"""Point on object to position light and reflection"""
bl_idname = "light_studio.fast_3d_edit"
bl_label = "Fast 3D Edit"
bl_options = {"UNDO"}
continuous: BoolProperty(default=False, name="Hold to use", description="Button behaviour.\n ON: Hold button to use. Release button to stop.\n OFF: Hold LMB to use, release LMB to stop.")
normal_type: BoolProperty(default=False, name="Light along normal", description="Default reflection type.\n ON: Light along normal\n OFF: surface reflection (what you are looking for in most cases)")
def modal(self, context, event):
screens = [window.screen for window in context.window_manager.windows]
regions3d = [(area.spaces[0].region_3d, region) for screen in screens for area in screen.areas if area.type == context.area.type for region in area.regions if region.type == context.region.type]
active_region = context.region
active_region_data = context.region_data
for region_data, region in regions3d:
if event.mouse_x >= region.x and event.mouse_x <= region.x + region.width and \
event.mouse_y >= region.y and event.mouse_y <= region.y + region.height:
active_region = region
active_region_data = region_data
break
override_context = OverrideContext()
override_context.region = active_region
override_context.region_data = active_region_data
override_context.visible_objects = context.visible_objects
override_context.active_object = context.active_object
if hasattr(context, 'depsgraph'):
override_context.depsgraph = context.depsgraph
else:
override_context.depsgraph = context.evaluated_depsgraph_get()
override_event = OverrideEvent()
override_event.mouse_region_x = event.mouse_x - active_region.x
override_event.mouse_region_y = event.mouse_y - active_region.y
global key_released
context.area.header_text_set(text=f"[LM] Select Face, [ESC/RM] Quit, [N] {'Reflection | <Normal>' if self.normal_type else '<Reflection> | Normal'}")
# print(event.type, event.value, ':', event.type_prev,event.value_prev)
if self.continuous:
if event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE', 'LEFT_SHIFT', 'LEFT_ALT', 'LEFT_CTRL'}:
# allow navigation
return {'PASS_THROUGH'}
elif event.type in {'RIGHTMOUSE', 'ESC', 'RET', 'NUMPAD_ENTER'}:
context.area.header_text_set(text=None)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=self.beginning_tool)
return {'FINISHED'}
elif event.type == 'N' and event.value == 'PRESS':
self.normal_type = not self.normal_type
return {'RUNNING_MODAL'}
elif event.type == 'MOUSEMOVE':
raycast(override_context, override_event, self.normal_type)
return {'PASS_THROUGH'}
elif event.value == 'RELEASE' and not event.type in {'MOUSEMOVE', 'INBETWEEN_MOUSEMOVE', 'N'}:
context.area.header_text_set(text=None)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=self.beginning_tool)
return {'FINISHED'}
else:
if event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE', 'LEFT_SHIFT', 'LEFT_ALT', 'LEFT_CTRL'}:
# allow navigation
return {'PASS_THROUGH'}
elif event.type in {'RIGHTMOUSE', 'ESC', 'RET', 'NUMPAD_ENTER'}:
context.area.header_text_set(text=None)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=self.beginning_tool)
return {'FINISHED'}
elif event.type in {self.keymap_key, 'LEFTMOUSE'} and event.value == 'RELEASE' and not key_released:
key_released = True
return {'RUNNING_MODAL'}
elif not key_released:
return {'RUNNING_MODAL'}
elif event.type == 'N' and event.value == 'PRESS':
self.normal_type = not self.normal_type
return {'RUNNING_MODAL'}
elif event.type == 'LEFTMOUSE' and event.value == 'PRESS':
raycast(override_context, override_event, self.normal_type)
return {'PASS_THROUGH'}
elif event.type == 'MOUSEMOVE' and event.type_prev=='LEFTMOUSE' and event.value_prev == 'PRESS':
raycast(override_context, override_event, self.normal_type)
return {'PASS_THROUGH'}
elif event.type == 'LEFTMOUSE' and event.value == 'RELEASE':
context.area.header_text_set(text=None)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=self.beginning_tool)
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
context.window_manager.modal_handler_add(self)
# set workspace tool to select
self.beginning_tool = context.workspace.tools.from_space_view3d_mode("OBJECT", create=False).idname
print(self.beginning_tool)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name='builtin.select_box')
km, kmi = get_user_keymap_item('Object Mode', self.__class__.bl_idname)
self.keymap_key = kmi.type if kmi else 'F'
global key_released
key_released = False
if self.continuous:
raycast(context, event, self.normal_type)
return {'RUNNING_MODAL'}
class OT_LLS3DAddLight(bpy.types.Operator, LightOperator):
"""Point and add light"""
bl_idname = "light_studio.3d_add_light"
bl_label = "Add Light in 3D"
bl_options = {"UNDO"}
@classmethod
def poll(cls, context):
object = context.active_object
return context.area.type == 'VIEW_3D' and \
context.mode == 'OBJECT' and \
context.space_data.type == 'VIEW_3D' and \
context.scene.LLStudio.initialized
continuous: BoolProperty(default=False, name="Hold to use", description="Button behaviour.\n ON: Hold button to use. Release button to stop.\n OFF: Hold LMB to use, release LMB to stop.")
normal_type: BoolProperty(default=False, name="Light along normal", description="Default reflection type.\n ON: Light along normal\n OFF: surface reflection (what you are looking for in most cases)")
def modal(self, context, event):
screens = [window.screen for window in context.window_manager.windows]
regions3d = [(area.spaces[0].region_3d, region) for screen in screens for area in screen.areas if area.type == context.area.type for region in area.regions if region.type == context.region.type]
active_region = context.region
active_region_data = context.region_data
for region_data, region in regions3d:
if event.mouse_x >= region.x and event.mouse_x <= region.x + region.width and \
event.mouse_y >= region.y and event.mouse_y <= region.y + region.height:
active_region = region
active_region_data = region_data
break
override_context = OverrideContext()
override_context.region = active_region
override_context.region_data = active_region_data
override_context.visible_objects = context.visible_objects
override_context.active_object = context.active_object
if hasattr(context, 'depsgraph'):
override_context.depsgraph = context.depsgraph
else:
override_context.depsgraph = context.evaluated_depsgraph_get()
override_event = OverrideEvent()
override_event.mouse_region_x = event.mouse_x - active_region.x
override_event.mouse_region_y = event.mouse_y - active_region.y
global key_released
context.area.header_text_set(text=f"[LM] Select Face, [ESC/RM] Quit, [N] {'Reflection | <Normal>' if self.normal_type else '<Reflection> | Normal'}")
# print(event.type, event.value, ':', event.type_prev,event.value_prev)
if self.continuous:
if event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE', 'LEFT_SHIFT', 'LEFT_ALT', 'LEFT_CTRL'}:
# allow navigation
return {'PASS_THROUGH'}
elif event.type in {'RIGHTMOUSE', 'ESC', 'RET', 'NUMPAD_ENTER'}:
context.area.header_text_set(text=None)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=self.beginning_tool)
return {'FINISHED'}
elif event.type == 'N' and event.value == 'PRESS':
self.normal_type = not self.normal_type
return {'RUNNING_MODAL'}
elif event.type == 'MOUSEMOVE':
self.is_added = raycast_add_light(override_context, override_event, self.normal_type, not self.is_added)
return {'PASS_THROUGH'}
elif event.value == 'RELEASE' and not event.type in {'MOUSEMOVE', 'INBETWEEN_MOUSEMOVE', 'N'}:
context.area.header_text_set(text=None)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=self.beginning_tool)
return {'FINISHED'}
else:
if event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE', 'LEFT_SHIFT', 'LEFT_ALT', 'LEFT_CTRL'}:
# allow navigation
return {'PASS_THROUGH'}
elif event.type in {'RIGHTMOUSE', 'ESC', 'RET', 'NUMPAD_ENTER'}:
context.area.header_text_set(text=None)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=self.beginning_tool)
return {'FINISHED'}
elif event.type in {self.keymap_key, 'LEFTMOUSE'} and event.value == 'RELEASE' and not key_released:
key_released = True
return {'RUNNING_MODAL'}
elif not key_released:
return {'RUNNING_MODAL'}
elif event.type == 'N' and event.value == 'PRESS':
self.normal_type = not self.normal_type
return {'RUNNING_MODAL'}
elif event.type == 'LEFTMOUSE' and event.value == 'PRESS':
self.is_added = raycast_add_light(override_context, override_event, self.normal_type, not self.is_added)
return {'PASS_THROUGH'}
elif event.type == 'MOUSEMOVE' and event.type_prev=='LEFTMOUSE' and event.value_prev == 'PRESS':
self.is_added = raycast_add_light(override_context, override_event, self.normal_type, not self.is_added)
return {'PASS_THROUGH'}
elif event.type == 'LEFTMOUSE' and event.value == 'RELEASE':
context.area.header_text_set(text=None)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name=self.beginning_tool)
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
context.window_manager.modal_handler_add(self)
# set workspace tool to select
self.beginning_tool = context.workspace.tools.from_space_view3d_mode("OBJECT", create=False).idname
print(self.beginning_tool)
bpy.ops.wm.tool_set_by_id('INVOKE_DEFAULT', name='builtin.select_box')
km, kmi = get_user_keymap_item('Object Mode', self.__class__.bl_idname)
self.keymap_key = kmi.type if kmi else 'F'
global key_released
key_released = False
self.is_added = False
# if self.continuous:
# raycast_add_light(context, event, self.normal_type)
return {'RUNNING_MODAL'}
addon_keymaps = []
def add_shortkeys():
wm = bpy.context.window_manager
addon_km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type="EMPTY")
addon_kmi = addon_km.keymap_items.new(OT_LLSFast3DEdit.bl_idname, 'F', 'PRESS')
addon_kmi.properties.continuous = False
addon_kmi = addon_km.keymap_items.new(OT_LLS3DAddLight.bl_idname, 'F', 'PRESS', ctrl=True)
addon_kmi.properties.continuous = False
addon_keymaps.append((addon_km, addon_kmi))
def remove_shortkeys():
wm = bpy.context.window_manager
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()