-
Notifications
You must be signed in to change notification settings - Fork 8
/
sollumz_preferences.py
505 lines (403 loc) · 16.8 KB
/
sollumz_preferences.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import bpy
from bpy.props import (
StringProperty,
IntProperty,
BoolProperty,
CollectionProperty,
)
import os
import ast
from typing import Any
from .sollumz_properties import SollumType
from configparser import ConfigParser
from typing import Optional
PREFS_FILE_NAME = "sollumz_prefs.ini"
def _save_preferences(self, context):
addon_prefs = get_addon_preferences(context)
prefs_path = get_prefs_path()
config = ConfigParser()
prefs_dict = _get_data_block_as_dict(addon_prefs)
main_prefs: dict[str, Any] = {}
for key, value in prefs_dict.items():
if isinstance(value, bpy.types.PropertyGroup):
config[key] = _get_data_block_as_dict(value)
continue
if isinstance(value, bpy.types.bpy_prop_collection):
# Convert CollectionProperty to a list of tuples
value = list(map(lambda d: (d.path, d.recursive), value))
main_prefs[key] = value
config["main"] = main_prefs
with open(prefs_path, "w") as f:
config.write(f)
class SollumzExportSettings(bpy.types.PropertyGroup):
limit_to_selected: bpy.props.BoolProperty(
name="Limit to Selected",
description="Export selected and visible objects only",
default=True,
update=_save_preferences
)
auto_calculate_inertia: bpy.props.BoolProperty(
name="Auto Calculate Inertia",
description="Automatically calculate inertia for physics objects (applies to yfts and ydrs too)",
default=False,
update=_save_preferences
)
auto_calculate_volume: bpy.props.BoolProperty(
name="Auto Calculate Volume",
description="Automatically calculate volume for physics objects (applies to yfts and ydrs too)",
default=False,
update=_save_preferences
)
exclude_skeleton: bpy.props.BoolProperty(
name="Exclude Skeleton",
description="Exclude skeleton from export. Usually done with mp ped components",
default=False,
update=_save_preferences
)
export_with_ytyp: bpy.props.BoolProperty(
name="Export with ytyp",
description="Exports a .ytyp.xml with an archetype for every drawable or drawable dictionary being exported",
default=False,
update=_save_preferences
)
ymap_exclude_entities: bpy.props.BoolProperty(
name="Exclude Entities",
description="If enabled, ignore all Entities from the selected ymap(s)",
default=False,
update=_save_preferences
)
ymap_box_occluders: bpy.props.BoolProperty(
name="Exclude Box Occluders",
description="If enabled, ignore all Box occluders from the selected ymap(s)",
default=False,
update=_save_preferences
)
ymap_model_occluders: bpy.props.BoolProperty(
name="Exclude Model Occluders",
description="If enabled, ignore all Model occluders from the selected ymap(s)",
default=False,
update=_save_preferences
)
ymap_car_generators: bpy.props.BoolProperty(
name="Exclude Car Generators",
description="If enabled, ignore all Car Generators from the selected ymap(s)",
default=False,
update=_save_preferences
)
export_lods: bpy.props.EnumProperty(
name="Toggle LODs",
description="Toggle LODs to export",
options={"ENUM_FLAG"},
default=({"sollumz_export_very_high", "sollumz_export_main_lods"}),
items=(
("sollumz_export_very_high", "Very High",
"Export Very High LODs into a _hi.yft"),
("sollumz_export_main_lods", "High - Very Low",
"Export all LODs except Very High")
),
update=_save_preferences
)
apply_transforms: bpy.props.BoolProperty(
name="Apply Parent Transforms",
description="Apply Drawable/Fragment scale and rotation",
default=False,
update=_save_preferences
)
@property
def export_hi(self):
return "sollumz_export_very_high" in self.export_lods
@property
def export_non_hi(self):
return "sollumz_export_main_lods" in self.export_lods
class SollumzImportSettings(bpy.types.PropertyGroup):
import_as_asset: bpy.props.BoolProperty(
name="Import as asset",
description="Create an asset from the .ydr/.yft high LOD",
default=False,
update=_save_preferences
)
import_with_hi: bpy.props.BoolProperty(
name="Import with _hi",
description="Import the selected .yft.xml with the <name>_hi.yft.xml placed in the very high LOD (must be in the same directory)",
default=True,
update=_save_preferences
)
split_by_group: bpy.props.BoolProperty(
name="Split Mesh by Group",
description="Splits the mesh by vertex groups",
default=True,
update=_save_preferences
)
import_ext_skeleton: bpy.props.BoolProperty(
name="Import External Skeleton",
description="Imports the first found yft skeleton in the same folder as the selected file",
default=False,
update=_save_preferences
)
ymap_skip_missing_entities: bpy.props.BoolProperty(
name="Skip Missing Entities",
description="If enabled, missing entities wont be created as an empty object",
default=True,
update=_save_preferences
)
ymap_exclude_entities: bpy.props.BoolProperty(
name="Exclude Entities",
description="If enabled, ignore all entities from the selected ymap(s)",
default=False,
update=_save_preferences
)
ymap_box_occluders: bpy.props.BoolProperty(
name="Exclude Box Occluders",
description="If enabled, ignore all Box occluders from the selected ymap(s)",
default=False,
update=_save_preferences
)
ymap_model_occluders: bpy.props.BoolProperty(
name="Exclude Model Occluders",
description="If enabled, ignore all Model occluders from the selected ymap(s)",
default=False,
update=_save_preferences
)
ymap_car_generators: bpy.props.BoolProperty(
name="Exclude Car Generators",
description="If enabled, ignore all Car Generators from the selected ymap(s)",
default=False,
update=_save_preferences
)
ymap_instance_entities: bpy.props.BoolProperty(
name="Instance Entities",
description="If enabled, instance all entities from the selected ymap(s).",
default=False,
)
ytyp_mlo_instance_entities: bpy.props.BoolProperty(
name="Instance MLO Entities",
description=(
"If enabled, MLO entities will be linked to a copy of the object matching the archetype name, instead of"
"the object itself."
),
default=True,
)
class SzSharedTexturesDirectory(bpy.types.PropertyGroup):
path: StringProperty(
name="Path",
description="Path to a directory with textures",
subtype="DIR_PATH",
update=_save_preferences,
)
recursive: BoolProperty(
name="Recursive",
description="Search this directory recursively",
default=True,
update=_save_preferences,
)
class SOLLUMZ_UL_prefs_shared_textures_directories(bpy.types.UIList):
bl_idname = "SOLLUMZ_UL_prefs_shared_textures_directories"
def draw_item(
self, context, layout, data, item, icon, active_data, active_propname, index
):
layout.prop(item, "path", text="", emboss=False)
layout.prop(item, "recursive", text="", icon="OUTLINER")
class SOLLUMZ_OT_prefs_shared_textures_directory_add(bpy.types.Operator):
bl_idname = "sollumz.prefs_shared_textures_directory_add"
bl_label = "Add Shared Textures Directory"
bl_description = "Add a new directory to search textures in"
path: StringProperty(
name="Path",
description="Path to a directory with textures",
subtype="DIR_PATH",
)
recursive: BoolProperty(
name="Recursive",
description="Search this directory recursively",
default=True,
)
def execute(self, context):
prefs = get_addon_preferences(context)
d = prefs.shared_textures_directories.add()
d.path = self.path
d.recursive = self.recursive
prefs.shared_textures_directories_index = len(prefs.shared_textures_directories) - 1
_save_preferences(None, context)
return {"FINISHED"}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
class SOLLUMZ_OT_prefs_shared_textures_directory_remove(bpy.types.Operator):
bl_idname = "sollumz.prefs_shared_textures_directory_remove"
bl_label = "Remove Shared Textures Directory"
bl_description = "Remove the selected directory"
@classmethod
def poll(cls, context):
prefs = get_addon_preferences(context)
return 0 <= prefs.shared_textures_directories_index < len(prefs.shared_textures_directories)
def execute(self, context):
prefs = get_addon_preferences(context)
prefs.shared_textures_directories.remove(prefs.shared_textures_directories_index)
context.scene.ytyps.remove(context.scene.ytyp_index)
prefs.shared_textures_directories_index = max(prefs.shared_textures_directories_index - 1, 0)
_save_preferences(None, context)
return {"FINISHED"}
class SOLLUMZ_OT_prefs_shared_textures_directory_move_up(bpy.types.Operator):
bl_idname = "sollumz.prefs_shared_textures_directory_move_up"
bl_label = "Increase Shared Texture Directory Priority"
bl_description = "Increase search priority of this directory"
@classmethod
def poll(self, context):
prefs = get_addon_preferences(context)
return 0 < prefs.shared_textures_directories_index < len(prefs.shared_textures_directories)
def execute(self, context):
prefs = get_addon_preferences(context)
indexA = prefs.shared_textures_directories_index
indexB = prefs.shared_textures_directories_index - 1
prefs.swap_shared_textures_directories(indexA, indexB)
prefs.shared_textures_directories_index -= 1
return {"FINISHED"}
class SOLLUMZ_OT_prefs_shared_textures_directory_move_down(bpy.types.Operator):
bl_idname = "sollumz.prefs_shared_textures_directory_move_down"
bl_label = "Decrease Shared Texture Directory Priority"
bl_description = "Decrease search priority of this directory"
@classmethod
def poll(self, context):
prefs = get_addon_preferences(context)
return 0 <= prefs.shared_textures_directories_index < (len(prefs.shared_textures_directories) - 1)
def execute(self, context):
prefs = get_addon_preferences(context)
indexA = prefs.shared_textures_directories_index
indexB = prefs.shared_textures_directories_index + 1
prefs.swap_shared_textures_directories(indexA, indexB)
prefs.shared_textures_directories_index += 1
return {"FINISHED"}
class SollumzAddonPreferences(bpy.types.AddonPreferences):
bl_idname = __package__.split(".")[0]
scale_light_intensity: bpy.props.BoolProperty(
name="Scale Light Intensity",
description="Scale light intensity by 500 on import/export",
default=True,
update=_save_preferences
)
show_vertex_painter: bpy.props.BoolProperty(
name="Show Vertex Painter",
description="Show the Vertex Painter panel in General Tools (Includes Terrain Painter)",
default=True,
update=_save_preferences
)
extra_color_swatches: bpy.props.BoolProperty(
name="Extra Vertex Color Swatches",
description="Add 3 extra color swatches to the Vertex Painter Panel (Max 6)",
default=True,
update=_save_preferences
)
sollumz_icon_header: bpy.props.BoolProperty(
name="Show Sollumz icon",
description="Show the Sollumz icon in properties section headers",
default=True,
update=_save_preferences
)
use_text_name_as_mat_name: bpy.props.BoolProperty(
name="Use Texture Name as Material Name",
description="Use the name of the texture as the material name",
default=True,
update=_save_preferences
)
shared_textures_directories: CollectionProperty(
name="Shared Textures",
type=SzSharedTexturesDirectory,
)
shared_textures_directories_index: IntProperty(
name="Selected Shared Textures Directory",
min=0
)
export_settings: bpy.props.PointerProperty(type=SollumzExportSettings, name="Export Settings")
import_settings: bpy.props.PointerProperty(type=SollumzImportSettings, name="Import Settings")
def swap_shared_textures_directories(self, indexA: int, indexB: int):
a = self.shared_textures_directories[indexA]
b = self.shared_textures_directories[indexB]
pathA, recA = a.path, a.recursive
pathB, recB = b.path, b.recursive
a.path, a.recursive = pathB, recB
b.path, b.recursive = pathA, recA
def draw(self, context):
layout = self.layout
layout.prop(self, "scale_light_intensity")
layout.prop(self, "show_vertex_painter")
layout.prop(self, "extra_color_swatches")
layout.prop(self, "sollumz_icon_header")
layout.prop(self, "use_text_name_as_mat_name")
from .sollumz_ui import draw_list_with_add_remove
layout.separator()
layout.label(text="Shared Textures")
_, side_col = draw_list_with_add_remove(
self.layout,
SOLLUMZ_OT_prefs_shared_textures_directory_add.bl_idname,
SOLLUMZ_OT_prefs_shared_textures_directory_remove.bl_idname,
SOLLUMZ_UL_prefs_shared_textures_directories.bl_idname, "",
self, "shared_textures_directories",
self, "shared_textures_directories_index",
rows=4
)
side_col.separator()
subcol = side_col.column(align=True)
subcol.operator(SOLLUMZ_OT_prefs_shared_textures_directory_move_up.bl_idname, text="", icon="TRIA_UP")
subcol.operator(SOLLUMZ_OT_prefs_shared_textures_directory_move_down.bl_idname, text="", icon="TRIA_DOWN")
def register():
_load_preferences()
def get_addon_preferences(context: Optional[bpy.types.Context] = None) -> SollumzAddonPreferences:
return context.preferences.addons[__package__.split(".")[0]].preferences
def get_import_settings(context: Optional[bpy.types.Context] = None) -> SollumzImportSettings:
return get_addon_preferences(context or bpy.context).import_settings
def get_export_settings(context: Optional[bpy.types.Context] = None) -> SollumzExportSettings:
return get_addon_preferences(context or bpy.context).export_settings
def _load_preferences():
# Preferences are loaded via an ini file in <user_blender_path>/<version>/config/sollumz_prefs.ini
addon_prefs = get_addon_preferences(bpy.context)
if addon_prefs is None:
return
prefs_path = get_prefs_path()
if not os.path.exists(prefs_path):
return
config = ConfigParser()
config.read(prefs_path)
for section in config.keys():
if section == "DEFAULT":
continue
if section == "main":
_apply_preferences(addon_prefs, config, section)
continue
if not hasattr(addon_prefs, section):
print(
f"Unknown preferences pointer property '{section}'! Skipping...")
continue
prop_group = getattr(addon_prefs, section)
_apply_preferences(prop_group, config, section)
def _apply_preferences(data_block: bpy.types.ID, config: ConfigParser, section: str):
for key in config[section].keys():
if not hasattr(data_block, key):
print(f"Unknown preference '{key}'! Skipping...")
continue
value_str = config.get(section, key)
value = ast.literal_eval(value_str)
if key == "shared_textures_directories":
# Special case to handle CollectionProperty
data_block.shared_textures_directories.clear()
for path, recursive in value:
d = data_block.shared_textures_directories.add()
d.path = path
d.recursive = recursive
else:
setattr(data_block, key, value)
def _get_data_block_as_dict(data_block: bpy.types.ID):
data_block_dict: dict[str, Any] = {}
for key in data_block.__annotations__.keys():
if not hasattr(data_block, key):
continue
value = getattr(data_block, key)
data_block_dict[key] = value
return data_block_dict
def get_prefs_path():
return os.path.join(bpy.utils.user_resource(resource_type="CONFIG"), PREFS_FILE_NAME)
def get_config_directory_path() -> str:
return bpy.utils.user_resource(resource_type="CONFIG", path="sollumz", create=True)
def register():
bpy.utils.register_class(SollumzAddonPreferences)
def unregister():
bpy.utils.unregister_class(SollumzAddonPreferences)