forked from Sollumz/Sollumz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sollumz_preferences.py
343 lines (267 loc) · 10.6 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
import bpy
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
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 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
)
export_settings: bpy.props.PointerProperty(
type=SollumzExportSettings, name="Export Settings")
import_settings: bpy.props.PointerProperty(
type=SollumzImportSettings, name="Import Settings")
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")
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)
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)