summaryrefslogtreecommitdiff
path: root/io_scene_hl1map/__init__.py
blob: cfe9cd292e6fb81beb245e8f52adbef88bac3781 (plain)
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
# ##### 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()