-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
404 lines (332 loc) · 12.9 KB
/
__init__.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# -*- coding: utf8 -*-
# Blender WCP IFF mesh import/export script by Kevin Caccamo
# Copyright © 2013-2016 Kevin Caccamo
# E-mail: kevin@ciinet.org
#
# 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, see <http://www.gnu.org/licenses/>.
#
# <pep8-80 compliant>
import bpy
import time
import warnings
from . import import_iff
from . import export_iff
# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ImportHelper, ExportHelper, axis_conversion
from bpy.props import (StringProperty, IntProperty, BoolProperty, EnumProperty,
FloatProperty)
from bpy.types import Operator
bl_info = {
"name": "WCP/SO Mesh File",
"author": "Kevin Caccamo",
"description": "Export to a WCP/SO mesh file.",
"version": (2, 2, 0),
"blender": (2, 65, 0),
"location": "File > Export",
"warning": "%{GIT_COMMIT}",
"wiki_url": "http://www.ciinet.org/kevin/bl_wcp_exporter/",
"category": "Import-Export"
}
class ImportIFF(Operator, ImportHelper):
"""Import a WCP/WCSO mesh file"""
# important since its how bpy.ops.import_test.some_data is constructed
bl_idname = "import_scene.iff"
bl_label = "Import WCP/SO IFF mesh file"
# ExportHelper mixin class uses this
filename_ext = ".iff"
filter_glob = StringProperty(
default="*.iff",
options={'HIDDEN'}
)
texname = StringProperty(
name="Texture name",
description="Name to use for the materials and textures. Uses model "
"filename if blank"
)
# import_all_lods = BoolProperty(
# name="Import all LODs",
# description="Import all LOD meshes as separate models",
# default=False
# )
# use_facetex = BoolProperty(
# name="Use Face Textures",
# description="Use face textures instead of materials for texturing",
# default=False
# )
# read_mats = BoolProperty(
# name="Read MATs",
# description="Attempt to read MAT files (experimental)",
# default=True
# )
backend_class_name = "IFFImporter"
def execute(self, context):
import_time = time.perf_counter()
warnings.resetwarnings()
# WIP
wc_orientation_matrix = axis_conversion("Z", "Y").to_4x4()
self.import_bsp = False
importer = getattr(import_iff, self.backend_class_name)(
self.filepath, self.texname, wc_orientation_matrix,
self.import_bsp
)
importer.load()
with warnings.catch_warnings(record=True) as wlist:
for warning in wlist:
self.report({"WARNING"}, warning.message)
import_time = time.perf_counter() - import_time
print("Import took", import_time, "seconds")
return {"FINISHED"}
class ExportIFF(Operator, ExportHelper):
"""Export to a WCP/WCSO mesh file"""
# important since its how bpy.ops.import_test.some_data is constructed
bl_idname = "export_scene.iff"
bl_label = "Export WCP/SO IFF mesh file"
# ExportHelper mixin class uses this
filename_ext = ".iff"
filter_glob = StringProperty(
default="*.iff",
options={'HIDDEN'},
)
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
texnum = IntProperty(
name="MAT number",
description="The number that the MAT texture indices will start at",
default=22000,
subtype="UNSIGNED",
min=0,
max=99999990
)
apply_modifiers = BoolProperty(
name="Apply Modifiers",
description="Apply Modifiers to exported model",
default=True
)
active_as_lod0 = BoolProperty(
name="Export only active object",
description="Only export the active object and its LODs.",
default=True
)
# NOTE: BSP Tree generation is not implemented!
# As a fallback measure, I'm hard-coding this attribute for now.
generate_bsp = BoolProperty(
name="Generate BSP",
description="Generate a BSP tree "
"(for corvette and capship hull/component meshes)",
default=False,
options={"HIDDEN"}
)
axis_forward = EnumProperty(
name="Forward Axis",
items=(('X', "X Forward", ""),
('Y', "Y Forward", ""),
('Z', "Z Forward", ""),
('-X', "-X Forward", ""),
('-Y', "-Y Forward", ""),
('-Z', "-Z Forward", ""),
),
default='Y',
)
axis_up = EnumProperty(
name="Up Axis",
items=(('X', "X Up", ""),
('Y', "Y Up", ""),
('Z', "Z Up", ""),
('-X', "-X Up", ""),
('-Y', "-Y Up", ""),
('-Z', "-Z Up", ""),
),
default='Z',
)
use_facetex = BoolProperty(
name="Use Face Textures",
description="Use face textures instead of materials for texturing",
default=False
)
include_far_chunk = BoolProperty(
name="Include FAR Chunk",
description="Include the 'FAR' CHUNK when exporting to IFF. Only "
"required if the mesh being exported is a fighter mesh.",
default=False
)
drang_increment = FloatProperty(
name="LOD Range increment",
description="The default increment value for LOD ranges, if the user "
"did not supply LOD ranges.",
subtype="UNSIGNED",
unit="LENGTH",
min=0.0,
max=10000.0,
default=500.0
)
test_run = BoolProperty(
name="Test run",
description="Do a test run; don't actually export anything. "
"Prevents Wing Blender from writing IFF files.",
default=False,
options={"HIDDEN"}
)
# output_version = EnumProperty(
# name="Mesh version",
# items=(("8", "Mesh version 8", "Use mesh version 8"),
# ("9", "Mesh version 9", "Use mesh version 9"),
# ("10", "Mesh version 10", "Use mesh version 10"),
# ("11", "Mesh version 11", "Use mesh version 11"),
# ("12", "Mesh version 12", "Use mesh version 12"),
# ("13", "Mesh version 13", "Use mesh version 13")),
# description="The mesh version to export the model(s) as. This "
# "determines how the game loads and uses the model(s). "
# "You should really leave this alone, unless you really "
# "know what you are doing.",
# default="12"
# )
backend_class_name = "IFFExporter"
def check(self, context):
from bpy_extras.io_utils import axis_conversion_ensure
return axis_conversion_ensure(self, "axis_forward", "axis_up")
def execute(self, context):
warnings.resetwarnings()
# Get the matrix to transform the model to "WCP/SO" orientation
wc_orientation_matrix = axis_conversion(
self.axis_forward, self.axis_up, "Z", "Y"
).to_4x4()
# self.output_version = "12"
exporter = getattr(export_iff, self.backend_class_name)(
self.filepath, self.texnum, self.apply_modifiers,
self.active_as_lod0, self.use_facetex, wc_orientation_matrix,
self.include_far_chunk, self.drang_increment, self.generate_bsp,
self.test_run
)
exporter.export()
with warnings.catch_warnings(record=True) as wlist:
for warning in wlist:
self.report({"WARNING"}, warning.message)
return {"FINISHED"}
class ExportXMF(Operator, ExportHelper):
"""Export to XMF source code for a WCP/WCSO IFF mesh file"""
# important since its how bpy.ops.import_test.some_data is constructed
bl_idname = "export_scene.xmf"
bl_label = "Export WCP/SO IFF mesh XMF source file"
# ExportHelper mixin class uses this
filename_ext = ".pas"
filter_glob = StringProperty(
default="*.pas",
options={'HIDDEN'},
)
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
texnum = IntProperty(
name="MAT number",
description="The number that the MAT texture indices"
" will start at",
default=22000,
subtype="UNSIGNED",
min=0,
max=99999990
)
apply_modifiers = BoolProperty(
name="Apply Modifiers",
description="Apply Modifiers to exported model",
default=True
)
active_as_lod0 = BoolProperty(
name="Active object is LOD0",
description="Use the active object as the LOD 0 mesh",
default=True
)
# Not implemented as of now
# generate_bsp = BoolProperty(
# name = "Generate BSP",
# description = "Generate a BSP tree "
# "(for corvette and capship component meshes)",
# default = False
# )
axis_forward = EnumProperty(
name="Forward Axis",
items=(('X', "X Forward", ""),
('Y', "Y Forward", ""),
('Z', "Z Forward", ""),
('-X', "-X Forward", ""),
('-Y', "-Y Forward", ""),
('-Z', "-Z Forward", ""),
),
default='Y',
)
axis_up = EnumProperty(
name="Up Axis",
items=(('X', "X Up", ""),
('Y', "Y Up", ""),
('Z', "Z Up", ""),
('-X', "-X Up", ""),
('-Y', "-Y Up", ""),
('-Z', "-Z Up", ""),
),
default='Z',
)
use_facetex = BoolProperty(
name="Use Face Textures",
description="Use face textures instead of materials for texturing",
default=False
)
backend_class_name = "XMFExporter"
def execute(self, context):
warnings.resetwarnings()
# Get the matrix to transform the model to "WCP/SO" orientation
wc_orientation_matrix = axis_conversion(
self.axis_forward, self.axis_up, "Z", "Y"
).to_4x4()
# Create the output file if it doesn't already exist
try:
outfile = open(self.filepath, "x")
outfile.close()
except FileExistsError:
self.report({"INFO"}, "File already exists!")
# NOTE: BSP Tree generation is not implemented!
# As a fallback measure, I'm hard-coding this attribute
self.generate_bsp = False
exporter = getattr(export_iff, self.backend_class_name)(
self.filepath, self.texnum, self.apply_modifiers,
self.active_as_lod0, self.use_facetex, wc_orientation_matrix,
self.generate_bsp
)
exporter.export()
with warnings.catch_warnings(record=True) as wlist:
for warning in wlist:
self.report({"WARNING"}, warning.message)
return {"FINISHED"}
# Only needed if you want to add into a dynamic menu
def menu_func_export_iff(self, context):
self.layout.operator(ExportIFF.bl_idname, text="WCP/SO IFF Mesh (.iff)")
def menu_func_import_iff(self, context):
self.layout.operator(ImportIFF.bl_idname, text="WCP/SO IFF Mesh (.iff)")
def menu_func_export_xmf(self, context):
self.layout.operator(ExportXMF.bl_idname,
text="WCP/SO IFF Mesh XMF source (.pas)")
def register():
bpy.utils.register_class(ImportIFF)
bpy.types.INFO_MT_file_import.append(menu_func_import_iff)
bpy.utils.register_class(ExportIFF)
bpy.types.INFO_MT_file_export.append(menu_func_export_iff)
# bpy.utils.register_class(ExportXMF)
# bpy.types.INFO_MT_file_export.append(menu_func_export_xmf)
def unregister():
bpy.utils.unregister_class(ImportIFF)
bpy.types.INFO_MT_file_import.append(menu_func_import_iff)
bpy.utils.unregister_class(ExportIFF)
bpy.types.INFO_MT_file_export.remove(menu_func_export_iff)
# bpy.utils.unregister_class(ExportXMF)
# bpy.types.INFO_MT_file_export.remove(menu_func_export_xmf)
if __name__ == "__main__":
register()