forked from kaosat-dev/Blenvy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
125 lines (99 loc) · 3.95 KB
/
settings.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
import json
import bpy
def upsert_settings(name, data, overwrite=False):
stored_settings = bpy.data.texts[name] if name in bpy.data.texts else None
if stored_settings is None:
stored_settings = bpy.data.texts.new(name)
stored_settings.write(json.dumps(data))
else:
if overwrite:
stored_settings.clear()
stored_settings.write(json.dumps(data))
else:
current_settings = json.loads(stored_settings.as_string())
stored_settings.clear()
current_settings = {**current_settings, **data}
stored_settings.write(json.dumps(current_settings))
def load_settings(name):
stored_settings = bpy.data.texts[name] if name in bpy.data.texts else None
if stored_settings is not None:
try:
return json.loads(stored_settings.as_string())
except:
return None
return None
def clear_settings(name):
texts = bpy.data.texts
stored_settings = texts.get(name, None)
if stored_settings is not None:
stored_settings.clear()
texts.remove(texts[name], do_unlink=True)
# given the input (actual) settings, filters out any invalid/useless params & params that are equal to defaults
def generate_complete_settings_dict(settings, presets, ignore_list=[], preset_defaults=True):
complete_preferences = {}
defaults = {}
def filter_out(pair):
key, value = pair
if key in ignore_list:
return False
return True
for k in presets.__annotations__:
item = presets.__annotations__[k]
default = item.keywords.get('default', None)
if default is not None:
defaults[k] = default
if preset_defaults:
complete_preferences[k] = default
#print("defaults", defaults)
for key in list(settings.keys()):
if key in defaults and settings[key] != defaults[key]: # only write out values different from defaults
value = getattr(settings, key, None) # this is needed for most of our settings (PropertyGroups)
if value is None:
value = settings[key] # and this for ...gltf settings
complete_preferences[key] = value
#print("setting", key, value, settings[key], settings)
complete_preferences = dict(filter(filter_out, dict(complete_preferences).items()))
return complete_preferences
# checks if old & new settings (dicts really) are identical
def are_settings_identical(old, new, white_list=None):
if old is None and new is None:
return True
if old is None and new is not None:
return False
if old is not None and new is None:
return False
old_items = sorted(old.items())
new_items = sorted(new.items())
if white_list is not None:
old_items_override = {}
new_items_override = {}
for key in white_list:
if key in old:
old_items_override[key] = old[key]
if key in new:
new_items_override[key] = new[key]
old_items = sorted(old_items_override.items())
new_items = sorted(new_items_override.items())
return old_items == new_items
# if one of the changed settings is not in the white list, it gets discarded
def changed_settings(old, new, white_list=[]):
if old is None and new is None:
return []
if old is None and new is not None:
return new.keys()
if old is not None and new is None:
return []
old_items = sorted(old.items())
new_items = sorted(new.items())
result = []
old_keys = list(old.keys())
new_keys =list(new.keys())
added = list(set(new_keys) - set(old_keys))
removed = list(set(old_keys) - set(new_keys))
result += added
result += removed
for key in new.keys():
if key in old:
if new[key] != old[key]:
result.append(key)
return [key for key in list(set(result)) if key in white_list]