1
# ##### BEGIN GPL LICENSE BLOCK #####
3
# This program is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU General Public License
5
# as published by the Free Software Foundation; either version 2
6
# of the License, or (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software Foundation,
15
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
# ##### END GPL LICENSE BLOCK #####
26
PREF_GRID_SNAP = False
28
# PREF_DEF_TEX_OPTS = Draw.Create(' 0 0 0 1 1\n') # not user settable yet
30
PREF_DEF_TEX_OPTS = ' 0 0 0 1 1 0 0 0\n' # not user settable yet
32
PREF_NULL_TEX = 'NULL' # not user settable yet
33
PREF_INVIS_TEX = 'common/caulk'
36
def face_uv_get(face):
37
""" Workaround 2.5x change.
40
uv_faces = me.uv_textures.active
42
return uv_faces.data[face.index]
47
def write_cube2brush(file, faces):
49
Takes 6 faces and writes a brush,
50
these faces can be from 1 mesh, 1 cube within a mesh of larger cubes
51
Faces could even come from different meshes or be contrived.
55
# file.write('// brush "%s", "%s"\n' % (ob.name, ob.data.name))
56
file.write('// brush from cube\n{\n')
59
format_vec = '( %d %d %d ) '
61
format_vec = '( %.8f %.8f %.8f ) '
64
# from 4 verts this gets them in reversed order and only 3 of them
67
for v in f.vertices[:][2::-1]:
68
file.write(format_vec % me.vertices[v].co[:])
73
file.write(PREF_INVIS_TEX)
75
image = uf.image if uf else None
78
file.write(os.path.splitext(os.path.basename(image.filename))[0])
80
file.write(PREF_NULL_TEX)
82
# Texture stuff ignored for now
83
file.write(PREF_DEF_TEX_OPTS)
94
def write_face2brush(file, face):
96
takes a face and writes it as a brush
97
each face is a cube/brush
101
format_vec = '( %d %d %d ) '
103
format_vec = '( %.8f %.8f %.8f ) '
105
image_text = PREF_NULL_TEX
107
uf = face_uv_get(face)
110
image_text = PREF_INVIS_TEX
112
image = uf.image if uf else None
115
image_text = os.path.splitext(os.path.basename(image.filename))[0]
117
# reuse face vertices
118
_v = face.id_data.vertices #XXX25
119
f_vertices = [_v[vi] for vi in face.vertices]
122
# original verts as tuples for writing
123
orig_vco = [v.co[:] for v in f_vertices]
125
# new verts that give the face a thickness
126
dist = PREF_SCALE * PREF_FACE_THICK
127
new_vco = [round_vec(v.co - (v.normal * dist)) for v in f_vertices]
128
#new_vco = [round_vec(v.co - (face.no * dist)) for v in face]
130
file.write('// brush from face\n{\n')
132
for co in orig_vco[2::-1]:
133
file.write(format_vec % co)
134
file.write(image_text)
135
# Texture stuff ignored for now
136
file.write(PREF_DEF_TEX_OPTS)
138
for co in new_vco[:3]:
139
file.write(format_vec % co)
140
if uf and uf.use_twoside:
141
file.write(image_text)
143
file.write(PREF_INVIS_TEX)
145
# Texture stuff ignored for now
146
file.write(PREF_DEF_TEX_OPTS)
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)
158
file.write(PREF_DEF_TEX_OPTS)
163
def is_cube_facegroup(faces):
165
Returens a bool, true if the faces make up a cube
167
# cube must have 6 faces
172
# Check for quads and that there are 6 unique verts
185
# Now check that each vert has 3 face users
191
for v in verts.values():
192
if v != 3: # vert has 3 users?
195
# Could we check for 12 unique edges??, probably not needed.
199
def is_tricyl_facegroup(faces):
201
is the face group a tri cylinder
202
Returens a bool, true if the faces make an extruded tri solid
205
# cube must have 5 faces
210
# Check for quads and that there are 6 unique verts
220
if len(verts) != 6 or tottri != 2:
223
# Now check that each vert has 3 face users
228
for v in verts.values():
229
if v != 3: # vert has 3 users?
232
# Could we check for 12 unique edges??, probably not needed.
236
def write_node_map(file, ob):
238
Writes the properties of an object (empty in this case)
239
as a MAP node as long as it has the property name - classname
240
returns True/False based on weather a node was written
242
props = [(p.name, p.value) for p in ob.game.properties]
245
for name, value in props:
246
if name == "classname":
255
for name_value in props:
256
file.write('"%s" "%s"\n' % name_value)
258
file.write('"origin" "%d %d %d"\n' %
259
tuple([round(axis * PREF_SCALE)
260
for axis in ob.matrix_world.to_translation()]))
262
file.write('"origin" "%.6f %.6f %.6f"\n' %
263
tuple([axis * PREF_SCALE
264
for axis in ob.matrix_world.to_translation()]))
270
def export_map(context, filepath):
273
('Scale:', PREF_SCALE, 1, 1000, 'Scale the blender scene by this value.'),\
274
('Face Width:', PREF_FACE_THICK, 0.01, 10, 'Thickness of faces exported as brushes.'),\
275
('Grid Snap', PREF_GRID_SNAP, 'snaps floating point values to whole numbers.'),\
277
('', PREF_NULL_TEX, 1, 128, 'Export textureless faces with this texture'),\
279
('', PREF_INVIS_TEX, 1, 128, 'Export invisible faces with this texture'),\
282
if not Draw.PupBlock('map export', pup_block):
286
from mathutils import Vector, Matrix
287
from bpy_extras import mesh_utils
290
print("Map Exporter 0.0")
291
file = open(filepath, 'w')
293
scene = context.scene
294
objects = context.selected_objects
302
SCALE_MAT[0][0] = SCALE_MAT[1][1] = SCALE_MAT[2][2] = PREF_SCALE
304
TOTBRUSH = TOTLAMP = TOTNODE = 0
310
elif type == 'SURFACE':
314
elif type == 'EMPTY':
317
if obs_mesh or obs_surf:
318
# brushes and surf's must be under worldspan
319
file.write('\n// entity 0\n')
321
file.write('"classname" "worldspawn"\n')
323
print("\twriting cubes from meshes")
325
dummy_mesh = ob.to_mesh(scene, True, 'PREVIEW')
327
#print len(mesh_split2connected(dummy_mesh))
329
# Is the object 1 cube? - object-is-a-brush
330
# 1 to tx the normals also
331
dummy_mesh.transform(ob.matrix_world * SCALE_MAT)
334
for v in dummy_mesh.verts:
335
v.co[:] = v.co.to_tuple(0)
337
# High quality normals
338
#XXX25: BPyMesh.meshCalcNormals(dummy_mesh)
340
# Split mesh into connected regions
341
for face_group in mesh_utils.mesh_linked_faces(dummy_mesh):
342
if is_cube_facegroup(face_group):
343
write_cube2brush(file, face_group)
345
elif is_tricyl_facegroup(face_group):
346
write_cube2brush(file, face_group)
350
write_face2brush(file, f)
353
#print 'warning, not exporting "%s" it is not a cube' % ob.name
354
bpy.data.meshes.remove(dummy_mesh)
356
valid_dims = 3, 5, 7, 9, 11, 13, 15
362
surf_name = data.name
363
mat = ob.matrix_world * SCALE_MAT
365
# This is what a valid patch looks like
375
( ( -64 -64 0 0 0 ) ( -64 0 0 0 -2 ) ( -64 64 0 0 -4 ) )
376
( ( 0 -64 0 2 0 ) ( 0 0 0 2 -2 ) ( 0 64 0 2 -4 ) )
377
( ( 64 -64 0 4 0 ) ( 64 0 0 4 -2 ) ( 80 88 0 4 -4 ) )
382
for i, nurb in enumerate(data.splines):
383
u = nurb.point_count_u
384
v = nurb.point_count_v
385
if u in valid_dims and v in valid_dims:
387
file.write('// brush %d surf_name\n' % i)
389
file.write('patchDef2\n')
392
file.write('( %d %d 0 0 0 )\n' % (u, v))
396
for p in nurb.points:
405
file.write(" ( %d %d %d 0 0 )" %
406
round_vec(p.co.xyz * mat))
408
file.write(' ( %.6f %.6f %.6f 0 0 )' %
420
# for p in nurb: print 'patch', p
423
print("Warning: not exporting patch",
424
surf_name, u, v, 'Unsupported')
426
if obs_mesh or obs_surf:
427
file.write('}\n') # end worldspan
429
print("\twriting lamps")
431
print("\t\t%s" % ob.name)
434
file.write('"classname" "light"\n')
435
file.write('"light" "%.6f"\n' % (lamp.distance * PREF_SCALE))
437
file.write('"origin" "%d %d %d"\n' %
438
tuple([round(axis * PREF_SCALE)
439
for axis in ob.matrix_world.to_translation()]))
441
file.write('"origin" "%.6f %.6f %.6f"\n' %
442
tuple([axis * PREF_SCALE
443
for axis in ob.matrix_world.to_translation()]))
445
file.write('"_color" "%.6f %.6f %.6f"\n' % tuple(lamp.color))
446
file.write('"style" "0"\n')
450
print("\twriting empty objects as nodes")
452
if write_node_map(file, ob):
453
print("\t\t%s" % ob.name)
456
print("\t\tignoring %s" % ob.name)
460
print("Exported Map in %.4fsec" % (time.time() - t))
461
print("Brushes: %d Nodes: %d Lamps %d\n" % (TOTBRUSH, TOTNODE, TOTLAMP))
469
export_map(context, filepath)