forked from Kirpal/Keyboard-Layout-Editor-for-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_keyboard.py
1255 lines (1065 loc) · 53.9 KB
/
import_keyboard.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
"""
This script imports JSON File format files to Blender.
It uses the JSON file downloaded from keyboard-layout-editor.com
Usage:
Execute this script from the "File->Import" menu and choose a JSON file to
open.
"""
# import needed modules
import bpy
import json
import urllib.request
import os
import re
from math import pi
try:
from HTMLParser import HTMLParser
except ImportError:
from html.parser import HTMLParser
labelMap = [
# 0 1 2 3 4 5 6 7 8 9 10 11 // align flags
[0, 6, 2, 8, 9, 11, 3, 5, 1, 4, 7, 10], # 0 = no centering
[1, 7, -1, -1, 9, 11, 4, -1, -1, -1, -1, 10], # 1 = center x
[3, -1, 5, -1, 9, 11, -1, -1, 4, -1, -1, 10], # 2 = center y
[4, -1, -1, -1, 9, 11, -1, -1, -1, -1, -1, 10], # 3 = center x & y
[0, 6, 2, 8, 10, -1, 3, 5, 1, 4, 7, -1], # 4 = center front (default)
[1, 7, -1, -1, 10, -1, 4, -1, -1, -1, -1, -1], # 5 = center front & x
[3, -1, 5, -1, 10, -1, -1, -1, 4, -1, -1, -1], # 6 = center front & y
[4, -1, -1, -1, 10, -1, -1, -1, -1, -1, -1, -1], # 7 = center front & x & y
]
gotham = bpy.data.fonts.load(os.path.join(os.path.dirname(
__file__), "gotham.ttf"))
noto = bpy.data.fonts.load(os.path.join(os.path.dirname(
__file__), "noto.ttf"))
fonts = [None for i in range(0, 12)]
googleFonts = json.load(open(os.path.join(os.path.dirname(
__file__), "fonts.json")))
# Function to parse profiles
fallbackProfile = "DCS"
def parseProfile(profileString, homing):
ret = profileString.upper().replace("R", "").replace("0", "").replace("5", "").replace(
"6", "").replace("7", "").replace("8", "").replace("9", "").replace(" ", "")
if ret != "SASPACE":
ret = ret.replace("SPACE", "")
# Normalize unsculpted SA profile to SA ROW 3
if ret == "SA":
ret = "SA3"
if ret == "" or ret not in ["DSA", "DCS", "SA1", "SA2", "SA3", "SA4", "SASPACE"]:
ret = fallbackProfile
# Set homing profile for supported profiles (currently only SA)
if homing and ret in ["SA1", "SA2", "SA3", "SA4"]:
ret = "SA3D"
return ret
# Function to parse legends
def reorderLabels(labels, align):
ret = ["", "", "", "", "", "", "", "", "", "", "", ""]
for pos, label in enumerate(labels):
ret[labelMap[align][pos]] = label
return ret
def reorderSizes(primary, secondary, individual, align):
if secondary is None:
secondary = primary
ret = [primary, secondary, secondary, secondary, secondary, secondary,
secondary, secondary, secondary, secondary, secondary, secondary]
if individual:
for pos, size in enumerate(individual):
if size == 0:
ret[labelMap[align][pos]] = primary
else:
ret[labelMap[align][pos]] = size
else:
ret = [primary, secondary, secondary, secondary, secondary, secondary,
secondary, secondary, secondary, secondary, secondary, secondary]
return ret
def reorderColors(default, colors, align):
ret = [default, default, default, default, default, default,
default, default, default, default, default, default]
individual = colors.split('\n')
if len(individual) > 1:
for pos, color in enumerate(individual):
if color is not None and color is not '':
ret[labelMap[align][pos]] = color
else:
ret = [individual[0], individual[0], individual[0], individual[0], individual[0], individual[
0], individual[0], individual[0], individual[0], individual[0], individual[0], individual[0]]
return ret
# convert HEX color to RGB
def hex2rgb(hex):
hex = hex.lstrip("#")
if len(hex) == 3:
r = int(str(hex[0:1]) + str(hex[0:1]), 16)
g = int(str(hex[1:2]) + str(hex[1:2]), 16)
b = int(str(hex[2:3]) + str(hex[2:3]), 16)
else:
r = int(str(hex[0:2]), 16)
g = int(str(hex[2:4]), 16)
b = int(str(hex[4:6]), 16)
rgb = [r, g, b]
return rgb
# Make and modify materials
class Material:
def set_cycles(self):
scn = bpy.context.scene
if not scn.render.engine == 'CYCLES':
scn.render.engine = 'CYCLES'
def make_material(self, name):
matNames = []
matPos = {}
for position, material in enumerate(bpy.data.materials):
matNames.append(material.name)
matPos[material.name] = position
if name in matNames:
bpy.data.materials[matPos[name]].name = name + ".000"
self.mat = bpy.data.materials.new(name)
self.mat.use_nodes = True
self.nodes = self.mat.node_tree.nodes
def link(self, from_node, from_slot_name, to_node, to_slot_name):
input = to_node.inputs[to_slot_name]
output = from_node.outputs[from_slot_name]
self.mat.node_tree.links.new(input, output)
def makeNode(self, type, name):
self.node = self.nodes.new(type)
self.node.name = name
self.xpos += 200
self.node.location = self.xpos, self.ypos
return self.node
def new_row():
self.xpos = 0
self.ypos += 200
def __init__(self):
self.xpos = 0
self.ypos = 0
# parses KLE Raw JSON into dict
def getKey(filePath):
# load JSON file
layout = json.load(open(filePath, encoding="UTF-8",
errors="replace"), strict=False)
# make empty keyboard dict
keyboard = {}
rowData = {}
# add list of keyboard rows
keyboard["rows"] = []
keyboard["keyCount"] = 0
y = 0
# default align
align = 4
# iterate over rows
for rowNum, row in enumerate(layout):
x = 0
# add empty row
keyboard["rows"].append([])
# check if item is a row or if it is a dict of keyboard properties
if type(row) != dict:
# get row data from previous row
rowData = rowData
rowData["y"] = y
# iterate over keys in row
for pos, value in enumerate(row):
# check if item is a key or dict of key properties
if type(value) == str:
# key is a dict with all the key's properties
key = {}
# if the previous item is a dict add the data to the rest
# of the row, or the current key, depending on what the
# property is
if type(row[pos - 1]) == dict:
# prev is the previous item in the row
prev = row[pos - 1]
# if prev has property set then add it to key
if "x" in prev:
key["xCoord"] = prev["x"]
x += key["xCoord"]
else:
key["xCoord"] = 0
if "y" in prev:
rowData["yCoord"] = prev["y"]
rowData["y"] += prev["y"]
y += prev["y"]
if "w" in prev:
key["w"] = prev["w"]
else:
key["w"] = 1
if "h" in prev:
key["h"] = prev["h"]
else:
key["h"] = 1
if "fa" in prev:
key["fa"] = prev["fa"]
if "x2" in prev:
key["x2"] = prev["x2"]
if "y2" in prev:
key["y2"] = prev["y2"]
if "w2" in prev:
key["w2"] = prev["w2"]
if "h2" in prev:
key["h2"] = prev["h2"]
if "l" in prev:
key["l"] = prev["l"]
if "n" in prev:
key["n"] = prev["n"]
if "c" in prev:
rowData["c"] = prev["c"]
if "t" in prev:
rowData["t"] = prev["t"]
if "g" in prev:
rowData["g"] = prev["g"]
if "a" in prev:
rowData["a"] = prev["a"]
if "f" in prev:
rowData["f"] = prev["f"]
if "f2" in prev:
rowData["f2"] = prev["f2"]
if "p" in prev:
rowData["p"] = prev["p"]
if "d" in prev:
key["d"] = prev["d"]
else:
key["d"] = False
if "r" in prev:
rowData["r"] = prev["r"]
rowData["rRow"] = 0
if "rx" in prev:
rowData["rx"] = prev["rx"]
if "ry" in prev:
if "yCoord" in rowData:
rowData["ry"] = prev["ry"]
rowData["y"] = prev["ry"] + rowData["yCoord"]
y = prev["ry"] + rowData["yCoord"]
else:
rowData["ry"] = prev["ry"]
rowData["y"] = prev["ry"]
y = prev["ry"]
elif "r" in prev and "ry" in rowData:
if "yCoord" in rowData:
rowData["y"] = rowData["ry"] + rowData["yCoord"]
y = rowData["ry"] + rowData["yCoord"]
else:
rowData["y"] = rowData["ry"]
y = rowData["ry"]
elif "r" in prev:
if "yCoord" in rowData:
rowData["ry"] = 0
rowData["y"] = rowData["yCoord"]
y = rowData["yCoord"]
else:
rowData["ry"] = 0
rowData["y"] = 0
y = 0
# if rowData has property set then add it to key
if "a" in rowData:
align = rowData["a"]
# if the previous item isn't a dict
else:
key["xCoord"] = 0
key["d"] = False
key["w"] = 1
key["h"] = 1
# if rowData has property set then add it to key
if "c" in rowData:
key["c"] = rowData["c"]
else:
key["c"] = "#cccccc"
if "t" in rowData:
key["t"] = rowData["t"]
else:
key["t"] = "#111111"
if "g" in rowData:
key["g"] = rowData["g"]
if "a" in rowData:
key["a"] = rowData["a"]
if "f" in rowData:
key["f"] = rowData["f"]
else:
key["f"] = 3
if "f2" in rowData:
key["f2"] = rowData["f2"]
else:
key["f2"] = None
if "r" in rowData:
key["r"] = rowData["r"]
if "rx" in rowData:
key["rx"] = rowData["rx"]
if "ry" in rowData:
key["ry"] = rowData["ry"]
if "p" not in rowData:
key["p"] = fallbackProfile
else:
key["p"] = parseProfile(rowData["p"], "n" in key)
if "fa" not in key:
key["fa"] = None
# set the text on the key
key["v"] = {}
key["v"]["labels"] = reorderLabels(value.split('\n'), align)
key["f"] = reorderSizes(key["f"], key["f2"], key["fa"], align)
key["t"] = reorderColors(None, key["t"], align)
key["v"]["raw"] = value
# set the row and column of the key
key["row"] = rowNum
key["col"] = pos
# set x and y coordinates of key
key["x"] = x
key["y"] = rowData["y"]
if "rx" in key:
key["x"] += key["rx"]
# add the key to the current row
keyboard["rows"][key["row"]].append(key)
keyboard["keyCount"] += 1
x += key["w"]
y += 1
else:
# if the current item is a dict then add the backcolor property to
# the keyboard
if "backcolor" in row:
keyboard["backcolor"] = row["backcolor"]
if "switchType" in row:
keyboard["switchType"] = row["switchType"]
if "led" in row:
keyboard["led"] = row["led"]
if "css" in row:
keyboard["css"] = row["css"]
return keyboard
def read(filepath):
bpy.context.window.cursor_set("WAIT")
# parse raw data into dict
keyboard = getKey(filepath)
# template objects that have to be appended in and then deleted at the end
defaultObjects = ["DCSTL", "DCSTM", "DCSTR", "DCSML", "DCSMM", "DCSMR", "DCSBL", "DCSBM", "DCSBR", "DCSTLF", "DCSTMF", "DCSTRF", "DCSMLF", "DCSMMF", "DCSMRF", "DCSBLF", "DCSBMF", "DCSBRF", "DCSTLS", "DCSTMS", "DCSTRS", "DCSMLS", "DCSMMS", "DCSMRS", "DCSBLS", "DCSBMS", "DCSBRS",
"DSATL", "DSATM", "DSATR", "DSAML", "DSAMM", "DSAMR", "DSABL", "DSABM", "DSABR", "DSATLF", "DSATMF", "DSATRF", "DSAMLF", "DSAMMF", "DSAMRF", "DSABLF", "DSABMF", "DSABRF", "DSATLS", "DSATMS", "DSATRS", "DSAMLS", "DSAMMS", "DSAMRS", "DSABLS", "DSABMS", "DSABRS",
"SA1TL", "SA1TM", "SA1TR", "SA1ML", "SA1MM", "SA1MR", "SA1BL", "SA1BM", "SA1BR", # SA R1
"SA2TL", "SA2TM", "SA2TR", "SA2ML", "SA2MM", "SA2MR", "SA2BL", "SA2BM", "SA2BR", # SA R2
"SA3TL", "SA3TM", "SA3TR", "SA3ML", "SA3MM", "SA3MR", "SA3BL", "SA3BM", "SA3BR", # SA R3
"SA3DTL", "SA3DTM", "SA3DTR", "SA3DML", "SA3DMM", "SA3DMR", "SA3DBL", "SA3DBM", "SA3DBR", # deep dish
"SA4TL", "SA4TM", "SA4TR", "SA4ML", "SA4MM", "SA4MR", "SA4BL", "SA4BM", "SA4BR", # SA R4
"SASPACETL", "SASPACETM", "SASPACETR", "SASPACEML", "SASPACEMM", "SASPACEMR", "SASPACEBL", "SASPACEBM", "SASPACEBR", # SA SPACE
"SATLF", "SATMF", "SATRF", "SAMLF", "SAMMF", "SAMRF", "SABLF", "SABMF", "SABRF", "SATLS", "SATMS", "SATRS", "SAMLS", "SAMMS", "SAMRS", "SABLS", "SABMS", "SABRS",
"case", "switch", "led"]
# blender file with template objects
templateBlend = os.path.join(os.path.dirname(
__file__), "template.blend", "Object")
# append all the template objects
for key in defaultObjects:
bpy.ops.wm.append(filepath=templateBlend + key,
directory=templateBlend, filename=key)
# get the current scene and change display device so colors are accurate
scn = bpy.context.scene
scn.display_settings.display_device = "None"
bpy.ops.group.create(name="Keyboard")
keyboard_empty = bpy.data.objects.new("Keyboard_whole", None)
keyboard_empty.location = (0, 0, 0)
scn.objects.link(keyboard_empty)
# initialize font list with default font
fonts = [gotham for i in range(0, 12)]
# get fonts from css
if "css" in keyboard:
selectors = []
props = []
css = re.sub(
r'(\@import [^;]+\;|\@font-face [^\}]+\}|\/\*.*\*\/)', "", keyboard["css"])
css = re.findall(r'([^\{]*){([^\}]*)}', css)
css = [[i.strip() for i in pair] for pair in css]
for pair in css:
props = pair[1].split(";")
props = [i.strip() for i in props]
props = filter(None, props)
selectors = filter(None, pair[0].split(","))
fontProperty = list(filter(lambda p: re.sub(r"\s+", "", p.split(":")[0]) == "font-family", props))
if len(fontProperty) > 0:
font = re.sub(r"(\'|\")", "", fontProperty[-1].split(":")[1]).strip()
if font in googleFonts.keys():
tempDir = bpy.app.tempdir
urllib.request.urlretrieve(
googleFonts[font], os.path.join(tempDir, font + ".ttf"))
font = bpy.data.fonts.load(
os.path.join(tempDir, font + ".ttf"))
else:
font = gotham
for selector in selectors:
if selector == "*":
fonts = [font for i in range(0, 12)]
elif re.fullmatch(r".keylabel[0-9][1-2]?") and int(selector.replace(".keylabel", "")) >= 0 and int(selector.replace(".keylabel", "")) <= 11:
fonts[int(selector.replace(
".keylabel", ""))] = font
bpy.context.window_manager.progress_begin(keyboard["keyCount"], 0)
bpy.context.window.cursor_set("DEFAULT")
currentKey = 0
# adjust legends based on keycap type
def alignLegendsProfile(p):
return {
"DCS": [0.25, 0.15, 0.25, 0.325],
"DSA": [0.2, 0.25, 0.2, 0.25],
"SA1": [0.2, 0.23, 0.2, 0.20],
"SA2": [0.2, 0.23, 0.2, 0.23],
"SA3": [0.2, 0.23, 0.2, 0.23],
"SA3D": [0.2, 0.23, 0.2, 0.23],
"SA4": [0.2, 0.23, 0.2, 0.23]
}.get(p, [0.25, 0.15, 0.25, 0.325])
# add text to keys
def addText(font, offset=0.0005):
# add text
new_label = bpy.data.curves.new(
type="FONT", name="keylabel")
new_label = bpy.data.objects.new(
"label", new_label)
label_text = re.sub(
"<br ?/?>", "\n", HTMLParser().unescape(key["v"]["labels"][pos]))
new_label.data.body = label_text
new_label.data.font = font
new_label.data.size = key["f"][pos] / 15
# Here are some computations for the clipping boxes
boxTop = key["y"] + \
alignLegendsProfile(key["p"])[1]
boxLeft = -1 * key["x"] - \
alignLegendsProfile(key["p"])[0]
label_verticalCorrection = - \
0.1 if label_text in [
",", ";", ".", "[", "]"] else 0
boxHeight = key["h"] - (alignLegendsProfile(
key["p"])[1] + alignLegendsProfile(key["p"])[3])
boxWidth = key["w"] - (alignLegendsProfile(
key["p"])[0] + alignLegendsProfile(key["p"])[2])
new_label.data.text_boxes[0].width = boxWidth
new_label.data.text_boxes[0].height = boxHeight + \
label_verticalCorrection * new_label.data.size
new_label.data.text_boxes[0].y = -1 * (
key["f"][pos] / 15) * legendVerticalCorrection[pos]
new_label.data.align_x = alignText[pos][0]
new_label.data.align_y = alignText[pos][1]
new_label.data.extrude = 0.01
new_label.location = [boxLeft, boxTop, 2]
new_label.rotation_euler[2] = pi
scn.objects.link(new_label)
scn.update()
# deselect everything
for obj in scn.objects:
obj.select = False
new_label.select = True
scn.objects.active = new_label
new_label.to_mesh(scn, True, "PREVIEW")
if legendLed:
new_label.active_material = bpy.data.materials["led: %s" %
key["t"][pos]]
else:
new_label.active_material = bpy.data.materials[key["t"][pos]]
bpy.ops.object.convert(target='MESH')
scn.objects.active = new_label
if key["f"][pos] > 6:
bpy.ops.object.modifier_add(type='REMESH')
new_label.modifiers["Remesh"].octree_depth = (4 if len(label_text) == 1 else 7)
new_label.modifiers["Remesh"].use_remove_disconnected = False
bpy.ops.object.modifier_apply(
apply_as='DATA', modifier="Remesh")
bpy.ops.object.modifier_add(type='SHRINKWRAP')
new_label.modifiers["Shrinkwrap"].offset = offset
new_label.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'
new_label.modifiers[
"Shrinkwrap"].use_project_z = True
new_label.modifiers[
"Shrinkwrap"].use_positive_direction = True
new_label.modifiers[
"Shrinkwrap"].use_negative_direction = True
new_label.modifiers[
"Shrinkwrap"].target = new_obj_tl
bpy.ops.object.modifier_apply(
apply_as='DATA', modifier="Shrinkwrap")
# create clipping cube
bpy.ops.mesh.primitive_cube_add(
location=(boxLeft-boxWidth*0.5, boxTop+boxHeight*0.5, 1))
cube = bpy.context.object
cube.scale[0] = 0.5*boxWidth
cube.scale[1] = 0.5*boxHeight
cube.name = 'clipCube'
new_label.select = True
scn.objects.active = new_label
bpy.ops.object.modifier_add(type='BOOLEAN')
bpy.context.object.modifiers["Boolean"].operation = 'INTERSECT'
bpy.context.object.modifiers["Boolean"].object = cube
bpy.ops.object.modifier_apply(
apply_as='DATA', modifier="Boolean")
bpy.data.objects.remove(cube, True)
for edge in bpy.context.object.data.edges:
edge.crease = 1
new_label.location[2] += cap_thickness
# deselect everything
for obj in scn.objects:
obj.select = False
new_label.select = True
new_obj_tl.select = True
scn.objects.active = new_obj_tl
bpy.ops.object.join()
# iterate over rows in keyboard
for row in keyboard["rows"]:
# iterate over keys in row
for key in row:
if key["d"] is False:
if key["c"] not in bpy.data.materials:
# new material for key
m = Material()
m.set_cycles()
m.make_material(key["c"])
# make new diffuse node
diffuseBSDF = m.nodes['Diffuse BSDF']
# convert key color to rgb and set material to that
rgb = hex2rgb(key["c"])
diffuseBSDF.inputs["Color"].default_value = [
rgb[0] / 255, rgb[1] / 255, rgb[2] / 255, 1]
# add material output node
materialOutput = m.nodes['Material Output']
# add glossy node
glossyBSDF = m.makeNode(
'ShaderNodeBsdfGlossy', 'Glossy BSDF')
# set glossy node color to white and roughness to 0.3
glossyBSDF.inputs["Color"].default_value = [1, 1, 1, 1]
glossyBSDF.inputs["Roughness"].default_value = 0.3
# add mix node
mixShader = m.makeNode('ShaderNodeMixShader', 'Mix Shader')
# set mix node factor to 0.8
mixShader.inputs['Fac'].default_value = 0.8
# connect glossy and diffuse nodes to the mix node, and connect
# that to the material output
m.link(glossyBSDF, 'BSDF', mixShader, 1)
m.link(diffuseBSDF, 'BSDF', mixShader, 2)
m.link(mixShader, 'Shader', materialOutput, 'Surface')
new_obj_enter_mm = None
TL = key["p"] + 'TL'
TM = key["p"] + 'TM'
TR = key["p"] + 'TR'
ML = key["p"] + 'ML'
MM = key["p"] + 'MM'
MR = key["p"] + 'MR'
BL = key["p"] + 'BL'
BM = key["p"] + 'BM'
BR = key["p"] + 'BR'
# if key is big ass enter or iso enter
if "x2" in key or "y2" in key or "w2" in key or "h2" in key:
# set default values if they aren't set
if "x2" not in key:
key["x2"] = 0
if "y2" not in key:
key["y2"] = 0
if "w2" not in key:
key["w2"] = 1
if "h2" not in key:
key["h2"] = 1
# set the outcropping x and y
key["x2"] = key["x"] + key["x2"]
key["y2"] = key["y"] + key["y2"]
profile_norow = key["p"].replace("1", "").replace(
"2", "").replace("3", "").replace("4", "")
if profile_norow in ["DSA", "SA"]:
TL = profile_norow + 'TLF'
TM = profile_norow + 'TMF'
TR = profile_norow + 'TRF'
ML = profile_norow + 'MLF'
MM = profile_norow + 'MMF'
MR = profile_norow + 'MRF'
BL = profile_norow + 'BLF'
BM = profile_norow + 'BMF'
BR = profile_norow + 'BRF'
if profile_norow == "DCS" and key["x2"] + key["w2"] > key["x"] + key["w"]:
TR = profile_norow + 'TRF'
MR = profile_norow + 'MRF'
BR = profile_norow + 'BRF'
if profile_norow == "DCS" and key["x2"] < key["x"]:
TL = profile_norow + 'TLF'
ML = profile_norow + 'MLF'
BL = profile_norow + 'BLF'
# check if key is "stepped"
if "l" in key and key["l"] is True:
ETL = profile_norow + 'TLS'
ETM = profile_norow + 'TMS'
ETR = profile_norow + 'TRS'
EML = profile_norow + 'MLS'
EMM = profile_norow + 'MMS'
EMR = profile_norow + 'MRS'
EBL = profile_norow + 'BLS'
EBM = profile_norow + 'BMS'
EBR = profile_norow + 'BRS'
else:
ETL = profile_norow + 'TLF'
ETM = profile_norow + 'TMF'
ETR = profile_norow + 'TRF'
EML = profile_norow + 'MLF'
EMM = profile_norow + 'MMF'
EMR = profile_norow + 'MRF'
EBL = profile_norow + 'BLF'
EBM = profile_norow + 'BMF'
EBR = profile_norow + 'BRF'
# add all the outcropping pieces
new_obj_enter_tl = bpy.data.objects[ETL].copy()
new_obj_enter_tl.data = bpy.data.objects[ETL].data.copy()
new_obj_enter_tl.animation_data_clear()
new_obj_enter_tl.location[0] = key["x2"] * -1 - 0.5
new_obj_enter_tl.location[1] = key["y2"] + 0.5
new_obj_enter_tm = bpy.data.objects[ETM].copy()
new_obj_enter_tm.data = bpy.data.objects[ETM].data.copy()
new_obj_enter_tm.animation_data_clear()
new_obj_enter_tm.location[0] = (
key["x2"] + key["w2"] / 2) * -1
new_obj_enter_tm.location[1] = key["y2"] + 0.5
new_obj_enter_tm.dimensions[0] = key[
"w2"] - 1 + 0.2 if key["w2"] - 1 + 0.2 > 0 else 0.2
new_obj_enter_tr = bpy.data.objects[ETR].copy()
new_obj_enter_tr.data = bpy.data.objects[ETR].data.copy()
new_obj_enter_tr.animation_data_clear()
new_obj_enter_tr.location[0] = key[
"x2"] * -1 - 0.5 - (key["w2"] - 1)
new_obj_enter_tr.location[1] = key["y2"] + 0.5
new_obj_enter_ml = bpy.data.objects[EML].copy()
new_obj_enter_ml.data = bpy.data.objects[EML].data.copy()
new_obj_enter_ml.animation_data_clear()
new_obj_enter_ml.location[0] = key["x2"] * -1 - 0.5
new_obj_enter_ml.location[1] = key[
"y2"] + 0.5 + (key["h2"] - 1) / 2
new_obj_enter_ml.dimensions[1] = key["h2"] - 1 + 0.2
new_obj_enter_mm = bpy.data.objects[EMM].copy()
new_obj_enter_mm.data = bpy.data.objects[EMM].data.copy()
new_obj_enter_mm.animation_data_clear()
new_obj_enter_mm.location[0] = (
key["x2"] + key["w2"] / 2) * -1
new_obj_enter_mm.location[1] = key[
"y2"] + 0.5 + (key["h2"] - 1) / 2
new_obj_enter_mm.dimensions = (key["w2"] - 1 + 0.2 if key["w2"] - 1 + 0.2 > 0 else 0.2, key[
"h2"] - 1 + 0.2, new_obj_enter_mm.dimensions[2])
new_obj_enter_mr = bpy.data.objects[EMR].copy()
new_obj_enter_mr.data = bpy.data.objects[EMR].data.copy()
new_obj_enter_mr.animation_data_clear()
new_obj_enter_mr.location[0] = (
key["x2"]) * -1 - 0.5 - (key["w2"] - 1)
new_obj_enter_mr.location[1] = key[
"y2"] + 0.5 + (key["h2"] - 1) / 2
new_obj_enter_mr.dimensions[1] = key["h2"] - 1 + 0.2
new_obj_enter_bl = bpy.data.objects[EBL].copy()
new_obj_enter_bl.data = bpy.data.objects[EBL].data.copy()
new_obj_enter_bl.animation_data_clear()
new_obj_enter_bl.location[0] = (key["x2"]) * -1 - 0.5
new_obj_enter_bl.location[1] = key[
"y2"] + 0.5 + key["h2"] - 1
new_obj_enter_bm = bpy.data.objects[EBM].copy()
new_obj_enter_bm.data = bpy.data.objects[EBM].data.copy()
new_obj_enter_bm.animation_data_clear()
new_obj_enter_bm.location[0] = (
key["x2"]) * -1 - 0.5 - (key["w2"] - 1) / 2
new_obj_enter_bm.location[1] = key[
"y2"] + 0.5 + key["h2"] - 1
new_obj_enter_bm.dimensions[0] = key[
"w2"] - 1 + 0.2 if key["w2"] - 1 + 0.2 > 0 else 0.2
new_obj_enter_br = bpy.data.objects[EBR].copy()
new_obj_enter_br.data = bpy.data.objects[EBR].data.copy()
new_obj_enter_br.animation_data_clear()
new_obj_enter_br.location[0] = (
key["x2"]) * -1 - 0.5 - (key["w2"] - 1)
new_obj_enter_br.location[1] = key[
"y2"] + 0.5 + key["h2"] - 1
# set outcropping material to the material that was just
# created
new_obj_enter_tl.active_material = bpy.data.materials[key["c"]]
new_obj_enter_tm.active_material = bpy.data.materials[key["c"]]
new_obj_enter_tr.active_material = bpy.data.materials[key["c"]]
new_obj_enter_ml.active_material = bpy.data.materials[key["c"]]
new_obj_enter_mm.active_material = bpy.data.materials[key["c"]]
new_obj_enter_mr.active_material = bpy.data.materials[key["c"]]
new_obj_enter_bl.active_material = bpy.data.materials[key["c"]]
new_obj_enter_bm.active_material = bpy.data.materials[key["c"]]
new_obj_enter_br.active_material = bpy.data.materials[key["c"]]
# add outcropping to scene
scn.objects.link(new_obj_enter_tl)
scn.objects.link(new_obj_enter_tm)
scn.objects.link(new_obj_enter_tr)
scn.objects.link(new_obj_enter_ml)
scn.objects.link(new_obj_enter_mm)
scn.objects.link(new_obj_enter_mr)
scn.objects.link(new_obj_enter_bl)
scn.objects.link(new_obj_enter_bm)
scn.objects.link(new_obj_enter_br)
# deselect everything
for obj in scn.objects:
obj.select = False
# combine all the pieces
new_obj_enter_tl.select = True
new_obj_enter_tm.select = True
new_obj_enter_tr.select = True
new_obj_enter_ml.select = True
new_obj_enter_mm.select = True
new_obj_enter_mr.select = True
new_obj_enter_bl.select = True
new_obj_enter_bm.select = True
new_obj_enter_br.select = True
scn.objects.active = new_obj_enter_mm
bpy.ops.object.join()
else:
# set default values if they aren't set
if "x2" not in key:
key["x2"] = key["x"]
if "y2" not in key:
key["y2"] = key["y"]
if "w2" not in key:
key["w2"] = key["w"]
if "h2" not in key:
key["h2"] = key["h"]
# check if we need the middle strip;
# force middle strip for DCS and DSA
if key["p"] in ["DCS", "DSA"] or key["h"] - 1 - 0.1 > 0:
middleh_needed = True
else:
middleh_needed = False
if key["p"] in ["DCS", "DSA"] or key["w"] - 1 - 0.1 > 0:
middlew_needed = True
else:
middlew_needed = False
# define overlap
overlap = 0.2 if key["p"] in ["DCS", "DSA"] else 0.0
# add all the key pieces
new_obj_tl = bpy.data.objects[TL].copy()
new_obj_tl.data = bpy.data.objects[TL].data.copy()
new_obj_tl.animation_data_clear()
new_obj_tl.location[0] = key["x"] * -1 - 0.5
new_obj_tl.location[1] = key["y"] + 0.5
if middlew_needed:
new_obj_tm = bpy.data.objects[TM].copy()
new_obj_tm.data = bpy.data.objects[TM].data.copy()
new_obj_tm.animation_data_clear()
new_obj_tm.location[0] = (key["x"] + key["w"] / 2) * -1
new_obj_tm.location[1] = key["y"] + 0.5
new_obj_tm.dimensions[0] = key["w"] - 1 + \
overlap if key["w"] - 1 + overlap > 0 else overlap
new_obj_tr = bpy.data.objects[TR].copy()
new_obj_tr.data = bpy.data.objects[TR].data.copy()
new_obj_tr.animation_data_clear()
new_obj_tr.location[0] = key["x"] * -1 - 0.5 - (key["w"] - 1)
new_obj_tr.location[1] = key["y"] + 0.5
if middleh_needed:
new_obj_ml = bpy.data.objects[ML].copy()
new_obj_ml.data = bpy.data.objects[ML].data.copy()
new_obj_ml.animation_data_clear()
new_obj_ml.location[0] = key["x"] * -1 - 0.5
new_obj_ml.location[1] = key["y"] + \
0.5 + (key["h"] - 1) / 2
new_obj_ml.dimensions[1] = key["h"] - 1 + overlap
if middlew_needed:
new_obj_mm = bpy.data.objects[MM].copy()
new_obj_mm.data = bpy.data.objects[MM].data.copy()
new_obj_mm.animation_data_clear()
new_obj_mm.location[0] = (key["x"] + key["w"] / 2) * -1
new_obj_mm.location[1] = key["y"] + \
0.5 + (key["h"] - 1) / 2
new_obj_mm.dimensions = (key["w"] - 1 + overlap if key["w"] - 1 + overlap >
0 else overlap, key["h"] - 1 + overlap, new_obj_mm.dimensions[2])
new_obj_mr = bpy.data.objects[MR].copy()
new_obj_mr.data = bpy.data.objects[MR].data.copy()
new_obj_mr.animation_data_clear()
new_obj_mr.location[0] = (
key["x"]) * -1 - 0.5 - (key["w"] - 1)
new_obj_mr.location[1] = key["y"] + \
0.5 + (key["h"] - 1) / 2
new_obj_mr.dimensions[1] = key["h"] - 1 + overlap
new_obj_bl = bpy.data.objects[BL].copy()
new_obj_bl.data = bpy.data.objects[BL].data.copy()
new_obj_bl.animation_data_clear()
new_obj_bl.location[0] = (key["x"]) * -1 - 0.5
new_obj_bl.location[1] = key["y"] + 0.5 + key["h"] - 1
if middlew_needed:
new_obj_bm = bpy.data.objects[BM].copy()
new_obj_bm.data = bpy.data.objects[BM].data.copy()
new_obj_bm.animation_data_clear()
new_obj_bm.location[0] = (
key["x"]) * -1 - 0.5 - (key["w"] - 1) / 2
new_obj_bm.location[1] = key["y"] + 0.5 + key["h"] - 1
new_obj_bm.dimensions[0] = key["w"] - 1 + \
overlap if key["w"] - 1 + overlap > 0 else overlap
new_obj_br = bpy.data.objects[BR].copy()
new_obj_br.data = bpy.data.objects[BR].data.copy()
new_obj_br.animation_data_clear()
new_obj_br.location[0] = (key["x"]) * -1 - 0.5 - (key["w"] - 1)
new_obj_br.location[1] = key["y"] + 0.5 + key["h"] - 1
# set key material to the material that was just created
new_obj_tl.active_material = bpy.data.materials[key["c"]]
if middlew_needed:
new_obj_tm.active_material = bpy.data.materials[key["c"]]
new_obj_tr.active_material = bpy.data.materials[key["c"]]
if middleh_needed:
new_obj_ml.active_material = bpy.data.materials[key["c"]]
if middlew_needed:
new_obj_mm.active_material = bpy.data.materials[key["c"]]
new_obj_mr.active_material = bpy.data.materials[key["c"]]
new_obj_bl.active_material = bpy.data.materials[key["c"]]
if middlew_needed:
new_obj_bm.active_material = bpy.data.materials[key["c"]]
new_obj_br.active_material = bpy.data.materials[key["c"]]
# add key to scene
scn.objects.link(new_obj_tl)
if middlew_needed:
scn.objects.link(new_obj_tm)
scn.objects.link(new_obj_tr)
if middleh_needed:
scn.objects.link(new_obj_ml)
if middlew_needed:
scn.objects.link(new_obj_mm)
scn.objects.link(new_obj_mr)
scn.objects.link(new_obj_bl)
if middlew_needed:
scn.objects.link(new_obj_bm)
scn.objects.link(new_obj_br)
# deselect everything
for obj in scn.objects:
obj.select = False
# combine all the pieces
new_obj_tl.select = True
if middlew_needed:
new_obj_tm.select = True
new_obj_tr.select = True
if middleh_needed:
new_obj_ml.select = True
if middlew_needed:
new_obj_mm.select = True
new_obj_mr.select = True
new_obj_bl.select = True
if middlew_needed:
new_obj_bm.select = True
new_obj_br.select = True
# if outcropping exists add it to the key
if new_obj_enter_mm is not None:
new_obj_enter_mm.select = True
scn.objects.active = new_obj_tl
bpy.ops.object.join()
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.remove_doubles()
bpy.ops.mesh.faces_shade_smooth()
bpy.ops.object.mode_set(mode='OBJECT')
# name the key
if key["v"]["raw"] == "" and key["w"] < 4.5:
new_obj_tl.name = "Blank"
elif key["v"]["raw"] == "" and key["w"] >= 4.5:
new_obj_tl.name = "Space"
else:
new_obj_tl.name = HTMLParser().unescape(
key["v"]["raw"].replace("\n", " "))
# add key switch
new_switch = bpy.data.objects["switch"].copy()
new_switch.data = bpy.data.objects["switch"].data.copy()
new_switch.animation_data_clear()
new_switch.location[0] = (key["x"]) * -1 - (key["w"]) / 2
new_switch.location[1] = key["y"] + key["h"] / 2
scn.objects.link(new_switch)
new_switch.name = "switch: %s-%s" % (key["row"], key["col"])
if "led" in keyboard:
# add led
new_led = bpy.data.objects["led"].copy()
new_led.data = bpy.data.objects["led"].data.copy()
new_led.animation_data_clear()
new_led.location[0] = (key["x"]) * -1 - (key["w"]) / 2