forked from Loyalists/Sollumz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ydrexport.py
1193 lines (932 loc) · 37.7 KB
/
ydrexport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.types import Operator
from bpy.props import StringProperty
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
from xml.dom import minidom
from mathutils import Vector, Matrix
import os
import sys
import shutil
import ntpath
from datetime import datetime
from . import shaderoperators as Shader
def prettify(elem):
rough_string = tostring(elem, encoding='UTF-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def get_obj_children(obj):
children = []
objects = bpy.context.scene.objects
for ob in obj.children:
if objects.get(ob.name):
children.append(ob)
return children
def order_vertex_list(list, vlayout):
layout_map = {
"Position": 0,
"Normal": 1,
"Colour0": 2,
"Colour1": 3,
"TexCoord0": 4,
"TexCoord1": 5,
"TexCoord2": 6,
"TexCoord3": 7,
"TexCoord4": 8,
"TexCoord5": 9,
"Tangent": 10,
"BlendWeights": 11,
"BlendIndices": 12,
}
newlist = []
for i in range(len(vlayout)):
layout_key = layout_map[vlayout[i]]
if layout_key != None:
if list[layout_key] == None:
raise TypeError("Missing layout item " + vlayout[i])
newlist.append(list[layout_key])
else:
print('Incorrect layout element', vlayout[i])
if (len(newlist) != len(vlayout)):
print('Incorrect layout parse')
return newlist
def vector_tostring(vector):
try:
string = [str(vector.x), str(vector.y)]
if(hasattr(vector, "z")):
string.append(str(vector.z))
if(hasattr(vector, "w")):
string.append(str(vector.w))
return " ".join(string)
except:
return None
def meshloopcolor_tostring(color):
try:
string = ""
string += str(round(color[0] * 255)) + " "
string += str(round(color[1] * 255)) + " "
string += str(round(color[2] * 255)) + " "
string += str(round(color[3] * 255))
return string
except:
return None
def process_uv(uv):
u = uv[0]
v = (uv[1] - 1.0) * -1
return [u, v]
def get_vertex_string(obj, vlayout, bones, depsgraph):
mesh = bpy.data.meshes.new_from_object(obj, preserve_all_data_layers=True, depsgraph=depsgraph)
# mesh = obj.to_mesh(preserve_all_data_layers=True, depsgraph=depsgraph)
# mesh = obj.data
allstrings = []
allstrings.append("\n") #makes xml a little prettier
vertamount = len(mesh.vertices)
vertices = [None] * vertamount
normals = [None] * vertamount
clr = [None] * vertamount
clr1 = [None] * vertamount
texcoords = {}
tangents = [None] * vertamount
blendw = [None] * vertamount
blendi = [None] * vertamount
for i in range(6):
texcoords[i] = [None] * vertamount
if mesh.has_custom_normals:
mesh.calc_normals_split()
else:
mesh.calc_normals()
mesh.calc_tangents()
vertex_groups = obj.vertex_groups
bones_index_dict = {}
for i in range(len(bones)):
bones_index_dict[bones[i].name] = i
clr0_layer = None
clr1_layer = None
if(mesh.vertex_colors == None):
clr0_layer = mesh.vertex_colors.new()
clr1_layer = mesh.vertex_colors.new()
else:
clr0_layer = mesh.vertex_colors[0]
if len(mesh.vertex_colors) >= 2:
clr1_layer = mesh.vertex_colors[1]
else:
clr1_layer = mesh.vertex_colors.new()
for uv_layer_id in range(len(mesh.uv_layers)):
uv_layer = mesh.uv_layers[uv_layer_id].data
for poly in mesh.polygons:
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
vi = mesh.loops[loop_index].vertex_index
uv = process_uv(uv_layer[loop_index].uv)
u = uv[0]
v = uv[1]
fixed_uv = Vector((u, v))
texcoords[uv_layer_id][vi] = fixed_uv
for poly in mesh.polygons:
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
vi = mesh.loops[loop_index].vertex_index
vertices[vi] = mesh.vertices[vi].co
# normals[vi] = mesh.vertices[vi].normal
normals[vi] = mesh.loops[loop_index].normal
clr[vi] = clr0_layer.data[loop_index].color
clr1[vi] = clr1_layer.data[loop_index].color
tangents[vi] = mesh.loops[loop_index].tangent.to_4d()
# https://github.com/labnation/MonoGame/blob/master/MonoGame.Framework.Content.Pipeline/Graphics/MeshHelper.cs#L298
# bitangent = bitangent_sign * cross(normal, tangent)
tangents[vi].w = mesh.loops[loop_index].bitangent_sign
#FIXME: one vert can only be influenced by 4 weights at most
vertex_group_elements = mesh.vertices[vi].groups
if len(vertex_group_elements) > 0:
blendw_list = []
blendi_list = []
valid_weights = 0
total_weights = 0
max_weights = 0
max_weights_position = -1
for element in vertex_group_elements:
if element.group >= len(vertex_groups):
continue
vertex_group = vertex_groups[element.group]
bone_index = bones_index_dict.get(vertex_group.name, -1)
# 1/255 = 0.0039 the minimal weight for one vertex group
weight = round(element.weight * 255)
if (vertex_group.lock_weight == False and bone_index != -1 and weight > 0 and valid_weights < 4):
blendw_list.append(weight)
blendi_list.append(bone_index)
if (max_weights < weight):
max_weights_position = valid_weights
max_weights = weight
valid_weights += 1
total_weights += weight
#fill the positions where there are no weights
if valid_weights < 4:
for i in range(4 - valid_weights):
blendw_list.append(0)
blendi_list.append(0)
# weights verification stuff
# wtf rockstar
# why do you even use int for weights
if valid_weights > 0 and max_weights_position != -1:
blendw_list[max_weights_position] = blendw_list[max_weights_position] + (255 - total_weights)
blendw[vi] = ' '.join(str(i) for i in blendw_list)
blendi[vi] = ' '.join(str(i) for i in blendi_list)
else:
blendw[vi] = "0 0 255 0"
blendi[vi] = "0 0 0 0"
for i in range(len(vertices)):
vstring = ""
tlist = []
tlist.append(vector_tostring(vertices[i]))
tlist.append(vector_tostring(normals[i]))
tlist.append(meshloopcolor_tostring(clr[i]))
tlist.append(meshloopcolor_tostring(clr1[i]))
tlist.append(vector_tostring(texcoords[0][i]))
tlist.append(vector_tostring(texcoords[1][i]))
tlist.append(vector_tostring(texcoords[2][i]))
tlist.append(vector_tostring(texcoords[3][i]))
tlist.append(vector_tostring(texcoords[4][i]))
tlist.append(vector_tostring(texcoords[5][i]))
tlist.append(vector_tostring(tangents[i]))
tlist.append(blendw[i])
tlist.append(blendi[i])
layoutlist = order_vertex_list(tlist, vlayout)
vstring = " " * 5
for l in layoutlist:
vstring += l
vstring += " " * 3
vstring += "\n"
allstrings.append(vstring)
vertex_string = ""
for s in allstrings:
vertex_string += s
return vertex_string
def get_index_string(mesh):
index_string = ""
for poly in mesh.polygons:
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
index_string += str(mesh.loops[loop_index].vertex_index) + " "
return index_string
def fix_shader_name(name, no_extension = False): #because blender renames everything to .00X
newname = ""
n = name.split(".")
if(len(n) == 3):
newname += n[0]
newname += "."
newname += n[1]
else:
newname = name
if(no_extension):
newname = newname[:-4]
return newname
def get_vertex_layout(shader):
shader = fix_shader_name(shader)
parameter_set = Shader.shaders.get(shader)
if parameter_set is not None:
for p in parameter_set:
if p.Type == "Layout":
return p.get_value()
print('Unknown shader: ', shader)
def write_model_node(objs, materials, bones):
m_node = Element("Item")
rm_node = Element("RenderMask")
rm_node.set("value", str(objs[0].mask))
flags_node = Element("Flags")
flags_node.set("value", "1")
has_skin_node = Element("HasSkin")
if len(objs[0].vertex_groups) > 0:
has_skin_node.set("value", "1")
else:
has_skin_node.set("value", "0")
bone_index_node = Element("BoneIndex")
bone_index_node.set("value", "0")
unk1_node = Element("Unknown1")
if bones != None:
unk1_node.set("value", str(len(bones)))
else:
unk1_node.set("value", "0")
geo_node = Element("Geometries")
# depsgraph stuff, for the purpose of auto-applying modifiers on exporting
depsgraph = bpy.context.evaluated_depsgraph_get()
for obj in objs:
obj_eval = obj.evaluated_get(depsgraph)
model = bpy.data.meshes.new_from_object(obj_eval, preserve_all_data_layers=True, depsgraph=depsgraph)
#model = obj.data
i_node = Element("Item")
shader_index = 0
shader = None
for idx in range(len(materials)):
if(model.materials[0] == materials[idx]):
shader_index = idx
shader = materials[idx]
shd_index = Element("ShaderIndex")
shd_index.set("value", str(shader_index))
bound_box_min = obj.bound_box[0]
bound_box_max = obj.bound_box[6]
bbmin_node = Element("BoundingBoxMin")
bbmin_node.set("x", str(bound_box_min[0]))
bbmin_node.set("y", str(bound_box_min[1]))
bbmin_node.set("z", str(bound_box_min[2]))
bbmin_node.set("w", "0")
bbmax_node = Element("BoundingBoxMax")
bbmax_node.set("x", str(bound_box_max[0]))
bbmax_node.set("y", str(bound_box_max[1]))
bbmax_node.set("z", str(bound_box_max[2]))
bbmax_node.set("w", "0")
boneids_node = Element("BoneIDs")
if bones != None:
boneids_node.text = ", ".join(str(i) for i in range(len(bones)))
vb_node = Element("VertexBuffer")
vbflags_node = Element("Flags")
vbflags_node.set("value", "0")
vblayout_node = Element("Layout")
vblayout_node.set("type", "GTAV1")
if(shader.sollumtype != "GTA"):
print("Error Material Type Is Not GTA!!")
return m_node
print('Processing shader', shader_index, shader.name)
vlayout = get_vertex_layout(shader.name)
data2_node = Element("Data2")
vertex_str = get_vertex_string(obj_eval, vlayout, bones, depsgraph)
data2_node.text = vertex_str
ib_node = Element("IndexBuffer")
data_node = Element("Data")
data_node.text = get_index_string(model)
ib_node.append(data_node)
for p in vlayout:
p_node = Element(p)
vblayout_node.append(p_node)
vb_node.append(vbflags_node)
vb_node.append(vblayout_node)
vb_node.append(data2_node)
i_node.append(shd_index)
i_node.append(bbmin_node)
i_node.append(bbmax_node)
i_node.append(boneids_node)
i_node.append(vb_node)
i_node.append(ib_node)
geo_node.append(i_node)
m_node.append(rm_node)
m_node.append(flags_node)
m_node.append(has_skin_node)
m_node.append(bone_index_node)
m_node.append(unk1_node)
m_node.append(geo_node)
#print(prettify(m_node))
return m_node
def write_drawablemodels_node(models, materials, bones):
high_models = []
med_models = []
low_models = []
for obj in models:
if(obj.level_of_detail == "High"):
high_models.append(obj)
if(obj.level_of_detail == "Medium"):
med_models.append(obj)
if(obj.level_of_detail == "Low"):
low_models.append(obj)
drawablemodels_high_node = None
drawablemodels_med_node = None
drawablemodels_low_node = None
if(len(high_models) != 0):
drawablemodels_high_node = Element("DrawableModelsHigh")
drawablemodels_high_node.append(write_model_node(high_models, materials, bones))
if(len(med_models) != 0):
drawablemodels_med_node = Element("DrawableModelsMedium")
drawablemodels_med_node.append(write_model_node(med_models, materials, bones))
if(len(low_models) != 0):
drawablemodels_low_node = Element("DrawableModelsLow")
drawablemodels_low_node.append(write_model_node(low_models, materials, bones))
dm_nodes = []
if(drawablemodels_high_node != None):
dm_nodes.append(drawablemodels_high_node)
if(drawablemodels_med_node != None):
dm_nodes.append(drawablemodels_med_node)
if(drawablemodels_low_node != None):
dm_nodes.append(drawablemodels_low_node)
return dm_nodes
def write_imageparam_node(node):
paramname = node.name
#if the same parameter is imported multiple time naming gets prefixed with .00X
#if("." in paramname):
# split = paramname.split(".")
# paramname = split[0]
iname = node.name
type = "Texture"
tname = "None" #givemechecker?
#if(node.image != None):
#tname = os.path.basename(node.image.filepath)
tname = node.texture_name[:-4] #delete file extension
i_node = Element("Item")
i_node.set("name", iname)
i_node.set("type", type)
if (tname != None and len(tname) > 0):
name_node = Element("Name")
name_node.text = tname
unk32_node = Element("Unk32")
unk32_node.set("value", "128")
i_node.append(name_node)
i_node.append(unk32_node)
return i_node
#some parameters use y,z,w
def write_vectorparam_node(nodes):
i_node = Element("Item")
name = nodes[0].name[:-2] #remove _x
type = "Vector"
i_node.set("name", name)
i_node.set("type", type)
i_node.set("x", str(nodes[0].outputs[0].default_value))
i_node.set("y", str(nodes[1].outputs[0].default_value))
i_node.set("z", str(nodes[2].outputs[0].default_value))
i_node.set("w", str(nodes[3].outputs[0].default_value))
return i_node
def write_shader_node(mat):
i_node = Element("Item")
name_node = Element("Name")
name_node.text = fix_shader_name(mat.name, True)
filename_node = Element("FileName")
filename_node.text = fix_shader_name(mat.name)
renderbucket_node = Element("RenderBucket")
renderbucket_node.set("value", "0")
parameter_set = Shader.shaders.get(fix_shader_name(mat.name))
if parameter_set is not None:
for p in parameter_set:
if p.Type == "RenderBucket":
renderbucket_node = p.write()
break
params_node = Element("Parameters")
mat_nodes = mat.node_tree.nodes
for node in mat_nodes:
if(isinstance(node, bpy.types.ShaderNodeTexImage)):
imgp_node = write_imageparam_node(node)
params_node.append(imgp_node)
if(isinstance(node, bpy.types.ShaderNodeValue)):
if(node.name[-1] == "x"):
x = node
y = mat_nodes[node.name[:-1] + "y"]
z = mat_nodes[node.name[:-1] + "z"]
w = mat_nodes[node.name[:-1] + "w"]
vnode = write_vectorparam_node([x, y, z, w])
params_node.append(vnode)
i_node.append(name_node)
i_node.append(filename_node)
i_node.append(renderbucket_node)
i_node.append(params_node)
return i_node
def write_shaders_node(materials):
shaders_node = Element("Shaders")
for mat in materials:
shader_node = write_shader_node(mat)
shaders_node.append(shader_node)
#print(prettify(shader_node))
return shaders_node
def write_tditem_node(exportpath, mat):
i_nodes = []
mat_nodes = mat.node_tree.nodes
for node in mat_nodes:
if(isinstance(node, bpy.types.ShaderNodeTexImage)):
if(node.embedded == False):
i_nodes.append(None)
else:
if(node.image != None):
foldername = "\\" + os.path.splitext(os.path.splitext(os.path.basename(exportpath))[0])[0]
if(os.path.isdir(os.path.dirname(exportpath) + foldername) == False):
os.mkdir(os.path.dirname(exportpath) + foldername)
txtpath = node.image.filepath
dstpath = os.path.dirname(exportpath) + foldername + "\\" + os.path.basename(node.image.filepath)
# SameFileError
if txtpath != dstpath:
shutil.copyfile(txtpath, dstpath)
else:
print("Missing Embedded Texture, please supply texture! The texture will not be copied to the texture folder until entered!")
#node.image.save_render(os.path.dirname(exportpath) + "\\untitled\\"+ os.path.basename(node.image.filepath), scene=None)
i_node = Element("Item")
name_node = Element("Name")
name_node.text = os.path.splitext(node.texture_name)[0]
i_node.append(name_node)
unk32_node = Element("Unk32")
unk32_node.set("value", "128")
i_node.append(unk32_node)
usage_node = Element("Usage")
usage_node.text = node.usage
i_node.append(usage_node)
uflags_node = Element("UsageFlags")
uflags_text = ""
if(node.not_half == True):
uflags_text += "NOT_HALF, "
if(node.hd_split == True):
uflags_text += "HD_SPLIT, "
if(node.flag_full == True):
uflags_text += "FULL, "
if(node.maps_half == True):
uflags_text += "MAPS_HALF, "
if(node.x2 == True):
uflags_text += "X2, "
if(node.x4 == True):
uflags_text += "X4, "
if(node.y4 == True):
uflags_text += "Y4, "
if(node.x8 == True):
uflags_text += "MAPS_HALF, "
if(node.x16 == True):
uflags_text += "X16, "
if(node.x32 == True):
uflags_text += "X32, "
if(node.x64 == True):
uflags_text += "X64, "
if(node.y64 == True):
uflags_text += "Y64, "
if(node.x128 == True):
uflags_text += "X128, "
if(node.x256 == True):
uflags_text += "X256, "
if(node.x512 == True):
uflags_text += "X512, "
if(node.y512 == True):
uflags_text += "Y512, "
if(node.x1024 == True):
uflags_text += "X1024, "
if(node.y1024 == True):
uflags_text += "Y1024, "
if(node.x2048 == True):
uflags_text += "X2048, "
if(node.y2048 == True):
uflags_text += "Y2048, "
if(node.embeddedscriptrt == True):
uflags_text += "EMBEDDEDSCRIPTRT, "
if(node.unk19 == True):
uflags_text += "UNK19, "
if(node.unk20 == True):
uflags_text += "UNK20, "
if(node.unk21 == True):
uflags_text += "UNK21, "
if(node.unk24 == True):
uflags_text += "UNK24, "
uflags_text = uflags_text[:-2] #remove , from str
uflags_node.text = uflags_text
i_node.append(uflags_node)
eflags_node = Element("ExtraFlags")
eflags_node.set("value", str(node.extra_flags))
i_node.append(eflags_node)
width_node = Element("Width")
size = [0, 0]
if(node.image != None):
size = node.image.size
width_node.set("value", str(size[0]))
i_node.append(width_node)
height_node = Element("Height")
height_node.set("value", str(size[1]))
i_node.append(height_node)
miplevels_node = Element("MipLevels")
miplevels_node.set("value", "8")
i_node.append(miplevels_node)
format_node = Element("Format")
format_node.text = "D3DFMT_" + node.format_type
i_node.append(format_node)
filename_node = Element("FileName")
filename_node.text = node.texture_name
i_node.append(filename_node)
i_nodes.append(i_node)
return i_nodes
def write_texturedictionary_node(materials, exportpath):
td_node = Element("TextureDictionary")
all_nodes = []
for mat in materials:
i_nodes = write_tditem_node(exportpath, mat)
for node in i_nodes:
if(node != None):
all_nodes.append(node)
#removes duplicate embedded textures!
for node in all_nodes:
t_name = node[0].text
append = True
for t in td_node:
if(t[0].text == t_name):
append = False
if(append == True):
td_node.append(node)
return td_node
def write_shader_group_node(materials, filepath):
shaderg_node = Element("ShaderGroup")
unk30_node = Element("Unknown30")
unk30_node.set("value", "0")
td_node = write_texturedictionary_node(materials, filepath)
shader_node = write_shaders_node(materials)
shaderg_node.append(unk30_node)
shaderg_node.append(td_node)
shaderg_node.append(shader_node)
return shaderg_node
def write_skeleton_node(obj):
bones = obj.pose.bones
if len(bones) == 0:
return None
skeleton_node = Element("Skeleton")
#TODO: the current implementation works but IMHO there should be something more meaningful than "0"
#as long as it doesn't break in game
unk1c_node = Element("Unknown1C")
unk1c_node.set("value", "16777216")
unk50_node = Element("Unknown50")
unk50_node.set("value", "0")
unk54_node = Element("Unknown54")
unk54_node.set("value", "0")
unk58_node = Element("Unknown58")
unk58_node.set("value", "0")
bones_node = Element("Bones")
skeleton_node.append(unk1c_node)
skeleton_node.append(unk50_node)
skeleton_node.append(unk54_node)
skeleton_node.append(unk58_node)
skeleton_node.append(bones_node)
ind = 0
for pbone in bones:
bone = pbone.bone
bone["BONE_INDEX"] = ind
ind = ind + 1
for pbone in bones:
bone = pbone.bone
bone_node = Element("Item")
bone_node_name = Element("Name")
bone_node_name.text = bone.name
bone_node.append(bone_node_name)
bone_node_tag = Element("Tag")
bone_node_tag.set("value", str(bone.bone_properties.tag))
bone_node.append(bone_node_tag)
bone_node_tag = Element("Index")
bone_node_tag.set("value", str(bone["BONE_INDEX"]))
bone_node.append(bone_node_tag)
bone_node_parent_index = Element("ParentIndex")
bone_node_sibling_index = Element("SiblingIndex")
if bone.parent != None:
bone_node_parent_index.set("value", str(bone.parent["BONE_INDEX"]))
children = bone.parent.children
sibling = -1
if len(children) > 1:
for i, child in enumerate(children):
if child["BONE_INDEX"] == bone["BONE_INDEX"] and i + 1 < len(children):
sibling = children[i + 1]["BONE_INDEX"]
break
bone_node_sibling_index.set("value", str(sibling))
else:
bone_node_parent_index.set("value", "-1")
bone_node_sibling_index.set("value", "-1")
bone_node.append(bone_node_parent_index)
bone_node.append(bone_node_sibling_index)
bone_node_flags = Element("Flags")
flags = []
for flag in bone.bone_properties.flags:
flags.append(flag.name)
if len(bone.children) > 0:
flags.append("Unk0")
bone_node_flags.text = ", ".join(flags)
bone_node.append(bone_node_flags)
mat = bone.matrix_local
if (bone.parent != None):
mat = bone.parent.matrix_local.inverted() @ bone.matrix_local
mat_decomposed = mat.decompose()
trans = mat_decomposed[0]
bone_node_translation = Element("Translation")
bone_node_translation.set("x", str(trans[0]))
bone_node_translation.set("y", str(trans[1]))
bone_node_translation.set("z", str(trans[2]))
bone_node.append(bone_node_translation)
#quat = bone.rotation_euler.to_quaternion()
quat = mat_decomposed[1]
bone_node_rotation = Element("Rotation")
bone_node_rotation.set("x", str(quat[1]))
bone_node_rotation.set("y", str(quat[2]))
bone_node_rotation.set("z", str(quat[3]))
bone_node_rotation.set("w", str(quat[0]))
bone_node.append(bone_node_rotation)
scale = mat_decomposed[2]
bone_node_scale = Element("Scale")
bone_node_scale.set("x", str(scale[0]))
bone_node_scale.set("y", str(scale[1]))
bone_node_scale.set("z", str(scale[2]))
bone_node.append(bone_node_scale)
bone_node_transform_unk = Element("TransformUnk")
bone_node_transform_unk.set("x", "0")
bone_node_transform_unk.set("y", "4")
bone_node_transform_unk.set("z", "-3")
bone_node_transform_unk.set("w", "0")
bone_node.append(bone_node_transform_unk)
bones_node.append(bone_node)
return skeleton_node
def get_bbs(objs):
bounding_boxs = []
for obj in objs:
bounding_boxs.append(obj.bound_box)
bounding_boxmin = []
bounding_boxmax = []
for b in bounding_boxs:
bounding_boxmin.append(b[0])
bounding_boxmax.append(b[6])
min_xs = []
min_ys = []
min_zs = []
for v in bounding_boxmin:
min_xs.append(v[0])
min_ys.append(v[1])
min_zs.append(v[2])
max_xs = []
max_ys = []
max_zs = []
for v in bounding_boxmax:
max_xs.append(v[0])
max_ys.append(v[1])
max_zs.append(v[2])
bounding_box_min = []
bounding_box_min.append(min(min_xs))
bounding_box_min.append(min(min_ys))
bounding_box_min.append(min(min_zs))
bounding_box_max = []
bounding_box_max.append(max(max_xs))
bounding_box_max.append(max(max_ys))
bounding_box_max.append(max(max_zs))
return [bounding_box_min, bounding_box_max]
def add_vector_list(list1, list2):
x = list1[0] + list2[0]
y = list1[1] + list2[1]
z = list1[2] + list2[2]
return [x, y, z]
def subtract_vector_list(list1, list2):
x = list1[0] - list2[0]
y = list1[1] - list2[1]
z = list1[2] - list2[2]
return [x, y, z]
def multiple_vector_list(list, num):
x = list[0] * num
y = list[1] * num
z = list[2] * num
return [x, y, z]
def get_vector_list_length(list):
sx = list[0]**2
sy = list[1]**2
sz = list[2]**2
length = (sx + sy + sz) ** 0.5
return length
def get_sphere_bb(objs, bbminmax):
allverts = []
for obj in objs:
mesh = obj.data
for vert in mesh.vertices:
allverts.append(vert)
bscen = [0, 0, 0]
bsrad = 0
av = add_vector_list(bbminmax[0], bbminmax[1])
bscen = multiple_vector_list(av, 0.5)
for v in allverts:
bsrad = max(bsrad, get_vector_list_length(subtract_vector_list(v.co, bscen)))
return [bscen, bsrad]
#still presents a problem where in a scenario you had other ydrs you didnt want to export in the scene it would pick up
#them materials also
def get_used_materials(drawable):
materials = []
children = drawable.children
for obj in children:
if(obj.sollumtype == "Geometry"):
mat = obj.active_material
if(mat != None):
materials.append(mat)
return materials
def write_drawable(obj, filepath, root_name="Drawable", bones=None):
children = get_obj_children(obj)
bbminmax = get_bbs(children)
bbsphere = get_sphere_bb(children, bbminmax)
drawable_node = Element(root_name)
name_node = Element("Name")
name_node.text = obj.name.split(".")[0]
bsc_node = Element("BoundingSphereCenter")
bsc_node.set("x", str(bbsphere[0][0]))
bsc_node.set("y", str(bbsphere[0][1]))
bsc_node.set("z", str(bbsphere[0][2]))
bsr_node = Element("BoundingSphereRadius")
bsr_node.set("value", str(bbsphere[1]))
bbmin_node = Element("BoundingBoxMin")
bbmin_node.set("x", str(bbminmax[0][0]))
bbmin_node.set("y", str(bbminmax[0][1]))
bbmin_node.set("z", str(bbminmax[0][2]))
bbmax_node = Element("BoundingBoxMax")
bbmax_node.set("x", str(bbminmax[1][0]))
bbmax_node.set("y", str(bbminmax[1][1]))
bbmax_node.set("z", str(bbminmax[1][2]))
ldh_node = Element("LodDistHigh")
ldh_node.set("value", str(obj.drawble_distance_high))
ldm_node = Element("LodDistMed")
ldm_node.set("value", str(obj.drawble_distance_medium))
ldl_node = Element("LodDistLow")
ldl_node.set("value", str(obj.drawble_distance_low))
ldvl_node = Element("LodDistVlow")
ldvl_node.set("value", str(obj.drawble_distance_vlow))
geometrys = []
materials = get_used_materials(obj)
bounds = []
if bones == None:
bones = obj.pose.bones
flagshigh = 0
flagsmed = 0
flagslow = 0
flagsvlow = 0
for c in children:
if(c.sollumtype == "Geometry"):
geometrys.append(c)
if(c.level_of_detail == "High"):
flagshigh += 1
if(c.level_of_detail == "Medium"):
flagsmed += 1
if(c.level_of_detail == "Low"):
flagslow += 1
if(c.level_of_detail == "Very Low"):
flagsvlow += 1
flagsh_node = Element("FlagsHigh")
flagsh_node.set("value", str(flagshigh))