Skip to content

Commit 515c006

Browse files
committed
Fix offline mode not working consistently
1 parent 222ad86 commit 515c006

2 files changed

Lines changed: 67 additions & 60 deletions

File tree

__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class GafferPreferences(bpy.types.AddonPreferences):
110110
name="Offline Mode",
111111
description=("Stop Gaffer from checking polyhaven.com for the latest HDRI and tag lists"),
112112
default=False,
113+
update=functions.update_offline_mode,
113114
)
114115
auto_refresh_light_list: bpy.props.BoolProperty(
115116
name="Auto-Refresh Light List",

functions.py

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,65 @@
3232
TAG_REFRESH_LIGHT_LIST = False
3333

3434

35+
# Persistent settings functions
36+
37+
38+
def init_persistent_settings(set_name=None, set_value=None):
39+
"""Initialize persistent settings file with option to change a default value"""
40+
41+
settings = {}
42+
43+
# Some settings might already exist
44+
if os.path.exists(const.settings_file):
45+
with open(const.settings_file) as f:
46+
try:
47+
settings = json.load(f)
48+
except json.JSONDecodeError:
49+
settings = {}
50+
51+
# First time use in 2.8, copy path from 2.7
52+
if "hdri_paths" not in settings and "hdri_path" in settings:
53+
settings["hdri_paths"] = [settings["hdri_path"]]
54+
55+
defaults = {"show_hdri_haven": True, "hdri_path": "", "hdri_paths": [""]} # Legacy
56+
for d in defaults:
57+
if d not in settings:
58+
settings[d] = defaults[d]
59+
60+
if set_name is not None:
61+
settings[set_name] = set_value
62+
63+
with open(const.settings_file, "w") as f:
64+
f.write(json.dumps(settings, indent=4))
65+
66+
return settings
67+
68+
69+
def get_persistent_setting(name):
70+
if os.path.exists(const.settings_file):
71+
with open(const.settings_file) as f:
72+
try:
73+
settings = json.load(f)
74+
except json.JSONDecodeError:
75+
settings = {}
76+
if name in settings:
77+
return settings[name]
78+
79+
initial_settings = init_persistent_settings()
80+
return initial_settings[name] if name in initial_settings else None
81+
82+
83+
def set_persistent_setting(name, value):
84+
if not os.path.exists(const.settings_file):
85+
init_persistent_settings(name, value)
86+
else:
87+
with open(const.settings_file) as f:
88+
settings = json.load(f)
89+
settings[name] = value
90+
with open(const.settings_file, "w") as f:
91+
f.write(json.dumps(settings, indent=4))
92+
93+
3594
# Utils
3695

3796

@@ -2036,6 +2095,11 @@ def set_defaults(context, hdri_name):
20362095
f.write(json.dumps(defaults, indent=4))
20372096

20382097

2098+
def update_offline_mode(self, context):
2099+
prefs = context.preferences.addons[__package__].preferences
2100+
set_persistent_setting("offline_mode", prefs.offline_mode)
2101+
2102+
20392103
def get_hdri_haven_list(force_update=False):
20402104
"""Get Poly Haven list from web once per week, otherwise fetch from file"""
20412105

@@ -2049,8 +2113,8 @@ def get_hdri_haven_list(force_update=False):
20492113

20502114
prefs = bpy.context.preferences.addons[__package__].preferences
20512115

2052-
if prefs and prefs.offline_mode:
2053-
print("Offline mode enabled, using local data")
2116+
if (prefs and prefs.offline_mode) or get_persistent_setting("offline_mode"):
2117+
print("Gaffer not fetching HDRIs from Poly Haven, offline mode enabled, using local data if available")
20542118
return offline_data
20552119

20562120
if not force_update:
@@ -2153,61 +2217,3 @@ def progress_update(context, value, text):
21532217
def progress_end(context):
21542218
context.scene.gaf_props.Progress = 0
21552219
context.scene.gaf_props.ShowProgress = False
2156-
2157-
2158-
# Persistent settings functions
2159-
2160-
2161-
def init_persistent_settings(set_name=None, set_value=None):
2162-
"""Initialize persistent settings file with option to change a default value"""
2163-
2164-
settings = {}
2165-
2166-
# Some settings might already exist
2167-
if os.path.exists(const.settings_file):
2168-
with open(const.settings_file) as f:
2169-
try:
2170-
settings = json.load(f)
2171-
except json.JSONDecodeError:
2172-
settings = {}
2173-
2174-
# First time use in 2.8, copy path from 2.7
2175-
if "hdri_paths" not in settings and "hdri_path" in settings:
2176-
settings["hdri_paths"] = [settings["hdri_path"]]
2177-
2178-
defaults = {"show_hdri_haven": True, "hdri_path": "", "hdri_paths": [""]} # Legacy
2179-
for d in defaults:
2180-
if d not in settings:
2181-
settings[d] = defaults[d]
2182-
2183-
if set_name is not None:
2184-
settings[set_name] = set_value
2185-
2186-
with open(const.settings_file, "w") as f:
2187-
f.write(json.dumps(settings, indent=4))
2188-
2189-
return settings
2190-
2191-
2192-
def get_persistent_setting(name):
2193-
if os.path.exists(const.settings_file):
2194-
with open(const.settings_file) as f:
2195-
try:
2196-
settings = json.load(f)
2197-
except json.JSONDecodeError:
2198-
settings = {}
2199-
if name in settings:
2200-
return settings[name]
2201-
2202-
return init_persistent_settings()[name]
2203-
2204-
2205-
def set_persistent_setting(name, value):
2206-
if not os.path.exists(const.settings_file):
2207-
init_persistent_settings(name, value)
2208-
else:
2209-
with open(const.settings_file) as f:
2210-
settings = json.load(f)
2211-
settings[name] = value
2212-
with open(const.settings_file, "w") as f:
2213-
f.write(json.dumps(settings, indent=4))

0 commit comments

Comments
 (0)