-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_display_addon.py
1342 lines (1146 loc) · 42.3 KB
/
segment_display_addon.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bpy
import typing
import copy
import os
import mathutils
from bpy.types import Scene, WindowManager, Image, ShaderNodeTree, ShaderNodeGroup
import bpy.utils.previews
bl_info = {
"name": "Segment Display Generator",
"description": "Generates 7 segment displays in various formats and styles.",
"author": "DUDSS",
"version": (1, 0, 1),
"blender": (3, 0, 0),
"location": "3D View > Right sidebar > Segment display",
"warning": "",
"doc_url": "https://github.com/xDUDSSx/segment-display-blender-addon",
"tracker_url": "https://github.com/xDUDSSx/segment-display-blender-addon",
"category": "3D View"
}
################################################################################
# DATA
################################################################################
class SegmentAddonData(bpy.types.PropertyGroup):
# Display type
########################################################################
display_type: bpy.props.EnumProperty(
name = "Display type",
items = [
("numeric", "Numeric", "A display showing decimal numbers, including decimal places."),
("clock", "Clock", "A display showing time in a specific format (hours, minutes, seconds, milliseconds)"),
]
)
digits: bpy.props.IntProperty(
name = "Digits",
min = 1, max = 10,
default = 3
)
fraction_digits: bpy.props.IntProperty(
name = "Decimal places",
min = 0, max = 10,
default = 2
)
millisecond_digits: bpy.props.IntProperty(
name = "Millisecond digits",
min = 0, max = 3,
default = 0
)
second_digits: bpy.props.IntProperty(
name = "Second digits",
min = 0, max = 2,
default = 2
)
minute_digits: bpy.props.IntProperty(
name = "Minute digits",
min = 0, max = 2,
default = 2
)
hour_digits: bpy.props.IntProperty(
name = "Hour digits",
min = 0, max = 3,
default = 0
)
show_dot: bpy.props.BoolProperty(
name = "Show dot",
default = True,
description="Show decimal or millisecond dot separator"
)
show_colons: bpy.props.BoolProperty(
name = "Show colons",
default = True,
description="Separate clock digits with colons"
)
# Display value
########################################################################
# Display value numeric
display_value_numeric: bpy.props.EnumProperty(
name = "Numeric display value",
items = [
("number", "Number", "Show a specific decimal value"),
("frame", "Frame", "Show the current animation frame"),
("timer", "Timer", "Animate the display to go from one value to another"),
]
)
number: bpy.props.FloatProperty(
name = "Number",
min = 0,
default = 43.12
)
frame_divisor: bpy.props.FloatProperty(
name = "Divisor",
min = 0,
default = 1,
description = "What number to divide the frame number with. Since frames are integers to get a changing fractional part use a divisor like 1.237"
)
frame_offset: bpy.props.IntProperty(
name = "Frame offset",
default = 0,
description = "Offsets the current frame number by an amount (Can be negative)"
)
timer_number_from: bpy.props.FloatProperty(
name = "From number",
min = 0,
default = 1
)
timer_number_to: bpy.props.FloatProperty(
name = "To number",
min = 0,
default = 50
)
timer_frame_start: bpy.props.IntProperty(
name = "Frame Start",
min = 0,
default = 50
)
timer_frame_end: bpy.props.IntProperty(
name = "End",
min = 0,
default = 250
)
# Display value clock
display_value_clock: bpy.props.EnumProperty(
name = "Clock display value ",
items = [
("seconds", "Seconds", "Show a specific time for a number of seconds"),
("time", "Time", "Show a specific time"),
("frame", "Frame", "Show the current animation frame converted to time in seconds"),
("timer", "Timer", "Animate the display count down/up from one time to another"),
]
)
hours: bpy.props.IntProperty(
name = "Hours",
min = 0,
default = 12
)
minutes: bpy.props.IntProperty(
name = "Minutes",
min = 0, max = 59,
default = 32
)
seconds: bpy.props.IntProperty(
name = "Seconds",
min = 0, max = 59,
default = 5
)
milliseconds: bpy.props.IntProperty(
name = "Milliseconds",
min = 0,
default = 374
)
number_of_seconds: bpy.props.FloatProperty(
name = "Seconds",
min = 0,
default = 3666.143
)
clock_frame_divisor: bpy.props.FloatProperty(
name = "Divisor",
min = 0,
default = 1,
description = "What number to divide the frame number with. This can be set to the current FPS to show realtime animation time."
)
timer_time_from: bpy.props.FloatProperty(
name = "From time",
min = 0,
default = 90,
description = "The initial time in seconds"
)
timer_time_to: bpy.props.FloatProperty(
name = "To time",
min = 0,
default = 0,
description = "The target time in seconds"
)
# Appeareance
########################################################################
digit_foreground: bpy.props.FloatVectorProperty(
name="Digit foreground",
subtype='COLOR',
default=(1.0, 0.043735, 0),
min=0.0, max=1.0,
description="Color of the lit digit segments"
)
digit_background: bpy.props.FloatVectorProperty(
name="Digit background",
subtype='COLOR',
default=(1.0, 1.0, 1.0),
min=0.0, max=1.0,
description="Color of the unlit digit segments"
)
digit_background_override: bpy.props.BoolProperty(
name = "Set manually",
description = "Specify the digit background. Otherwise it is determined from the foreground color",
default = False
)
background: bpy.props.FloatVectorProperty(
name="Background",
subtype='COLOR',
default=(0.0, 0.0, 0.0),
min=0.0, max=1.0,
description="Color of the display background"
)
emission_strength: bpy.props.FloatProperty(
name = "Emission strength",
min = 0,
default = 2.5
)
normal_strength: bpy.props.FloatProperty(
name = "Normal strength",
min = 0,
default = 0.5
)
hide_background: bpy.props.BoolProperty(
name = "Remove background",
default = False,
description="Deletes the faces making up the background, leaving just the digit segments"
)
skew: bpy.props.FloatProperty(
name = "Skew",
default = 0,
min = -1.5, max = 1.5,
step = 0.05,
description = "Skews the display"
)
extrude: bpy.props.FloatProperty(
name = "Extrude to 3D",
default = 0,
min = -20, max = 20,
description = "Extrudes the display on the Z axis. This may cause issues/glitches with style shaders!"
)
#style: bpy.props.EnumProperty - Set during register()
# Classic style
background_noise_strength: bpy.props.FloatProperty(
name = "BG noise strength",
min = 0,
default = 1
)
background_noise_scale: bpy.props.FloatProperty(
name = "BG noise scale",
default = 2
)
# LCD style
lcd_cell_width: bpy.props.FloatProperty(
name = "Pixel width",
description = "Pixel width factor",
min = 0,
default = 1
)
lcd_cell_height: bpy.props.FloatProperty(
name = "Pixel height",
description = "Pixel height factor",
min = 0,
default = 1
)
lcd_scale: bpy.props.FloatProperty(
name = "Scale",
default = 2
)
lcd_unit_strength: bpy.props.FloatProperty(
name = "Unlit strength",
description = "How bright unlit lcd pixels should appear",
default = 0.010
)
lcd_cell_border_x_width: bpy.props.FloatProperty(
name = "Pixel x gap",
description = "Gap between pixels on the x axis",
default = 0.1
)
lcd_cell_border_y_width: bpy.props.FloatProperty(
name = "Pixel y gap",
description = "Gap between pixels on the y axis",
default = 0.1
)
lcd_cell_subpixel_border_width: bpy.props.FloatProperty(
name = "Subpixel gap",
description = "Gap between individual r,g,b subpixels",
default = 0.02
)
# Advanced
########################################################################
float_correction: bpy.props.FloatProperty(
name = "Float correction",
min = 0,
default = 0.0001
)
auto_background_dim_factor: bpy.props.FloatProperty(
name = "Auto digit BG dim factor",
description = "If digit background is set to auto, it is set to the foreground color multiplied by this factor.",
default = 0.035
)
object_scale: bpy.props.FloatProperty(
name = "Object scale",
default = 0.1
)
join_display: bpy.props.BoolProperty(
name = "Join display into a single object",
default = True
)
fuse_display: bpy.props.BoolProperty(
name = "Physically merge neighbouring digit vertexes",
default = True
)
################################################################################
# UI
################################################################################
class SegmentPanel():
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
@classmethod
def poll(cls, context):
return True
class MainPanel(SegmentPanel, bpy.types.Panel):
bl_label = "Segment display"
bl_idname = "SEGMENT_PT_main_panel"
bl_category = "Segment display"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
def draw(self, context):
layout = self.layout
scene = context.scene
data = scene.segment_addon_data
layout.label(text="Generate 7 segment displays", icon="INFO")
class DisplayTypePanel(SegmentPanel, bpy.types.Panel):
bl_label = "Display type / format"
bl_idname = "SEGMENT_PT_display_type_panel"
bl_parent_id = "SEGMENT_PT_main_panel"
#bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
scene = context.scene
data = scene.segment_addon_data
layout.prop(data, "display_type", expand=True)
layout.use_property_split = True
layout.use_property_decorate = False
col = layout.column(align=True)
if data.display_type == "numeric":
col.prop(data, "digits")
col.prop(data, "fraction_digits")
col.separator()
col.prop(data, "show_dot")
elif data.display_type == "clock":
col.prop(data, "hour_digits")
col.prop(data, "minute_digits")
col.prop(data, "second_digits")
col.prop(data, "millisecond_digits")
col.separator()
col.prop(data, "show_dot")
col.prop(data, "show_colons")
class DisplayValuePanel(SegmentPanel, bpy.types.Panel):
bl_label = "Display value"
bl_idname = "SEGMENT_PT_display_value"
bl_parent_id = "SEGMENT_PT_main_panel"
#bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
scene = context.scene
data = scene.segment_addon_data
if data.display_type == "numeric":
layout.prop(data, "display_value_numeric", expand=True)
layout.use_property_split = True
layout.use_property_decorate = False
col = layout.column(align=True)
if data.display_value_numeric == "number":
col.prop(data, "number")
elif data.display_value_numeric == "frame":
col.prop(data, "frame_divisor")
col.prop(data, "frame_offset")
elif data.display_value_numeric == "timer":
col.prop(data, "timer_number_from")
col.prop(data, "timer_number_to")
col.separator()
col.prop(data, "timer_frame_start")
col.prop(data, "timer_frame_end")
elif data.display_type == "clock":
layout.prop(data, "display_value_clock", expand=True)
if data.display_value_clock == "seconds":
layout.use_property_split = True
layout.use_property_decorate = False
col = layout.column(align=True)
col.prop(data, "number_of_seconds")
elif data.display_value_clock == "time":
row = layout.row(align=True)
row.label(text="Time to show (H, M, S, Ms):", icon="TIME")
row = layout.row(align=True)
row.prop(data, "hours", text="")
row.prop(data, "minutes", text="")
row.prop(data, "seconds", text="")
row.prop(data, "milliseconds", text="")
elif data.display_value_clock == "frame":
layout.use_property_split = True
layout.use_property_decorate = False
col = layout.column(align=True)
col.prop(data, "clock_frame_divisor")
col.prop(data, "frame_offset")
elif data.display_value_clock == "timer":
layout.use_property_split = True
layout.use_property_decorate = False
col = layout.column(align=True)
col.prop(data, "timer_time_from")
col.prop(data, "timer_time_to")
col.separator()
col.prop(data, "timer_frame_start")
col.prop(data, "timer_frame_end")
class DisplayAppearancePanel(SegmentPanel, bpy.types.Panel):
bl_label = "Appeareance"
bl_idname = "SEGMENT_PT_display_appearance"
bl_parent_id = "SEGMENT_PT_main_panel"
#bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
scene = context.scene
data = scene.segment_addon_data
layout.use_property_split = True
layout.use_property_decorate = False
col = layout.column(align=True)
col.prop(data, "digit_foreground")
col2 = col.column(align=True)
col2.enabled = data.digit_background_override
col2.prop(data, "digit_background")
col.prop(data, "digit_background_override")
col.prop(data, "emission_strength")
col.prop(data, "normal_strength")
col.separator()
col.prop(data, "background")
col.prop(data, "hide_background")
col.separator()
col.prop(data, "skew")
col.prop(data, "extrude")
class DisplayStylePanel(SegmentPanel, bpy.types.Panel):
bl_label = "Display style"
bl_idname = "SEGMENT_PT_display_style"
bl_parent_id = "SEGMENT_PT_display_appearance"
def draw(self, context):
layout = self.layout
scene = context.scene
data = scene.segment_addon_data
layout.prop(data, "style", text="", icon="BLANK1")
layout.template_icon_view(data, "style", show_labels=True)
layout.use_property_split = True
layout.use_property_decorate = False
if data.style == 'classic':
col = layout.column(align=True)
col.prop(data, "background_noise_strength")
col.prop(data, "background_noise_scale")
elif data.style == 'lcd':
col = layout.column(align=True)
col.prop(data, "lcd_cell_width")
col.prop(data, "lcd_cell_height")
col.separator()
col.prop(data, "lcd_scale")
col.prop(data, "lcd_unit_strength")
col.separator()
col.prop(data, "lcd_cell_border_x_width")
col.prop(data, "lcd_cell_border_y_width")
col.prop(data, "lcd_cell_subpixel_border_width")
class AdvancedPanel(SegmentPanel, bpy.types.Panel):
bl_label = "Advanced"
bl_idname = "SEGMENT_PT_advanced"
bl_parent_id = "SEGMENT_PT_main_panel"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
scene = context.scene
data = scene.segment_addon_data
layout.use_property_split = True
layout.use_property_decorate = False
col = layout.column(align=True)
col.prop(data, "object_scale")
col.prop(data, "float_correction")
col.prop(data, "auto_background_dim_factor")
layout.use_property_split = False
layout.prop(data, "join_display")
layout.prop(data, "fuse_display")
layout.operator("segment_addon.reset_to_defaults")
class GeneratePanel(SegmentPanel, bpy.types.Panel):
bl_label = "Generate display"
bl_idname = "SEGMENT_PT_generate_display"
bl_parent_id = "SEGMENT_PT_main_panel"
def draw(self, context):
layout = self.layout
scene = context.scene
data = scene.segment_addon_data
layout.operator("segment_addon.create", icon="RESTRICT_VIEW_OFF")
################################################################################
# OPERATORS
################################################################################
class CreateDisplayOperator(bpy.types.Operator):
bl_idname = "segment_addon.create"
bl_label = "Create display"
bl_description = "Create the segment display"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scene = context.scene
data = scene.segment_addon_data
print(f"Creating segment display [digits: {data.digits}]")
print("Blend file path: " + SegmentAddon.addon_blend_path)
# Load resources from the segment blend file
link = False
with bpy.data.libraries.load(SegmentAddon.addon_blend_path, link=link) as (data_src, data_dst):
# Objects that only need to be loaded once
objects_to_load = [SEGMENT_DIGIT, SEGMENT_EMPTY, SEGMENT_DOT, SEGMENT_COLON]
for o in objects_to_load:
if o not in bpy.data.objects:
print("Appending prototype object: " + str(o))
data_dst.objects.append(o)
# Avoid duplicates
#data_dst.meshes = ['segment_digit_mesh']
#data_dst.objects = ['segment_digit', 'segment_empty', 'segment_dot', 'segment_colon']
data_dst.materials = ['.7SegmentDisplay', '.7SegmentDisplayBackground']
data_dst.node_groups = [
'.7SegmentDecimalProcessor', '.7SegmentClockProcessor',
'.7SegmentTimerResolver',
'.7SegmentClassicShader', '.7SegmentPlainShader', '.7SegmentPlainLCDShader'
]
resource = data_dst
segment_addon = SegmentAddon(context, data, resource)
if not segment_addon.validate_data():
msg = "SegmentDisplayAddon: Invalid display type!"
print(msg)
self.report({'ERROR'}, msg)
# Setup material
segment_addon.setup_segment_material()
# Generate digits
digit_prototype = bpy.data.objects[SEGMENT_DIGIT]
generated_objects = []
if data.display_type == "numeric":
segment_addon.create_numeric_display(digit_prototype, generated_objects)
elif data.display_type == "clock":
segment_addon.create_clock_display(digit_prototype, generated_objects)
# Join objects
print(f"SegmentDisplayAddon: Generated {len(generated_objects)} objects!")
bpy.ops.object.select_all(action='DESELECT')
for o in generated_objects:
o.select_set(True)
if len(generated_objects) > 0:
active_obj = generated_objects[0]
bpy.context.view_layer.objects.active = active_obj
# Joining
if data.join_display:
bpy.ops.object.join()
# Rename joined object
active_obj.name = "SegmentDisplay" + data.display_type.capitalize() + data.style.capitalize()
else:
# Name individual objects
for i, o in enumerate(generated_objects, 1):
o.name = "SegmentDisplay" + data.display_type.capitalize() + data.style.capitalize() + "_digit_" + str(i)
# Remove doubles
if data.fuse_display:
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.remove_doubles()
bpy.ops.object.mode_set(mode='OBJECT')
# Apply skew
if data.skew > 0:
skew_value = data.skew
skew_value *= -1
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.shear(value=skew_value, orient_axis='Z', orient_axis_ortho='X', orient_type='GLOBAL')
bpy.ops.object.mode_set(mode='OBJECT')
# Delete background
if data.hide_background:
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.context.object.active_material_index = 1
old_select_mode = SegmentAddon.get_select_mode()
bpy.ops.mesh.select_mode(type="FACE")
bpy.ops.object.material_slot_select()
bpy.ops.mesh.select_all(action='INVERT')
bpy.ops.mesh.delete(type='FACE')
bpy.ops.mesh.select_mode(type=old_select_mode)
bpy.ops.object.mode_set(mode='OBJECT')
# Extrude
if data.extrude != 0:
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
extrude_value = data.extrude
bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={'use_normal_flip': False,
'use_dissolve_ortho_edges': False,
'mirror': False},
TRANSFORM_OT_translate={
'value': (0, 0, extrude_value),
'orient_axis_ortho': 'X',
'orient_type': 'NORMAL',
'constraint_axis': (False, False, True),
'mirror': False,
'use_proportional_edit': False,
'proportional_edit_falloff': 'SMOOTH',
'proportional_size': 1,
'use_proportional_connected': False,
'use_proportional_projected': False,
'snap': False,
'snap_target': 'CLOSEST',
'snap_point': (0, 0, 0),
'snap_align': False,
'snap_normal': (0, 0, 0),
'gpencil_strokes': False,
'cursor_transform': False,
'texture_space': False,
'remove_on_cancel': False,
'view2d_edge_pan': False,
'release_confirm': False,
'use_accurate': False,
'use_automerge_and_split': False,
})
bpy.ops.object.mode_set(mode='OBJECT')
# Move to origin and scale
cursor = bpy.context.scene.cursor.location
# if not data.join_display:
# # We have to move and scale all objects
# for i, o in enumerate(generated_objects, 1):
# o.name = "SegmentDisplay" + data.display_type.capitalize() + data.style.capitalize() + "_digit_" + str(i)
# active_obj.name = "SegmentDisplay" + data.display_type.capitalize() + data.style.capitalize()
# else:
# # Just scale the active object
bpy.ops.transform.translate(
value=(cursor[0], cursor[1], cursor[2]),
orient_axis_ortho='X',
orient_type='GLOBAL',
orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)),
orient_matrix_type='GLOBAL',
mirror=False,
use_proportional_edit=False,
proportional_edit_falloff='SMOOTH',
proportional_size=1,
use_proportional_connected=False,
use_proportional_projected=False,
)
old_pivot_point = bpy.context.scene.tool_settings.transform_pivot_point
bpy.context.scene.tool_settings.transform_pivot_point = 'ACTIVE_ELEMENT'
scale_factor = data.object_scale
bpy.ops.transform.resize(
value=(scale_factor, scale_factor, scale_factor),
orient_type='GLOBAL',
orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)),
orient_matrix_type='GLOBAL',
mirror=False,
use_proportional_edit=False,
proportional_edit_falloff='SMOOTH',
proportional_size=1,
use_proportional_connected=False,
use_proportional_projected=False,
)
bpy.context.scene.tool_settings.transform_pivot_point = old_pivot_point
else:
self.report({'WARNING'}, "SegmentDisplayAddon: No objects generated!")
return {'FINISHED'}
class ResetToDefaultsOperator(bpy.types.Operator):
bl_idname = "segment_addon.reset_to_defaults"
bl_label = "Reset settings to defaults"
bl_description = "Resets all the current settings to their defaults"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scene = context.scene
data = scene.segment_addon_data
print(f"SegmentAddon: Resetting settings to defaults.")
scene.property_unset("segment_addon_data")
self.report({'INFO'}, "SegmentDisplayAddon: Reset settings to defaults.")
return {'FINISHED'}
################################################################################
# CORE
################################################################################
SEGMENT_DIGIT = 'segment_digit'
SEGMENT_EMPTY = 'segment_empty'
SEGMENT_DOT = 'segment_dot'
SEGMENT_COLON = 'segment_colon'
class SegmentAddon:
VC_STEP = 0.1
VC_STEP_FAILSAFE = 0.01
DIGIT_WIDTH = 12
DIGIT_SEPARATOR_WIDTH = 3
addon_directory_path = None
addon_resources_dir = None
addon_blend_path = None
style_previews_dir = None
previews = dict()
style_previews = "styles_preview"
def __init__(self, context, data: SegmentAddonData, resource):
self.context = context
self.data = data
self.resource = resource
def validate_data(self) -> bool:
if self.data.display_type == "numeric":
pass
elif self.data.display_type == "clock":
pass
else:
return False
return True
def create_numeric_display(self, digit_prototype, generated):
offset_step = self.DIGIT_WIDTH
offset = 0
offset = self.create_digits(digit_prototype, offset, self.data.fraction_digits/10.0, self.data.fraction_digits, offset_step, generated)
if self.data.fraction_digits > 0:
self.create_dot(offset, generated)
offset -= self.DIGIT_SEPARATOR_WIDTH
offset = self.create_digits(digit_prototype, offset, 0, self.data.digits, offset_step, generated)
def create_clock_display(self, digit_prototype, generated):
offset_step = self.DIGIT_WIDTH
offset = 0
h = self.data.hour_digits
m = self.data.minute_digits
s = self.data.second_digits
ms = self.data.millisecond_digits
if ms > 0:
offset = self.create_digits(digit_prototype, offset, 0, ms, offset_step, generated)
if s > 0 or m > 0 or h > 0:
self.create_dot(offset, generated)
offset -= self.DIGIT_SEPARATOR_WIDTH
if s > 0:
offset = self.create_digits(digit_prototype, offset, 0.1, s, offset_step, generated)
if m > 0 or h > 0:
self.create_colon(offset, generated)
offset -= self.DIGIT_SEPARATOR_WIDTH
if m > 0:
offset = self.create_digits(digit_prototype, offset, 0.2, m, offset_step, generated)
if h > 0:
self.create_colon(offset, generated)
offset -= self.DIGIT_SEPARATOR_WIDTH
if h > 0:
offset = self.create_digits(digit_prototype, offset, 0.3, h, offset_step, generated)
def setup_segment_material(self):
mat = self.resource.materials[0]
self.setup_segment_display_processor(mat)
self.setup_display_value(mat)
self.setup_display_shader(mat)
bg_mat = self.resource.materials[1]
self.setup_background_material(bg_mat)
# Float correction
mat.node_tree.nodes["segment_base"].inputs[2].default_value = self.data.float_correction
def setup_background_material(self, mat):
mat.node_tree.nodes['RGB'].outputs[0].default_value = (self.data.background.r, self.data.background.g, self.data.background.b, 1.0)
def setup_display_shader(self, mat):
"""
Sets up the node group responsible for styling the segment base mask output.
This is referred to as "display style" in the settings.
"""
# Create the appropriate shader depending on settings
shader_node_group = self.create_display_style_shader(mat)
Utils.move_node(shader_node_group, 1020, 671)
shader_node_group.width = 212
# Connect mask to the shader group
mat.node_tree.links.new(mat.node_tree.nodes['segment_base'].outputs[0], shader_node_group.inputs[0])
# Set style specific settings
if self.data.style == "plain":
pass
elif self.data.style == "classic":
shader_node_group.inputs[5].default_value = self.data.background_noise_strength
shader_node_group.inputs[6].default_value = self.data.background_noise_scale
elif self.data.style == "lcd":
shader_node_group.inputs[5].default_value = self.data.lcd_cell_width
shader_node_group.inputs[6].default_value = self.data.lcd_cell_height
shader_node_group.inputs[7].default_value = self.data.lcd_scale
shader_node_group.inputs[8].default_value = self.data.lcd_unit_strength
# Process and set the rgb cell border
cell_x_ramp = shader_node_group.node_tree.nodes['segment_lcd_shader'].node_tree.nodes['cell_x_ramp']
cell_y_ramp = shader_node_group.node_tree.nodes['segment_lcd_shader'].node_tree.nodes['cell_y_ramp']
x_points = SegmentAddon.lcd_style_calculate_x_ramp(self.data.lcd_cell_border_x_width, self.data.lcd_cell_subpixel_border_width)
y_points = SegmentAddon.lcd_style_calculate_y_ramp(self.data.lcd_cell_border_y_width)
print(x_points)
print(y_points)
for i, p in enumerate(x_points):
cell_x_ramp.color_ramp.elements[i+1].position = p
for i, p in enumerate(y_points):
cell_y_ramp.color_ramp.elements[i+1].position = p
# Set common settings
digit_foreground = self.color_property_to_rgba_tuple(self.data.digit_foreground)
digit_background = self.color_property_to_rgba_tuple(self.data.digit_background)
if not self.data.digit_background_override:
if (self.data.style == 'lcd'):
digit_background = (0., 0., 0., 1.)
else:
dim_factor = self.data.auto_background_dim_factor
digit_background = self.rgba_tuple_multiply(digit_foreground, dim_factor)
shader_node_group.inputs[1].default_value = digit_foreground
shader_node_group.inputs[2].default_value = digit_background
shader_node_group.inputs[3].default_value = self.data.emission_strength
shader_node_group.inputs[4].default_value = self.data.normal_strength
# Connect the shader group to the principled shader
principled = mat.node_tree.nodes['segment_principled']
mat.node_tree.links.new(shader_node_group.outputs[0], principled.inputs[0]) # Base
mat.node_tree.links.new(shader_node_group.outputs[1], principled.inputs[9]) # Roughness
mat.node_tree.links.new(shader_node_group.outputs[2], principled.inputs[19]) # Emission
mat.node_tree.links.new(shader_node_group.outputs[3], principled.inputs[20]) # Emission strength
mat.node_tree.links.new(shader_node_group.outputs[4], principled.inputs[22]) # Normal
def create_display_style_shader(self, mat):
node_tree = None
if self.data.style == "plain":
node_tree = self.resource.node_groups[4]
elif self.data.style == "classic":
node_tree = self.resource.node_groups[3]
elif self.data.style == 'lcd':
node_tree = self.resource.node_groups[5]
# Rename to be unique
node_group = mat.node_tree.nodes.new(type='ShaderNodeGroup')
node_group.node_tree = node_tree
node_group.name = node_tree.name
return node_group
def setup_display_value(self, mat):
"""
Sets up the 7SegmentBase number input to reflect display value settings.
"""
if self.data.display_type == "numeric":
if self.data.display_value_numeric == "number":
self.setup_numeric_number_display_value(mat, self.data.number)
elif self.data.display_value_numeric == "frame":
self.setup_numeric_frame_display_value(mat, self.data.frame_divisor)
elif self.data.display_value_numeric == "timer":
self.setup_numeric_timer_display_value(mat, self.data.timer_number_from, self.data.timer_number_to)
elif self.data.display_type == 'clock':
if self.data.display_value_clock == "seconds":
self.setup_numeric_number_display_value(mat, self.data.number_of_seconds)
elif self.data.display_value_clock == "time":
self.setup_clock_time_display_value(mat)
elif self.data.display_value_clock == "frame":
self.setup_numeric_frame_display_value(mat, self.data.clock_frame_divisor)
elif self.data.display_value_clock == "timer":
self.setup_numeric_timer_display_value(mat, self.data.timer_time_from, self.data.timer_time_to)
def setup_numeric_number_display_value(self, mat, number):
"""
Connects a simple value node to the number input.
"""
# Get number from the settings and set it as input for the segment base group
value_node = mat.node_tree.nodes.new(type="ShaderNodeValue")
segment_base_group = mat.node_tree.nodes['segment_base']
value_node.outputs[0].default_value = number
mat.node_tree.links.new(value_node.outputs[0], segment_base_group.inputs[0])
Utils.move_node(value_node, 450, 300)
# Set divisor to 1
mat.node_tree.nodes["segment_base"].inputs[1].default_value = 1
def setup_numeric_frame_display_value(self, mat, divisor):
"""
Connects an animated frame value node to the number input.
"""
frame_node = Utils.create_frame_value_node(mat.node_tree, self.data.frame_offset)
segment_base_group = mat.node_tree.nodes['segment_base']
mat.node_tree.links.new(frame_node.outputs[0], segment_base_group.inputs[0])
mat.node_tree.nodes["segment_base"].inputs[1].default_value = divisor
Utils.move_node(frame_node, 450, 300)
def setup_numeric_timer_display_value(self, mat, from_value, to_value):
"""
Timer is realized by running the frame value through the 7SegmentTimerResolver node.
"""
# Create frame node
frame_node = Utils.create_frame_value_node(mat.node_tree)
segment_base_group = mat.node_tree.nodes['segment_base']
Utils.move_node(frame_node, 450-190, 300)
# Create timer resolver group
timer_resolver_tree = self.resource.node_groups[2]
timer_resolver_group = mat.node_tree.nodes.new(type='ShaderNodeGroup')
timer_resolver_group.node_tree = timer_resolver_tree
timer_resolver_group.name = timer_resolver_tree.name
Utils.move_node(timer_resolver_group, 463, 370)
# Link nodes
mat.node_tree.links.new(frame_node.outputs[0], timer_resolver_group.inputs[4])
mat.node_tree.links.new(timer_resolver_group.outputs[0], segment_base_group.inputs[0])
# Set divisor to 1
mat.node_tree.nodes["segment_base"].inputs[1].default_value = 1
# Set remaining timer resolver inputs according to settings
timer_resolver_group.inputs[0].default_value = from_value
timer_resolver_group.inputs[1].default_value = to_value
timer_resolver_group.inputs[2].default_value = self.data.timer_frame_start
timer_resolver_group.inputs[3].default_value = self.data.timer_frame_end
def setup_clock_time_display_value(self, mat):
hours = self.data.hours
minutes = self.data.minutes;
seconds = self.data.seconds
milliseconds = self.data.milliseconds
number = hours * 3600 + minutes * 60 + seconds + (milliseconds/1000)
self.setup_numeric_number_display_value(mat, number)
def setup_segment_display_processor(self, mat):
"""
Sets the node group responsible for adjusting the 7SegmentCore number input using the display vertex color.
"""
segment_base_group = mat.node_tree.nodes['segment_base']
number_node = segment_base_group.node_tree.nodes['number_adjusted']
display_node = segment_base_group.node_tree.nodes['display_converted']