-
Notifications
You must be signed in to change notification settings - Fork 29
/
modifymaterial.py
2131 lines (1866 loc) · 117 KB
/
modifymaterial.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 file performs the following operations
· Remove unused material slots on all objects
· Remap duplicate slots on all objects
· Replace all body materials with material templates from the KK Shader file
· Replace all hair materials with KK Shader templates
· Replace all outfit materials with KK Shader templates
· Replace shadowcast materials
· Replace tears and tongue and gag eye mats with templates
· Remove all duplicate node groups after importing everything
· Get shadow colors from MaterialData json and store on Body object
· Import all textures from .pmx directory to blender and create dark variants of all maintex files
· Saturate all maintexes using the in game LUTs
· Load images to correct spot on the body template
· Load images to correct spot on the face template
· Load Eyes, eyeline, tongue, gag eyes, tears, all hairs, all outfits
· Set materials correctly based on mainImage and plain mainImage presence
· Import all colors from the json, saturate them and apply them to the shaders
· Create dark colors for all colors, saturate them and apply them to the shaders
· Sets up the gag eye material drivers using the values from the gag eye shapekeys
· Adds an outline modifier and outline materials to the face, body, hair and outfit meshes (based on single outline mode choice)
· Sets up the GFN empty
· Autopack all files
Color and image saturation code taken from MediaMoots https://github.com/FlailingFog/KK-Blender-Porter-Pack/blob/ecad6a136e86aaf6c51194705157200797f91e5f/importing/importcolors.py
Dark color conversion code taken from Xukmi https://github.com/xukmi/KKShadersPlus/tree/main/Shaders
'''
import bpy, sys, os, bgl, gpu, numpy, math, time
from pathlib import Path
from .. import common as c
class modify_material(bpy.types.Operator):
bl_idname = "kkbp.modifymaterial"
bl_label = bl_idname
bl_description = bl_idname
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
try:
self.retreive_stored_tags()
self.remove_unused_material_slots()
self.remap_duplcate_material_slots()
self.replace_materials_for_body()
self.replace_materials_for_hair()
self.replace_materials_for_outfits()
self.replace_materials_for_shadowcast()
self.replace_materials_for_tears_tongue_gageye()
self.remove_duplicate_node_groups()
self.load_images_and_shadow_colors()
self.link_textures_for_body()
self.link_textures_for_hair()
self.link_textures_for_clothes()
self.link_textures_for_tongue_tear_gag()
self.import_and_setup_gfn()
self.setup_gag_eye_material_drivers()
self.add_outlines_to_body()
self.add_outlines_to_hair()
self.add_outlines_to_clothes()
self.load_luts()
self.convert_main_textures()
self.load_json_colors()
self.set_color_management()
c.clean_orphaned_data()
return {'FINISHED'}
except Exception as error:
c.handle_error(self, error)
return {"CANCELLED"}
# %% Main functions
def retreive_stored_tags(self):
'''Gets the tag from each object to repopulate the class variables below'''
self.hairs = []
self.outfits = []
self.outfit_alternates = []
self.hitboxes = []
for object in [o for o in bpy.data.objects if o.type == 'MESH']:
if object.get('KKBP tag'):
if object['KKBP tag'] == 'body':
self.body = object
elif object['KKBP tag'] == 'outfit':
self.outfits.append(object)
elif object['KKBP tag'] == 'alt':
self.outfit_alternates.append(object)
elif object['KKBP tag'] == 'hair':
self.hairs.append(object)
elif object['KKBP tag'] == 'hitbox':
self.hitboxes.append(object)
for object in [o for o in bpy.data.objects if o.type == 'ARMATURE']:
if object.get('KKBP tag'):
if object['KKBP tag'] == 'armature':
self.armature = object
c.print_timer('retreive_stored_tags')
def remove_unused_material_slots(self):
'''Remove unused mat slots on all visible objects'''
for object in [o for o in bpy.data.objects if o.type == 'MESH']:
try:
c.switch(object, 'object')
bpy.ops.object.material_slot_remove_unused()
except:
pass
c.print_timer('remove_unused_material_slots')
def remap_duplcate_material_slots(self):
c.switch(self.body, 'object')
for cat in [[self.body], self.hairs, self.outfit_alternates, self.outfits]:
for obj in cat:
#combine duplicated material slots
c.switch(obj, 'object')
bpy.ops.object.material_slot_remove_unused()
c.switch(obj, 'edit')
#remap duplicate materials to the base one
material_list = obj.data.materials
for mat in material_list:
mat_name_list = []
mat_name_list.extend(self.body['SMR materials']['cf_Ohitomi_L02'])
mat_name_list.extend(self.body['SMR materials']['cf_Ohitomi_R02'])
mat_name_list.extend(self.body['SMR materials']['cf_Ohitomi_L'])
mat_name_list.extend(self.body['SMR materials']['cf_Ohitomi_R'])
mat_name_list.extend(self.body['SMR materials']['cf_O_namida_L'])
mat_name_list.extend(self.body['SMR materials']['cf_O_namida_M'])
mat_name_list.extend(self.body['SMR materials']['cf_O_namida_S'])
mat_name_list.extend(self.body['SMR materials']['o_tang'])
mat_name_list.extend(self.body['SMR materials']['o_tang_rigged'])
#don't merge the above materials if categorize by SMR is chosen.
eye_flag = mat.name not in mat_name_list if bpy.context.scene.kkbp.categorize_dropdown == 'D' else True
if '.' in mat.name[-4:] and eye_flag:
try:
#the material name is normal
base_name, dupe_number = mat.name.split('.',2)
except:
#someone (not naming names) left a .### in the material name
base_name, rest_of_base_name, dupe_number = mat.name.split('.',2)
base_name = base_name + rest_of_base_name
#remap material if it's a dupe, but don't touch the eye dupe
if material_list.get(base_name) and int(dupe_number) and 'cf_m_hitomi_00' not in base_name and self.body['SMR materials']['o_tang'][0] not in base_name:
mat.user_remap(material_list[base_name])
bpy.data.materials.remove(mat)
else:
c.kklog("Somehow found a false duplicate material but didn't merge: " + mat.name, 'warn')
#then clean material slots by going through each slot and reassigning the slots that are repeated
repeats = {}
for index, mat in enumerate(material_list):
if mat.name not in repeats:
repeats[mat.name] = [index]
# print("First entry of {} in slot {}".format(mat.name, index))
else:
repeats[mat.name].append(index)
# print("Additional entry of {} in slot {}".format(mat.name, index))
for material_name in list(repeats.keys()):
if len(repeats[material_name]) > 1:
for repeated_slot in repeats[material_name]:
#don't touch the first slot
if repeated_slot == repeats[material_name][0]:
continue
c.kklog("Moving duplicate material {} in slot {} to the original slot {}".format(material_name, repeated_slot, repeats[material_name][0]))
obj.active_material_index = repeated_slot
bpy.ops.object.material_slot_select()
obj.active_material_index = repeats[material_name][0]
bpy.ops.object.material_slot_assign()
bpy.ops.mesh.select_all(action='DESELECT')
c.switch(obj, 'object')
bpy.ops.object.material_slot_remove_unused()
c.print_timer('remap_duplcate_material_slots')
def replace_materials_for_body(self):
c.switch(self.body, 'object')
templateList = [
'KK Body',
'KK Outline',
'KK Body Outline',
'KK Tears',
'KK Gag00',
'KK Gag01',
'KK Gag02',
'KK EyeR (hitomi)',
'KK EyeL (hitomi)',
'KK Eyebrows (mayuge)',
'KK Eyeline down',
'KK Eyeline Kage',
'KK Eyeline up',
'KK Eyewhites (sirome)',
'KK Face',
'KK General',
'KK Hair',
'KK Mixed Metal or Shiny',
'KK Nose',
'KK Shadowcast',
'KK Teeth (tooth)',
'KK Fangs (tooth.001)',
'KK Simple'
]
c.import_from_library_file(category='Material', list_of_items=templateList, use_fake_user=True)
#Replace all materials on the body with templates
def swap_body_material(original, template):
for mat in original:
try:
self.body.material_slots[mat].material = bpy.data.materials[template]
except:
c.kklog('material or template wasn\'t found when replacing body materials: ' + str(mat) + ' / ' + template, 'warn')
swap_body_material(self.body['SMR materials']['cf_O_face'],'KK Face')
swap_body_material(self.body['SMR materials']['cf_O_mayuge'],'KK Eyebrows (mayuge)')
swap_body_material(self.body['SMR materials']['cf_O_noseline'],'KK Nose')
swap_body_material(self.body['SMR materials']['cf_O_eyeline'],'KK Eyeline up')
swap_body_material(self.body['SMR materials']['cf_O_eyeline_low'],'KK Eyeline down')
swap_body_material(['cf_m_eyeline_kage'],'KK Eyeline Kage')
swap_body_material(['Eyeline_Over'],'KK Eyeline Kage')
swap_body_material(self.body['SMR materials']['cf_Ohitomi_L'],'KK Eyewhites (sirome)')
swap_body_material(self.body['SMR materials']['cf_Ohitomi_R'],'KK Eyewhites (sirome)')
swap_body_material(self.body['SMR materials']['cf_Ohitomi_L02'],'KK EyeL (hitomi)')
swap_body_material(self.body['SMR materials']['cf_Ohitomi_R02'],'KK EyeR (hitomi)')
swap_body_material(self.body['SMR materials']['o_body_a'],'KK Body')
swap_body_material(self.body['SMR materials']['cf_O_tooth'],'KK Teeth (tooth)')
swap_body_material([self.body['SMR materials']['cf_O_tooth'][0] + '.001'],'KK Fangs (tooth.001)')
swap_body_material(self.body['SMR materials']['o_tang'],'KK General')
c.print_timer('replace_materials_for_body')
def replace_materials_for_hair(self):
'''Replace all of the Hair materials with hair templates and name accordingly'''
for hair in self.hairs:
for original_material in hair.material_slots:
template = bpy.data.materials['KK Hair'].copy()
template.name = 'KK ' + original_material.name
original_material.material = bpy.data.materials[template.name]
c.print_timer('replace_materials_for_hair')
def replace_materials_for_outfits(self):
#Replace all other materials with the general template and name accordingly
for cat in [self.outfits, self.outfit_alternates]:
for ob in cat:
for original_material in ob.material_slots:
template = bpy.data.materials['KK General'].copy()
template.name = 'KK ' + original_material.name
original_material.material = bpy.data.materials[template.name]
c.print_timer('replace_materials_for_outfits')
def replace_materials_for_shadowcast(self):
#give the shadowcast object a template as well
if bpy.data.objects.get('Shadowcast'):
shadowcast = bpy.data.objects['Shadowcast']
template = bpy.data.materials['KK Shadowcast']
shadowcast.material_slots[0].material = bpy.data.materials[template.name]
c.print_timer('replace_materials_for_shadowcast')
def replace_materials_for_tears_tongue_gageye(self):
#give the tears a material template
if bpy.data.objects.get('Tears'):
tears = bpy.data.objects['Tears']
template = bpy.data.materials['KK Tears']
tears.material_slots[0].material = bpy.data.materials[template.name]
#Make the tongue material unique so parts of the General Template aren't overwritten
tongue_template = bpy.data.materials['KK General'].copy()
tongue_template.name = 'KK Tongue'
self.body.material_slots['KK General'].material = tongue_template
#Make the texture group unique
newNode = tongue_template.node_tree.nodes['Gentex'].node_tree.copy()
tongue_template.node_tree.nodes['Gentex'].node_tree = newNode
newNode.name = 'Tongue Textures'
#Make the tongue shader group unique
newNode = tongue_template.node_tree.nodes['Shader'].node_tree.copy()
tongue_template.node_tree.nodes['Shader'].node_tree = newNode
newNode.name = 'Tongue Shader'
#give the rigged tongue the existing material template
if bpy.data.objects.get('Tongue (rigged)'):
tongue = bpy.data.objects['Tongue (rigged)']
tongue.material_slots[0].material = bpy.data.materials['KK Tongue']
#give the gag eyes a material template if they exist and have shapekeys setup
if bpy.data.objects.get('Gag Eyes'):
gag = bpy.data.objects['Gag Eyes']
gag.material_slots[self.body['SMR materials']['cf_O_gag_eye_00'][0]].material = bpy.data.materials['KK Gag00']
gag.material_slots[self.body['SMR materials']['cf_O_gag_eye_01'][0]].material = bpy.data.materials['KK Gag01']
gag.material_slots[self.body['SMR materials']['cf_O_gag_eye_02'][0]].material = bpy.data.materials['KK Gag02']
c.print_timer('replace_materials_for_tears_tongue_gageye')
def remove_duplicate_node_groups(self):
# Get rid of the duplicate node groups cause there's a lot
#stolen from somewhere
def eliminate(node):
node_groups = bpy.data.node_groups
# Get the node group name as 3-tuple (base, separator, extension)
(base, sep, ext) = node.node_tree.name.rpartition('.')
# Replace the numeric duplicate
if ext.isnumeric():
if base in node_groups:
#print(" Replace '%s' with '%s'" % (node.node_tree.name, base))
node.node_tree.use_fake_user = False
node.node_tree = node_groups.get(base)
#--- Search for duplicates in actual node groups
node_groups = bpy.data.node_groups
for group in node_groups:
for node in group.nodes:
if node.type == 'GROUP':
eliminate(node)
#--- Search for duplicates in materials
mats = list(bpy.data.materials)
worlds = list(bpy.data.worlds)
for mat in mats + worlds:
if mat.use_nodes:
for node in mat.node_tree.nodes:
if node.type == 'GROUP':
eliminate(node)
c.print_timer('remove_duplicate_node_groups')
def load_images_and_shadow_colors(self):
'''Load all images from the pmx folder'''
c.switch(self.body, 'object')
#get images for body object
directory = bpy.context.scene.kkbp.import_dir + ('/' if (sys.platform == 'linux' or sys.platform == 'darwin') else '\\')
fileList = Path(directory).glob('*.*')
files = [file for file in fileList if file.is_file()]
#c.kklog('this is my directory')
#c.kklog(directory)
#c.kklog('gettin this filelist')
#c.kklog(files)
#get images from outfit directory based on outfit ID numbers
id_list = []
for obj in [obj for obj in bpy.data.objects if obj.get('KKBP outfit ID') != None and obj.type == 'MESH']:
if obj['KKBP outfit ID'] not in id_list:
id_list.append(obj['KKBP outfit ID'])
#c.kklog(id_list)
for outfit_id in id_list:
#c.kklog(directory)
#c.kklog(directory + 'Outfit ' + str(outfit_id))
#c.kklog(Path(directory + 'Outfit ' + str(outfit_id)))
fileList = Path(directory + 'Outfit ' + str(outfit_id)).glob('*.*')
files_to_append = [file for file in fileList if file.is_file()]
#c.kklog('gettin this additional clothes fileList')
#c.kklog(files_to_append)
for outfit_file in files_to_append:
files.append(outfit_file)
#get shadow colors for each material and store the dictionary on the body object
json_material_data = c.get_json_file('KK_MaterialData.json')
color_dict = {}
supporting_entries = ['Shader Forge/create_body', 'Shader Forge/create_head', 'Shader Forge/create_eyewhite', 'Shader Forge/create_eye', 'Shader Forge/create_topN']
for line in json_material_data:
if line['MaterialName'] in supporting_entries:
line['MaterialName'] = line['MaterialName'].replace('create_','').replace('_create','')
labels = line['ShaderPropNames']
data = line['ShaderPropTextures']
data.extend(line['ShaderPropTextureValues'])
data.extend(line['ShaderPropColorValues'])
data.extend(line['ShaderPropFloatValues'])
data = dict(zip(labels, data))
for entry in data:
if '_ShadowColor ' in entry:
color_dict[line['MaterialName']] = data[entry]
break
#default to [.764, .880, 1] if shadow color is not available for the material
color_dict[line['MaterialName']] = {"r":0.764,"g":0.880,"b":1,"a":1}
self.body['KKBP shadow colors'] = color_dict
#open all images into blender and create dark variants if the image is a maintex
#c.kklog('gettin these files')
#c.kklog(files)
for image in files:
bpy.ops.image.open(filepath=str(image), use_udim_detecting=False)
try:
bpy.data.images[image.name].pack()
except:
c.kklog('This image was not automatically loaded in because its name exceeds 64 characters: ' + image.name, type = 'error')
try:
skip_list = ['cf_m_gageye', 'cf_m_eyeline', 'cf_m_mayuge', 'cf_m_namida_00', 'cf_m_noseline_00', 'cf_m_sirome_00', 'cf_m_tooth', '_cf_Ohitomi_', 'cf_m_emblem']
convert_this = True
for item in skip_list:
if item in image.name:
convert_this = False
if '_MT_CT' in image.name and convert_this:
material_name = image.name[:-10]
shadow_color = [self.body['KKBP shadow colors'][material_name]['r'], self.body['KKBP shadow colors'][material_name]['g'], self.body['KKBP shadow colors'][material_name]['b']]
darktex = self.create_darktex(bpy.data.images[image.name], shadow_color) #create the darktex now and load it in later
except:
c.kklog('Tried to create a dark version of {} but it was missing a shadow color. Defaulting to shadow color of [.764, .880, 1].'.format(image.name), type='warn')
skip_list = ['cf_m_gageye', 'cf_m_eyeline', 'cf_m_mayuge', 'cf_m_namida_00', 'cf_m_noseline_00', 'cf_m_sirome_00', 'cf_m_tooth', '_cf_Ohitomi_', 'cf_m_emblem']
convert_this = True
for item in skip_list:
if item in image.name:
convert_this = False
if '_MT_CT' in image.name and convert_this:
material_name = image.name[:-10]
darktex = self.create_darktex(bpy.data.images[image.name], [.764, .880, 1]) #create the darktex now and load it in later
c.print_timer('load_images_and_shadow_colors')
def link_textures_for_body(self):
'''Load the textures for the body materials to the correct spot'''
self.image_load('KK Body', 'Gentex', 'BodyMain', self.body['SMR materials']['o_body_a'][0] + '_MT_CT.png')
self.image_load('KK Body', 'Gentex', 'Darktex', self.body['SMR materials']['o_body_a'][0] + '_MT_DT.png')
#check there's a maintex, if not there fallback to colors
if not self.body.material_slots['KK Body'].material.node_tree.nodes['Gentex'].node_tree.nodes['BodyMain'].image:
self.body.material_slots['KK Body'].material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use maintex instead?'].default_value = 0
#but if it is, make sure the body darktex is being used as default
else:
self.body.material_slots['KK Body'].material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use maintex instead?'].default_value = 1
self.image_load('KK Body', 'Gentex', 'BodyMC', self.body['SMR materials']['o_body_a'][0] + '_CM.png')
self.image_load('KK Body', 'Gentex', 'BodyMD', self.body['SMR materials']['o_body_a'][0] + '_DM.png') #cfm female
self.image_load('KK Body', 'Gentex', 'BodyLine', self.body['SMR materials']['o_body_a'][0] + '_LM.png')
self.image_load('KK Body', 'Gentex', 'BodyNorm', self.body['SMR materials']['o_body_a'][0] + '_NMP_CNV.png')
self.image_load('KK Body', 'Gentex', 'BodyNormDetail', self.body['SMR materials']['o_body_a'][0] + '_NMPD_CNV.png')
self.image_load('KK Body', 'Gentex', 'BodyMD', 'cm_m_body_DM.png') #cmm male
self.image_load('KK Body', 'Gentex', 'BodyLine', 'cm_m_body_LM.png')
self.image_load('KK Body', 'NSFWTextures', 'Genital', self.body['SMR materials']['o_body_a'][0] + '_MT.png') #chara main texture
self.image_load('KK Body', 'NSFWTextures', 'Underhair', self.body['SMR materials']['o_body_a'][0] + '_ot2.png') #pubic hair
self.image_load('KK Body', 'NSFWTextures', 'NipR', self.body['SMR materials']['o_body_a'][0] + '_ot1.png') #cfm female
self.image_load('KK Body', 'NSFWTextures', 'NipL', self.body['SMR materials']['o_body_a'][0] + '_ot1.png')
self.image_load('KK Body', 'NSFWTextures', 'NipR', 'cm_m_body_ot1.png') #cmm male
self.image_load('KK Body', 'NSFWTextures', 'NipL', 'cm_m_body_ot1.png')
self.image_load('KK Body', 'Gentex', 'overone', self.body['SMR materials']['o_body_a'][0] + '_T3.png') #body overlays
self.image_load('KK Body', 'Gentex', 'overtwo', self.body['SMR materials']['o_body_a'][0] + '_T4.png')
self.set_uv_type('KK Body', 'NSFWpos', 'nippleuv', 'uv_nipple_and_shine')
self.set_uv_type('KK Body', 'NSFWpos', 'underuv', 'uv_underhair')
#find the appropriate alpha mask
alpha_mask = None
if bpy.data.images.get(self.body['SMR materials']['o_body_a'][0] + '_AM.png'):
alpha_mask = bpy.data.images.get(self.body['SMR materials']['o_body_a'][0] + '_AM.png')
elif bpy.data.images.get(self.body['SMR materials']['o_body_a'][0] + '_AM_00.png'):
alpha_mask = bpy.data.images.get(self.body['SMR materials']['o_body_a'][0] + '_AM_00.png')
else:
#check the other alpha mask numbers
for image in bpy.data.images:
if '_m_body_AM_' in image.name and image.name[-6:-4].isnumeric():
alpha_mask = image
break
if alpha_mask:
self.body.material_slots['KK Body'].material.node_tree.nodes['Gentex'].node_tree.nodes['Bodyalpha'].image = bpy.data.images[alpha_mask.name] #female
self.apply_texture_data_to_image(alpha_mask.name, 'KK Body', 'Gentex', 'Bodyalpha')
else:
#disable transparency if no alpha mask is present
self.body.material_slots['KK Body'].material.node_tree.nodes['Shader'].node_tree.nodes['BodyTransp'].inputs['Built in transparency toggle'].default_value = 0
self.image_load('KK Face', 'Gentex', 'FaceMain', self.body['SMR materials']['cf_O_face'][0] + '_MT_CT.png')
self.image_load('KK Face', 'Gentex', 'Darktex', self.body['SMR materials']['cf_O_face'][0] + '_MT_DT.png')
#default to colors if there's no face maintex
if not self.body.material_slots['KK Face'].material.node_tree.nodes['Gentex'].node_tree.nodes['FaceMain'].image:
self.body.material_slots['KK Face'].material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use maintex instead?'].default_value = 0
else:
self.body.material_slots['KK Face'].material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use maintex instead?'].default_value = 1
self.image_load('KK Face', 'Gentex', 'FaceMC', self.body['SMR materials']['cf_O_face'][0] + '_CM.png')
self.image_load('KK Face', 'Gentex', 'FaceMD', self.body['SMR materials']['cf_O_face'][0] + '_DM.png')
self.image_load('KK Face', 'Gentex', 'BlushMask', self.body['SMR materials']['cf_O_face'][0] + '_T4.png')
self.image_load('KK Face', 'Gentex', 'FaceTongue', self.body['SMR materials']['cf_O_face'][0] + '_MT.png') #face main texture
self.image_load('KK Face', 'Gentex', 'linemask', self.body['SMR materials']['cf_O_face'][0] + '_LM.png')
self.image_load('KK Face', 'Gentex', 'lowerlip', self.body['SMR materials']['cf_O_face'][0] + '_T5.png')
self.image_load('KK Face', 'Gentex', 'lipstick', self.body['SMR materials']['cf_O_face'][0] + '_ot1.png')
self.image_load('KK Face', 'Gentex', 'flush', self.body['SMR materials']['cf_O_face'][0] + '_ot2.png')
self.image_load('KK Face', 'Gentex', 'overlay1', self.body['SMR materials']['cf_O_face'][0] + '_T6.png')
self.image_load('KK Face', 'Gentex', 'overlay2', self.body['SMR materials']['cf_O_face'][0] + '_T7.png')
self.image_load('KK Face', 'Gentex', 'overlay3', self.body['SMR materials']['cf_O_face'][0] + '_T8.png')
self.image_load('KK Face', 'Gentex', 'EyeshadowMask', self.body['SMR materials']['cf_O_face'][0] + '_ot3.png')
self.set_uv_type('KK Face', 'Facepos', 'eyeshadowuv', 'uv_eyeshadow') #face extra texture
self.image_load('KK Eyebrows (mayuge)', 'Gentex', 'Eyebrow', self.body['SMR materials']['cf_O_mayuge'][0] + '_MT_CT.png')
self.image_load('KK Nose', 'Gentex', 'Nose', self.body['SMR materials']['cf_O_noseline'][0] + '_MT_CT.png')
self.image_load('KK Teeth (tooth)', 'Gentex', 'Teeth', self.body['SMR materials']['cf_O_tooth'][0] + '_MT_CT.png')
self.image_load('KK Eyewhites (sirome)', 'Gentex', 'Eyewhite', self.body['SMR materials']['cf_Ohitomi_R'][0] + '_MT_CT.png')
self.image_load('KK Eyeline up', 'Gentex', 'EyelineUp', self.body['SMR materials']['cf_O_eyeline'][0] + '_MT_CT.png')
self.image_load('KK Eyeline up', 'Gentex', 'EyelineUp.001', self.body['SMR materials']['cf_O_eyeline'][0] + '_MT_CT.png')
self.image_load('KK Eyeline up', 'Gentex', 'EyelineDown', self.body['SMR materials']['cf_O_eyeline_low'][0] + '_MT_CT.png')
self.image_load('KK Eyeline up', 'Gentex', 'EyelineDown.001', self.body['SMR materials']['cf_O_eyeline_low'][0] + '_MT_CT.png')
self.image_load('KK Eyeline up', 'Gentex', 'EyelineKage', 'cf_m_eyeline_kage_MT.png')
self.image_load('KK Eyeline up', 'Gentex', 'EyelineKage', 'Eyeline_Over_MT_CT.png')
self.image_load('KK EyeR (hitomi)', 'Gentex', 'eyeAlpha', self.body['SMR materials']['cf_Ohitomi_R02'][0] + '_MT_CT.png')
self.image_load('KK EyeR (hitomi)', 'Gentex', 'EyeHU', self.body['SMR materials']['cf_Ohitomi_R02'][0] + '_ot1.png')
self.image_load('KK EyeR (hitomi)', 'Gentex', 'EyeHD', self.body['SMR materials']['cf_Ohitomi_R02'][0] + '_ot2.png')
self.image_load('KK EyeR (hitomi)', 'Gentex', 'expression0', self.body['SMR materials']['cf_Ohitomi_R'][0] + '_cf_t_expression_00_EXPR.png')
self.image_load('KK EyeR (hitomi)', 'Gentex', 'expression1', self.body['SMR materials']['cf_Ohitomi_R'][0] + '_cf_t_expression_01_EXPR.png')
self.image_load('KK EyeL (hitomi)', 'Gentex', 'eyeAlpha', self.body['SMR materials']['cf_Ohitomi_L02'][0] + '_MT_CT.png')
self.image_load('KK EyeL (hitomi)', 'Gentex', 'EyeHU', self.body['SMR materials']['cf_Ohitomi_L02'][0] + '_ot1.png')
self.image_load('KK EyeL (hitomi)', 'Gentex', 'EyeHD', self.body['SMR materials']['cf_Ohitomi_L02'][0] + '_ot2.png')
self.image_load('KK EyeL (hitomi)', 'Gentex', 'expression0', self.body['SMR materials']['cf_Ohitomi_L02'][0] + '_cf_t_expression_00_EXPR.png')
self.image_load('KK EyeL (hitomi)', 'Gentex', 'expression1', self.body['SMR materials']['cf_Ohitomi_L02'][0] + '_cf_t_expression_01_EXPR.png')
c.print_timer('link_textures_for_body')
def link_textures_for_hair(self):
#for each material slot in each hair object, load in the hair detail mask, colormask
for current_obj in self.hairs:
for hairMat in current_obj.material_slots:
hairType = hairMat.name.replace('KK ','')
#make a copy of the node group, use it to replace the current node group and rename it so each piece of hair has it's own unique hair texture group
newNode = hairMat.material.node_tree.nodes['Gentex'].node_tree.copy()
hairMat.material.node_tree.nodes['Gentex'].node_tree = newNode
newNode.name = hairType + ' Textures'
self.image_load(hairMat.name, 'Gentex', 'hairMainTex', hairType+'_MT_CT.png')
self.image_load(hairMat.name, 'Gentex', 'hairDarkTex', hairType+'_MT_DT.png')
self.image_load(hairMat.name, 'Gentex', 'hairDetail', hairType+'_DM.png')
self.image_load(hairMat.name, 'Gentex', 'hairFade', hairType+'_CM.png')
self.image_load(hairMat.name, 'Gentex', 'hairShine', hairType+'_HGLS.png')
self.image_load(hairMat.name, 'Gentex', 'hairAlpha', hairType+'_AM.png')
self.set_uv_type(hairMat.name, 'Hairpos', 'hairuv', 'uv_nipple_and_shine')
#If a maintex exists for this hair material, make a new node group so the rest of the hair is not affected
if hairMat.material.node_tree.nodes['Gentex'].node_tree.nodes['hairMainTex'].image:
newNode = hairMat.material.node_tree.nodes['Shader'].node_tree.copy()
hairMat.material.node_tree.nodes['Shader'].node_tree = newNode
newNode.name = hairType + ' Hair Shader'
#If no alpha mask wasn't loaded in disconnect the hair alpha node to make sure this piece of hair is visible
if hairMat.material.node_tree.nodes['Gentex'].node_tree.nodes['hairAlpha'].image == None and hairMat.material.node_tree.nodes['Gentex'].node_tree.nodes['hairMainTex'].image == None:
getOut = hairMat.material.node_tree.nodes['Gentex'].node_tree.nodes['Group Output'].inputs['Hair alpha'].links[0]
hairMat.material.node_tree.nodes['Gentex'].node_tree.links.remove(getOut)
c.print_timer('link_textures_for_hair')
def link_textures_for_clothes(self):
'''Loop through each material in the general object and load the textures, if any, into unique node groups
also make unique shader node groups so all materials are unique
make a copy of the node group, use it to replace the current node group'''
for cat in [self.outfits, self.outfit_alternates]:
for outfit in cat:
outfit = outfit
for genMat in outfit.material_slots:
genType = genMat.name.replace('KK ','')
#make a copy of the node group, use it to replace the current node group and rename it so each mat has a unique texture group
newNode = genMat.material.node_tree.nodes['Gentex'].node_tree.copy()
genMat.material.node_tree.nodes['Gentex'].node_tree = newNode
newNode.name = genType + ' Textures'
#make a copy of the node group, use it to replace the current node group and rename it so each mat has a unique position group
posNode = genMat.material.node_tree.nodes['Genpos'].node_tree.copy()
genMat.material.node_tree.nodes['Genpos'].node_tree = posNode
posNode.name = genType + ' Position'
self.image_load(genMat.name, 'Gentex', 'Maintexplain', genType+ '_MT.png')
self.image_load(genMat.name, 'Gentex', 'Maintex', genType+ '_MT.png')
self.image_load(genMat.name, 'Gentex', 'Maintex', genType+'_MT_CT.png')
self.image_load(genMat.name, 'Gentex', 'Darktex', genType+'_MT_DT.png')
self.image_load(genMat.name, 'Gentex', 'MainCol', genType+'_CM.png')
self.image_load(genMat.name, 'Gentex', 'MainDet', genType+'_DM.png')
self.image_load(genMat.name, 'Gentex', 'MainNorm', genType+'_NMP.png')
self.image_load(genMat.name, 'Gentex', 'MainNormDetail', genType+'_NMPD_CNV.png') #load detail map if it's there
self.image_load(genMat.name, 'Gentex', 'Alphamask', genType+'_AM.png')
# image_load(genMat.name, 'Gentex', 'PatBase', genType+'_PM1.png')
self.image_load(genMat.name, 'Gentex', 'PatRed', genType+'_PM1.png')
self.image_load(genMat.name, 'Gentex', 'PatGreen', genType+'_PM2.png')
self.image_load(genMat.name, 'Gentex', 'PatBlue', genType+'_PM3.png')
MainImage = genMat.material.node_tree.nodes['Gentex'].node_tree.nodes['Maintex'].image
DarkImage = genMat.material.node_tree.nodes['Gentex'].node_tree.nodes['Darktex'].image
AlphaImage = genMat.material.node_tree.nodes['Gentex'].node_tree.nodes['Alphamask'].image
#set dark colors to use the maintex if there was a dark image loaded in
if DarkImage and 'Template: Pattern Placeholder' not in DarkImage.name:
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use dark maintex?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Ignore colormask?'].default_value = 1
#Also, make a copy of the General shader node group, as it's unlikely everything using it will be the same color
newNode = genMat.material.node_tree.nodes['Shader'].node_tree.copy()
genMat.material.node_tree.nodes['Shader'].node_tree = newNode
newNode.name = genType + ' Shader'
#If an alpha mask was loaded in, enable the alpha mask toggle in the KK shader
if AlphaImage != None:
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['alphatoggle'].inputs['Transparency toggle'].default_value = 1
#If no main image was loaded in, there's no alpha channel being fed into the KK Shader.
#Unlink the input node and make the alpha channel pure white
if not MainImage:
getOut = genMat.material.node_tree.nodes['Shader'].node_tree.nodes['alphatoggle'].inputs['maintex alpha'].links[0]
genMat.material.node_tree.nodes['Shader'].node_tree.links.remove(getOut)
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['alphatoggle'].inputs['maintex alpha'].default_value = (1,1,1,1)
#check maintex config
plainMain = not genMat.material.node_tree.nodes['Gentex'].node_tree.nodes['Maintexplain'].image.name == 'Template: Maintex plain placeholder'
if not MainImage and not plainMain:
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use Maintex?'].default_value = 0
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Ignore colormask?'].default_value = 0
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use Maintex?'].default_value = 0
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Ignore colormask?'].default_value = 0
elif not MainImage and plainMain:
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use Maintex?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use colored maintex?'].default_value = 0
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Ignore colormask?'].default_value = 0
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use colored maintex?'].default_value = 0
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Ignore colormask?'].default_value = 0
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use Maintex?'].default_value = 1
elif MainImage and not plainMain:
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use Maintex?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use colored maintex?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Ignore colormask?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use colored maintex?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Ignore colormask?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use Maintex?'].default_value = 1
else: #MainImage and plainMain
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use Maintex?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Use colored maintex?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsLight'].inputs['Ignore colormask?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use colored maintex?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Ignore colormask?'].default_value = 1
genMat.material.node_tree.nodes['Shader'].node_tree.nodes['colorsDark'].inputs['Use Maintex?'].default_value = 1
c.print_timer('link_textures_for_clothes')
def link_textures_for_tongue_tear_gag(self):
self.image_load('KK Tongue', 'Gentex', 'Maintex', self.body['SMR materials']['o_tang'][0] + '_CM.png') #done on purpose
self.image_load('KK Tongue', 'Gentex', 'MainCol', self.body['SMR materials']['o_tang'][0] + '_CM.png')
self.image_load('KK Tongue', 'Gentex', 'MainDet', self.body['SMR materials']['o_tang'][0] + '_DM.png')
self.image_load('KK Tongue', 'Gentex', 'MainNorm', self.body['SMR materials']['o_tang'][0] + '_NMP.png')
self.image_load('KK Tongue', 'Gentex', 'MainNormDetail', self.body['SMR materials']['o_tang'][0] + '_NMP_CNV.png') #load regular map by default
self.image_load('KK Tongue', 'Gentex', 'MainNormDetail', self.body['SMR materials']['o_tang'][0] + '_NMPD_CNV.png') #then the detail map if it's there
#load all gag eyes in if it exists
if bpy.data.objects.get('Gag Eyes'):
self.image_load('KK Gag00', 'Gentex', '00gag00', self.body['SMR materials']['cf_O_gag_eye_00'][0] + '_cf_t_gageye_00_MT_CT.png')
self.image_load('KK Gag00', 'Gentex', '00gag02', self.body['SMR materials']['cf_O_gag_eye_00'][0] + '_cf_t_gageye_02_MT_CT.png')
self.image_load('KK Gag00', 'Gentex', '00gag04', self.body['SMR materials']['cf_O_gag_eye_00'][0] + '_cf_t_gageye_04_MT_CT.png')
self.image_load('KK Gag00', 'Gentex', '00gag05', self.body['SMR materials']['cf_O_gag_eye_00'][0] + '_cf_t_gageye_05_MT_CT.png')
self.image_load('KK Gag00', 'Gentex', '00gag06', self.body['SMR materials']['cf_O_gag_eye_00'][0] + '_cf_t_gageye_06_MT_CT.png')
self.image_load('KK Gag01', 'Gentex', '01gag03', self.body['SMR materials']['cf_O_gag_eye_01'][0] + '_cf_t_gageye_03_MT_CT.png')
self.image_load('KK Gag01', 'Gentex', '01gag01', self.body['SMR materials']['cf_O_gag_eye_01'][0] + '_cf_t_gageye_01_MT_CT.png')
self.image_load('KK Gag02', 'Gentex', '02gag07', self.body['SMR materials']['cf_O_gag_eye_02'][0] + '_cf_t_gageye_07_MT_CT.png')
self.image_load('KK Gag02', 'Gentex', '02gag08', self.body['SMR materials']['cf_O_gag_eye_02'][0] + '_cf_t_gageye_08_MT_CT.png')
self.image_load('KK Gag02', 'Gentex', '02gag09', self.body['SMR materials']['cf_O_gag_eye_02'][0] + '_cf_t_gageye_09_MT_CT.png')
#load the tears texture in
if bpy.data.objects.get('Tears'):
self.image_load('KK Tears', 'Gentex', 'Maintex', self.body['SMR materials']['cf_O_namida_L'][0] + '_MT_CT.png')
c.print_timer('link_textures_for_tongue_tear_gag')
def import_and_setup_gfn(self):
'''Sets up the Generated Face Normals (GFN) empty for smooth face normals'''
#setup face normals
try:
#import gfn face node group, cycles node groups as well
c.import_from_library_file('NodeTree', ['Raw Shading (face)'], bpy.context.scene.kkbp.use_material_fake_user)
c.switch(self.armature, 'edit')
head_location = (self.armature.data.edit_bones['Head'].tail.x+1, self.armature.data.edit_bones['Head'].tail.y+1, self.armature.data.edit_bones['Head'].tail.z+1)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.empty_add(type='CUBE', align='WORLD', location=head_location)
empty = bpy.context.view_layer.objects.active
empty.location.x -= 1
empty.location.y -= 1
empty.location.z -= 1
empty.scale = (0.15, 0.15, 0.15)
empty.name = 'GFN Empty'
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = self.armature
empty.select_set(True)
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action='DESELECT')
self.armature.data.bones['Head'].select = True
self.armature.data.bones.active = self.armature.data.bones['Head']
bpy.ops.object.parent_set(type='BONE')
bpy.ops.pose.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.data.node_groups['Generated Face Normals'].nodes['GFNEmpty'].object = empty
bpy.context.view_layer.objects.active = empty
empty.select_set(True)
bpy.ops.object.move_to_collection(collection_index=1)
empty.hide = True
empty.hide_render = True
except:
#i don't feel like dealing with any errors related to this
c.kklog('The GFN empty wasnt setup correctly. Oh well.', 'warn')
pass
c.print_timer('import_and_setup_gfn')
def setup_gag_eye_material_drivers(self):
'''setup gag eye drivers'''
if bpy.data.objects.get('Gag Eyes'):
gag_keys = [
'Circle Eyes 1',
'Circle Eyes 2',
'Spiral Eyes',
'Heart Eyes',
'Fiery Eyes',
'Cartoony Wink',
'Vertical Line',
'Cartoony Closed',
'Horizontal Line',
'Cartoony Crying'
]
skey_driver = bpy.data.materials['KK Gag00'].node_tree.nodes['Parser'].inputs[0].driver_add('default_value')
skey_driver.driver.type = 'SCRIPTED'
for key in gag_keys:
newVar = skey_driver.driver.variables.new()
newVar.name = key.replace(' ','')
newVar.type = 'SINGLE_PROP'
newVar.targets[0].id_type = 'KEY'
newVar.targets[0].id = self.body.data.shape_keys
newVar.targets[0].data_path = 'key_blocks["' + key + '"].value'
skey_driver.driver.expression = '0 if CircleEyes1 else 1 if CircleEyes2 else 2 if CartoonyClosed else 3 if VerticalLine else 4'
skey_driver = bpy.data.materials['KK Gag00'].node_tree.nodes['hider'].inputs[0].driver_add('default_value')
skey_driver.driver.type = 'SCRIPTED'
for key in gag_keys:
newVar = skey_driver.driver.variables.new()
newVar.name = key.replace(' ','')
newVar.type = 'SINGLE_PROP'
newVar.targets[0].id_type = 'KEY'
newVar.targets[0].id = self.body.data.shape_keys
newVar.targets[0].data_path = 'key_blocks["' + key + '"].value'
skey_driver.driver.expression = 'CircleEyes1 or CircleEyes2 or CartoonyClosed or VerticalLine or HorizontalLine'
skey_driver = bpy.data.materials['KK Gag01'].node_tree.nodes['Parser'].inputs[0].driver_add('default_value')
skey_driver.driver.type = 'SCRIPTED'
for key in gag_keys:
newVar = skey_driver.driver.variables.new()
newVar.name = key.replace(' ','')
newVar.type = 'SINGLE_PROP'
newVar.targets[0].id_type = 'KEY'
newVar.targets[0].id = self.body.data.shape_keys
newVar.targets[0].data_path = 'key_blocks["' + key + '"].value'
skey_driver.driver.expression = '0 if HeartEyes else 1'
skey_driver = bpy.data.materials['KK Gag01'].node_tree.nodes['hider'].inputs[0].driver_add('default_value')
skey_driver.driver.type = 'SCRIPTED'
for key in gag_keys:
newVar = skey_driver.driver.variables.new()
newVar.name = key.replace(' ','')
newVar.type = 'SINGLE_PROP'
newVar.targets[0].id_type = 'KEY'
newVar.targets[0].id = self.body.data.shape_keys
newVar.targets[0].data_path = 'key_blocks["' + key + '"].value'
skey_driver.driver.expression = 'HeartEyes or SpiralEyes'
skey_driver = bpy.data.materials['KK Gag02'].node_tree.nodes['Parser'].inputs[0].driver_add('default_value')
skey_driver.driver.type = 'SCRIPTED'
for key in gag_keys:
newVar = skey_driver.driver.variables.new()
newVar.name = key.replace(' ','')
newVar.type = 'SINGLE_PROP'
newVar.targets[0].id_type = 'KEY'
newVar.targets[0].id = self.body.data.shape_keys
newVar.targets[0].data_path = 'key_blocks["' + key + '"].value'
skey_driver.driver.expression = '0 if CartoonyCrying else 1 if CartoonyWink else 2'
skey_driver = bpy.data.materials['KK Gag02'].node_tree.nodes['hider'].inputs[0].driver_add('default_value')
skey_driver.driver.type = 'SCRIPTED'
for key in gag_keys:
newVar = skey_driver.driver.variables.new()
newVar.name = key.replace(' ','')
newVar.type = 'SINGLE_PROP'
newVar.targets[0].id_type = 'KEY'
newVar.targets[0].id = self.body.data.shape_keys
newVar.targets[0].data_path = 'key_blocks["' + key + '"].value'
skey_driver.driver.expression = 'CartoonyCrying or CartoonyWink or FieryEyes'
c.print_timer('setup_gag_eye_material_drivers')
def add_outlines_to_body(self):
#Add face and body outlines, then load in the clothes transparency mask to body outline
c.switch(self.body, 'object')
mod = self.body.modifiers.new(type='SOLIDIFY', name='Outline Modifier')
mod.thickness = 0.0005
mod.offset = 0
mod.material_offset = len(self.body.material_slots)
mod.use_flip_normals = True
mod.use_rim = False
mod.name = 'Outline Modifier'
mod.show_expanded = False
#face first
faceOutlineMat = bpy.data.materials['KK Outline'].copy()
faceOutlineMat.name = 'KK Face Outline'
self.body.data.materials.append(faceOutlineMat)
faceOutlineMat.blend_method = 'CLIP'
#body second
self.body.data.materials.append(bpy.data.materials['KK Body Outline'])
if not bpy.data.materials['KK Body Outline'].node_tree.nodes['Gentex'].node_tree.nodes['Bodyalpha'].image:
#An alpha mask for the clothing wasn't present in the Textures folder
bpy.data.materials['KK Body Outline'].node_tree.nodes['Clipping prevention toggle'].inputs[0].default_value = 0
c.print_timer('add_outlines_to_body')
def add_outlines_to_hair(self):
#Give each piece of hair with an alphamask on each hair object it's own outline group
if not bpy.context.scene.kkbp.use_single_outline:
for ob in self.hairs:
#Get the length of the material list before starting
outlineStart = len(ob.material_slots)
#link all polygons to material name
mats_to_gons = {}
for slot in ob.material_slots:
mats_to_gons[slot.material.name] = []
for gon in ob.data.polygons:
mats_to_gons[ob.material_slots[gon.material_index].material.name].append(gon)
#find all materials that use an alpha mask or maintex
alpha_users = []
for mat in ob.material_slots:
AlphaImage = mat.material.node_tree.nodes['Gentex'].node_tree.nodes['hairAlpha'].image
MainImage = mat.material.node_tree.nodes['Gentex'].node_tree.nodes['hairMainTex'].image
if AlphaImage or MainImage:
alpha_users.append(mat.material.name)
#reorder material_list to place alpha/maintex users first
new_mat_list_order = [mat_slot.material.name for mat_slot in ob.material_slots if mat_slot.material.name not in alpha_users]
new_mat_list_order = alpha_users + new_mat_list_order
#reorder mat slot list
for index, mat_slot in enumerate(ob.material_slots):
mat_slot.material = bpy.data.materials[new_mat_list_order[index]]
#create empty slots for new alpha user outlines
for mat in alpha_users:
ob.data.materials.append(None)
#fill alpha user outline materials, and fill image node
for index, mat in enumerate(alpha_users):
OutlineMat = bpy.data.materials['KK Outline'].copy()
OutlineMat.name = mat.replace('KK ', 'Outline ')
AlphaImage = ob.material_slots[mat].material.node_tree.nodes['Gentex'].node_tree.nodes['hairAlpha'].image
MainImage = ob.material_slots[mat].material.node_tree.nodes['Gentex'].node_tree.nodes['hairMainTex'].image
if AlphaImage:
OutlineMat.node_tree.nodes['outlinealpha'].image = AlphaImage
OutlineMat.node_tree.nodes['maintexoralpha'].inputs[0].default_value = 1.0
OutlineMat.node_tree.nodes['maintexoralpha'].blend_type = 'MULTIPLY'
elif MainImage:
OutlineMat.node_tree.nodes['outlinealpha'].image = MainImage
OutlineMat.node_tree.nodes['maintexoralpha'].inputs[0].default_value = 1.0
OutlineMat.node_tree.nodes['outlinetransparency'].inputs[0].default_value = 1.0
ob.material_slots[index + outlineStart].material = OutlineMat
#update polygon material indexes
for mat in mats_to_gons:
for gon in mats_to_gons[mat]:
gon.material_index = new_mat_list_order.index(mat)
#Add a general outline that covers the rest of the materials on the hair object that don't need transparency
for ob in self.hairs:
bpy.context.view_layer.objects.active = ob
mod = ob.modifiers.new(
type='SOLIDIFY',
name='Outline Modifier')
mod.thickness = 0.0005
mod.offset = 1
mod.material_offset = outlineStart if not bpy.context.scene.kkbp.use_single_outline else 200
mod.use_flip_normals = True
mod.use_rim = False
mod.show_expanded = False
hairOutlineMat = bpy.data.materials['KK Outline'].copy()
hairOutlineMat.name = 'KK Hair Outline'
ob.data.materials.append(hairOutlineMat)
#hide alts
if ob.name[:12] == 'Hair Outfit ' and ob.name != 'Hair Outfit 00':
ob.hide = True
ob.hide_render = True
c.print_timer('add_outlines_to_hair')
def add_outlines_to_clothes(self):
#Add a standard outline to all other objects
#keep a dictionary of the material length list for the next loop
outlineStart = {}
c.switch(self.body, 'object')
if not bpy.context.scene.kkbp.use_single_outline:
#If the material has a maintex or alphamask then give it it's own outline, mmdtools style
for cat in [self.outfits, self.outfit_alternates]:
for ob in cat:
#Get the length of the material list before starting
outlineStart[ob.name] = len(ob.material_slots)
#link all polygons to material name
mats_to_gons = {}
for slot in ob.material_slots:
mats_to_gons[slot.material.name] = []
for gon in ob.data.polygons:
mats_to_gons[ob.material_slots[gon.material_index].material.name].append(gon)
#find all materials that use an alpha mask or maintex
alpha_users = []
for mat in ob.material_slots:
AlphaImage = mat.material.node_tree.nodes['Gentex'].node_tree.nodes['Alphamask'].image
MainImage = mat.material.node_tree.nodes['Gentex'].node_tree.nodes['Maintex'].image
if AlphaImage or MainImage:
alpha_users.append(mat.material.name)
#reorder material_list to place alpha/maintex users first
new_mat_list_order = [mat_slot.material.name for mat_slot in ob.material_slots if mat_slot.material.name not in alpha_users]
new_mat_list_order = alpha_users + new_mat_list_order
#reorder mat slot list
for index, mat_slot in enumerate(ob.material_slots):
mat_slot.material = bpy.data.materials[new_mat_list_order[index]]
#create empty slots for new alpha user outlines
for mat in alpha_users:
ob.data.materials.append(None)
#fill alpha user outline materials, and fill image node
for index, mat in enumerate(alpha_users):
OutlineMat = bpy.data.materials['KK Outline'].copy()
OutlineMat.name = mat.replace('KK ', 'Outline ')
AlphaImage = ob.material_slots[mat].material.node_tree.nodes['Gentex'].node_tree.nodes['Alphamask'].image
MainImage = ob.material_slots[mat].material.node_tree.nodes['Gentex'].node_tree.nodes['Maintex'].image
if AlphaImage:
OutlineMat.node_tree.nodes['outlinealpha'].image = AlphaImage
OutlineMat.node_tree.nodes['maintexoralpha'].inputs[0].default_value = 1.0
OutlineMat.node_tree.nodes['maintexoralpha'].blend_type = 'MULTIPLY'
elif MainImage:
OutlineMat.node_tree.nodes['outlinealpha'].image = MainImage
OutlineMat.node_tree.nodes['maintexoralpha'].inputs[0].default_value = 1.0
OutlineMat.node_tree.nodes['outlinetransparency'].inputs[0].default_value = 1.0
ob.material_slots[index + outlineStart[ob.name]].material = OutlineMat
#update polygon material indexes
for mat in mats_to_gons:
for gon in mats_to_gons[mat]:
gon.material_index = new_mat_list_order.index(mat)
for cat in [self.outfits, self.outfit_alternates]:
for ob in cat:
#Add a general outline that covers the rest of the materials on the object that don't need transparency
mod = ob.modifiers.new(
type='SOLIDIFY',
name='Outline Modifier')
mod.thickness = 0.0005
mod.offset = 1
mod.material_offset = outlineStart[ob.name] if not bpy.context.scene.kkbp.use_single_outline else 200
mod.use_flip_normals = True
mod.use_rim = False
mod.show_expanded = False
ob.data.materials.append(bpy.data.materials['KK Outline'])
c.print_timer('add_outlines_to_clothes')
@classmethod
def load_luts(cls):
self = cls
self.lut_selection = bpy.context.scene.kkbp.colors_dropdown
if self.lut_selection == 'A':
self.lut_dark = 'Lut_TimeNight.png'
elif self.lut_selection == 'B':
self.lut_dark = 'Lut_TimeSunset.png'
else:
self.lut_dark = 'Lut_TimeDay.png'
self.lut_light = 'Lut_TimeDay.png'
self.lut_path = os.path.dirname(os.path.abspath(__file__)) + '/luts/'
day_lut = bpy.data.images.load(self.lut_path + self.lut_light, check_existing=True)
day_lut.use_fake_user = True
#day_lut.save()
night_lut = bpy.data.images.load(self.lut_path + self.lut_dark, check_existing=True)
night_lut.use_fake_user = True
#night_lut.save()
def convert_main_textures(self):
ignore_list = [
"cf_m_eyeline_00_up_MT_CT.png",
"cf_m_eyeline_down_MT_CT.png",
"cf_m_noseline_00_MT_CT.png",
"cf_m_mayuge_00_MT_CT.png",
"cf_m_eyeline_kage_MT.png",
]
bpy.data.use_autopack = True #enable autopack on file save
images = bpy.data.images
first = True
for image in images:
if "_MT" in image.name and image.name not in ignore_list:
image.reload()
# Need to run image_to_KK twice for the first image due to a weird bug
if first:
self.image_to_KK(image, self.lut_light)
first = False
new_pixels, width, height = self.image_to_KK(image, self.lut_light)
image.pixels = new_pixels
#image.save()
c.print_timer('convert_main_textures')
def load_json_colors(self):
json_color_data = c.get_json_file('KK_MaterialData.json')
self.update_shaders(json_color_data, self.lut_selection, self.lut_light, light = True) # Set light colors
self.update_shaders(json_color_data, self.lut_selection, self.lut_dark, light = False) # Set dark colors
c.print_timer('load_json_colors')
def set_color_management(self):
bpy.data.scenes[0].display_settings.display_device = 'sRGB'
bpy.data.scenes[0].view_settings.view_transform = 'Standard'