Skip to content

Transform your 3D texturing workflow with the power of generative AI, directly within Blender!

keep the panel's operators from crashing Blender 4.5 beside server checks

sakalonddd4824e28128
diff --git a/stablegen/core/callbacks.py b/stablegen/core/callbacks.py
index f15639e..8104c88 100644
--- a/stablegen/core/callbacks.py
+++ b/stablegen/core/callbacks.py
@@ -264,13 +264,16 @@ def update_combined(self, context):
print("[StableGen] Server address changed, checking asynchronously...")
+ # Read the preferences here; the worker thread must not touch bpy.
+ ping_timeout, api_timeout = get_timeout('ping'), get_timeout('api')
+
def _bg_work():
result = {}
- result['online'] = check_server_availability(server_address, timeout=get_timeout('ping'))
+ result['online'] = check_server_availability(server_address, timeout=ping_timeout)
if result['online']:
- result['trellis2'] = check_trellis2_available(server_address, timeout=get_timeout('api'))
- result['pbr'] = check_pbr_available(server_address, timeout=get_timeout('api'))
- result['skintokens'] = check_skintokens_autorig_available(server_address, timeout=get_timeout('api'))
+ result['trellis2'] = check_trellis2_available(server_address, timeout=api_timeout)
+ result['pbr'] = check_pbr_available(server_address, timeout=api_timeout)
+ result['skintokens'] = check_skintokens_autorig_available(server_address, timeout=api_timeout)
else:
result['trellis2'] = False
result['pbr'] = False
diff --git a/stablegen/core/preferences.py b/stablegen/core/preferences.py
index 90c61c9..6636d5f 100644
--- a/stablegen/core/preferences.py
+++ b/stablegen/core/preferences.py
@@ -306,14 +306,16 @@ class CheckServerStatus(bpy.types.Operator):
server_addr = prefs.server_address
print(f"[StableGen] Checking server status at {server_addr}...")
+ # Read the preferences here; the worker thread must not touch bpy.
+ ping_timeout, api_timeout = get_timeout('ping'), get_timeout('api')
def _bg_work():
result = {}
- result['online'] = check_server_availability(server_addr, timeout=get_timeout('ping'))
+ result['online'] = check_server_availability(server_addr, timeout=ping_timeout)
if result['online']:
- result['trellis2'] = check_trellis2_available(server_addr, timeout=get_timeout('api'))
- result['pbr'] = check_pbr_available(server_addr, timeout=get_timeout('api'))
- result['skintokens'] = check_skintokens_autorig_available(server_addr, timeout=get_timeout('api'))
+ result['trellis2'] = check_trellis2_available(server_addr, timeout=api_timeout)
+ result['pbr'] = check_pbr_available(server_addr, timeout=api_timeout)
+ result['skintokens'] = check_skintokens_autorig_available(server_addr, timeout=api_timeout)
else:
result['trellis2'] = False
result['pbr'] = False
diff --git a/stablegen/core/server_monitor.py b/stablegen/core/server_monitor.py
index 52ed3e9..96741bc 100644
--- a/stablegen/core/server_monitor.py
+++ b/stablegen/core/server_monitor.py
@@ -75,9 +75,9 @@ def _tick():
if (_in_flight or wrapper is None or not wrapper.preferences.server_address
or not bpy.app.online_access or not _sidebar_open(context) or _busy(context)):
return INTERVAL
- address = wrapper.preferences.server_address
+ address, timeout = wrapper.preferences.server_address, get_timeout('ping')
_in_flight = True
- _run_async(lambda: check_server_availability(address, timeout=get_timeout('ping')),
+ _run_async(lambda: check_server_availability(address, timeout=timeout),
lambda online: _apply(address, online))
except Exception:
_in_flight = False
diff --git a/stablegen/ui/studio.py b/stablegen/ui/studio.py
index 976594f..4a8954d 100644
--- a/stablegen/ui/studio.py
+++ b/stablegen/ui/studio.py
@@ -4,6 +4,7 @@ GPU drawing lives only in this tab's UI region. A lightweight input operator
passes viewport and native-tab events through. Every workflow command invokes
its existing Blender operator; every setting writes its original RNA property.
"""
+import contextlib
import json
import logging
from pathlib import Path
@@ -153,13 +154,33 @@ _font_path = ''
_log = logging.getLogger(__name__)
+def _in_area(session):
+ return bpy.context.window == session.window and bpy.context.area == session.area
+
+
+def _override(session, region):
+ # Input events already run in the session's area, so override only when the context differs. temp_override
+ # installs a Python context dict that operators read while bpy.ops has released the GIL. Python 3.11 (Blender
+ # 4.x, 5.0) then skips taking the GIL whenever a worker thread holds it, and Blender crashes (#126).
+ if _in_area(session) and bpy.context.region == region:
+ return contextlib.nullcontext()
+ return bpy.context.temp_override(window=session.window, area=session.area, region=region)
+
+
def _context(session):
- return bpy.context.temp_override(window=session.window, area=session.area, region=session.region)
+ return _override(session, session.region)
def _viewport(session):
- region = next(r for r in session.area.regions if r.type == 'WINDOW')
- return bpy.context.temp_override(window=session.window, area=session.area, region=region)
+ return _override(session, next(r for r in session.area.regions if r.type == 'WINDOW'))
+
+
+def _viewport_mode(session, mode):
+ """Context and execution mode that run an operator in the session's 3D viewport region. From its own area
+ Blender picks that region itself (the *_REGION_WIN modes), so no Python context dict is installed."""
+ if _in_area(session):
+ return contextlib.nullcontext(), mode.replace('_DEFAULT', '_REGION_WIN')
+ return _viewport(session), mode
def _undo(message):
@@ -400,9 +421,10 @@ class StudioSession:
self.commit()
self.clear_hover(leave=True)
try:
- with _viewport(self):
+ scope, mode = _viewport_mode(self, mode)
+ with scope:
op = model.operator(name)
- if not op.poll():
+ if not op.poll(mode):
self.tell('This command is unavailable for the current scene or operation.')
return
if diagnostics.active(bpy.context.scene) and name not in {
@@ -461,9 +483,9 @@ class StudioSession:
def write_binding(self, binding, value, undo=True):
if binding.path == 'sg_studio_diagnostics':
- with _viewport(self):
- bpy.ops.stablegen.studio_diagnostic('EXEC_DEFAULT', setting=binding.property,
- value=json.dumps(value))
+ scope, mode = _viewport_mode(self, 'EXEC_DEFAULT')
+ with scope:
+ bpy.ops.stablegen.studio_diagnostic(mode, setting=binding.property, value=json.dumps(value))
_undo('Change diagnostic display')
else:
binding.set(bpy.context, value)