-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
347 lines (286 loc) · 11.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
import pathlib
import platform
import bpy
from bpy_extras.io_utils import ImportHelper, ExportHelper
from . import importer, exporter
ADDON_LOCATION = pathlib.Path(__file__).parent
class AddonPreferences(bpy.types.AddonPreferences):
bl_idname = __package__
mod_folder: bpy.props.StringProperty(
name="Mod folder",
description='Directory containing your mod data. Used for locating textures and other linked data',
subtype='DIR_PATH',
default=str((pathlib.Path(
'C:/Program Files' if platform.system() == 'Windows' else '~/.local/share/Steam/steamapps/common'
) / 'Dawn of War/My_Mod').expanduser()),
)
primary_color: bpy.props.FloatVectorProperty(
name='Primary',
default=(0.43, 0.08, 0.00),
subtype='COLOR',
)
secondary_color: bpy.props.FloatVectorProperty(
name='Secondary',
default=(0.63, 0.53, 0.38),
subtype='COLOR',
)
trim_color: bpy.props.FloatVectorProperty(
name='Trim',
default=(0.22, 0.16, 0.16),
subtype='COLOR',
)
weapons_color: bpy.props.FloatVectorProperty(
name='Weapon',
default=(0.16, 0.16, 0.16),
subtype='COLOR',
)
eyes_color: bpy.props.FloatVectorProperty(
name='Eyes',
default=(0.01, 0.01, 0.01),
subtype='COLOR',
)
badge_path: bpy.props.StringProperty(
name="Badge",
description='Path to the badge image',
subtype='FILE_PATH',
default=str(ADDON_LOCATION/'default_badge.tga'),
)
banner_path: bpy.props.StringProperty(
name="Banner",
description='Path to the banner image',
subtype='FILE_PATH',
default=str(ADDON_LOCATION/'default_banner.tga'),
)
def draw(self, context):
self.layout.prop(self, 'mod_folder')
teamcolor_panel_header, teamcolor_panel = self.layout.panel('default_teamcolor')
teamcolor_panel_header.label(text='Default teamcolor')
if teamcolor_panel is not None:
teamcolor_panel.row().prop(self, 'primary_color')
teamcolor_panel.row().prop(self, 'secondary_color')
teamcolor_panel.row().prop(self, 'trim_color')
teamcolor_panel.row().prop(self, 'weapon_color')
teamcolor_panel.row().prop(self, 'eyes_color')
teamcolor_panel.prop(self, 'badge_path')
teamcolor_panel.prop(self, 'banner_path')
class ImportWhm(bpy.types.Operator, ImportHelper):
"""Import Dawn of War .whm model file"""
bl_idname = 'import_model.dow_whm'
bl_label = 'Import .whm file'
bl_options = {'REGISTER', 'UNDO'}
filename_ext = '.whm'
filter_glob: bpy.props.StringProperty(
default='*.whm',
options={'HIDDEN'},
maxlen=255,
)
new_project: bpy.props.BoolProperty(
name='New project',
description='Create a new project for the imported model',
default=True,
)
load_wtp: bpy.props.BoolProperty(
name='Team colourable',
description='Load matching .wtp files',
default=False,
)
create_cameras: bpy.props.BoolProperty(
name='Cameras',
description='Create cameras and focus objects',
default=False,
)
def execute(self, context):
if self.new_project:
bpy.ops.wm.read_homefile(app_template='')
for mesh in bpy.data.meshes:
bpy.data.meshes.remove(mesh)
for material in bpy.data.materials:
material.user_clear()
bpy.data.materials.remove(material)
for cam in bpy.data.cameras:
bpy.data.cameras.remove(cam)
preferences = context.preferences
addon_prefs: AddonPreferences = preferences.addons[__package__].preferences
with open(self.filepath, 'rb') as f:
reader = importer.ChunkReader(f)
loader = importer.WhmLoader(
pathlib.Path(addon_prefs.mod_folder),
load_wtp=self.load_wtp,
create_cameras=self.create_cameras,
context=context,
)
window = context.window_manager.windows[0]
with context.temp_override(window=window):
try:
loader.load(reader)
if self.load_wtp:
loader.apply_teamcolor({
**{k: getattr(addon_prefs, f'{k}_color') for k in loader.TEAMCOLORABLE_LAYERS},
**{k: getattr(addon_prefs, f'{k}_path') for k in loader.TEAMCOLORABLE_IMAGES},
})
for area in context.screen.areas:
if area.type == 'VIEW_3D':
space = area.spaces.active
if space.type == 'VIEW_3D':
space.shading.type = 'MATERIAL'
finally:
for message_lvl, message in loader.messages:
self.report({message_lvl}, message)
return {'FINISHED'}
class ImportTeamcolor(bpy.types.Operator, ImportHelper):
"""Import Dawn of War .teamcolour file"""
bl_idname = 'import_model.dow_teamcolour'
bl_label = 'Import .teamcolour file'
bl_options = {'REGISTER', 'UNDO'}
filename_ext = '.teamcolour'
filter_glob: bpy.props.StringProperty(
default='*.teamcolour',
options={'HIDDEN'},
maxlen=255,
)
set_as_defaul: bpy.props.BoolProperty(
name='Save as default',
description='Keep this color scheme as the default for other models',
default=False,
)
def execute(self, context):
preferences = context.preferences
addon_prefs: AddonPreferences = preferences.addons[__package__].preferences
loader = importer.WhmLoader(pathlib.Path(addon_prefs.mod_folder), context=context)
try:
teamcolor = loader.load_teamcolor(self.filepath)
loader.apply_teamcolor(teamcolor)
if self.set_as_defaul:
for c in loader.TEAMCOLORABLE_LAYERS:
setattr(addon_prefs, f'{c}_color', teamcolor.get(c, getattr(addon_prefs, f'{c}_color')))
for c in loader.TEAMCOLORABLE_IMAGES:
setattr(addon_prefs, f'{c}_path', str(teamcolor.get(c, getattr(addon_prefs, f'{c}_path'))))
finally:
for message_lvl, message in loader.messages:
self.report({message_lvl}, message)
return {'FINISHED'}
class ExportModel:
object_name: bpy.props.StringProperty(
default='',
name='Object name',
)
meta: bpy.props.StringProperty(
default='',
name='Metadata',
description='Custom metadata, e.g. username',
)
convert_textures: bpy.props.BoolProperty(
name='Convert textures',
description='Convert textures from .dds to .rsh',
default=True,
)
max_texture_size: bpy.props.IntProperty(
name='Max texture size',
description='Resize exported textures to the given max size.',
default=768,
)
default_texture_path: bpy.props.StringProperty(
default='art/ebps/races/space_marines/texture_share',
name='Default texture folder',
)
data_location: bpy.props.EnumProperty(
name='Data store location',
description='How to store textures',
items=(
('mod_root', 'Mod root', 'Put new filed into the mod folder'),
('nearby', 'Standalone folder', 'Create a directory with data alongside the exported file'),
),
default='nearby',
)
store_layout: bpy.props.EnumProperty(
name='Texture store layout',
description='How to organize textures',
items=(
('flat', 'Flat', 'Put everything in the root folder'),
('flat_folders', 'Flat folders', 'Create a directory for each texture'),
('full_path', 'Full path', 'Create nested directories according to data paths'),
),
default='flat',
)
FORMAT: exporter.ExportFormat = None
def execute(self, context):
assert self.FORMAT is not None
preferences: AddonPreferences = context.preferences
addon_prefs = preferences.addons[__package__].preferences
filepath = pathlib.Path(self.filepath)
data_folder = addon_prefs.mod_folder if self.data_location == 'mod_root' else filepath.with_suffix('')
paths = exporter.FileDispatcher(data_folder, layout={
'flat': exporter.FileDispatcher.Layout.FLAT,
'flat_folders': exporter.FileDispatcher.Layout.FLAT_FOLDERS,
'full_path': exporter.FileDispatcher.Layout.FULL_PATH,
}[self.store_layout])
object_name = self.object_name if self.object_name else filepath.stem
with open(self.filepath, 'wb') as f:
writer = exporter.ChunkWriter(f, exporter.CHUNK_VERSIONS[self.FORMAT])
ex = exporter.Exporter(paths,
format=self.FORMAT,
default_texture_path=self.default_texture_path,
convert_textures=self.convert_textures,
install_requirements=False,
max_texture_size=self.max_texture_size,
context=context)
try:
ex.export(writer, object_name=object_name, meta=self.meta)
if self.data_location == 'nearby':
paths.dump_info()
finally:
for message_lvl, message in ex.messages:
self.report({message_lvl}, message)
return {'FINISHED'}
class ExportWhm(bpy.types.Operator, ExportModel, ExportHelper):
"""Export Dawn of War .whm model file"""
bl_idname = 'export_model.dow_whm'
bl_label = 'Export .whm file'
bl_options = {'REGISTER'}
filename_ext = ".whm"
filter_glob: bpy.props.StringProperty(
default='*.whm',
options={'HIDDEN'},
maxlen=255,
)
FORMAT = exporter.ExportFormat.WHM
class ExportSgm(bpy.types.Operator, ExportModel, ExportHelper):
"""Export Dawn of War Object Editor .sgm model"""
bl_idname = 'export_model.dow_sgm'
bl_label = 'Export .sgm file'
bl_options = {'REGISTER'}
filename_ext = ".sgm"
filter_glob: bpy.props.StringProperty(
default='*.sgm',
options={'HIDDEN'},
maxlen=255,
)
FORMAT = exporter.ExportFormat.SGM
def import_menu_whm_func(self, context):
self.layout.operator(ImportWhm.bl_idname, text='Dawn of War model (.whm)')
def import_menu_teamcolor_func(self, context):
self.layout.operator(ImportTeamcolor.bl_idname, text='Dawn of War color scheme (.teamcolour)')
def export_menu_whm_func(self, context):
self.layout.operator(ExportWhm.bl_idname, text='Dawn of War model (.whm)')
def export_menu_sgm_func(self, context):
self.layout.operator(ExportSgm.bl_idname, text='Dawn of War Object Editor model (.sgm)')
def register():
bpy.utils.register_class(ImportWhm)
bpy.utils.register_class(ImportTeamcolor)
bpy.utils.register_class(ExportWhm)
bpy.utils.register_class(ExportSgm)
bpy.utils.register_class(AddonPreferences)
bpy.types.TOPBAR_MT_file_import.append(import_menu_whm_func)
bpy.types.TOPBAR_MT_file_import.append(import_menu_teamcolor_func)
bpy.types.TOPBAR_MT_file_export.append(export_menu_whm_func)
bpy.types.TOPBAR_MT_file_export.append(export_menu_sgm_func)
def unregister():
bpy.types.TOPBAR_MT_file_export.remove(export_menu_sgm_func)
bpy.types.TOPBAR_MT_file_export.remove(export_menu_whm_func)
bpy.types.TOPBAR_MT_file_import.remove(import_menu_teamcolor_func)
bpy.types.TOPBAR_MT_file_import.remove(import_menu_whm_func)
bpy.utils.unregister_class(AddonPreferences)
bpy.utils.unregister_class(ExportSgm)
bpy.utils.unregister_class(ExportWhm)
bpy.utils.unregister_class(ImportTeamcolor)
bpy.utils.unregister_class(ImportWhm)