-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblender-vmf-export.py
203 lines (159 loc) · 7.93 KB
/
blender-vmf-export.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
bl_info = {
"name": "VMF Exporter",
"blender": (3, 6, 0),
"category": "Export",
}
import bpy
import math
import mathutils
def apply_modifiers_to_obj(obj):
for modifier in obj.modifiers:
bpy.context.view_layer.objects.active = obj
bpy.ops.object.modifier_apply(modifier=modifier.name)
def separate_loose_parts(obj):
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.separate(type='LOOSE')
bpy.ops.object.mode_set(mode='OBJECT')
def calculate_uv_scale(obj, mesh, face):
default_uv_scale = 0.25
if obj.get("useMeshUV") == 1:
if not mesh.uv_layers:
return default_uv_scale
uv_layer = mesh.uv_layers.active.data
uvs = [uv_layer[loop_index].uv for loop_index in face.loop_indices]
uv_width = max(uv.x for uv in uvs) - min(uv.x for uv in uvs)
uv_height = max(uv.y for uv in uvs) - min(uv.y for uv in uvs)
uv_scale = (uv_width + uv_height) / 2 if (uv_width + uv_height) > 0 else default_uv_scale
return uv_scale
return default_uv_scale
def rename_objects_in_collection(collection, start_id):
id = start_id
for obj in collection.objects:
if obj.type == 'MESH':
obj.name = f"brush_{id}"
id += 1
class ExportVMFOperator(bpy.types.Operator):
bl_idname = "export.vmf"
bl_label = "Export VMF"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
settings = context.scene.vmf_export_settings
write_vmf(settings.filepath, rename_objects=settings.rename_objects)
return {'FINISHED'}
def write_vmf(filepath, rename_objects):
scene = bpy.context.scene
collection_name = "brushes"
collection = bpy.data.collections.get(collection_name)
if not collection:
print(f"Collection '{collection_name}' not found!")
return
if rename_objects:
rename_objects_in_collection(collection, start_id=1)
with open(filepath, 'w', encoding='utf-8') as f:
id = 1
f.write('world\n')
f.write('{\n')
f.write('\t"id" "1"\n')
f.write('\t"skyname" "sky_day01_01"\n')
f.write('\t"maxpropscreenwidth" "-1"\n')
f.write('\t"detailvbsp" "detail.vbsp"\n')
f.write('\t"detailmaterial" "detail/detailsprites"\n')
f.write('\t"mapversion" "68"\n')
f.write('\t"classname" "worldspawn"\n')
for obj in bpy.context.scene.objects:
if obj.type == 'EMPTY':
id += 1
f.write('\tentity\n\t{\n')
f.write('\t\t"id" "{}"\n'.format(id))
f.write('\t\t"origin" "{} {} {}"\n'.format(*obj.location))
f.write('\t\t"angles" "{} {} {}"\n'.format(*obj.rotation_euler))
if obj.name == "info_player_start":
f.write('\t\t"classname" "info_player_start"\n')
f.write('\t}\n')
if collection.name in [collection.name for collection in obj.users_collection]:
if obj.type == 'MESH':
id += 1
apply_modifiers_to_obj(obj)
bpy.ops.object.select_all(action='DESELECT')
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
separate_loose_parts(obj)
f.write('\tsolid\n\t{\n')
f.write('\t\t"id" "{}"\n'.format(id))
mesh = obj.data
for face in mesh.polygons:
f.write('\t\tside\n\t\t{\n')
id += 1
f.write('\t\t\t"id" "{}"\n'.format(id))
v1_world = obj.matrix_world @ mesh.vertices[face.vertices[0]].co
v2_world = obj.matrix_world @ mesh.vertices[face.vertices[1]].co
v3_world = obj.matrix_world @ mesh.vertices[face.vertices[2]].co
coords1 = (v1_world.x, -v1_world.y, v1_world.z)
coords2 = (v2_world.x, -v2_world.y, v2_world.z)
coords3 = (v3_world.x, -v3_world.y, v3_world.z)
f.write('\t\t\t"plane" "({:.6f} {:.6f} {:.6f}) ({:.6f} {:.6f} {:.6f}) ({:.6f} {:.6f} {:.6f})"\n'.format(*coords1, *coords2, *coords3))
f.write('\t\t\tvertices_plus\n\t\t\t{\n')
f.write('\t\t\t\t"v" "({:.6f} {:.6f} {:.6f})"\n'.format(*coords1))
f.write('\t\t\t\t"v" "({:.6f} {:.6f} {:.6f})"\n'.format(*coords2))
f.write('\t\t\t\t"v" "({:.6f} {:.6f} {:.6f})"\n'.format(*coords3))
f.write('\t\t\t}\n')
material_name = "dev/dev_blendmeasure"
if obj.material_slots and face.material_index < len(obj.material_slots):
material = obj.material_slots[face.material_index].material
if material:
material_name = material.name.upper()
f.write('\t\t\t"material" "{}"\n'.format(material_name))
uv_scale = calculate_uv_scale(obj, mesh, face)
normal = face.normal
if abs(normal.z) < 0.1:
f.write('\t\t\t"uaxis" "[0 1 0 0] {:.6f}"\n'.format(uv_scale))
f.write('\t\t\t"vaxis" "[0 0 -1 0] {:.6f}"\n'.format(uv_scale))
else:
f.write('\t\t\t"uaxis" "[1 0 0 0] {:.6f}"\n'.format(uv_scale))
f.write('\t\t\t"vaxis" "[0 -1 0 0] {:.6f}"\n'.format(uv_scale))
if abs(normal.y) > 0.9:
f.write('\t\t\t"uaxis" "[1 0 0 0] {:.6f}"\n'.format(uv_scale))
f.write('\t\t\t"vaxis" "[0 0 -1 0] {:.6f}"\n'.format(uv_scale))
f.write('\t\t\t"rotation" "0"\n')
f.write('\t\t\t"lightmapscale" "16"\n')
f.write('\t\t\t"smoothing_groups" "0"\n')
f.write('\t\t}\n')
f.write('\t}\n')
f.write('}\n')
class VMFExportSettings(bpy.types.PropertyGroup):
filepath: bpy.props.StringProperty(
name="File Path",
description="Path to save the VMF file",
default="//",
subtype='FILE_PATH'
)
rename_objects: bpy.props.BoolProperty(
name="Rename Objects to ID",
description="Rename objects in the collection 'brushes' to their ID",
default=True
)
class VMFExportPanel(bpy.types.Panel):
bl_label = "Export Scene to VMF"
bl_idname = "PT_VMF_Export"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Level Editor'
def draw(self, context):
layout = self.layout
settings = context.scene.vmf_export_settings
layout.prop(settings, "filepath")
layout.prop(settings, "rename_objects")
layout.operator("export.vmf", text="Export VMF")
def register():
bpy.utils.register_class(ExportVMFOperator)
bpy.utils.register_class(VMFExportSettings)
bpy.utils.register_class(VMFExportPanel)
bpy.types.Scene.vmf_export_settings = bpy.props.PointerProperty(type=VMFExportSettings)
def unregister():
bpy.utils.unregister_class(ExportVMFOperator)
bpy.utils.unregister_class(VMFExportSettings)
bpy.utils.unregister_class(VMFExportPanel)
del bpy.types.Scene.vmf_export_settings
if __name__ == "__main__":
register()