forked from denetii/io_thps_scene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial.py
1486 lines (1299 loc) · 71.3 KB
/
material.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
#############################################
# THUG1/2 MATERIAL IMPORT/CONFIGURE/EXPORT
#############################################
import bpy
import struct
import mathutils
import math
import os, sys
from bpy.props import *
from . constants import *
from . helpers import *
from . tex import *
# METHODS
#############################################
# Convert an Underground+ material to BI texture passes, so that it can be visualized in the viewport
def ugplus_material_update(mat, context):
return
# Checks if our PBR material is completely diffuse (non-metallic, 0 reflectance),
# so we can use the less expensive Diffuse BRDF rather than the full PBR shader
def is_full_diffuse(mprops):
if not mprops.ugplus_matslot_specular.tex_image:
for i in range(3):
if mprops.ugplus_matslot_specular.tex_color[i] != 0.0:
return False
if not mprops.ugplus_matslot_weathermask.tex_image:
return True
return False
def _ensure_default_material_exists():
if "_THUG_DEFAULT_MATERIAL_" in bpy.data.materials:
return
default_mat = bpy.data.materials.new(name="_THUG_DEFAULT_MATERIAL_")
if "_THUG_DEFAULT_MATERIAL_TEXTURE_" not in bpy.data.textures:
texture = bpy.data.textures.new("_THUG_DEFAULT_MATERIAL_TEXTURE_", "NONE")
texture.thug_material_pass_props.color = (0.5, 0.5, 0.5)
tex_slot = default_mat.texture_slots.add()
tex_slot.texture = bpy.data.textures["_THUG_DEFAULT_MATERIAL_TEXTURE_"]
def _thug_material_pass_props_color_updated(self, context):
from bl_ui.properties_material import active_node_mat
if not context or not context.object:
return
mat = context.object.active_material
if not mat:
return
idblock = active_node_mat(mat)
r, g, b = self.color
idblock.active_texture.factor_red = r * 2
idblock.active_texture.factor_green = g * 2
idblock.active_texture.factor_blue = b * 2
def rename_imported_materials():
for mat in bpy.data.materials:
if "thug_mat_name_checksum" in mat and mat["thug_mat_name_checksum"] != "":
mat.name = mat["thug_mat_name_checksum"]
def read_materials(reader, printer, num_materials, directory, operator, output_file=None, texture_map=None, texture_prefix=None):
import os
r = reader
p = printer
for i in range(num_materials):
p("material {}", i)
mat_checksum = p(" material checksum: {}", to_hex_string(r.u32()))
mat_name_checksum = p(" material name checksum: {}", to_hex_string(r.u32()))
if bpy.data.materials.get(mat_name_checksum):
blender_mat = bpy.data.materials.get(mat_name_checksum)
else:
blender_mat = bpy.data.materials.new(mat_checksum)
ps = blender_mat.thug_material_props
blender_mat["thug_mat_name_checksum"] = mat_name_checksum
num_passes = p(" material passes: {}", r.u32())
# MATERIAL_PASSES[mat_name_checksum] = num_passes
ps.alpha_cutoff = p(" alpha cutoff: {}", r.u32() % 256)
ps.sorted = p(" sorted: {}", r.bool())
ps.draw_order = p(" draw order: {}", r.f32())
ps.single_sided = p(" single_sided: {}", r.bool())
ps.no_backface_culling = p(" no backface culling: {}", r.bool())
ps.z_bias = p(" z bias: {}", r.i32())
ps.grass_props.grassify = p(" grassify: {}", r.bool())
if ps.grass_props.grassify:
ps.grass_props.grass_height = p(" grass height: {}", r.f32())
ps.grass_props.grass_layers = p(" grass layers: {}", r.i32())
ps.specular_power = p(" specular power: {}", r.f32())
if ps.specular_power > 0.0:
ps.specular_color = p(" specular color: {}", r.read("3f"))
blender_mat.use_transparency = True
blender_mat.diffuse_color = (1, 1, 1)
blender_mat.diffuse_intensity = 1
blender_mat.specular_intensity = 0
blender_mat.alpha = 0
for j in range(num_passes):
blender_tex = get_texture("{}/{}".format(mat_name_checksum, j))
if blender_mat.texture_slots[j]:
tex_slot = blender_mat.texture_slots[j]
else:
tex_slot = blender_mat.texture_slots.add()
tex_slot.texture = blender_tex
pps = blender_tex.thug_material_pass_props
p(" pass #{}", j)
tex_checksum = p(" pass texture checksum: {}", r.u32())
actual_tex_checksum = to_hex_string(tex_checksum)
image_name = str(actual_tex_checksum) #+ ".png"
blender_tex.image = bpy.data.images.get(image_name)
full_path = os.path.join(directory, image_name)
full_path2 = os.path.join(directory, str("tex\\{:08x}.tga".format(tex_checksum)))
full_path3 = os.path.join(directory, str("tex\\{:08x}.png".format(tex_checksum)))
if not blender_tex.image:
if os.path.exists(full_path):
blender_tex.image = bpy.data.images.load(full_path)
elif os.path.exists(full_path2):
blender_tex.image = bpy.data.images.load(full_path2)
elif os.path.exists(full_path3):
blender_tex.image = bpy.data.images.load(full_path3)
#else:
# blender_tex.image = bpy.data.images.new(image_name)
tex_slot.uv_layer = str(j)
pass_flags = p(" pass material flags: {}", r.u32())
p(" pass has color: {}", r.bool())
pps.color = p(" pass color: {}", r.read("3f"))
# p(" pass alpha register: {}", r.u64())
pps.blend_mode = BLEND_MODES[r.u32()]
pps.blend_fixed_alpha = r.u32() & 0xFF
if j == 0:
tex_slot.use_map_alpha = True
tex_slot.blend_type = {
"vBLEND_MODE_ADD": "ADD",
"vBLEND_MODE_ADD_FIXED": "ADD",
"vBLEND_MODE_SUBTRACT": "SUBTRACT",
"vBLEND_MODE_SUB_FIXED": "SUBTRACT",
"vBLEND_MODE_BRIGHTEN": "LIGHTEN",
"vBLEND_MODE_BRIGHTEN_FIXED": "LIGHTEN",
"vBLEND_MODE_MODULATE": "MULTIPLY"
}.get(pps.blend_mode, "MIX")
tmp_addressing = [ "Repeat", "Clamp", "Border" ]
pps.u_addressing = p(" pass u addressing: {}", tmp_addressing[r.u32()])
pps.v_addressing = p(" pass v addressing: {}", tmp_addressing[r.u32()])
if pps.u_addressing == 'Border' or pps.v_addressing == 'Border':
raise Exception("This is actually used!")
if blender_tex.image:
blender_tex.image.use_clamp_x = pps.u_addressing == "Clamp"
blender_tex.image.use_clamp_y = pps.v_addressing == "Clamp"
pps.envmap_multiples = p(" pass envmap uv tiling multiples: {}", r.read("2f"))
pps.filtering_mode = r.u32()
p(" pass filtering mode: {}", pps.filtering_mode)
pps.test_passes = pass_flags
if pass_flags & MATFLAG_TEXTURED:
pps.pf_textured = True
if pass_flags & MATFLAG_TRANSPARENT:
pps.pf_transparent = True
else:
pps.pf_transparent = False
if pass_flags & MATFLAG_ENVIRONMENT:
pps.pf_environment = True
if pass_flags & MATFLAG_DECAL:
pps.pf_decal = True
if pass_flags & MATFLAG_SMOOTH:
pps.pf_smooth = True
if pass_flags & MATFLAG_PASS_IGNORE_VERTEX_ALPHA:
pps.ignore_vertex_alpha = True
else:
pps.ignore_vertex_alpha = False
if pass_flags & MATFLAG_UV_WIBBLE:
p(" pass has uv wibble!", None)
pps.has_uv_wibbles = True
uvs = pps.uv_wibbles
uvs.uv_velocity = r.read("2f")
uvs.uv_frequency = r.read("2f")
uvs.uv_amplitude = r.read("2f")
uvs.uv_phase = r.read("2f")
if j == 0 and pass_flags & MATFLAG_VC_WIBBLE:
p(" pass has vc wibble!", None)
for k in range(r.u32()):
num_keys = r.u32()
r.i32()
r.read(str(num_keys * 2) + "i") # sVCWibbleKeyframe
if pass_flags & MATFLAG_PASS_TEXTURE_ANIMATES:
p(" pass texture animates!", None)
pps.has_animated_texture = True
at = pps.animated_texture
num_keyframes = r.i32()
at.period = r.i32()
at.iterations = r.i32()
at.phase = r.i32()
for k in range(num_keyframes):
atkf = at.keyframes.add()
atkf.time = r.u32()
atkf.image = to_hex_string(r.u32())
if tex_checksum: # mipmap info
p(" mmag: {}", r.u32())
p(" mmin: {}", r.u32())
pps.lod_bias = p(" k: {}", r.f32()+8.0)
p(" l: {}", r.f32())
else:
r.read("4I")
def get_ugplus_shader_flags(mprops):
SHADERFLAG_DEFAULT_LIT = 0x01
SHADERFLAG_LIGHTMAPPED = 0x02
SHADERFLAG_LIGHTMAPPED_HL2 = 0x04
SHADERFLAG_USES_WEATHER = 0x08
SHADERFLAG_USES_POM = 0x10
SHADERFLAG_USES_REFLECTIONS = 0x20
SHADERFLAG_USES_REFRACTION = 0x40
mat_flags = 0x00
# Set lighting mode flag
if mprops.ugplus_lighting_mode == 'Lit':
mat_flags |= SHADERFLAG_DEFAULT_LIT
elif mprops.ugplus_lighting_mode == 'Baked':
mat_flags |= SHADERFLAG_DEFAULT_LIT
mat_flags |= SHADERFLAG_LIGHTMAPPED
elif mprops.ugplus_lighting_mode == 'BakedHL2':
mat_flags |= SHADERFLAG_DEFAULT_LIT
mat_flags |= SHADERFLAG_LIGHTMAPPED_HL2
if mprops.ugplus_shader_weather:
mat_flags |= SHADERFLAG_USES_WEATHER
if mprops.ugplus_shader_disp:
mat_flags |= SHADERFLAG_USES_POM
if mprops.ugplus_shader in [ 'PBR', 'Glass', 'Water', 'Water_Custom', 'Ocean' ]:
mat_flags |= SHADERFLAG_USES_REFLECTIONS
if mprops.ugplus_shader in [ 'Glass', 'Water', 'Water_Custom' ]:
mat_flags |= SHADERFLAG_USES_REFRACTION
return mat_flags
def export_ugplus_material(m, output_file, target_game, operator=None):
def w(fmt, *args):
output_file.write(struct.pack(fmt, *args))
mprops = m.thug_material_props
# Get the shader ID
shader_id = 0.0
if mprops.ugplus_shader == 'PBR':
shader_id = 5.40
elif mprops.ugplus_shader == 'Water':
shader_id = 1.08
elif mprops.ugplus_shader == 'Water_Custom':
shader_id = 3.16
elif mprops.ugplus_shader == 'Ocean':
shader_id = 23.42
elif mprops.ugplus_shader == 'Skybox':
shader_id = 8.15
elif mprops.ugplus_shader == 'PhysicalSky':
shader_id = 8.15
elif mprops.ugplus_shader == 'Cloud':
shader_id = 16.0
elif mprops.ugplus_shader == 'Grass':
shader_id = 24.0
elif mprops.ugplus_shader == 'Glass':
shader_id = 32.0
elif mprops.ugplus_shader == 'Emission':
shader_id = 32.0
elif mprops.ugplus_shader == 'EditorGuide':
shader_id = -1.08
elif mprops.ugplus_shader == 'Unlit':
shader_id = -3.16
mat_flags = 0
export_textures = []
# Now we export the textures in a specific order, depending on the shader
if mprops.ugplus_shader == 'PBR':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_diffuse, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_normal, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_detail, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_lightmap, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_lightmap2, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_lightmap3, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_weathermask, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_snow, 'flags': 0 })
elif mprops.ugplus_shader == 'Glass':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_diffuse, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_normal, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_weathermask, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_snow, 'flags': 0 })
elif mprops.ugplus_shader == 'Skybox':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_diffuse, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_diffuse_evening, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_diffuse_night, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_diffuse_morning, 'flags': 0 })
elif mprops.ugplus_shader == 'PhysicalSky':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_diffuse_night, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_detail, 'flags': 0 })
elif mprops.ugplus_shader == 'Cloud':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_cloud, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_detail, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_fallback, 'flags': 0 })
elif mprops.ugplus_shader == 'Grass':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_diffuse, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_detail, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_normal, 'flags': 0 })
elif mprops.ugplus_shader == 'Water':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_fallback, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_reflection, 'flags': 0 })
elif mprops.ugplus_shader == 'Water_Custom':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_normal, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_normal2, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_fallback, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_detail, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_lightmap, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_lightmap2, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_lightmap3, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_lightmap4, 'flags': 0 })
elif mprops.ugplus_shader == 'Ocean':
export_textures.append({ 'mat_node': mprops.ugplus_matslot_normal, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_normal2, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_detail, 'flags': 0 })
export_textures.append({ 'mat_node': mprops.ugplus_matslot_fallback, 'flags': 0 })
num_passes = 4 if len(export_textures) > 4 else len(export_textures)
if is_hex_string(m.name):
checksum = int(m.name, 0)
else:
checksum = crc_from_string(bytes(m.name, 'ascii'))
w("I", checksum) # material checksum
w("I", checksum) # material name checksum
w("I", num_passes) # material passes
w("I", mprops.alpha_cutoff) # alpha cutoff (actually an unsigned byte)
w("?", mprops.sorted) # sorted?
w("f", mprops.draw_order) # draw order
w("?", (mprops.no_backface_culling == False)) # single sided
w("?", mprops.no_backface_culling) # no backface culling
w("i", mprops.z_bias) # z-bias
#grassify = False
w("?", mprops.grass_props.grassify) # grassify
if mprops.grass_props.grassify: # if grassify
print("EXPORTING GRASS MATERIAL!")
w("f", mprops.grass_props.grass_height) # grass height
w("i", mprops.grass_props.grass_layers) # grass layers
w("f", shader_id) # Shader ID (previously Specular power)
if shader_id > 0.0: # Additional material props (replaces specular color field)
w("f", mprops.ugplus_extra1)
w("f", mprops.ugplus_extra2)
w("f", mprops.ugplus_extra3)
shader_flags = get_ugplus_shader_flags(mprops)
# Export all the textures we need for the shader, as gathered above
tex_count = -1
for node in export_textures:
tex_count += 1
tex = node['mat_node']
tex_flags = node['flags']
# Use the name of the texture image, or generate the color name (will be generated during .tex export)
if tex.tex_image == None:
if bpy.data.images.get(tex.tex_image_name):
tex_checksum = crc_from_string(bytes(tex.tex_image_name, 'ascii'))
else:
colortex_name = 'io_thps_scene_Color_' + ''.join('{:02X}'.format(int(255*a)) for a in tex.tex_color)
tex_checksum = crc_from_string(bytes(colortex_name, 'ascii'))
elif is_hex_string(tex.tex_image.name):
if not bpy.data.images.get(tex.tex_image.name):
raise Exception("Image {} not found.".format(tex.tex_image.name))
tex_checksum = int(tex.tex_image.name, 0)
else:
if not bpy.data.images.get(tex.tex_image.name):
raise Exception("Image {} not found.".format(tex.tex_image.name))
tex_checksum = crc_from_string(bytes(tex.tex_image.name, 'ascii'))
# If the texture count is beyond the 4 texture limit, we are simply exporting frames for an animated texture
# which is created at the bottom of this loop
if tex_count > 3:
w("I", (tex_count - 3))
w("I", tex_checksum) # texture checksum
continue
w("I", tex_checksum) # texture checksum
pass_flags = MATFLAG_SMOOTH | MATFLAG_TEXTURED
pass_flags |= tex_flags
if tex_count == 3 and len(export_textures) > 4:
pass_flags |= MATFLAG_PASS_TEXTURE_ANIMATES
if tex_count <= 3 and tex.has_uv_wibbles:
pass_flags |= MATFLAG_UV_WIBBLE
if tex_count == 0 and mprops.ugplus_trans:
pass_flags |= MATFLAG_TRANSPARENT
if tex_count == 0 and mprops.ugplus_shader == 'Water':
pass_flags |= MATFLAG_WATER_EFFECT
w("I", pass_flags) # flags # 4132
w("?", True) # has color flag; seems to be ignored
w("3f", *((tex.tex_color[0],tex.tex_color[1],tex.tex_color[2]) ))
#w("3f", *(m.diffuse_color / 2.0)) # color
w("I", globals()[tex.blend_mode] if tex else vBLEND_MODE_DIFFUSE)
w("I", int(tex.tex_color[3]*127.0)) #w("I", pprops.blend_fixed_alpha if pprops else 0)
w("I", 0) # u adressing (wrap, clamp, etc)
w("I", 0) # v adressing
w("2f", *((3.0, 3.0))) # envmap multiples
w("I", shader_flags) # new material flags (originally unused filtering mode)
# Export UV wibbles on the first 4 passes
if pass_flags & MATFLAG_UV_WIBBLE:
w("2f", *tex.uv_wibbles.uv_velocity)
w("2f", *tex.uv_wibbles.uv_frequency)
w("2f", *tex.uv_wibbles.uv_amplitude)
w("2f", *tex.uv_wibbles.uv_phase)
# If we're going beyond pass 4, place additional textures in the animated texture slot for pass 4
if tex_count == 3 and len(export_textures) > 4:
w("i", len(export_textures) - 3)
w("i", 108) # period
w("i", 0) # iterations
w("i", 0) # phase
#for keyframe in at.keyframes:
# Export the first animated frame here, since it will be the 4th texture slot we want anyway
w("I", tex_count - 3)
w("I", tex_checksum) # texture checksum
continue
w("I", 1) # MMAG
w("I", 4) # MMIN
w("f", tex.lod_bias-8.0) # K
w("f", -8.0) # L
if tex_count > 3:
# Need to write these after for the final texture pass if the material uses more than 4 passes
w("I", 1) # MMAG
w("I", 4) # MMIN
w("f", -8.0) # K
w("f", -8.0) # L
# After exporting materials, this ensures any temporary materials/textures created during
# the grass effect export are removed before running another export
def cleanup_grass_materials():
print("Removing temporary grass materials/textures...")
for tex in bpy.data.textures:
if tex.name.startswith('Grass-Tx_Grass_Layer'):
print("Cleaning up grass texture: {}".format(tex.name))
tex.name = 'TEMP_GrassTx'
tex.user_clear()
#bpy.data.textures.remove(tex)
for mat in bpy.data.materials:
if mat.name.startswith('Grass-Grass_Layer'):
print("Cleaning up grass material: {}".format(mat.name))
mat.name = 'TEMP_GrassMat'
mat.user_clear()
#bpy.data.materials.remove(mat)
print("Done!")
def get_material_replace_flag(mprops):
if not mprops.allow_replace:
return 0
return MATFLAG_REPLACE
'''for i in range(1, 9):
if mprops.replace_group_index == i:
return globals()['MATFLAG_REPLACE_GROUP' + str(i)]
return 0'''
def export_materials(output_file, target_game, operator=None, is_model=False):
def w(fmt, *args):
output_file.write(struct.pack(fmt, *args))
# out_objects = [o for o in bpy.data.objects if o.type == "MESH"]
# out_materials = {o.active_material for o in out_objects if o.active_material}
_ensure_default_material_exists()
out_materials = bpy.data.materials[:]
num_materials = len(out_materials)
grass_counter = -1 # Index of the grass material, required to support multiple unique grass effects per level
grass_materials = []
for m in out_materials:
mprops = m.thug_material_props
if mprops.grass_props.grassify:
grass_counter += 1
num_grass_layers = len(m.thug_material_props.grass_props.grass_textures)
source_material = bpy.data.materials.get(m.thug_material_props.grass_props.source_material)
if not source_material:
raise Exception("Source material {} referenced in grass material {} not found.".format(m.thug_material_props.grass_props.source_material, m.name))
num_materials += num_grass_layers
for layer in range(num_grass_layers):
# Clone the base material as many times as we have grass layers, and append to out_materials
# Also make sure to increment num_materials
new_mat = source_material.copy()
new_mat.thug_material_props.grass_props.grassify = False
if not new_mat.thug_material_props.use_new_mats:
if not new_mat.texture_slots[0] or not new_mat.texture_slots[0].texture:
raise Exception('Source material for grass effect must have at least one texture pass!')
texture = new_mat.texture_slots[0].texture
new_texture = texture.copy()
if not hasattr(texture, 'image') or not texture.image:
raise Exception('Source material for grass effect must have at least one image texture!')
new_texture.image = m.thug_material_props.grass_props.grass_textures[layer].tex_image
new_texture.image.thug_image_props.compression_type = 'DXT5'
new_texture.image.thug_image_props.mip_levels = source_material.texture_slots[0].texture.image.thug_image_props.mip_levels
new_texture.name = "Grass-Tx_Grass_Layer{}_{}".format(layer, grass_counter)
pprops = new_texture.thug_material_pass_props
pprops.pf_textured = True
pprops.pf_smooth = True
pprops.pf_transparent = True
pprops.blend_mode = 'vBLEND_MODE_BLEND'
if layer > 0: # Add 'wind' UV wibbles
pprops.has_uv_wibbles = True
wibble_multi = layer / num_grass_layers
wibble_multi2 = wibble_multi * wibble_multi
pprops.uv_wibbles.uv_velocity[0] = 0.0
pprops.uv_wibbles.uv_velocity[1] = 0.0
pprops.uv_wibbles.uv_frequency = mprops.grass_props.uv_frequency
pprops.uv_wibbles.uv_amplitude = mprops.grass_props.uv_amplitude
pprops.uv_wibbles.uv_amplitude[0] *= wibble_multi2
pprops.uv_wibbles.uv_amplitude[1] *= wibble_multi2
pprops.uv_wibbles.uv_phase[0] = 0.0
pprops.uv_wibbles.uv_phase[1] = 0.0
new_mat.texture_slots[0].texture = new_texture
else:
new_mat.thug_material_props.ugplus_matslot_diffuse.tex_image = m.thug_material_props.grass_props.grass_textures[layer].tex_image
new_mat.thug_material_props.ugplus_matslot_diffuse.tex_image.thug_image_props.compression_type = 'DXT5'
new_mat.thug_material_props.ugplus_matslot_diffuse.tex_image.thug_image_props.mip_levels = source_material.thug_material_props.ugplus_matslot_diffuse.tex_image.thug_image_props.mip_levels
if layer > 0: # Add 'wind' UV wibbles
new_mat.thug_material_props.ugplus_matslot_diffuse.has_uv_wibbles = True
wibble_multi = layer / num_grass_layers
wibble_multi2 = wibble_multi * wibble_multi
new_mat.thug_material_props.ugplus_matslot_diffuse.uv_wibbles.uv_velocity[0] = 0.0
new_mat.thug_material_props.ugplus_matslot_diffuse.uv_wibbles.uv_velocity[1] = 0.0
new_mat.thug_material_props.ugplus_matslot_diffuse.uv_wibbles.uv_frequency = mprops.grass_props.uv_frequency
new_mat.thug_material_props.ugplus_matslot_diffuse.uv_wibbles.uv_amplitude = mprops.grass_props.uv_amplitude
new_mat.thug_material_props.ugplus_matslot_diffuse.uv_wibbles.uv_amplitude[0] *= wibble_multi2
new_mat.thug_material_props.ugplus_matslot_diffuse.uv_wibbles.uv_amplitude[1] *= wibble_multi2
new_mat.thug_material_props.ugplus_matslot_diffuse.uv_wibbles.uv_phase[0] = 0.0
new_mat.thug_material_props.ugplus_matslot_diffuse.uv_wibbles.uv_phase[1] = 0.0
new_mat.name = "Grass-Grass_Layer{}_{}".format(layer, grass_counter)
grass_materials.append(new_mat)
mprops.grass_props.grass_index = grass_counter
# Append grass materials
for g in grass_materials:
out_materials.append(g)
w("I", num_materials)
for m in out_materials:
LOG.debug("writing material: {}".format(m.name))
mprops = m.thug_material_props
# Export shader
if mprops.use_new_mats and mprops.ugplus_shader != 'None':
LOG.debug("exporting new material system properties...")
export_ugplus_material(m, output_file, target_game, operator)
continue
#denetii - only include texture slots that affect the diffuse color in the Blender material
passes = [tex_slot.texture for tex_slot in m.texture_slots if tex_slot and tex_slot.use and (tex_slot.use_map_color_diffuse or tex_slot.use_map_normal)]
if len(passes) > 4:
if operator:
operator.report(
{"WARNING"},
"Material {} has more than 4 passes (enabled texture slots). Using only the first 4.".format(m.name))
passes = passes[:4]
if not passes and m.name != "_THUG_DEFAULT_MATERIAL_":
if operator:
if not m.name.startswith('io_thps_scene_'):
operator.report({"WARNING"}, "Material {} has no passes (enabled texture slots). Using it's diffuse color.".format(m.name))
passes = [None]
if is_hex_string(m.name):
checksum = int(m.name, 0)
else:
checksum = crc_from_string(bytes(m.name, 'ascii'))
w("I", checksum) # material checksum
w("I", checksum) # material name checksum
w("I", len(passes) or 1) # material passes
w("I", mprops.alpha_cutoff) # alpha cutoff (actually an unsigned byte)
w("?", mprops.sorted) # sorted?
w("f", mprops.draw_order) # draw order
w("?", mprops.single_sided) # single sided
w("?", mprops.no_backface_culling) # no backface culling
w("i", mprops.z_bias) # z-bias
w("?", mprops.grass_props.grassify) # grassify
if mprops.grass_props.grassify: # if grassify
print("EXPORTING GRASS MATERIAL!")
w("f", mprops.grass_props.grass_height) # grass height
num_grass_layers = len(mprops.grass_props.grass_textures)
w("i", num_grass_layers) # grass layers
w("f", 0.01) # specular power
w("f", 0.0) # specular color
w("f", 0.0) # specular color
w("f", mprops.grass_props.grass_index) # grass index (actually specular color B channel)
else:
w("f", mprops.specular_power) # specular power
if mprops.specular_power > 0.0:
w("3f", *mprops.specular_color) # specular color
# using_default_texture = not passes
tex_count = -1
for texture in passes:
tex_count += 1
pprops = texture and texture.thug_material_pass_props
tex_checksum = 0
if texture and hasattr(texture, 'image') and texture.image:
if is_hex_string(texture.image.name):
tex_checksum = int(texture.image.name, 0)
else:
tex_checksum = crc_from_string(bytes(texture.image.name, 'ascii'))
w("I", tex_checksum) # texture checksum
pass_flags = 0 # MATFLAG_SMOOTH
if tex_checksum and pprops.pf_textured:
pass_flags |= MATFLAG_TEXTURED
if pprops and pprops.has_uv_wibbles:
pass_flags |= MATFLAG_UV_WIBBLE
if (pprops and
pprops.has_animated_texture and
len(pprops.animated_texture.keyframes)):
pass_flags |= MATFLAG_PASS_TEXTURE_ANIMATES
if pprops and pprops.pf_transparent:
pass_flags |= MATFLAG_TRANSPARENT
if pprops and pprops.ignore_vertex_alpha:
pass_flags |= MATFLAG_PASS_IGNORE_VERTEX_ALPHA
if pprops and pprops.pf_decal:
pass_flags |= MATFLAG_DECAL
if pprops and pprops.pf_smooth:
pass_flags |= MATFLAG_SMOOTH
if pprops and pprops.pf_environment:
pass_flags |= MATFLAG_ENVIRONMENT
if pprops and pprops.pf_bump:
print("EXPORTING BUMP MAP TEXTURE!")
pass_flags |= MATFLAG_BUMP_SIGNED_TEXTURE
#pass_flags |= MATFLAG_BUMP_LOAD_MATRIX
if pprops and pprops.pf_water:
print("EXPORTING WATER TEXTURE!")
pass_flags |= MATFLAG_WATER_EFFECT
if pprops and pprops.pf_static:
pass_flags |= MATFLAG_STATIC
if mprops.allow_recolor:
pass_flags |= MATFLAG_ALLOW_RECOLOR
if mprops.allow_replace:
pass_flags |= get_material_replace_flag(mprops)
if mprops.fixed_scale:
pass_flags |= MATFLAG_FIXED_SCALE
w("I", pass_flags) # flags # 4132
w("?", True) # has color flag; seems to be ignored
w("3f", *(pprops.color if pprops else m.diffuse_color / 2.0)) # color
# alpha register values, first u32 - a BLEND_MODE, second u32 - fixed alpha (clipped to u8)
# w("Q", 5)
w("I", globals()[pprops.blend_mode] if pprops else vBLEND_MODE_DIFFUSE)
w("I", pprops.blend_fixed_alpha if pprops else 0)
w("I", 0 if (not pprops) or pprops.u_addressing == "Repeat" else 1) # u adressing (wrap, clamp, etc)
w("I", 0 if (not pprops) or pprops.v_addressing == "Repeat" else 1) # v adressing
w("2f", *(pprops.envmap_multiples if pprops else (3.0, 3.0))) # envmap multiples
if mprops.allow_replace:
w("I", mprops.replace_group_index if tex_count == 0 else 65540) # Material replacement group ID
else:
w("I", TERRAIN_TYPES.index(mprops.terrain_type) if tex_count == 0 else 65540) # terrain type (previously: unused filtering mode)
# uv wibbles
if pprops and pass_flags & MATFLAG_UV_WIBBLE:
w("2f", *pprops.uv_wibbles.uv_velocity)
w("2f", *pprops.uv_wibbles.uv_frequency)
w("2f", *pprops.uv_wibbles.uv_amplitude)
w("2f", *pprops.uv_wibbles.uv_phase)
# vertex color wibbles
# anims
if pass_flags & MATFLAG_PASS_TEXTURE_ANIMATES:
at = pprops.animated_texture
w("i", len(at.keyframes))
w("i", at.period)
w("i", at.iterations)
w("i", at.phase)
for keyframe in at.keyframes:
w("I", keyframe.time)
w("I", crc_from_string(bytes(keyframe.image, 'ascii')))
w("I", 1) # MMAG
w("I", 4) # MMIN
w("f", (pprops.lod_bias-8.0) if hasattr(pprops, 'lod_bias') else -8.0) # K
w("f", -8.0) # L
#----------------------------------------------------------------------------------
def _material_pass_settings_draw(self, context):
if not context.object:
return
ob = context.object
if not ob.active_material or not ob.active_material.active_texture:
return
attrs = [
"color",
"blend_mode",
"blend_fixed_alpha",
"lod_bias",
"u_addressing",
"v_addressing"]
#"filtering_mode",
#"test_passes"]
pass_props = ob.active_material.active_texture.thug_material_pass_props
for attr in attrs:
self.layout.prop(
pass_props,
attr)
box = self.layout.box().column()
box.row().prop(pass_props, "pf_textured")
img = getattr(ob.active_material.active_texture, 'image', None)
if img and pass_props.pf_textured:
box.row().prop(img.thug_image_props, 'compression_type')
box.row().prop(img.thug_image_props, 'img_flags')
box.row().prop(img.thug_image_props, 'max_size')
box.row().prop(img.thug_image_props, 'mip_levels')
box.row().prop(pass_props, "pf_bump")
box.row().prop(pass_props, "pf_water")
box.row().prop(pass_props, "pf_environment")
if pass_props.pf_environment:
box.row().prop(pass_props, "envmap_multiples")
box.row().prop(pass_props, "pf_decal")
box.row().prop(pass_props, "pf_smooth")
box.row().prop(pass_props, "pf_transparent")
box.row().prop(pass_props, "pf_static")
box.row().prop(pass_props, "ignore_vertex_alpha")
box.row().prop(pass_props, "has_uv_wibbles")
box.row().prop(pass_props, 'has_animated_texture')
if pass_props.has_uv_wibbles:
box = self.layout.box().column(True)
box.row().prop(pass_props.uv_wibbles, "uv_velocity")
box.row().prop(pass_props.uv_wibbles, "uv_frequency")
box.row().prop(pass_props.uv_wibbles, "uv_amplitude")
box.row().prop(pass_props.uv_wibbles, "uv_phase")
if pass_props.has_animated_texture:
at = pass_props.animated_texture
box = self.layout.box()
col = box.column(True)
col.prop(at, "period")
col.prop(at, "iterations")
col.prop(at, "phase")
row = box.row(True)
row.operator("object.thug_add_texture_keyframe", text="Add")
row.operator("object.thug_remove_texture_keyframe", text="Remove")
box.row().template_list("THUGAnimatedTextureKeyframesUIList", "", at, "keyframes", at, "keyframes_index", rows=1)
# box.row().operator(at, "keyframes")
#----------------------------------------------------------------------------------
def _material_settings_draw(self, context):
if not context.scene: return
scn = context.scene
if not context.object: return
ob = context.object
if not ob.active_material: return
mps = ob.active_material.thug_material_props
row = self.layout.row()
row.prop(mps, "terrain_type")
row = self.layout.row()
row.prop(mps, "alpha_cutoff")
row.prop(mps, "sorted")
row = self.layout.row()
row.prop(mps, "z_bias")
row.prop(mps, "single_sided")
row = self.layout.row()
row.prop(mps, "draw_order")
row.prop(mps, "no_backface_culling")
row = self.layout.row()
row.prop(mps, "specular_power")
row.prop(mps, "no_skater_shadow")
row = self.layout.row()
row.prop(mps, "specular_color")
gps = mps.grass_props
self.layout.row().prop(gps, "grassify", toggle=True, icon="HAIR")
if gps.grassify:
box = self.layout.box()
row = box.row()
col = row.column(True)
col.prop_search(gps, "source_material", bpy.data, "materials", text="")
col.prop(gps, "grass_height")
#col.prop(gps, "grass_layers")
col.label(text="Grass Layers: {}/{}".format(len(gps.grass_textures), 32))
row = box.row()
row.operator("object.thug_add_grass_texture", text="Add")
row.operator("object.thug_remove_grass_texture", text="Remove")
row = box.row()
col = row.column(True)
col.template_list("THUGGrassTextureUIList", "", gps, "grass_textures", gps, "texture_index", rows=1)
row = box.row(True)
col = row.column(align=True)
col.scale_x = 0.5
row = col.row()
row.prop(gps, "uv_frequency")
row = col.row()
row.prop(gps, "uv_amplitude")
if scn.thug_level_props.export_props.target_game != 'THUG1':
return
self.layout.row().prop(mps, "fixed_scale", toggle=True, icon="MATERIAL")
self.layout.row().prop(mps, "allow_recolor", toggle=True, icon="MATERIAL")
self.layout.row().prop(mps, "allow_replace", toggle=True, icon="MATERIAL")
if mps.allow_replace:
self.layout.row().prop(mps, "replace_group_index")
self.layout.row().prop(mps, "use_new_mats", toggle=True, icon="MATERIAL")
if mps.use_new_mats:
box = self.layout.box().column()
row = box.row(True).column()
row.prop(mps, "ugplus_shader")
row.prop(mps, "ugplus_lighting_mode")
# Draw shader-specific settings first
if mps.ugplus_shader == 'PBR':
row.separator()
split = row.split()
#c = split.column()
#c.prop(mps, "ugplus_shader_baked", toggle=True, icon='TEXTURE_SHADED')
c = split.column()
c.prop(mps, "ugplus_shader_weather", toggle=True, icon='MOD_FLUIDSIM')
split = row.split()
c = split.column()
c.prop(mps, "ugplus_shader_disp", toggle=True, icon='MOD_DISPLACE')
c = split.column()
c.enabled = (mps.ugplus_shader_disp != False)
c.prop(mps, "ugplus_extra1", text='Disp Strength')
elif mps.ugplus_shader == 'Water' or mps.ugplus_shader == 'Ocean':
row.separator()
split = row.split()
c = split.column()
c.prop(mps, "ugplus_extra1", text='Bump Strength')
c = split.column()
c.prop(mps, "ugplus_shader_disp", toggle=True, icon='MOD_DISPLACE')
c = split.column()
c.enabled = (mps.ugplus_shader_disp != False)
c.prop(mps, "ugplus_extra2", text='Height')
elif mps.ugplus_shader == 'Water_Custom':
row.separator()
split = row.split()
c = split.column()
c.prop(mps, "ugplus_extra1", text='Bump Strength')
row.separator()
# Then draw the texture slots for each shader
if mps.ugplus_shader == 'PBR':
ugplus_matslot_draw(mps.ugplus_matslot_diffuse, box, title='Diffuse', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Detail', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_normal, box, title='Normal', allow_uv_wibbles=False)
if mps.ugplus_shader_weather:
ugplus_matslot_draw(mps.ugplus_matslot_weathermask, box, title='Rain/Snow Mask')
ugplus_matslot_draw(mps.ugplus_matslot_snow, box, title='Snow', allow_uv_wibbles=False)
elif mps.ugplus_shader == 'Water':
ugplus_matslot_draw(mps.ugplus_matslot_fallback, box, title='Diffuse', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_reflection, box, title='Reflection')
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Detail', allow_blending=True)
elif mps.ugplus_shader == 'Water_Custom':
ugplus_matslot_draw(mps.ugplus_matslot_normal, box, title='Normal Map 1', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_normal2, box, title='Normal Map 2')
ugplus_matslot_draw(mps.ugplus_matslot_fallback, box, title='Mask')
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Detail')
elif mps.ugplus_shader == 'Ocean':
ugplus_matslot_draw(mps.ugplus_matslot_normal, box, title='Normal Map 1', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_normal2, box, title='Normal Map 2')
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Foam')
ugplus_matslot_draw(mps.ugplus_matslot_fallback, box, title='Disp Map 1')
elif mps.ugplus_shader == 'Glass':
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Detail', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_normal, box, title='Normal', allow_uv_wibbles=False)
elif mps.ugplus_shader == 'Emission':
ugplus_matslot_draw(mps.ugplus_matslot_diffuse, box, title='Diffuse', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Emissive Map', allow_uv_wibbles=False)
elif mps.ugplus_shader == 'PhysicalSky':
ugplus_matslot_draw(mps.ugplus_matslot_diffuse_night, box, title='Night Sky', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Moon', allow_blending=False)
elif mps.ugplus_shader == 'Skybox':
ugplus_matslot_draw(mps.ugplus_matslot_diffuse, box, title='Day')
ugplus_matslot_draw(mps.ugplus_matslot_diffuse_evening, box, title='Evening')
ugplus_matslot_draw(mps.ugplus_matslot_diffuse_night, box, title='Night')
ugplus_matslot_draw(mps.ugplus_matslot_diffuse_morning, box, title='Morning')
elif mps.ugplus_shader == 'Cloud':
ugplus_matslot_draw(mps.ugplus_matslot_cloud, box, title='Cloud/Weather Mask', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Cloud/Weather Mask', allow_blending=False)
ugplus_matslot_draw(mps.ugplus_matslot_fallback, box, title='Cloud/Weather Mask', allow_blending=False)
elif mps.ugplus_shader == 'Grass':
ugplus_matslot_draw(mps.ugplus_matslot_diffuse, box, title='Layer Mask', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Detail', allow_blending=False)
ugplus_matslot_draw(mps.ugplus_matslot_normal, box, title='Noise', allow_blending=False)
elif mps.ugplus_shader == 'EditorGuide' or mps.ugplus_shader == 'Unlit':
ugplus_matslot_draw(mps.ugplus_matslot_diffuse, box, title='Base Texture', allow_blending=True)
ugplus_matslot_draw(mps.ugplus_matslot_detail, box, title='Detail', allow_blending=True)
# PROPERTIES
#############################################
class THUGImageProps(bpy.types.PropertyGroup):
compression_type = EnumProperty(items=(
("DXT1", "DXT1", "DXT1. 1-bit alpha. 1:8 compression for RGBA, 1:6 for RGB"),
("DXT5", "DXT5", "DXT5. Full alpha. 1:4 compression")),
name="Compression Type",
default="DXT1")
mip_levels = IntProperty(name="Mip Levels", min=1, max=8, default=0, description="Maximum number of mip levels (0 to use automatic settings)")
max_size = IntProperty(name="Max Size", min=0, max=16384, default=0, description="Maximum width/height of image allowed during export (0 to disable)")
img_flags = EnumProperty(items=(
("1", "Invert Alpha", "Invert alpha channel on this image"),
("2", "Grayscale", "Export as grayscale")
),
name="Options",
description="Flags/options used when exporting to the tex file",
options={'ENUM_FLAG'} )
#----------------------------------------------------------------------------------
class AddTextureKeyframe(bpy.types.Operator):
bl_idname = "object.thug_add_texture_keyframe"
bl_label = "Add THUG Texture Keyframe"
bl_options = {"REGISTER", "UNDO", "INTERNAL"}
@classmethod
def poll(cls, context):
if not context:
return False
ob = context.object
if not ob:
return False
mat = ob.active_material
if not mat:
return False
tex = mat.active_texture
if not tex:
return False
mpp = tex.thug_material_pass_props
if not mpp or not mpp.has_animated_texture:
return False
return True
def execute(self, context):
at = context.object.active_material.active_texture.thug_material_pass_props.animated_texture
at.keyframes.add()
at.keyframes_index = len(at.keyframes) - 1
return {"FINISHED"}