forked from Loyalists/Sollumz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ybnexport.py
391 lines (299 loc) · 10.8 KB
/
ybnexport.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
import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.types import Operator
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
from xml.dom import minidom
from mathutils import Vector
import os
import sys
import shutil
import ntpath
from datetime import datetime
from .ydrexport import get_obj_children, get_bbs, get_sphere_bb, prettify
def append_bbs(obj, node):
# children = get_obj_children(obj)
bbminmax = get_bbs([obj])
bbmin_node = Element("BoxMin")
bbmin_node.set("x", str(bbminmax[0][0]))
bbmin_node.set("y", str(bbminmax[0][1]))
bbmin_node.set("z", str(bbminmax[0][2]))
node.append(bbmin_node)
bbmax_node = Element("BoxMax")
bbmax_node.set("x", str(bbminmax[1][0]))
bbmax_node.set("y", str(bbminmax[1][1]))
bbmax_node.set("z", str(bbminmax[1][2]))
node.append(bbmax_node)
# TODO
loc = obj.matrix_world.to_translation()
pos_node = Element("CompositePosition")
pos_node.set("x", str(loc.x))
pos_node.set("y", str(loc.y))
pos_node.set("z", str(loc.z))
node.append(pos_node)
# TODO
loc = obj.matrix_world.to_translation()
rot_node = Element("CompositeRotation")
rot_node.set("x", str(0))
rot_node.set("y", str(0))
rot_node.set("z", str(0))
rot_node.set("w", str(1))
node.append(rot_node)
# TODO
loc = obj.matrix_world.to_translation()
scale_node = Element("CompositeScale")
scale_node.set("x", str(1))
scale_node.set("y", str(1))
scale_node.set("z", str(1))
node.append(scale_node)
# TODO
bbmax_node = Element("BoxCenter")
bbmax_node.set("x", str(0))
bbmax_node.set("y", str(0))
bbmax_node.set("z", str(0))
node.append(bbmax_node)
def append_junk(obj, node):
# TODO
inertia_node = Element("Inertia")
inertia_node.set("x", "1")
inertia_node.set("y", "1")
inertia_node.set("z", "1")
node.append(inertia_node)
# TODO
geomcenter_node = Element("GeometryCenter")
geomcenter_node.set("x", "0")
geomcenter_node.set("y", "0")
geomcenter_node.set("z", "0")
node.append(geomcenter_node)
# TODO
matind_node = Element("MaterialIndex")
matind_node.set("value", "0")
node.append(matind_node)
# TODO
matcolind_node = Element("MaterialColourIndex")
matcolind_node.set("value", "0")
node.append(matcolind_node)
# TODO
procid_node = Element("ProceduralID")
procid_node.set("value", "0")
node.append(procid_node)
# TODO
roomid_node = Element("RoomID")
roomid_node.set("value", "0")
node.append(roomid_node)
# TODO
peddensity_node = Element("PedPensity")
peddensity_node.set("value", "0")
node.append(peddensity_node)
# TODO
unkflags_node = Element("UnkFlags")
unkflags_node.set("value", "0")
node.append(unkflags_node)
# TODO
polyflags_node = Element("PolyFlags")
polyflags_node.set("value", "0")
node.append(polyflags_node)
# TODO
unktype_node = Element("UnkType")
unktype_node.set("value", "2")
node.append(unktype_node)
def write_bound_disc(obj):
node = Element("Item")
node.set("type", "Disc")
append_bbs(obj, node)
append_junk(obj, node)
length = obj.bounds_length
radius = obj.bounds_radius
margin_node = Element("Margin")
margin_node.set("value", str(length))
node.append(margin_node)
bsc_node = Element("SphereCenter")
bsc_node.set("x", str(0))
bsc_node.set("y", str(0))
bsc_node.set("z", str(0))
node.append(bsc_node)
bsr_node = Element("SphereRadius")
bsr_node.set("value", str(radius))
node.append(bsr_node)
return node
def write_bound_geometry_disc(obj, vertex_index):
return None, None, None
def write_bound_geometry_sphere(obj, vertex_index):
return None, None, None
def write_bound_geometry_cylinder(obj, vertex_index):
return None, None, None
def write_bound_geometry_box(obj, vertex_index):
return None, None, None
def write_bound_geometry_capsule(obj, vertex_index):
verts = []
polys = []
mats = []
length = obj.bounds_length
radius = obj.bounds_radius
loc = obj.matrix_world.to_translation()
verts.append(loc-Vector((0,0,length/2)))
verts.append(loc+Vector((0,0,length/2)))
capsule_node = Element("Capsule")
capsule_node.set("m", "0")
capsule_node.set("v1", str(vertex_index))
capsule_node.set("v2", str(vertex_index+1))
capsule_node.set("radius", str(radius))
polys.append(capsule_node)
return verts, polys, mats
def write_bound_geometry_geometry(obj, vertex_index):
verts = []
polys = []
mats = []
mesh = obj.data
mesh.calc_loop_triangles()
for tri in mesh.loop_triangles:
tri_vert1 = mesh.vertices[tri.vertices[0]].co
tri_vert2 = mesh.vertices[tri.vertices[1]].co
tri_vert3 = mesh.vertices[tri.vertices[2]].co
verts.append(tri_vert1)
verts.append(tri_vert2)
verts.append(tri_vert3)
vertex_index += 3
triangle_node = Element("Triangle")
triangle_node.set("m", "0")
triangle_node.set("v1", str(vertex_index-3))
triangle_node.set("v2", str(vertex_index-2))
triangle_node.set("v3", str(vertex_index-1))
triangle_node.set("f0", str(0))
triangle_node.set("f1", str(0))
triangle_node.set("f2", str(0))
polys.append(triangle_node)
return verts, polys, mats
def write_bound_geometry_item(obj, vertex_index):
if(obj.sollumtype == "Bound Disc"): return write_bound_geometry_disc(obj, vertex_index)
if(obj.sollumtype == "Bound Sphere"): return write_bound_geometry_sphere(obj, vertex_index)
if(obj.sollumtype == "Bound Cylinder"): return write_bound_geometry_cylinder(obj, vertex_index)
if(obj.sollumtype == "Bound Box"): return write_bound_geometry_box(obj, vertex_index)
if(obj.sollumtype == "Bound Capsule"): return write_bound_geometry_capsule(obj, vertex_index)
if(obj.sollumtype == "Bound Geometry"): return write_bound_geometry_geometry(obj, vertex_index)
return None
def write_bound_geometry(obj):
bvh = obj.bounds_bvh
node = Element("Item")
node.set("type", "GeometryBVH" if bvh else "Geometry")
append_bbs(obj, node)
append_junk(obj, node)
vertices_node = Element("Vertices")
node.append(vertices_node)
polygons_node = Element("Polygons")
node.append(polygons_node)
materials_node = Element("Materials")
node.append(materials_node)
vertices, polygons, materials = write_bound_geometry_geometry(obj, 0)
vertex_index = len(vertices)
children = get_obj_children(obj)
for c in children:
verts, polys, mats = write_bound_geometry_item(c, vertex_index)
if verts is not None:
vertex_index += len(verts)
vertices += verts
polygons += polys
materials += mats
#children_node.append(child_node)
vertices_text = '\n'.join(map(lambda v: " {0}, {1}, {2}".format(v.x, v.y, v.z), vertices))
vertices_node.text = vertices_text
for p in polygons:
polygons_node.append(p)
#TODO: materials
return node
def write_bound_sphere(obj):
# TODO
return None
def write_bound_cylinder(obj):
# TODO
return None
def write_bound_box(obj):
# TODO
return None
def write_bound_capsule(obj):
# TODO
return None
def write_bound_item(obj):
if(obj.sollumtype == "Bound Disc"): return write_bound_disc(obj)
if(obj.sollumtype == "Bound Sphere"): return write_bound_sphere(obj)
if(obj.sollumtype == "Bound Cylinder"): return write_bound_cylinder(obj)
if(obj.sollumtype == "Bound Box"): return write_bound_box(obj)
if(obj.sollumtype == "Bound Capsule"): return write_bound_capsule(obj)
if(obj.sollumtype == "Bound Geometry"): return write_bound_geometry(obj)
return None
def write_bound_composite(obj):
children = get_obj_children(obj)
bbminmax = get_bbs(children)
bbsphere = get_sphere_bb(children, bbminmax)
bound_node = Element("Bounds")
bound_node.set("type", "Composite")
append_bbs(obj, bound_node)
append_junk(obj, bound_node)
bsc_node = Element("SphereCenter")
bsc_node.set("x", str(bbsphere[0][0]))
bsc_node.set("y", str(bbsphere[0][1]))
bsc_node.set("z", str(bbsphere[0][2]))
bound_node.append(bsc_node)
bsr_node = Element("SphereRadius")
bsr_node.set("value", str(bbsphere[1]))
bound_node.append(bsr_node)
# TODO
margin_node = Element("Margin")
margin_node.set("value", "0")
bound_node.append(margin_node)
# TODO
volume_node = Element("Volume")
volume_node.set("value", "0")
bound_node.append(volume_node)
children_node = Element("Children")
bound_node.append(children_node)
for c in children:
child_node = write_bound_item(c)
if child_node is not None:
children_node.append(child_node)
return bound_node
def write_ybn_xml(context, filepath):
root = None
objects = bpy.context.scene.collection.objects
if(len(objects) == 0):
return "No objects in scene for Sollumz export"
root = Element("BoundsFile")
for obj in objects:
if(obj.sollumtype == "Bound Composite"):
xml_node = write_bound_composite(obj)
root.append(xml_node)
print("*** Complete ***")
xmlstr = prettify(root)
with open(filepath, "w") as f:
f.write(xmlstr)
return "Sollumz Drawable was succesfully exported to " + filepath
class ExportYBN(Operator, ExportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "exportxml.ybn" # important since its how bpy.ops.import_test.some_data is constructed
bl_label = "Export Ybn Xml (.ybn.xml)"
# ExportHelper mixin class uses this
filename_ext = ".ybn.xml"
def execute(self, context):
start = datetime.now ()
#try:
result = write_ybn_xml(context, self.filepath)
self.report({'INFO'}, result)
#except Exception:
# self.report({"ERROR"}, str(Exception) )
finished = datetime.now()
difference = (finished - start).total_seconds()
print("Exporting : " + self.filepath)
print("Export Time:")
print("start time: " + str(start))
print("end time: " + str(finished))
print("difference: " + str(difference) + " seconds")
return {'FINISHED'}
# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
self.layout.operator(ExportYBN.bl_idname, text="Ybn Xml Export (.ybn.xml)")
def register():
bpy.utils.register_class(ExportYBN)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(ExportYBN)
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)