summaryrefslogtreecommitdiff
path: root/io_scene_hl1map
diff options
context:
space:
mode:
Diffstat (limited to 'io_scene_hl1map')
-rwxr-xr-xio_scene_hl1map/__init__.py128
-rwxr-xr-xio_scene_hl1map/export_hl1map.py178
2 files changed, 306 insertions, 0 deletions
diff --git a/io_scene_hl1map/__init__.py b/io_scene_hl1map/__init__.py
new file mode 100755
index 0000000..cfe9cd2
--- /dev/null
+++ b/io_scene_hl1map/__init__.py
@@ -0,0 +1,128 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+bl_info = {
+ "name": "Export Half-Life 1 Tools Format (.map)",
+ "author": "Ludovic Pouzenc",
+ "version": (0, 2, 0),
+ "blender": (2, 79, 0),
+ "location": "File > Export > Half-Life 1 tools (.map)",
+ "description": "Export mesh to Half-Life 1 tools (.map)",
+ "warning": "",
+ #"wiki_url": "http://to.do",
+ "category": "Import-Export"}
+
+"""
+Related links:
+https://developer.valvesoftware.com/wiki/MAP_file_format
+
+Usage Notes:
+Map file could be opened in Valve Hammer 3.4 editor (prefered bit windows only)
+or passed to orignal's or ZLHT's compiler tools (use wine to run them on linux)
+
+MAP format defines solids (brushes) as a set of intersecting planes.
+Planes a defined by 3 points in space (could be different from face's vertices)
+
+This is (very) unusual. This cannot reprents things such as :
+* Concave meshes
+* Non-planar faces
+Existing tools that use this .map format probably don't cope with :
+* ngons with n > 32
+
+For now this script do :
+* For each mesh in current scene
+** Simplify it (remove doubles in vertices, fill holes, connect vertices to not have concave faces nor non planar faces)
+** Recalc face normals to have them pointing to this outside of the mesh
+* For each object in current scene that is type 'MESH'
+** Take the mesh and local to world matrix
+** Make a .map brush for each mesh's blender face (world coords, scaled by blender_to_map_scale_factor)
+** Put AAATRIGGER texture stupidly
+** Output it as part of worldspawn entity in .map
+
+Line termination aren't well specified, this plugin will use system default (CRLF on Win, LF on Lin...)
+"""
+
+if "bpy" in locals():
+ import importlib
+ if "export_hl1map" in locals():
+ importlib.reload(export_hl1map)
+
+import bpy
+from bpy.props import (
+ BoolProperty,
+ CollectionProperty,
+ FloatProperty,
+ StringProperty,
+ )
+from bpy_extras.io_utils import (
+ ExportHelper,
+ )
+
+class ExportHL1MAP(bpy.types.Operator, ExportHelper):
+ """Export Half-Life 1 Tools Format (.map)"""
+ bl_idname = 'export_scene.hl1_map'
+ bl_label = 'Export HL1 .map'
+
+ filename_ext = '.map'
+
+ filter_glob = StringProperty(default='*'+filename_ext, options={'HIDDEN'})
+
+ use_selection = BoolProperty(
+ name='Selection Only',
+ description='Export selected objects only',
+ default=False,
+ )
+
+ blender_to_map_scale_factor = FloatProperty(
+ name='Upscaling factor',
+ description='Scale from blender world coordinates to map brush coordinates',
+ default=100,
+ )
+
+ worldspawn_props = {
+ 'classname': 'worldspawn',
+ 'sounds': 1,
+ 'MaxRange': 4096,
+ 'mapversion': 220,
+ 'wad': '\\half-life\\valve\\xeno.wad;\\half-life\\valve\\decals.wad;\\half-life\\valve\\halflife.wad;\\half-life\\valve\\liquids.wad'
+ }
+
+ def execute(self, context):
+ from . import export_hl1map
+ keywords = self.as_keywords(ignore=('filter_glob','check_existing'))
+ keywords['worldspawn_props'] = self.worldspawn_props
+ print('export_hl1map.save(self, context, ', keywords, ')')
+ return export_hl1map.save(self, context, **keywords)
+
+
+
+# Add to a menu
+def menu_func_export(self, context):
+ self.layout.operator(ExportHL1MAP.bl_idname, text="Half-Life 1 tools (.map)")
+
+def register():
+ bpy.utils.register_module(__name__)
+ bpy.types.INFO_MT_file_export.append(menu_func_export)
+
+
+def unregister():
+ bpy.utils.unregister_module(__name__)
+ bpy.types.INFO_MT_file_export.remove(menu_func_export)
+
+if __name__ == "__main__":
+ register()
diff --git a/io_scene_hl1map/export_hl1map.py b/io_scene_hl1map/export_hl1map.py
new file mode 100755
index 0000000..45cc1b9
--- /dev/null
+++ b/io_scene_hl1map/export_hl1map.py
@@ -0,0 +1,178 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+import bpy
+import bmesh
+from mathutils import Vector
+from bpy_extras.io_utils import create_derived_objects, free_derived_objects
+
+# https://developer.valvesoftware.com/wiki/MAP_file_format
+
+def fmt3(vec3):
+ return '%f %f %f'%(vec3.x, vec3.y, vec3.z)
+ #return ' '.join(['%%.%df']*3)%tuple([precision]*3)%(vec3.x, vec3.y, vec3.z)
+def fmt_plane(plane3dots):
+ return '( %s ) ( %s ) ( %s )'%(fmt3(plane3dots[0]), fmt3(plane3dots[1]), fmt3(plane3dots[2]))
+def fmt_tex(tev, toff):
+ return '[ %s %.1f ]'%(fmt3(tev), toff)
+def fmt_face(plane3dots, tename, tev1, teoff1, tev2, teoff2, rot, scaleX, scaleY):
+ return '%s %s %s %s %s'%(
+ fmt_plane(plane3dots), tename,
+ fmt_tex(tev1, teoff1),
+ fmt_tex(tev2, teoff2),
+ fmt3(Vector([rot, scaleX, scaleY]))
+ )
+
+def output_entity_start(dict_props, fh):
+ fh.write('{\n')
+ for k,v in dict_props.items():
+ fh.write('\t%-16s "%s"\n'%('"'+k+'"',v))
+
+def output_brush_start(fh):
+ fh.write('\t{\n')
+
+def output_brush_face(plane3dots, tename, tev1, teoff1, tev2, teoff2, rot, scaleX, scaleY, fh):
+ fh.write('\t\t%s\n'%fmt_face(plane3dots, tename, tev1, teoff1, tev2, teoff2, rot, scaleX, scaleY))
+
+def output_brush_end(fh):
+ fh.write('\t}\n')
+
+def output_entity_end(fh):
+ fh.write('}\n')
+
+def normal(plane3dots):
+ v01 = plane3dots[1] - plane3dots[0]
+ v02 = plane3dots[2] - plane3dots[0]
+ n = v01.cross(v02)
+ n.normalize()
+ return n
+
+def flip(plane3dots):
+ tmp = plane3dots[2]
+ plane3dots[2] = plane3dots[1]
+ plane3dots[1] = tmp
+
+def debug1(o=''):
+ #print(o)
+ return None
+
+def debug2(fano,plane3dots):
+ fano.normalize()
+ debug1(fano)
+ v01 = plane3dots[1] - plane3dots[0]
+ v02 = plane3dots[2] - plane3dots[0]
+ n = v01.cross(v02)
+ n.normalize()
+ debug1(n)
+ debug1(fano-n)
+ debug1()
+
+
+def save(operator, context, filepath, worldspawn_props, blender_to_map_scale_factor, use_selection=True):
+ """Save the Blender scene to a map file."""
+
+ # Make sure that data we want to access is not out of sync because edit mode is in use
+ if bpy.ops.object.mode_set.poll():
+ bpy.ops.object.mode_set(mode='OBJECT')
+
+ sc = context.scene
+ if use_selection:
+ objects = list(ob for ob in sc.objects if ob.is_visible(sc) and ob.type == 'MESH' and ob.data and ob.select)
+ else:
+ objects = list(ob for ob in sc.objects if ob.is_visible(sc) and ob.type == 'MESH' and ob.data)
+
+ # Simplify each mesh once (a mesh could be used by multiple objects)
+ for mesh in set([ob.data for ob in objects]):
+ bm = bmesh.new()
+ bm.from_mesh(mesh)
+ bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=1/blender_to_map_scale_factor)
+ bmesh.ops.holes_fill(bm, edges=bm.edges) #, sides=0
+ bmesh.ops.connect_verts_concave(bm, faces=bm.faces)
+ bmesh.ops.connect_verts_nonplanar(bm, faces=bm.faces) #, angle_limit=0.0
+ bmesh.ops.planar_faces(bm, faces=bm.faces)
+ bmesh.ops.recalc_face_normals(bm, faces=bm.faces)
+ bm.to_mesh(mesh)
+ bm.free()
+
+ # Iterate over objects, make computations (set of cross planes) and print them as worldspawn entity brushes
+ with open(filepath, 'w') as fh:
+ output_entity_start(worldspawn_props, fh)
+ for ob in objects:
+ debug1(ob.name)
+ mat_to_map = ob.matrix_world * blender_to_map_scale_factor
+ mesh = ob.data
+ mesh.update(calc_tessface=True)
+ for fa in mesh.tessfaces:
+ output_brush_start(fh)
+ tename='AAATRIGGER'
+ tev1 = Vector([1,0,0])
+ teoff1 = 0
+ tev2 = Vector([0,-1,0])
+ teoff2 = 0
+ rot = 0
+ scaleX = 1
+ scaleY = 1
+ # Make a brush in .map for each face in blender
+ # Brushes are (strangely) defined as a set of intersecting planes in .map
+ # Planes are defined with 3 3D points belonging to it
+ # "They must be in a clockwise order when facing the outside of the plane
+ # that is, the side that points outwards from the brush"
+ brfront = [None,None,None]
+ brback = [None,None,None]
+ # For now this code take the 3 first vectices of the face
+ # TODO This can cause troubles if they are colinear or if they have narrow angle
+ for i in [0,1,2]:
+ vi = mesh.vertices[fa.vertices[i]].co
+ # front plane in brush will match face in blender (in global coords, with a scale factor)
+ brfront[i] = mat_to_map * vi
+ # back plane will be 1 (map) unit inside (normal facing outside, so substract it)
+ brback[i] = mat_to_map * ( vi - fa.normal / blender_to_map_scale_factor )
+ # Check if coords are in clockwise order, else flip them
+ fano = mat_to_map.to_3x3() * fa.normal
+ frno = normal(brfront)
+ epsilon = 0.1
+ if ( (fano - frno).length_squared > epsilon ):
+ flip(brfront)
+ debug1('Front Flipped')
+ else:
+ flip(brback)
+ debug1('Back Flipped')
+ debug2(mat_to_map.to_3x3() * fa.normal, brfront)
+ debug2(mat_to_map.to_3x3() *-fa.normal, brback)
+ output_brush_face(brfront, tename, tev1, teoff1, tev2, teoff2, rot, scaleX, scaleY, fh)
+ output_brush_face(brback, tename, tev1, teoff1, tev2, teoff2, rot, scaleX, scaleY, fh)
+ # make 1 side face in brush per blender edge
+ for i,j in fa.edge_keys:
+ brside = [None,None,None]
+ brside[0] = mat_to_map * ( mesh.vertices[i].co )
+ brside[1] = mat_to_map * ( mesh.vertices[j].co )
+ brside[2] = mat_to_map * ( mesh.vertices[j].co - fa.normal )
+ # Let have a plane define by a point A and a normal n
+ # Let M a point in space. M is on the plane if AM.n = 0
+ # Now we want to know if the "side" face normal is poiting outwards of the brush (<0)
+ # Take A = side[0], M = fa.center (that is inside the brush), n = normal(side)
+ if ( (mat_to_map * fa.center - brside[0]).dot(normal(brside)) < 0):
+ flip(brside)
+ debug1('Side Flipped')
+ debug1(normal(brside))
+ output_brush_face(brside, tename, tev1, teoff1, tev2, teoff2, rot, scaleX, scaleY, fh)
+ output_brush_end(fh)
+ # endfor fa in mesh.tessfaces:
+ output_entity_end(fh)
+ return {'FINISHED'}
+