-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmf.py
275 lines (225 loc) · 6.78 KB
/
vmf.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import types
GLOBAL = {
"global_id": 0,
}
class VMF(object):
def __init__(self):
self.properties = {
"versioninfo": VersionInfo(),
"visgroups": VisGroups(),
"world": World(),
"cameras": Cameras(),
"cordon": Cordon(),
}
class VersionInfo(object):
def __init__(self):
self.properties = {
"editorversion": "400",
"editorbuild": "6412",
"mapversion": "0",
"formatversion": "100",
"prefab": "0",
}
class VisGroups(object):
def __init__(self):
self.properties = {}
class World(object):
def __init__(self):
self.properties = {
"id": get_unique_id(),
"mapversion": "1",
"classname": "worldspawn",
"skyname": "sky_wasteland02",
"solid": [],
}
def add_solid(self, pos, size, displacement=None):
new_solid = Solid()
far = (pos[0]+size[0], pos[1]+size[1], pos[2]+size[2])
uaxis = []
uaxis.append("[1 0 0 0] 0.25")
uaxis.append("[1 0 0 0] 0.25")
uaxis.append("[0 1 0 0] 0.25")
uaxis.append("[0 1 0 0] 0.25")
uaxis.append("[1 0 0 0] 0.25")
uaxis.append("[1 0 0 0] 0.25")
vaxis = []
vaxis.append("[0 -1 0 0] 0.25")
vaxis.append("[0 -1 0 0] 0.25")
vaxis.append("[0 0 -1 0] 0.25")
vaxis.append("[0 0 -1 0] 0.25")
vaxis.append("[0 0 -1 0] 0.25")
vaxis.append("[0 0 -1 0] 0.25")
planes = []
planes.append( [[0, 0, 1], [0, 1, 1], [1, 1, 1]] )
planes.append( [[0, 1, 0], [0, 0, 0], [1, 0, 0]] )
planes.append( [[0, 0, 0], [0, 1, 0], [0, 1, 1]] )
planes.append( [[1, 1, 0], [1, 0, 0], [1, 0, 1]] )
planes.append( [[0, 1, 0], [1, 1, 0], [1, 1, 1]] )
planes.append( [[1, 0, 0], [0, 0, 0], [0, 0, 1]] )
side_index = -1
for each_plane in planes:
side_index += 1
current_plane = ""
for each_point in each_plane:
current_plane += "("
number_index = -1
for each_number in each_point:
number_index += 1
if each_number == 0:
point_number = str(pos[number_index])
else:
point_number = str(far[number_index])
#omit space before parentheses
if number_index != 2:
current_plane += point_number + " "
else:
current_plane += point_number
current_plane += ") "
new_solid.add_side(current_plane, uaxis[side_index], vaxis[side_index])
if displacement != None:
new_solid.properties["side"][0].properties["dispinfo"].append(displacement)
self.properties["solid"].append(new_solid)
class Solid(object):
def __init__(self):
self.properties = {
"id": get_unique_id(),
"side": [],
"editor": Editor(),
}
def add_side(self, plane, uaxis, vaxis):
new_side = Side()
new_side.properties["plane"] = plane
new_side.properties["uaxis"] = uaxis
new_side.properties["vaxis"] = vaxis
self.properties["side"].append(new_side)
class Side(object):
def __init__(self):
self.properties = {
"id": get_unique_id(),
"plane": "(512 -512 -512) (-512 -512 -512) (-512 -512 512)",
"material": "BRICK/BRICKFLOOR001A",
"uaxis": "[1 0 0 0] 0.25",
"vaxis": "[0 0 -1 0] 0.25",
"rotation": "0",
"lightmapscale": "16",
"smoothing_groups": "0",
"dispinfo": [],
}
def add_displacement(self, power, pos):
new_displacement = DispInfo(power, pos)
self.properties["dispinfo"].append(new_displacement)
class DispInfo(object):
def __init__(self, power, pos):
self.properties = {
"power": str(power),
"startposition": "[" + str(pos[0]) + " " + str(pos[1]) + " " + str(pos[2]) + "]",
"elevation": "0",
"subdiv": "0",
"normals": Normals(power),
"distances": Distances(power),
"offsets": Offsets(power),
"offset_normals": OffsetNormals(power),
"alphas": Alphas(power),
"triangle_tags": TriangleTags(power),
"allowed_verts": AllowedVerts(power),
}
class Normals(object):
def __init__(self, power):
self.properties = {}
vertex_width = (2 ** power) + 1
print("Normals Created")
#go through each row
for each_row in range(vertex_width):
row_value = ""
#each number in the row is either 0 or 1, every 3rd entry is 1 for the z axis normal
for number_index in range(vertex_width*3):
if (number_index+1) % 3 == 0:
row_value += "1 "
else:
row_value += "0 "
self.properties["row" + str(each_row)] = row_value
class Distances(object):
def __init__(self, power):
self.properties = {}
vertex_width = (2 ** power) + 1
for each_row in range(vertex_width):
self.properties["row" + str(each_row)] = "0 " * vertex_width
class Offsets(object):
def __init__(self, power):
self.properties = {}
vertex_width = (2 ** power) + 1
for each_row in range(vertex_width):
self.properties["row" + str(each_row)] = "0 " * vertex_width*3
class OffsetNormals(object):
def __init__(self, power):
self.properties = {}
vertex_width = (2 ** power) + 1
for each_row in range(vertex_width):
self.properties["row" + str(each_row)] = "0 " * vertex_width*3
class Alphas(object):
def __init__(self, power):
self.properties = {}
vertex_width = (2 ** power) + 1
for each_row in range(vertex_width):
self.properties["row" + str(each_row)] = "0 " * vertex_width
class TriangleTags(object):
def __init__(self, power):
self.properties = {}
vertex_width = (2 ** power) + 1
for each_row in range(vertex_width-1):
self.properties["row" + str(each_row)] = "9 " * (vertex_width-1)*2
class AllowedVerts(object):
def __init__(self, power):
self.properties = {
"10": "-1 -1 -1 -1 -1 -1 -1 -1 -1 -1",
}
class Editor(object):
def __init__(self):
self.properties = {
"color": "0 200 0",
"visgroupshown": "1",
"visgroupautoshown": "1",
}
class Cameras(object):
def __init__(self):
self.properties = {
"activecamera": "-1",
}
class Cordon(object):
def __init__(self):
self.properties = {
"mins": "(-1024 -1024 -1024)",
"maxs": "(1024 1024 1024)",
"active": "0",
}
def get_unique_id():
GLOBAL["global_id"] += 1
return str(GLOBAL["global_id"])
def write_properties(file, property_object, indent=0):
#print(indent)
#print(str(property_object))
file.truncate()
prop_dict = property_object.properties
for each_key in prop_dict:
#print(type(prop_dict[each_key]))
#STRINGS
if type(prop_dict[each_key]) == types.StringType:
#print(prop_dict[each_key])
file.write('\t'*indent + '"' + each_key + '" "' + prop_dict[each_key] + '"\n')
#VARIABLE AMOUNT OF OBJECTS (LIST OF OBJECTS)
elif type(prop_dict[each_key]) == types.ListType:
for each_object in prop_dict[each_key]:
file.write('\t'*indent + each_key + '\n')
file.write('\t'*indent + "{\n")
indent += 1
write_properties(file, each_object, indent)
indent -= 1
file.write('\t'*indent + '}\n')
#OBJECTS
else:
file.write('\t'*indent + each_key + '\n')
file.write('\t'*indent + "{\n")
indent += 1
write_properties(file, prop_dict[each_key], indent)
indent -= 1
file.write('\t'*indent + '}\n')