forked from Kirpal/Keyboard-Layout-Editor-for-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_keyboard.py
1272 lines (1099 loc) · 56.2 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 legends
def reorderLabels(labels, align):
ret = ["", "", "", "", "", "", "", "", "", "", "", ""]
for pos, label in enumerate(labels):
ret[labelMap[align][pos]] = label
return ret
def reorderSizes(default, individual, align):
ret = [default, default, default, default, default, default,
default, default, default, default, default, default]
if individual:
for pos, size in enumerate(individual):
if size == 0:
ret[labelMap[align][pos]] = default
else:
ret[labelMap[align][pos]] = size
else:
ret = [default, default, default, default, default, default,
default, default, default, default, default, default]
return ret
def reorderColors(default, individual, align):
ret = [default, default, default, default, default, default,
default, default, default, default, default, default]
if len(individual) > 1:
for pos, color in enumerate(individual):
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 + 0.05
# default align
align = 4
# iterate over rows
for rowNum, row in enumerate(layout):
x = 0.05
# 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"] - 0.05
else:
key["w"] = 1 - 0.05
if "h" in prev:
key["h"] = prev["h"]
else:
key["h"] = 1
if "x2" in prev:
key["x2"] = prev["x2"]
if "y2" in prev:
key["y2"] = prev["y2"]
if "w2" in prev:
key["w2"] = prev["w2"] - 0.05
if "h2" in prev:
key["h2"] = prev["h2"] - 0.05
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 "fa" in prev:
rowData["fa"] = prev["fa"]
else:
rowData["fa"] = None
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:
rowData["ry2"] = prev["ry"]
rowData["ry"] = prev["ry"]
elif "ry" in rowData and "r" in prev:
rowData["ry2"] = rowData["ry"]
# if rowData has property set then add it to key
if "c" in rowData:
key["c"] = rowData["c"]
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"]
align = key["a"]
if "f" in rowData:
key["f"] = rowData["f"]
else:
key["f"] = 3
if "fa" in rowData:
key["fa"] = rowData["fa"]
else:
key["fa"] = None
if "f2" in rowData:
key["f2"] = rowData["f2"]
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 "ry2" in rowData:
if "yCoord" in rowData:
key["ry2"] = rowData["ry2"] + rowData["yCoord"]
else:
key["ry2"] = rowData["ry2"]
if "p" in rowData:
key["p"] = rowData["p"].replace("R", "").replace("r", "").replace("0", "").replace("1", "").replace("2", "").replace("3", "").replace("4", "").replace(
"5", "").replace("6", "").replace("7", "").replace("8", "").replace("9", "").replace("SPACE", "").replace("space", "").replace(" ", "")
else:
key["p"] = "DCS"
if key["p"] == "" or key["p"] not in ["DSA", "DCS"]:
key["p"] = "DCS"
# set the text on the key
key["v"] = {}
key["v"]["labels"] = reorderLabels(
value.split('\n'), align)
key["f"] = reorderSizes(key["f"], key["fa"], align)
key["t"] = reorderColors(
None, key["t"].split('\n'), align)
key["v"]["raw"] = value
# set the row and column of the key
key["row"] = rowNum
key["col"] = pos
# set x and y coordinate of key
key["x"] = x
key["y"] = rowData["y"]
if "rx" in key:
key["x"] += key["rx"]
if "ry2" in key:
key["y"] = key["ry2"]
# add the key to the current row
keyboard["rows"][key["row"]].append(key)
keyboard["keyCount"] += 1
# if the previous item isn't a dict
else:
# if rowData has property set then add it to key
if "c" in rowData:
key["c"] = rowData["c"]
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 "fa" in rowData:
key["fa"] = rowData["fa"]
else:
key["fa"] = None
if "f2" in rowData:
key["f2"] = rowData["f2"]
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 "ry2" in rowData:
if "yCoord" in rowData:
key["ry2"] = rowData["ry2"] + rowData["yCoord"]
else:
key["ry2"] = rowData["ry2"]
if "p" in rowData:
key["p"] = rowData["p"].replace("R", "").replace("r", "").replace("0", "").replace("1", "").replace("2", "").replace("3", "").replace("4", "").replace(
"5", "").replace("6", "").replace("7", "").replace("8", "").replace("9", "").replace("SPACE", "").replace("space", "").replace(" ", "")
else:
key["p"] = "DCS"
if key["p"] == "" or key["p"] not in ["DSA", "DCS"]:
key["p"] = "DCS"
key["xCoord"] = 0
key["d"] = False
key["w"] = 1 - 0.05
key["h"] = 1
# set the text on the key
key["v"] = {}
key["v"]["labels"] = reorderLabels(
value.split('\n'), align)
key["f"] = reorderSizes(key["f"], key["fa"], align)
key["t"] = reorderColors(
None, key["t"].split('\n'), 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"]
if "ry2" in key:
key["y"] = key["ry2"]
# add the key to the current row
keyboard["rows"][key["row"]].append(key)
keyboard["keyCount"] += 1
x += key["w"] + 0.05
y += 1 + 0.05
if "ry2" in key:
rowData["ry2"] += 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", "side", "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"
# set width and height of keyboard
width = 0
height = 0
# get fonts from css
if "css" in keyboard:
selectors = []
props = []
rules = []
css = keyboard["css"]
css = re.sub(r'(\@import [^;]+\;|\@font-face [^\}]+\})', "", css)
css = re.split(r'\{|\}', css)
css = filter(None, css)
for idx, item in enumerate(css):
if idx % 2 == 0:
selectors.append(item)
else:
props.append(item)
selectors = [re.split(r" |\,", i) for i in selectors]
selectors = [filter(None, i) for i in selectors]
selectors = [[re.sub(r"\s+", "", s) for s in sList]
for sList in selectors]
props = [i.split(";") for i in props]
fontProps = [None for i in props]
for idx, propList in enumerate(props):
for prop in propList:
if re.sub(r"\s+", "", prop.split(":")[0]) == "font-family":
fontProps[idx] = re.sub(r"\s+", "", prop.split(":")[1])
if fontProps[idx] == None:
selectors[idx] = None
for idx, selector in enumerate(selectors):
if selector is not None and fontProps[idx] is not None:
rules.append({"selectors": selector, "font": fontProps[idx]})
for rule in rules:
for selector in rule["selectors"]:
if selector == "*":
for labelSelector in range(0, 12):
fonts[labelSelector] = rule["font"].replace(
'"', "").replace("'", "")
elif int(selector.replace(".keylabel", "")) >= 0 and int(selector.replace(".keylabel", "")) <= 11:
fonts[int(selector.replace(
".keylabel", ""))] = rule["font"].replace(
'"', "").replace("'", "")
for pos, font in enumerate(fonts):
if font == None or font not in googleFonts.keys():
fonts[pos] = gotham
else:
tempDir = bpy.app.tempdir
urllib.request.urlretrieve(
googleFonts[font], os.path.join(tempDir, font + ".ttf"))
fonts[pos] = bpy.data.fonts.load(
os.path.join(tempDir, font + ".ttf"))
bpy.context.window_manager.progress_begin(keyboard["keyCount"], 0)
bpy.context.window.cursor_set("DEFAULT")
currentKey = 0
# iterate over rows in keyboard
for row in keyboard["rows"]:
# iterate over keys in row
for key in row:
if key["d"] is False:
# new material for key
m = Material()
m.set_cycles()
m.make_material("%s-%s" % (key["row"], key["col"]))
# make new diffuse node
diffuseBSDF = m.nodes['Diffuse BSDF']
# if key color is set convert hex to rgb and set diffuse color
# to that value, otherwise set it to rgba(0.8, 0.8, 0.8,
# 1)/#cccccc
if "c" in key:
c = key["c"]
rgb = hex2rgb(key["c"])
diffuseBSDF.inputs["Color"].default_value = [
rgb[0] / 255, rgb[1] / 255, rgb[2] / 255, 1]
else:
diffuseBSDF.inputs["Color"].default_value = [
0.8, 0.8, 0.8, 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"]
if key["p"] == "DSA":
TL = key["p"] + 'TLF'
TM = key["p"] + 'TMF'
TR = key["p"] + 'TRF'
ML = key["p"] + 'MLF'
MM = key["p"] + 'MMF'
MR = key["p"] + 'MRF'
BL = key["p"] + 'BLF'
BM = key["p"] + 'BMF'
BR = key["p"] + 'BRF'
if key["p"] == "DCS" and key["x2"] + key["w2"] > key["x"] + key["w"]:
TR = key["p"] + 'TRF'
MR = key["p"] + 'MRF'
BR = key["p"] + 'BRF'
if key["p"] == "DCS" and key["x2"] < key["x"]:
TL = key["p"] + 'TLF'
ML = key["p"] + 'MLF'
BL = key["p"] + 'BLF'
# check if key is "stepped"
if "l" in key and key["l"] is True:
ETL = key["p"] + 'TLS'
ETM = key["p"] + 'TMS'
ETR = key["p"] + 'TRS'
EML = key["p"] + 'MLS'
EMM = key["p"] + 'MMS'
EMR = key["p"] + 'MRS'
EBL = key["p"] + 'BLS'
EBM = key["p"] + 'BMS'
EBR = key["p"] + 'BRS'
else:
ETL = key["p"] + 'TLF'
ETM = key["p"] + 'TMF'
ETR = key["p"] + 'TRF'
EML = key["p"] + 'MLF'
EMM = key["p"] + 'MMF'
EMR = key["p"] + 'MRF'
EBL = key["p"] + 'BLF'
EBM = key["p"] + 'BMF'
EBR = key["p"] + '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[
"%s-%s" % (key["row"], key["col"])]
new_obj_enter_tm.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_enter_tr.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_enter_ml.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_enter_mm.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_enter_mr.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_enter_bl.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_enter_bm.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_enter_br.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
# 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"] = 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
# 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
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 + \
0.2 if key["w"] - 1 + 0.2 > 0 else 0.2
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
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 + 0.2
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 + 0.2 if key["w"] - 1 + 0.2 > 0 else 0.2, key[
"h"] - 1 + 0.2, 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 + 0.2
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
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 + \
0.2 if key["w"] - 1 + 0.2 > 0 else 0.2
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[
"%s-%s" % (key["row"], key["col"])]
new_obj_tm.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_tr.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_ml.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_mm.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_mr.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_bl.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_bm.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
new_obj_br.active_material = bpy.data.materials[
"%s-%s" % (key["row"], key["col"])]
# add key to scene
scn.objects.link(new_obj_tl)
scn.objects.link(new_obj_tm)
scn.objects.link(new_obj_tr)
scn.objects.link(new_obj_ml)
scn.objects.link(new_obj_mm)
scn.objects.link(new_obj_mr)
scn.objects.link(new_obj_bl)
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
new_obj_tm.select = True
new_obj_tr.select = True
new_obj_ml.select = True
new_obj_mm.select = True
new_obj_mr.select = True
new_obj_bl.select = True
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_mm
bpy.ops.object.join()
# name the key
if key["v"]["raw"] == "" and key["w"] < 4.5:
new_obj_mm.name = "Blank"
elif key["v"]["raw"] == "" and key["w"] >= 4.5:
new_obj_mm.name = "Space"
else:
new_obj_mm.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
new_led.location[1] = key["y"] + key["h"] / 2
scn.objects.link(new_led)
new_led.name = "led: %s-%s" % (key["row"], key["col"])
for pos, label in enumerate(key["v"]["labels"]):
if label != "":
# new material for legend
m = Material()
m.set_cycles()
m.make_material("legend: %s-%s" %
(key["row"], key["col"]))
if "t" in key and key["t"][pos] != None and "led" in keyboard and hex2rgb(key["t"][pos]) == keyboard["led"]:
# make new emission node
emission = m.makeNode(
'ShaderNodeEmission', 'Emission')
# set legend color
emission.inputs["Color"].default_value = [
keyboard["led"][0] / 255, keyboard["led"][1] / 255, keyboard["led"][2] / 255, 1]
emission.inputs[
"Strength"].default_value = keyboard["led"][3] * 5
# add material output node
materialOutput = m.nodes['Material Output']
# attach emission to material output
m.link(emission, 'Emission',
materialOutput, 'Surface')
else:
# make new diffuse node
diffuseBSDF = m.nodes['Diffuse BSDF']
# if legend color is set convert hex to rgb and set diffuse color
# to that value, otherwise set it to rgba(0.8, 0.8, 0.8,
# 1)/#cccccc
if "t" in key and key["t"][pos] != None:
if len(key["t"]) > 1:
c = key["t"][pos]
rgb = hex2rgb(key["t"][pos])
diffuseBSDF.inputs["Color"].default_value = [
rgb[0] / 255, rgb[1] / 255, rgb[2] / 255, 1]
else:
c = key["t"][0]
rgb = hex2rgb(key["t"][pos])
diffuseBSDF.inputs["Color"].default_value = [
rgb[0] / 255, rgb[1] / 255, rgb[2] / 255, 1]
else:
diffuseBSDF.inputs["Color"].default_value = [
0, 0, 0, 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')
alignText = [
["LEFT", "TOP"],
["CENTER", "TOP"],
["RIGHT", "TOP"],
["LEFT", "CENTER"],
["CENTER", "CENTER"],
["RIGHT", "CENTER"],
["LEFT", "BOTTOM"],
["CENTER", "BOTTOM"],
["RIGHT", "BOTTOM"]
]
# 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]
}.get(p, [0.25, 0.15, 0.25, 0.325])
try:
# add text
new_label = bpy.data.curves.new(
type="FONT", name="keylabel")
new_label = bpy.data.objects.new(
"label", new_label)
new_label.data.body = key[
"v"]["labels"][pos].upper()
new_label.data.font = fonts[pos]
new_label.data.size = key["f"][pos] / 15
new_label.data.text_boxes[0].width = new_obj_mm.dimensions[
0] - (alignLegendsProfile(key["p"])[0] + alignLegendsProfile(key["p"])[2])
new_label.data.text_boxes[0].height = new_obj_mm.dimensions[
1] - (alignLegendsProfile(key["p"])[1] + alignLegendsProfile(key["p"])[3])
new_label.data.text_boxes[
0].y = -1 * (key["f"][pos] / 15)
new_label.data.align_x = alignText[pos][0]
new_label.data.align_y = alignText[pos][1]
new_label.location = [-1 * key["x"] - alignLegendsProfile(
key["p"])[0], key["y"] + alignLegendsProfile(key["p"])[1], 0.8]
new_label.rotation_euler[2] = pi
scn.objects.link(new_label)
scn.update()