-
Notifications
You must be signed in to change notification settings - Fork 20
/
projector.py
667 lines (540 loc) · 23.6 KB
/
projector.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import logging
import math
import os
from enum import Enum
import bpy
from bpy.types import Operator
from .helper import (ADDON_ID, auto_offset,
get_projectors, get_projector, random_color)
logging.basicConfig(
format='[Projectors Addon]: %(name)s - %(levelname)s - %(message)s')
log = logging.getLogger(name=__file__)
class Textures(Enum):
CHECKER = 'checker_texture'
COLOR_GRID = 'color_grid_texture'
CUSTOM_TEXTURE = 'custom_texture'
RESOLUTIONS = [
# 16:10 aspect ratio
('1280x800', 'WXGA (1280x800) 16:10', '', 1),
('1440x900', 'WXGA+ (1440x900) 16:10', '', 2),
('1920x1200', 'WUXGA (1920x1200) 16:10', '', 3),
# 16:9 aspect ratio
('1280x720', '720p (1280x720) 16:9', '', 4),
('1920x1080', '1080p (1920x1080) 16:9', '', 5),
('3840x2160', '4K Ultra HD (3840x2160) 16:9', '', 6),
# 4:3 aspect ratio
('800x600', 'SVGA (800x600) 4:3', '', 7),
('1024x768', 'XGA (1024x768) 4:3', '', 8),
('1400x1050', 'SXGA+ (1400x1050) 4:3', '', 9),
('1600x1200', 'UXGA (1600x1200) 4:3', '', 10),
# 17:9 aspect ratio
('4096x2160', 'Native 4K (4096x2160) 17:9', '', 11),
# 1:1 aspect ratio
('1000x1000', 'Square (1000x1000) 1:1', '', 12)
]
PROJECTED_OUTPUTS = [(Textures.CHECKER.value, 'Checker', '', 1),
(Textures.COLOR_GRID.value, 'Color Grid', '', 2),
(Textures.CUSTOM_TEXTURE.value, 'Custom Texture', '', 3)]
class PROJECTOR_OT_change_color_randomly(Operator):
""" Randomly change the color of the projected checker texture."""
bl_idname = 'projector.change_color'
bl_label = 'Change color of projection checker texture'
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return len(get_projectors(context, only_selected=True)) == 1
def execute(self, context):
projectors = get_projectors(context, only_selected=True)
new_color = random_color(alpha=True)
for projector in projectors:
projector.proj_settings['projected_color'] = new_color[:-1]
update_checker_color(projector.proj_settings, context)
return {'FINISHED'}
def create_projector_textures():
""" This function checks if the needed images exist and if not creates them. """
name_template = '_proj.tex.{}'
for res in RESOLUTIONS:
img_name = name_template.format(res[0])
w, h = res[0].split('x')
if not bpy.data.images.get(img_name):
log.debug(f'Create projection texture: {res}')
bpy.ops.image.new(name=img_name,
width=int(w),
height=int(h),
color=(0.0, 0.0, 0.0, 1.0),
alpha=True,
generated_type='COLOR_GRID',
float=False)
bpy.data.images[img_name].use_fake_user = True
def add_projector_node_tree_to_spot(spot):
"""
This function turns a spot light into a projector.
This is achieved through a texture on the spot light and some basic math.
"""
spot.data.use_nodes = True
root_tree = spot.data.node_tree
root_tree.nodes.clear()
node_group = bpy.data.node_groups.new('_Projector', 'ShaderNodeTree')
# Create output sockets for the node group.
if(bpy.app.version >= (4, 0)):
node_group.interface.new_socket('texture vector', in_out="OUTPUT", socket_type='NodeSocketVector')
node_group.interface.new_socket('color', in_out="OUTPUT", socket_type='NodeSocketColor')
else:
output = node_group.outputs
output.new('NodeSocketVector', 'texture vector')
output.new('NodeSocketColor', 'color')
# # Inside Group Node #
# #####################
# Hold important nodes inside a group node.
group = spot.data.node_tree.nodes.new('ShaderNodeGroup')
group.node_tree = node_group
group.label = "!! Don't touch !!"
nodes = group.node_tree.nodes
tree = group.node_tree
auto_pos = auto_offset()
tex = nodes.new('ShaderNodeTexCoord')
tex.location = auto_pos(200)
geo = nodes.new('ShaderNodeNewGeometry')
geo.location = auto_pos(0, -300)
vec_transform = nodes.new('ShaderNodeVectorTransform')
vec_transform.location = auto_pos(200)
vec_transform.vector_type = 'NORMAL'
map_1 = nodes.new('ShaderNodeMapping')
map_1.vector_type = 'TEXTURE'
# Flip the image horizontally and vertically to display it the intended way.
if bpy.app.version < (2, 81):
map_1.scale[0] = -1
map_1.scale[1] = -1
else:
map_1.inputs[3].default_value[0] = -1
map_1.inputs[3].default_value[1] = -1
map_1.location = auto_pos(200)
sep = nodes.new('ShaderNodeSeparateXYZ')
sep.location = auto_pos(350)
div_1 = nodes.new('ShaderNodeMath')
div_1.operation = 'DIVIDE'
div_1.name = ADDON_ID + 'div_01'
div_1.location = auto_pos(200)
div_2 = nodes.new('ShaderNodeMath')
div_2.operation = 'DIVIDE'
div_2.name = ADDON_ID + 'div_02'
div_2.location = auto_pos(y=-200)
com = nodes.new('ShaderNodeCombineXYZ')
com.inputs['Z'].default_value = 1.0
com.location = auto_pos(200)
map_2 = nodes.new('ShaderNodeMapping')
map_2.location = auto_pos(200)
map_2.vector_type = 'TEXTURE'
add = nodes.new('ShaderNodeMixRGB')
add.blend_type = 'ADD'
add.inputs[0].default_value = 1
add.location = auto_pos(350)
# Texture
# a) Image
img = nodes.new('ShaderNodeTexImage')
img.extension = 'CLIP'
img.location = auto_pos(200)
# b) Generated checker texture.
checker_tex = nodes.new('ShaderNodeTexChecker')
# checker_tex.inputs['Color2'].default_value = random_color(alpha=True)
checker_tex.inputs[3].default_value = 8
checker_tex.inputs[1].default_value = (1, 1, 1, 1)
checker_tex.location = auto_pos(y=-300)
mix_rgb = nodes.new('ShaderNodeMixRGB')
mix_rgb.name = 'Mix.001'
mix_rgb.inputs[1].default_value = (0, 0, 0, 0)
mix_rgb.location = auto_pos(200, y=-300)
group_output_node = node_group.nodes.new('NodeGroupOutput')
group_output_node.location = auto_pos(200)
# # Root Nodes #
# ##############
auto_pos_root = auto_offset()
# Image Texture
user_texture = root_tree.nodes.new('ShaderNodeTexImage')
user_texture.extension = 'CLIP'
user_texture.label = 'Add your Image Texture or Movie here'
user_texture.location = auto_pos_root(200, y=200)
# Emission
emission = root_tree.nodes.new('ShaderNodeEmission')
emission.inputs['Strength'].default_value = 1
emission.location = auto_pos_root(300)
# Material Output
output = root_tree.nodes.new('ShaderNodeOutputLight')
output.location = auto_pos_root(200)
# # LINK NODES #
# ##############
# Link inside group node
if(bpy.app.version >= (4, 0)):
tree.links.new(geo.outputs['Incoming'], vec_transform.inputs['Vector'])
tree.links.new(vec_transform.outputs['Vector'], map_1.inputs['Vector'])
else:
tree.links.new(tex.outputs['Normal'], map_1.inputs['Vector'])
tree.links.new(map_1.outputs['Vector'], sep.inputs['Vector'])
tree.links.new(sep.outputs[0], div_1.inputs[0]) # X -> value0
tree.links.new(sep.outputs[2], div_1.inputs[1]) # Z -> value1
tree.links.new(sep.outputs[1], div_2.inputs[0]) # Y -> value0
tree.links.new(sep.outputs[2], div_2.inputs[1]) # Z -> value1
tree.links.new(div_1.outputs[0], com.inputs[0])
tree.links.new(div_2.outputs[0], com.inputs[1])
tree.links.new(com.outputs['Vector'], map_2.inputs['Vector'])
# Textures
# a) generated texture
tree.links.new(map_2.outputs['Vector'], add.inputs['Color1'])
tree.links.new(add.outputs['Color'], img.inputs['Vector'])
tree.links.new(add.outputs['Color'], group_output_node.inputs[0])
# b) checker texture
tree.links.new(add.outputs['Color'], checker_tex.inputs['Vector'])
tree.links.new(img.outputs['Alpha'], mix_rgb.inputs[0])
tree.links.new(checker_tex.outputs['Color'], mix_rgb.inputs[2])
# Link in root
root_tree.links.new(group.outputs['texture vector'], user_texture.inputs['Vector'])
root_tree.links.new(group.outputs['color'], emission.inputs['Color'])
root_tree.links.new(emission.outputs['Emission'], output.inputs['Surface'])
# Pixel Grid Setup
pixel_grid_group = create_pixel_grid_node_group()
pixel_grid_node = spot.data.node_tree.nodes.new('ShaderNodeGroup')
pixel_grid_node.node_tree = pixel_grid_group
pixel_grid_node.label = "Pixel Grid"
pixel_grid_node.name = 'pixel_grid'
loc = root_tree.nodes['Emission'].location
pixel_grid_node.location = (loc[0], loc[1] - 150)
root_tree.links.new(group.outputs[0], pixel_grid_node.inputs[1])
root_tree.links.new(emission.outputs[0], pixel_grid_node.inputs[0])
def get_resolution(proj_settings, context):
""" Find out what resolution is currently used and return it.
Resolution from the dropdown or the resolution from the custom texture.
"""
if proj_settings.use_custom_texture_res and proj_settings.projected_texture == Textures.CUSTOM_TEXTURE.value:
projector = get_projector(context)
root_tree = projector.children[0].data.node_tree
image = root_tree.nodes['Image Texture'].image
if image:
w = image.size[0]
h = image.size[1]
else:
w, h = 300, 300
else:
w, h = proj_settings.resolution.split('x')
return float(w), float(h)
def update_throw_ratio(proj_settings, context):
"""
Adjust some settings on a camera to achieve a throw ratio
"""
projector = get_projector(context)
# Update properties of the camera.
throw_ratio = proj_settings.get('throw_ratio')
distance = 1
alpha = math.atan((distance/throw_ratio)*.5) * 2
projector.data.lens_unit = 'FOV'
projector.data.angle = alpha
projector.data.sensor_width = 10
projector.data.display_size = 1
# Adjust Texture to fit new camera ###
w, h = get_resolution(proj_settings, context)
aspect_ratio = w/h
inverted_aspect_ratio = 1/aspect_ratio
# Projected Texture
update_projected_texture(proj_settings, context)
# Update spotlight properties.
spot = projector.children[0]
nodes = spot.data.node_tree.nodes['Group'].node_tree.nodes
if bpy.app.version < (2, 81):
nodes['Mapping.001'].scale[0] = 1 / throw_ratio
nodes['Mapping.001'].scale[1] = 1 / throw_ratio * inverted_aspect_ratio
else:
nodes['Mapping.001'].inputs[3].default_value[0] = 1 / throw_ratio
nodes['Mapping.001'].inputs[3].default_value[1] = 1 / \
throw_ratio * inverted_aspect_ratio
# Update lens shift because it depends on the throw ratio.
update_lens_shift(proj_settings, context)
def update_lens_shift(proj_settings, context):
"""
Apply the shift to the camera and texture.
"""
projector = get_projector(context)
h_shift = proj_settings.get('h_shift', 0.0) / 100
v_shift = proj_settings.get('v_shift', 0.0) / 100
throw_ratio = proj_settings.get('throw_ratio')
w, h = get_resolution(proj_settings, context)
inverted_aspect_ratio = h/w
# Update the properties of the camera.
cam = projector
cam.data.shift_x = h_shift
cam.data.shift_y = v_shift * inverted_aspect_ratio
# Update spotlight node setup.
spot = projector.children[0]
nodes = spot.data.node_tree.nodes['Group'].node_tree.nodes
if bpy.app.version < (2, 81):
nodes['Mapping.001'].translation[0] = h_shift / throw_ratio
nodes['Mapping.001'].translation[1] = v_shift / throw_ratio * inverted_aspect_ratio
else:
nodes['Mapping.001'].inputs[1].default_value[0] = h_shift / throw_ratio
nodes['Mapping.001'].inputs[1].default_value[1] = v_shift / throw_ratio * inverted_aspect_ratio
def update_resolution(proj_settings, context):
projector = get_projector(context)
nodes = projector.children[0].data.node_tree.nodes['Group'].node_tree.nodes
# Change resolution image texture
nodes['Image Texture'].image = bpy.data.images[f'_proj.tex.{proj_settings.resolution}']
update_throw_ratio(proj_settings, context)
update_pixel_grid(proj_settings, context)
def update_checker_color(proj_settings, context):
# Update checker texture color
nodes = get_projector(
context).children[0].data.node_tree.nodes['Group'].node_tree.nodes
c = proj_settings.projected_color
nodes['Checker Texture'].inputs['Color2'].default_value = [c.r, c.g, c.b, 1]
def update_power(proj_settings, context):
# Update spotlight power
spot = get_projector(context).children[0]
spot.data.energy = proj_settings["power"]
def update_pixel_grid(proj_settings, context):
""" Update the pixel grid. Meaning, make it visible by linking the right node and updating the resolution. """
root_tree = get_projector(context).children[0].data.node_tree
nodes = root_tree.nodes
pixel_grid_nodes = nodes['pixel_grid'].node_tree.nodes
width, height = get_resolution(proj_settings, context)
pixel_grid_nodes['_width'].outputs[0].default_value = width
pixel_grid_nodes['_height'].outputs[0].default_value = height
if proj_settings.show_pixel_grid:
root_tree.links.new(nodes['pixel_grid'].outputs[0], nodes['Light Output'].inputs[0])
else:
root_tree.links.new(nodes['Emission'].outputs[0], nodes['Light Output'].inputs[0])
def create_pixel_grid_node_group():
node_group = bpy.data.node_groups.new(
'_Projectors-Addon_PixelGrid', 'ShaderNodeTree')
# Create input/output sockets for the node group.
if(bpy.app.version >= (4, 0)):
node_group.interface.new_socket('Shader', socket_type='NodeSocketShader')
node_group.interface.new_socket('Vector', socket_type='NodeSocketVector')
node_group.interface.new_socket('Shader', in_out='OUTPUT', socket_type='NodeSocketShader')
else:
inputs = node_group.inputs
inputs.new('NodeSocketShader', 'Shader')
inputs.new('NodeSocketVector', 'Vector')
outputs = node_group.outputs
outputs.new('NodeSocketShader', 'Shader')
nodes = node_group.nodes
auto_pos = auto_offset()
group_input = nodes.new('NodeGroupInput')
group_input.location = auto_pos(200)
sepXYZ = nodes.new('ShaderNodeSeparateXYZ')
sepXYZ.location = auto_pos(200)
in_width = nodes.new('ShaderNodeValue')
in_width.name = '_width'
in_width.label = 'Width'
in_width.location = auto_pos(100)
in_height = nodes.new('ShaderNodeValue')
in_height.name = '_height'
in_height.label = 'Height'
in_height.location = auto_pos(y=-200)
mul1 = nodes.new('ShaderNodeMath')
mul1.operation = 'MULTIPLY'
mul1.location = auto_pos(100)
mul2 = nodes.new('ShaderNodeMath')
mul2.operation = 'MULTIPLY'
mul2.location = auto_pos(y=-200)
mod1 = nodes.new('ShaderNodeMath')
mod1.operation = 'MODULO'
mod1.inputs[1].default_value = 1
mod1.location = auto_pos(100)
mod2 = nodes.new('ShaderNodeMath')
mod2.operation = 'MODULO'
mod2.inputs[1].default_value = 1
mod2.location = auto_pos(y=-200)
col_ramp1 = nodes.new('ShaderNodeValToRGB')
col_ramp1.color_ramp.elements[1].position = 0.025
col_ramp1.color_ramp.interpolation = 'CONSTANT'
col_ramp1.location = auto_pos(100)
col_ramp2 = nodes.new('ShaderNodeValToRGB')
col_ramp2.color_ramp.elements[1].position = 0.025
col_ramp2.color_ramp.interpolation = 'CONSTANT'
col_ramp2.location = auto_pos(y=-200)
mix_rgb = nodes.new('ShaderNodeMixRGB')
mix_rgb.use_clamp = True
mix_rgb.blend_type = 'MULTIPLY'
mix_rgb.inputs[0].default_value = 1
mix_rgb.location = auto_pos(200)
transparent = nodes.new('ShaderNodeBsdfTransparent')
transparent.location = auto_pos(y=-200)
mix_shader = nodes.new('ShaderNodeMixShader')
mix_shader.location = auto_pos(100)
group_output = nodes.new('NodeGroupOutput')
group_output.location = auto_pos(100)
# Link Nodes
links = node_group.links
links.new(group_input.outputs[0], mix_shader.inputs[2])
links.new(group_input.outputs[1], sepXYZ.inputs[0])
links.new(in_width.outputs[0], mul1.inputs[1])
links.new(in_height.outputs[0], mul2.inputs[1])
links.new(sepXYZ.outputs[0], mul1.inputs[0])
links.new(sepXYZ.outputs[1], mul2.inputs[0])
links.new(mul1.outputs[0], mod1.inputs[0])
links.new(mul2.outputs[0], mod2.inputs[0])
links.new(mod1.outputs[0], col_ramp1.inputs[0])
links.new(mod2.outputs[0], col_ramp2.inputs[0])
links.new(col_ramp1.outputs[0], mix_rgb.inputs[1])
links.new(col_ramp2.outputs[0], mix_rgb.inputs[2])
links.new(mix_rgb.outputs[0], mix_shader.inputs[0])
links.new(transparent.outputs[0], mix_shader.inputs[1])
links.new(mix_shader.outputs[0], group_output.inputs[0])
return node_group
def create_projector(context):
"""
Create a new projector composed out of a camera (parent obj) and a spotlight (child not intended for user interaction).
The camera is the object intended for the user to manipulate and custom properties are stored there.
The spotlight with a custom nodetree is responsible for actual projection of the texture.
"""
create_projector_textures()
log.debug('Creating projector.')
# Create a camera and a spotlight
# ### Spot Light ###
bpy.ops.object.light_add(type='SPOT', location=(0, 0, 0))
spot = context.object
spot.name = 'Projector.Spot'
spot.scale = (.01, .01, .01)
spot.data.spot_size = math.pi - 0.001
spot.data.spot_blend = 0
spot.data.shadow_soft_size = 0.0
spot.hide_select = True
spot[ADDON_ID.format('spot')] = True
spot.data.cycles.use_multiple_importance_sampling = False
add_projector_node_tree_to_spot(spot)
# ### Camera ###
bpy.ops.object.camera_add(enter_editmode=False,
location=(0, 0, 0),
rotation=(0, 0, 0))
cam = context.object
cam.name = 'Projector'
# Parent light to cam.
spot.parent = cam
# Move newly create projector (cam and spotlight) to 3D-Cursor position.
cam.location = context.scene.cursor.location
cam.rotation_euler = context.scene.cursor.rotation_euler
return cam
def init_projector(proj_settings, context):
# # Add custom properties to store projector settings on the camera obj.
proj_settings.throw_ratio = 0.8
proj_settings.power = 1000.0
proj_settings.projected_texture = Textures.CHECKER.value
proj_settings.h_shift = 0.0
proj_settings.v_shift = 0.0
proj_settings.projected_color = random_color()
proj_settings.resolution = '1920x1080'
proj_settings.use_custom_texture_res = True
# Init Projector
update_throw_ratio(proj_settings, context)
update_projected_texture(proj_settings, context)
update_resolution(proj_settings, context)
update_checker_color(proj_settings, context)
update_lens_shift(proj_settings, context)
update_power(proj_settings, context)
update_pixel_grid(proj_settings, context)
class PROJECTOR_OT_create_projector(Operator):
"""Create Projector"""
bl_idname = 'projector.create'
bl_label = 'Create a new Projector'
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def execute(self, context):
projector = create_projector(context)
init_projector(projector.proj_settings, context)
return {'FINISHED'}
def update_projected_texture(proj_settings, context):
""" Update the projected output source. """
projector = get_projectors(context, only_selected=True)[0]
root_tree = projector.children[0].data.node_tree
group_tree = root_tree.nodes['Group'].node_tree
group_output_node = group_tree.nodes['Group Output']
group_node = root_tree.nodes['Group']
emission_node = root_tree.nodes['Emission']
# Switch between the three possible cases by relinking some nodes.
case = proj_settings.projected_texture
if case == Textures.CHECKER.value:
mix_node = group_tree.nodes['Mix.001']
group_tree.links.new(
mix_node.outputs['Color'], group_output_node.inputs[1])
root_tree.links.new(group_node.outputs[1], emission_node.inputs[0])
elif case == Textures.COLOR_GRID.value:
img_node = group_tree.nodes['Image Texture']
group_tree.links.new(img_node.outputs[0], group_output_node.inputs[1])
root_tree.links.new(group_node.outputs[1], emission_node.inputs[0])
elif case == Textures.CUSTOM_TEXTURE.value:
custom_tex_node = root_tree.nodes['Image Texture']
root_tree.links.new(
custom_tex_node.outputs[0], emission_node.inputs[0])
class PROJECTOR_OT_delete_projector(Operator):
"""Delete Projector"""
bl_idname = 'projector.delete'
bl_label = 'Delete Projector'
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return bool(get_projectors(context, only_selected=True))
def execute(self, context):
selected_projectors = get_projectors(context, only_selected=True)
for projector in selected_projectors:
for child in projector.children:
bpy.data.objects.remove(child, do_unlink=True)
else:
bpy.data.objects.remove(projector, do_unlink=True)
return {'FINISHED'}
class ProjectorSettings(bpy.types.PropertyGroup):
throw_ratio: bpy.props.FloatProperty(
name="Throw Ratio",
soft_min=0.4, soft_max=3,
update=update_throw_ratio,
subtype='FACTOR')
power: bpy.props.FloatProperty(
name="Projector Power",
soft_min=0, soft_max=999999,
update=update_power,
unit='POWER')
resolution: bpy.props.EnumProperty(
items=RESOLUTIONS,
default='1920x1080',
description="Select a Resolution for your Projector",
update=update_resolution)
use_custom_texture_res: bpy.props.BoolProperty(
name="Let Image Define Projector Resolution",
default=True,
description="Use the resolution from the image as the projector resolution. Warning: After selecting a new image toggle this checkbox to update",
update=update_throw_ratio)
h_shift: bpy.props.FloatProperty(
name="Horizontal Shift",
description="Horizontal Lens Shift",
soft_min=-20, soft_max=20,
update=update_lens_shift,
subtype='PERCENTAGE')
v_shift: bpy.props.FloatProperty(
name="Vertical Shift",
description="Vertical Lens Shift",
soft_min=-20, soft_max=20,
update=update_lens_shift,
subtype='PERCENTAGE')
projected_color: bpy.props.FloatVectorProperty(
subtype='COLOR',
update=update_checker_color)
projected_texture: bpy.props.EnumProperty(
items=PROJECTED_OUTPUTS,
default=Textures.CHECKER.value,
description="What do you to project?",
update=update_throw_ratio)
show_pixel_grid: bpy.props.BoolProperty(
name="Show Pixel Grid",
description="When checked the image is divided into a pixel grid with the dimensions of the image resolution.",
default=False,
update=update_pixel_grid)
def register():
bpy.utils.register_class(ProjectorSettings)
bpy.utils.register_class(PROJECTOR_OT_create_projector)
bpy.utils.register_class(PROJECTOR_OT_delete_projector)
bpy.utils.register_class(PROJECTOR_OT_change_color_randomly)
bpy.types.Object.proj_settings = bpy.props.PointerProperty(
type=ProjectorSettings)
def unregister():
bpy.utils.unregister_class(PROJECTOR_OT_change_color_randomly)
bpy.utils.unregister_class(PROJECTOR_OT_delete_projector)
bpy.utils.unregister_class(PROJECTOR_OT_create_projector)
bpy.utils.unregister_class(ProjectorSettings)