7
Tooltip: 'Export to Quake map format'
10
__author__ = 'Campbell Barton'
12
__email__ = "cbarton@metavr.com"
14
This script Exports a Quake 3 map format.
16
Supports meshes, lights and nurbs patch surfaces
19
# ***** BEGIN GPL LICENSE BLOCK *****
21
# Script copyright (C): Campbell Barton
23
# This program is free software; you can redistribute it and/or
24
# modify it under the terms of the GNU General Public License
25
# as published by the Free Software Foundation; either version 2
26
# of the License, or (at your option) any later version.
28
# This program is distributed in the hope that it will be useful,
29
# but WITHOUT ANY WARRANTY; without even the implied warranty of
30
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
# GNU General Public License for more details.
33
# You should have received a copy of the GNU General Public License
34
# along with this program; if not, write to the Free Software Foundation,
35
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37
# ***** END GPL LICENCE BLOCK *****
38
# --------------------------------------------------------------------------
46
PREF_SCALE= Draw.Create(100)
47
PREF_FACE_THICK= Draw.Create(0.1)
48
PREF_GRID_SNAP= Draw.Create(0)
50
# PREF_DEF_TEX_OPTS= Draw.Create(' 0 0 0 1 1\n') # not user settable yet
52
PREF_DEF_TEX_OPTS= Draw.Create(' 0 0 0 1 1 0 0 0\n') # not user settable yet
54
PREF_NULL_TEX= Draw.Create('NULL') # not user settable yet
55
PREF_INVIS_TEX= Draw.Create('common/caulk')
57
def write_cube2brush(file, faces):
59
Takes 6 faces and writes a brush,
60
these faces can be from 1 mesh, 1 cube within a mesh of larger cubes
61
Faces could even come from different meshes or be contrived.
64
# file.write('// brush "%s", "%s"\n' % (ob.name, ob.getData(name_only=1)))
65
file.write('// brush from cube\n{\n')
67
if PREF_GRID_SNAP.val: format_vec= '( %d %d %d ) '
68
else: format_vec= '( %.8f %.8f %.8f ) '
71
# from 4 verts this gets them in reversed order and only 3 of them
74
file.write(format_vec % tuple(v.co) )
79
if mode & Mesh.FaceModes.INVISIBLE:
80
file.write(PREF_INVIS_TEX.val)
85
if image: file.write(sys.splitext(sys.basename(image.filename))[0])
86
else: file.write(PREF_NULL_TEX.val)
88
# Texture stuff ignored for now
89
file.write(PREF_DEF_TEX_OPTS.val)
94
if PREF_GRID_SNAP.val:
95
return round(v.x), round(v.y), round(v.z)
99
def write_face2brush(file, face):
101
takes a face and writes it as a brush
102
each face is a cube/brush
105
if PREF_GRID_SNAP.val: format_vec= '( %d %d %d ) '
106
else: format_vec= '( %.8f %.8f %.8f ) '
109
image_text= PREF_NULL_TEX.val
114
if mode & Mesh.FaceModes.INVISIBLE:
115
image_text= PREF_INVIS_TEX.val
117
try: image= face.image
119
if image: image_text = sys.splitext(sys.basename(image.filename))[0]
121
# original verts as tuples for writing
122
orig_vco= [tuple(v.co) for v in face]
124
# new verts that give the face a thickness
125
dist= PREF_SCALE.val * PREF_FACE_THICK.val
126
new_vco= [round_vec(v.co - (v.no * dist)) for v in face]
127
#new_vco= [round_vec(v.co - (face.no * dist)) for v in face]
129
file.write('// brush from face\n{\n')
131
for co in orig_vco[2::-1]:
132
file.write(format_vec % co )
133
file.write(image_text)
134
# Texture stuff ignored for now
135
file.write(PREF_DEF_TEX_OPTS.val)
138
for co in new_vco[:3]:
139
file.write(format_vec % co )
140
if mode & Mesh.FaceModes.TWOSIDE:
141
file.write(image_text)
143
file.write(PREF_INVIS_TEX.val)
145
# Texture stuff ignored for now
146
file.write(PREF_DEF_TEX_OPTS.val)
149
if len(orig_vco)==3: # Tri, it seemms tri brushes are supported.
150
index_pairs= ((0,1), (1,2), (2,0))
152
index_pairs= ((0,1), (1,2), (2,3), (3,0))
154
for i1, i2 in index_pairs:
155
for co in orig_vco[i1], orig_vco[i2], new_vco[i2]:
156
file.write( format_vec % co )
157
file.write(PREF_INVIS_TEX.val)
158
file.write(PREF_DEF_TEX_OPTS.val)
162
def is_cube_facegroup(faces):
164
Returens a bool, true if the faces make up a cube
166
# cube must have 6 faces
171
# Check for quads and that there are 6 unique verts
183
# Now check that each vert has 3 face users
188
for v in verts.itervalues():
189
if v != 3: # vert has 3 users?
192
# Could we check for 12 unique edges??, probably not needed.
195
def is_tricyl_facegroup(faces):
197
is the face group a tri cylinder
198
Returens a bool, true if the faces make an extruded tri solid
201
# cube must have 5 faces
206
# Check for quads and that there are 6 unique verts
216
if len(verts) != 6 or tottri != 2:
219
# Now check that each vert has 3 face users
224
for v in verts.itervalues():
225
if v != 3: # vert has 3 users?
228
# Could we check for 12 unique edges??, probably not needed.
231
def write_node_map(file, ob):
233
Writes the properties of an object (empty in this case)
234
as a MAP node as long as it has the property name - classname
235
returns True/False based on weather a node was written
237
props= [(p.name, p.data) for p in ob.properties]
240
for name, value in props:
241
if name=='classname':
250
for name_value in props:
251
file.write('"%s" "%s"\n' % name_value)
256
def export_map(filepath):
259
('Scale:', PREF_SCALE, 1, 1000, 'Scale the blender scene by this value.'),\
260
('Face Width:', PREF_FACE_THICK, 0.01, 10, 'Thickness of faces exported as brushes.'),\
261
('Grid Snap', PREF_GRID_SNAP, 'snaps floating point values to whole numbers.'),\
263
('', PREF_NULL_TEX, 1, 128, 'Export textureless faces with this texture'),\
265
('', PREF_INVIS_TEX, 1, 128, 'Export invisible faces with this texture'),\
268
if not Draw.PupBlock('map export', pup_block):
273
print 'Map Exporter 0.0'
274
file= open(filepath, 'w')
282
SCALE_MAT= Mathutils.Matrix()
283
SCALE_MAT[0][0]= SCALE_MAT[1][1]= SCALE_MAT[2][2]= PREF_SCALE.val
285
dummy_mesh= Mesh.New()
287
TOTBRUSH= TOTLAMP= TOTNODE= 0
289
for ob in Object.GetSelected():
291
if type == 'Mesh': obs_mesh.append(ob)
292
elif type == 'Surf': obs_surf.append(ob)
293
elif type == 'Lamp': obs_lamp.append(ob)
294
elif type == 'Empty': obs_empty.append(ob)
296
if obs_mesh or obs_surf:
297
# brushes and surf's must be under worldspan
298
file.write('\n// entity 0\n')
300
file.write('"classname" "worldspawn"\n')
303
print '\twriting cubes from meshes'
305
dummy_mesh.getFromObject(ob.name)
307
#print len(mesh_split2connected(dummy_mesh))
309
# Is the object 1 cube? - object-is-a-brush
310
dummy_mesh.transform(ob.matrixWorld*SCALE_MAT) # 1 to tx the normals also
312
if PREF_GRID_SNAP.val:
313
for v in dummy_mesh.verts:
319
# High quality normals
320
BPyMesh.meshCalcNormals(dummy_mesh)
322
# Split mesh into connected regions
323
for face_group in BPyMesh.mesh2linkedFaces(dummy_mesh):
324
if is_cube_facegroup(face_group):
325
write_cube2brush(file, face_group)
327
elif is_tricyl_facegroup(face_group):
328
write_cube2brush(file, face_group)
332
write_face2brush(file, f)
335
#print 'warning, not exporting "%s" it is not a cube' % ob.name
338
dummy_mesh.verts= None
341
valid_dims= 3,5,7,9,11,13,15
346
surf_name= ob.getData(name_only=1)
347
data= Curve.Get(surf_name)
348
mat = ob.matrixWorld*SCALE_MAT
350
# This is what a valid patch looks like
360
( ( -64 -64 0 0 0 ) ( -64 0 0 0 -2 ) ( -64 64 0 0 -4 ) )
361
( ( 0 -64 0 2 0 ) ( 0 0 0 2 -2 ) ( 0 64 0 2 -4 ) )
362
( ( 64 -64 0 4 0 ) ( 64 0 0 4 -2 ) ( 80 88 0 4 -4 ) )
367
for i, nurb in enumerate(data):
370
if u in valid_dims and v in valid_dims:
372
file.write('// brush %d surf_name\n' % i)
374
file.write('patchDef2\n')
377
file.write('( %d %d 0 0 0 )\n' % (u, v) )
389
if PREF_GRID_SNAP.val:
390
file.write(' ( %d %d %d 0 0 )' % round_vec(Mathutils.Vector(p[0:3]) * mat))
392
file.write(' ( %.6f %.6f %.6f 0 0 )' % tuple(Mathutils.Vector(p[0:3]) * mat))
405
# for p in nurb: print 'patch', p
408
print "NOT EXPORTING PATCH", surf_name, u,v, 'Unsupported'
411
file.write('}\n') # end worldspan
414
print '\twriting lamps'
416
print '\t\t%s' % ob.name
419
file.write('"classname" "light"\n')
420
file.write('"light" "%.6f"\n' % (lamp.dist* PREF_SCALE.val))
421
if PREF_GRID_SNAP.val:
422
file.write('"origin" "%d %d %d"\n' % tuple([round(axis*PREF_SCALE.val) for axis in ob.getLocation('worldspace')]) )
424
file.write('"origin" "%.6f %.6f %.6f"\n' % tuple([axis*PREF_SCALE.val for axis in ob.getLocation('worldspace')]) )
425
file.write('"_color" "%.6f %.6f %.6f"\n' % tuple(lamp.col))
426
file.write('"style" "0"\n')
431
print '\twriting empty objects as nodes'
433
if write_node_map(file, ob):
434
print '\t\t%s' % ob.name
437
print '\t\tignoring %s' % ob.name
441
print 'Exported Map in %.4fsec' % (sys.time()-time)
442
print 'Brushes: %d Nodes: %d Lamps %d\n' % (TOTBRUSH, TOTNODE, TOTLAMP)
446
Window.FileSelector(export_map, 'EXPORT MAP', '*.map')
448
if __name__ == '__main__': main()
449
# export_map('/foo.map')
b'\\ No newline at end of file'