-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_exporter.py
608 lines (432 loc) · 19 KB
/
xml_exporter.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
#!BPY
# Copyright (C) 2015 Coos Baakman
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
"""
# Name: 'xml mesh type (.xml)...'
# Blender: 249
# Group: 'Export'
# Tooltip: 'Export to xml file format.'
"""
__author__ = "Coos Baakman"
__version__ = "2.0"
__bpydoc__ = """\
This script exports a Blender mesh to an xml formatted file
format.
"""
import Blender
from Blender import Types, Object, Material,Armature,Mesh,Modifier
from Blender.Mathutils import *
from Blender import Draw, BGL
from Blender.BGL import *
from Blender.IpoCurve import ExtendTypes, InterpTypes
import math
import xml.etree.ElementTree as ET
EXTENSION='.xml'
def callback_sel (filename):
# A file path has been selected to export to
if not filename.endswith (EXTENSION):
filename += EXTENSION
xexport = Exporter()
xexport.exportSelected (filename)
def draw ():
# Make the menu in Blender: a single button to press.
glClearColor (0.55,0.6,0.6,1)
glClear (GL_COLOR_BUFFER_BIT)
glRasterPos2d (20,75)
Draw.Button ("Export Selected",1,20,155,100,30,"export the selected object")
def event (evt, val):
if evt == Draw.ESCKEY:
Draw.Exit()
def button_event (evt):
if evt == 1:
fname = Blender.sys.makename (ext = EXTENSION)
Blender.Window.FileSelector (callback_sel, "Export xml mesh", fname)
Draw.Exit ()
# Regeister the exported menu:
Draw.Register (draw, event, button_event)
# Makes a path a basename without extension.
def stripFilename (filename):
return os.path.basename (os.path.splitext (filename)[0])
# Removes whitespaces.
def makeAllowedNameString (name):
return ' %s ' % name.replace (' ', '_')
# Find an armature for a given mesh object:
def findMeshArmature (mesh_obj):
for mod in mesh_obj.modifiers:
if mod.type == Modifier.Types.ARMATURE:
# Return as an object:
return mod [Modifier.Settings.OBJECT]
return None
# Find a mesh, to which the armature belongs
def findArmatureMesh (arm_obj):
arm_name = arm_obj.getData(True, False)
for obj in Object.Get():
# Skip objects that are not meshes
if type (obj.getData(False, True)) != Types.MeshType:
continue
for mod in obj.modifiers:
# Skip modifiers that are not armatures
if mod.type != Modifier.Types.ARMATURE:
continue
objmodobj_name = mod [Modifier.Settings.OBJECT].getData(True, False)
if objmodobj_name == arm_name:
# This is my armature !
return obj
return None
# Tells if we need to flip face direction when applying this matrix.
def getFaceFactor (matrix):
f = 1.0
for i in range (3):
for j in range (3):
f *= matrix[i][j]
if f < 0:
return -1
else:
return 1
# Makes a string representation of a blender interpolation type
def interptypes2tring (i):
if i == InterpTypes.CONST:
return 'const' # curve remains constant from current BezTriple knot
elif i == InterpTypes.LINEAR:
return 'linear' # curve is linearly interpolated between adjacent knots
elif i == InterpTypes.BEZIER:
return 'bezier' # curve is interpolated by a Bezier curve between adjacent knots
else:
raise Exception ("not an interp type: %i" % i)
# Makes a string representation of a blender extension type
def extend2string (e):
if e == ExtendTypes.CONST:
return 'const' # curve is constant beyond first and last knots
elif e == ExtendTypes.EXTRAP:
return 'extrap' # curve maintains same slope beyond first and last knots
elif e == ExtendTypes.CYCLIC:
return 'cyclic' # curve values repeat beyond first and last knots
elif e == ExtendTypes.CYCLIC_EXTRAP:
return 'cyclic_extrap' # curve values repeat beyond first and last knots, but while retaining continuity
else:
raise Exception ("not an extend type: %i" % e)
def rotate (quat, vec):
# Copy quat first, before calling inverse.
# It changes the object itself!
inv = Quaternion (quat.w, quat.x, quat.y, quat.z)
inv = inv.inverse ()
return (quat * vec) * inv
def flips_chirality (matrix):
f = 0.5 * math.sqrt (2) # 45 degrees
# Perform 3 tests:
for point, axis in [(Vector (1.0, 0, 0), Vector (0, 1.0, 0)),
(Vector (0, 1.0, 0), Vector (0, 0, 1.0)),
(Vector (0, 0, 1.0), Vector (1.0, 0, 0))]:
rot = Quaternion (f, f * axis.x, f * axis.y, f * axis.z)
tpoint = matrix * point
trot = matrix * Vector (rot.x, rot.y, rot.z, rot.w)
trot = Quaternion (trot.w, trot.x, trot.y, trot.z)
res1 = rotate (rot, tpoint)
res2 = rotate (trot, point)
if res1 != res2:
return True
return False
class Exporter (object):
# Code used in this exporter is based on the following documentation:
# http://www.blender.org/api/248PythonDoc/
def __init__(self):
# Switch y and z:
self.transformVertex = Matrix ([1,0,0,0],
[0,0,1,0],
[0,1,0,0],
[0,0,0,1])
# Used on bone translocations:
self.transformLoc = Matrix(self.transformVertex)
for i in range(3):
self.transformLoc [3][i] = 0
self.transformLoc [i][3] = 0
self.transformLoc [3][3] = 1
# Used on normals and bone rotations:
self.transformRot = Matrix(self.transformLoc)
self.transformRot.invert()
self.transformRot.transpose()
self.color_channels = ['r', 'g', 'b', 'a']
def exportSelected(self, filename):
# Find the selected mesh (and maybe its armature)
mesh_obj = None
arm_obj = None
# The selected object might actually be an armature, with a mesh attached to it.
selected = Object.GetSelected()
if len(selected) == 0:
raise Exception ("No selection")
sel_data = selected[0].getData(False,True)
if type(sel_data) == Types.MeshType:
mesh_obj = selected [0]
arm_obj = findMeshArmature (mesh_obj)
elif type(sel_data) == Types.ArmatureType:
arm_obj = selected [0]
mesh_obj = findArmatureMesh (arm_obj)
if not mesh_obj:
raise Exception ("No mesh attached to selected armature")
else:
raise Exception ("The selected object %s is not a mesh or armature." % str(type(selected [0])))
root = self.toXML (mesh_obj, arm_obj)
f = open(filename, "w")
f.write(ET.tostring(root))
f.close()
def toXML (self, mesh_obj, arm_obj=None):
mesh = self.meshToXML (mesh_obj)
if arm_obj:
tag_armature = self.armatureToXML(arm_obj, mesh_obj)
mesh.append (tag_armature)
return mesh
def meshToXML (self, mesh_obj):
face_factor = -getFaceFactor (self.transformRot)
mesh = mesh_obj.getData(False, True)
root = ET.Element ('mesh')
# Convert the mesh's vertices to xml tags:
tag_vertices = ET.Element ('vertices')
root.append (tag_vertices)
for vertex in mesh.verts:
tag_vertex = self.vertexToXML (vertex)
tag_vertices.append(tag_vertex)
# Convert the mesh's faces to xml tags:
tag_faces = ET.Element ('faces')
root.append (tag_faces)
# Divide the mesh in subsets, based on their materials.
# This allows us to render different parts of the mesh with different OpenGL settings,
# like material-specific ones.
subset_faces = {}
for face in mesh.faces:
tag_face = self.faceToXML (face, face_factor)
tag_faces.append (tag_face)
if face.mat not in subset_faces:
subset_faces [face.mat] = []
subset_faces [face.mat].append (face)
# Did we even find materials?
if len (subset_faces) > 0:
tag_subsets = ET.Element ('subsets')
root.append (tag_subsets)
for material_i in subset_faces:
if material_i < len (mesh.materials):
material = mesh.materials [material_i]
else:
# material index is not found in mesh
material = None
tag_subset = self.subsetToXML (material, subset_faces [material_i])
tag_subset.attrib ['id'] = str (material_i)
tag_subsets.append (tag_subset)
return root
def subsetToXML (self, material, faces):
# Take the material color settings.
# We might need to add more in the future..
if material:
name = material.name
diffuse = material.rgbCol + [1.0]
specular = material.specCol + [1.0]
emission = [material.emit * diffuse [i] for i in range (4)]
else:
# No material associated with subset, use default values:
name = None
diffuse = [1.0, 1.0, 1.0, 1.0]
specular = [0.0, 0.0, 0.0, 0.0]
emission = [0.0, 0.0, 0.0, 0.0]
tag_subset = ET.Element ('subset')
# Add name to tag, makes it easier to find back in the file.
if name:
tag_subset.attrib ['name'] = str(name)
# Convert material colors to xml attributes
c = self.color_channels # strings, r,g,b,a
tag_diffuse = ET.Element('diffuse')
tag_subset.append(tag_diffuse)
tag_specular = ET.Element('specular')
tag_subset.append(tag_specular)
tag_emission = ET.Element('emission')
tag_subset.append(tag_emission)
for i in range (4):
tag_diffuse.attrib[c[i]] = '%.4f' % diffuse [i]
tag_specular.attrib[c[i]] = '%.4f' % specular [i]
tag_emission.attrib[c[i]] = '%.4f' % emission [i]
# Register all the subset's faces in xml tags:
tag_faces = ET.Element('faces')
tag_subset.append(tag_faces)
for face in faces:
if len (face.verts) == 4:
tag_face = ET.Element('quad')
elif len (face.verts) == 3:
tag_face = ET.Element('triangle')
tag_faces.append (tag_face)
tag_face.attrib ['id'] = str(face.index)
return tag_subset
def faceToXML (self, face, face_factor):
# Only faces with 3 or 4 faces are supported.
if len(face.verts) == 3:
tag_face = ET.Element('triangle')
elif len(face.verts) == 4:
tag_face = ET.Element('quad')
else:
raise Exception ("encountered a face with %i vertices" % len(face.v))
tag_face.attrib['id'] = str(face.index)
# Order determines which side is front.
order = range(len(face.verts))
if face_factor < 0:
order.reverse()
tag_face.attrib ['smooth'] = str (face.smooth)
for i in order:
tag_corner = ET.Element('corner')
tag_face.append (tag_corner)
# Reference to vertex:
tag_corner.attrib['vertex_id'] = str(face.verts[i].index)
# Texture coordinates:
try:
tag_corner.attrib['tex_u'] = '%.4f' % face.uv[i][0]
tag_corner.attrib['tex_v'] = '%.4f' % face.uv[i][1]
except:
tag_corner.attrib['tex_u'] = '0.0'
tag_corner.attrib['tex_v'] = '0.0'
return tag_face
def vertexToXML (self, vertex):
# Register vertex properties as attributes.
# The entire vertex will be one xml tag.
tag_vertex = ET.Element ('vertex')
tag_vertex.attrib ['id'] = str (vertex.index)
co = self.transformLoc * vertex.co
no = self.transformRot * vertex.no
# Vertex position in space:
tag_pos = ET.Element ('pos')
tag_pos.attrib['x'] = '%.4f' % co [0]
tag_pos.attrib['y'] = '%.4f' % co [1]
tag_pos.attrib['z'] = '%.4f' % co [2]
tag_vertex.append (tag_pos)
# Vertex normal, used for lighting:
tag_pos = ET.Element ('norm')
tag_pos.attrib['x'] = '%.4f' % no [0]
tag_pos.attrib['y'] = '%.4f' % no [1]
tag_pos.attrib['z'] = '%.4f' % no [2]
tag_vertex.append (tag_pos)
return tag_vertex
def armatureToXML (self, arm_obj, mesh_obj):
armature = arm_obj.getData ()
mesh = mesh_obj.getData (False, True)
vertex_groups = mesh.getVertGroupNames ()
arm_matrix = arm_obj.getMatrix()
# Any normal mesh transformation is expected to be invertible:
try:
mesh_inv_matrix = mesh_obj.getMatrix().invert()
except:
raise Exception ("mesh matrix isn't invertible")
arm2mesh = mesh_inv_matrix * arm_matrix
root = ET.Element('armature')
bones_tag = ET.Element('bones')
root.append (bones_tag)
# Iterate over the armature's bones:
bone_list = []
for bone_name in armature.bones.keys():
bone_tag = ET.Element('bone')
bones_tag.append (bone_tag)
# A tag for every bone:
bone = armature.bones [bone_name]
bone_tag.attrib['id'] = bone_name
bone_list.append (bone)
# Export position of bone head and tail in mesh space:
headpos = self.transformVertex * arm2mesh * (bone.head['ARMATURESPACE'])
tailpos = self.transformVertex * arm2mesh * (bone.tail['ARMATURESPACE'])
bone_tag.attrib['x'] = '%.4f' % headpos[0]
bone_tag.attrib['y'] = '%.4f' % headpos[1]
bone_tag.attrib['z'] = '%.4f' % headpos[2]
bone_tag.attrib['tail_x'] = '%.4f' % tailpos[0]
bone_tag.attrib['tail_y'] = '%.4f' % tailpos[1]
bone_tag.attrib['tail_z'] = '%.4f' % tailpos[2]
bone_tag.attrib['weight'] = '%.4f' % bone.weight
# Reference to parent bone:
if bone.hasParent():
bone_tag.attrib['parent_id'] = bone.parent.name
if bone_name in mesh.getVertGroupNames():
# Register references to the verices that this bone pulls at:
vertices_tag = ET.Element('vertices')
bone_tag.append(vertices_tag)
for vert_i in mesh.getVertsFromGroup(bone_name, False):
vertex_tag = ET.Element('vertex')
vertices_tag.append (vertex_tag)
vertex_tag.attrib['id'] = str(vert_i)
# The armature might have animations associated with it.
animations_tag = self.armatureAnimationsToXML (arm_obj, arm2mesh, bone_list)
root.append (animations_tag)
return root
def armatureAnimationsToXML (self, arm_obj, arm2mesh, bone_list):
root = ET.Element ('animations')
rotm = self.transformRot * arm2mesh.rotationPart().resize4x4()
locm = self.transformLoc * arm2mesh
# Check for chirality flip in the rotation transformation:
chirality_flip = flips_chirality (rotm)
# Check every action to see if it's an animation:
for action in Armature.NLA.GetActions ().values ():
animation_tag = ET.Element ('animation')
nLayers = 0
# Check every known bone:
for bone in bone_list:
action.setActive (arm_obj)
if bone.name not in action.getChannelNames ():
continue
ipo = action.getChannelIpo (bone.name)
if len (ipo.curves) <= 0:
# Bone doesn't move in this action
continue
parent_ipo = None
if bone.hasParent():
parent_ipo = action.getChannelIpo (bone.parent.name)
# A layer for every moving bone:
layer_tag = ET.Element('layer')
layer_tag.attrib ['bone_id'] = bone.name
animation_tag.append (layer_tag)
nLayers += 1
# Find all key frames for this bone:
frame_numbers = []
for curve in ipo.curves:
for bez_triple in curve.bezierPoints:
frame_num = int (bez_triple.pt [0])
if frame_num not in frame_numbers:
frame_numbers.append (frame_num)
# Iterate over key frames
for frame_num in frame_numbers:
# Get pose at specified frame number:
Blender.Set ("curframe", frame_num)
pose_bone = arm_obj.getPose().bones [bone.name]
m = pose_bone.localMatrix
if pose_bone.parent:
# We want the bone rotation relative to its parent.
# This was the only known way to do it:
n = pose_bone.parent.localMatrix.rotationPart().resize4x4()
n.invert()
m = m * n
# Register frame number, position and rotation of the bone:
q = m.toQuat()
rot = rotm * Vector (q.x, q.y, q.z, q.w)
rot = Quaternion (rot.w, rot.x, rot.y, rot.z)
# If flipped, compensate:
if chirality_flip:
rot = rot.inverse ()
loc = locm * pose_bone.loc
key_tag = ET.Element ('key')
layer_tag.append (key_tag)
key_tag.attrib ['frame'] = str (frame_num)
key_tag.attrib ['x'] = '%.4f' % loc.x
key_tag.attrib ['y'] = '%.4f' % loc.y
key_tag.attrib ['z'] = '%.4f' % loc.z
key_tag.attrib ['rot_x'] = '%.4f' % rot.x
key_tag.attrib ['rot_y'] = '%.4f' % rot.y
key_tag.attrib ['rot_z'] = '%.4f' % rot.z
key_tag.attrib ['rot_w'] = '%.4f' % rot.w
if nLayers > 0: # means the action applies to this armature
animation_tag.attrib ['name'] = action.name
length = max (action.getFrameNumbers())
animation_tag.attrib ['length'] = str (length)
root.append (animation_tag)
return root