Skip to content

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

run guidance renders and projection builds without blocking the ui

sakalond41ba654344cb
diff --git a/stablegen/texturing/generator.py b/stablegen/texturing/generator.py
index 2a1bd0e..791efb1 100644
--- a/stablegen/texturing/generator.py
+++ b/stablegen/texturing/generator.py
@@ -16,6 +16,7 @@ import threading
import traceback
import io
import colorsys
+from types import GeneratorType
from datetime import datetime
import math
from PIL import Image, ImageEnhance
@@ -29,6 +30,7 @@ from .rendering import export_emit_image, export_visibility, export_canny, bake_
from ..cameras.geometry import _SGCameraResolution, _get_camera_resolution, _get_resolution_align
from ..cameras.overlays import _sg_restore_square_display, _sg_remove_crop_overlay, _sg_ensure_crop_overlay, _sg_hide_label_overlay, _sg_restore_label_overlay
from .projection import project_image, reinstate_compare_nodes, _bake_visibility_weights, reset_projection_material_nodes, _is_projection_mesh, pop_svm_stack_warning
+from .jobs import operation, RenderRequest, MainThreadOperation, run_sync
from ..utils import get_last_material_index, get_generation_dirs, get_file_path, get_dir_path, remove_empty_dirs, get_compositor_node_tree, configure_output_node_paths, get_eevee_engine_id, sg_modal_active
from ..util.mirror_color import MirrorReproject, _get_viewport_ref_np, _apply_color_match_to_file
from ..timeout_config import get_timeout
@@ -459,12 +461,32 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
elif hasattr(self, '_original_texturing_frame'):
context.scene.frame_set(self._original_texturing_frame)
- def _run_on_main_thread(self, func):
+ def _run_on_main_thread(self, func, finish_on_cancel=False):
"""Execute *func* on Blender's main thread via a timer callback and
block until it completes. Sets ``self._error`` on failure."""
def _callback():
try:
- func()
+ if not ComfyUIGenerate._is_running:
+ raise RuntimeError('Generation cancelled')
+ result = func()
+ if isinstance(result, GeneratorType):
+ if bpy.app.background:
+ run_sync(result)
+ else:
+ def done(error):
+ if error is None and not ComfyUIGenerate._is_running:
+ error = RuntimeError('Generation cancelled')
+ if error is not None:
+ self._error = str(error)
+ self._main_operation = None
+ self._wait_event.set()
+ self._main_operation = MainThreadOperation(
+ result, done,
+ cancelled=lambda: (not finish_on_cancel
+ and not ComfyUIGenerate._is_running),
+ )
+ bpy.app.timers.register(self._main_operation.tick)
+ return None
except Exception as exc:
self._error = str(exc)
traceback.print_exc()
@@ -1254,6 +1276,8 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# Add modal timer
context.window_manager.modal_handler_add(self)
self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
+ self._projection_cache = {}
+ self._main_operation = None
print("[StableGen] Starting thread")
if context.scene.generation_method == 'grid':
self._thread = threading.Thread(target=self.async_generate, args=(context,))
@@ -1395,8 +1419,12 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
else context.scene.weight_exponent
)
+ materials = set()
for obj in getattr(self, '_to_texture', ()):
material = getattr(obj, 'active_material', None)
+ if material is None or material.as_pointer() in materials:
+ continue
+ materials.add(material.as_pointer())
reset_projection_material_nodes(material, reset_angle, reset_exponent)
# Material state is diagnostic state, not generation configuration.
@@ -1580,15 +1608,8 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# depth threshold and geometry, including runs that never had one.
# Other guidance maps and model inference are not needed here.
if getattr(context.scene, 'sg_rework_projection_guard', False):
- for i, camera in enumerate(cameras):
- self._stage = f"Rebuilding Projection Guards ({i+1}/{len(cameras)})"
- def _render_guard(_i=i, _cam=camera):
- self._activate_view(context, _i)
- with _SGCameraResolution(context, _cam):
- self.export_depthmap(context, camera_id=_i)
- self._run_on_main_thread(_render_guard)
- if self._error:
- return
+ self._run_on_main_thread(lambda: self.export_depthmaps(
+ context, cameras, stage='Rebuilding Projection Guards', _steps=True))
return
if context.scene.generation_mode in ('standard', 'regenerate_selected'):
@@ -1606,16 +1627,9 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
and context.scene.generation_method == 'sequential')
)
if need_depth and context.scene.generation_method != 'uv_inpaint':
- for i, camera in enumerate(cameras):
- self._stage = f"Rendering Depth Maps ({i+1}/{len(cameras)})"
- _i, _cam = i, camera
- def _render_depth(_i=_i, _cam=_cam):
- self._activate_view(context, _i)
- with _SGCameraResolution(context, _cam):
- self.export_depthmap(context, camera_id=_i)
- self._run_on_main_thread(_render_depth)
- if self._error:
- return
+ self._run_on_main_thread(lambda: self.export_depthmaps(context, cameras, _steps=True))
+ if self._error:
+ return
if context.scene.generation_method == 'grid':
self._run_on_main_thread(
lambda: self.combine_maps(context, cameras, type="depth"))
@@ -1630,9 +1644,9 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
def _render_canny(_i=_i, _cam=_cam):
self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
- export_canny(context, camera_id=_i,
+ yield from export_canny(context, camera_id=_i,
low_threshold=context.scene.canny_threshold_low,
- high_threshold=context.scene.canny_threshold_high)
+ high_threshold=context.scene.canny_threshold_high, _steps=True)
self._run_on_main_thread(_render_canny)
if self._error:
return
@@ -1654,7 +1668,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
def _render_normal(_i=_i, _cam=_cam):
self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
- self.export_normal(context, camera_id=_i)
+ yield from self.export_normal(context, camera_id=_i, _steps=True)
self._run_on_main_thread(_render_normal)
if self._error:
return
@@ -1680,9 +1694,9 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
def _render_wb(_i=_i, _cam=_cam):
self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
- export_render(context, camera_id=_i,
+ yield from export_render(context, camera_id=_i,
output_dir=workbench_dir, filename=f"render{_i}",
- background_color=guide_background)
+ background_color=guide_background, _steps=True)
self._run_on_main_thread(_render_wb)
if self._error:
return
@@ -1739,7 +1753,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
def _render_refine(_i=_i, _cam=_cam):
self._activate_view(context, _i)
with _SGCameraResolution(context, _cam):
- export_emit_image(context, self._to_texture, camera_id=_i)
+ yield from export_emit_image(context, self._to_texture, camera_id=_i, _steps=True)
if need_feather:
render_edge_feather_mask(
context, self._to_texture, _cam, _i,
@@ -1803,20 +1817,18 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
def image_reproject_callback():
try:
redraw_ui(context)
- project_image(
+ yield from project_image(
context, self._to_texture, self._material_id,
stop_index=self._current_image,
cameras=self._cameras,
view_plan=self._view_plan,
+ _steps=True,
)
except Exception as e:
self._error = str(e)
traceback.print_exc()
- self._wait_event.set()
return None
- bpy.app.timers.register(image_reproject_callback)
- self._wait_event.wait()
- self._wait_event.clear()
+ self._run_on_main_thread(image_reproject_callback, finish_on_cancel=True)
if self._error:
return
@@ -1918,18 +1930,19 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# export_visibility's alphabetical
# rediscovery assigns custom-order
# renders to the wrong camera.
- export_visibility(
+ yield from export_visibility(
context,
self._to_texture,
camera_visibility=_vis_cam,
camera_render=_cur_cam,
camera_id=self._current_image,
+ _steps=True,
)
_emit_cam = _cur_cam
with _SGCameraResolution(context, _emit_cam):
if context.scene.model_architecture in ('qwen_image_edit', 'qwen_image_21', 'flux2_klein'): # export custom bg and fallback for Qwen/Klein image edit
fallback_color, background_color = self._get_qwen_context_colors(context)
- export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=background_color, fallback_color=fallback_color) # Export render for next view
+ yield from export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=background_color, fallback_color=fallback_color, _steps=True)
if getattr(context.scene, 'qwen_use_confidence_context', False):
self._apply_qwen_binary_context_mask(
context,
@@ -1944,15 +1957,12 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
)
else:
# Use a gray (neutral) background and fallback for other architectures
- export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=(0.5, 0.5, 0.5), fallback_color=(0.5, 0.5, 0.5))
+ yield from export_emit_image(context, self._to_texture, camera_id=self._current_image, bg_color=(0.5, 0.5, 0.5), fallback_color=(0.5, 0.5, 0.5), _steps=True)
except Exception as e:
self._error = str(e)
traceback.print_exc()
- self._wait_event.set()
return None
- bpy.app.timers.register(context_callback)
- self._wait_event.wait()
- self._wait_event.clear()
+ self._run_on_main_thread(context_callback)
if self._error:
return
self._stage = "Uploading to Server"
@@ -2049,22 +2059,24 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
def image_project_callback():
try:
redraw_ui(context)
- project_image(
+ if not hasattr(self, '_projection_cache'):
+ self._projection_cache = {}
+ yield from project_image(
context, self._to_texture, self._material_id,
stop_index=self._current_image,
cameras=self._cameras,
view_plan=self._view_plan,
+ incremental_cache=self._projection_cache,
+ _steps=True,
)
except Exception as e:
self._error = str(e)
traceback.print_exc()
- # Set the event to signal the end of the process
- self._wait_event.set()
return None
- bpy.app.timers.register(image_project_callback)
- # Wait for the event to be set
- self._wait_event.wait()
- self._wait_event.clear()
+ # In-place shader construction must finish its topology
+ # before honoring cancellation. It still yields to the
+ # UI, and no next view starts after cancellation.
+ self._run_on_main_thread(image_project_callback, finish_on_cancel=True)
if self._error:
return
# Update info for the next iteration (if any)
@@ -2191,12 +2203,13 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
original_method = context.scene.generation_method
try:
context.scene.generation_method = 'separate'
- project_image(
+ yield from project_image(
context,
self._to_texture,
self._material_id,
cameras=self._cameras,
view_plan=self._view_plan,
+ _steps=True,
)
finally:
context.scene.generation_method = original_method
@@ -2213,10 +2226,11 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
self._stage = "Projecting Image"
redraw_ui(context)
if context.scene.generation_method != 'uv_inpaint':
- project_image(
+ yield from project_image(
context, self._to_texture, self._material_id,
cameras=self._cameras,
view_plan=self._view_plan,
+ _steps=True,
)
else:
# Apply the UV inpainted textures to each mesh
@@ -2262,7 +2276,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# Keep the worker alive until projection finishes. Otherwise modal
# cleanup can restore sequential mode before this callback executes,
# causing it to skip the entire reproject (and hide projection errors).
- self._run_on_main_thread(image_project_callback)
+ self._run_on_main_thread(image_project_callback, finish_on_cancel=True)
if self._error:
return
@@ -2300,6 +2314,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
return self.execute(context)
+ @operation
def _export_depthmap_impl(self, context, camera_id=None, guard_scale=1):
"""
Exports the depth map of the scene.
@@ -2326,11 +2341,26 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# Get the active view layer
view_layer = bpy.context.view_layer
- # Cycles exposes depth plus object/material index passes. Workbench and
- # Eevee in Blender 5 expose only Image/Alpha/Depth on Render Layers,
- # silently disabling the hand/dress part guard.
- bpy.context.scene.render.engine = 'CYCLES'
- bpy.context.scene.cycles.samples = 1
+ use_projection_guard = getattr(
+ context.scene, 'sg_rework_projection_guard', False
+ )
+ use_part_ids = (
+ use_projection_guard
+ or experimental_enabled(context.scene, 'qwen_regularize_context_mask')
+ )
+ # Workbench retains the fast ordinary depth-guidance path. Cycles is
+ # required whenever a consumer needs categorical object/material IDs;
+ # Workbench/Eevee do not expose those passes in Blender 5.
+ depth_engine = 'CYCLES' if use_part_ids else 'BLENDER_WORKBENCH'
+ # Even assigning the current engine invalidates viewport buffers. The
+ # asynchronous render then locks drawing before they can be rebuilt.
+ if bpy.context.scene.render.engine != depth_engine:
+ bpy.context.scene.render.engine = depth_engine
+ if use_part_ids:
+ bpy.context.scene.cycles.samples = 1
+ # Denoising computes shaded RGB we never consume here. Z/index
+ # passes stay unchanged; the outer transaction restores this flag.
+ bpy.context.scene.cycles.use_denoising = False
# Object/material indices are categorical data. Blender's default
# output dithering introduces one-LSB checkerboard noise into these
@@ -2345,17 +2375,21 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
original_pass_z = view_layer.use_pass_z
# Enable depth pass in the render settings
+ for name in _RenderStateTransaction._PASS_NAMES:
+ if hasattr(view_layer, name):
+ setattr(view_layer, name, name == 'use_pass_z' or (
+ use_part_ids and name in {'use_pass_object_index', 'use_pass_material_index'}))
view_layer.use_pass_z = True
if hasattr(view_layer, 'use_pass_object_index'):
- view_layer.use_pass_object_index = True
+ view_layer.use_pass_object_index = use_part_ids
if hasattr(view_layer, 'use_pass_material_index'):
- view_layer.use_pass_material_index = True
+ view_layer.use_pass_material_index = use_part_ids
material_indices = {}
next_material_index = 1
for object_index, render_object in enumerate(
(obj for obj in context.view_layer.objects
- if obj.type == 'MESH' and not obj.hide_render), start=1):
+ if use_part_ids and obj.type == 'MESH' and not obj.hide_render), start=1):
render_object.pass_index = min(object_index, 255)
for slot in render_object.material_slots:
material = slot.material
@@ -2423,9 +2457,6 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
links.new(divide.outputs[0], index_output.inputs[0])
return True
- use_projection_guard = getattr(
- context.scene, 'sg_rework_projection_guard', False
- )
# Preserve calibrated camera distance for receiver-aware occlusion
# feathering. The model's existing normalized depth input is unchanged.
distance_scale = None
@@ -2445,10 +2476,6 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
distance_output.format.color_depth = '16'
distance_output.format.color_mode = 'RGB'
links.new(distance_node.outputs[0], distance_output.inputs[0])
- use_part_ids = (
- use_projection_guard
- or experimental_enabled(context.scene, 'qwen_regularize_context_mask')
- )
has_object_ids = use_part_ids and _add_index_output(
"IndexOB", f"object_id{camera_id}", -200
)
@@ -2458,7 +2485,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
# Render the scene
try:
- bpy.ops.render.render(write_still=True)
+ yield RenderRequest(write_still=False)
finally:
_finish_compositor_render(node_tree, compositor_state)
@@ -2466,7 +2493,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
depth_path = os.path.join(output_dir, f"{output_file}{frame_suffix}.png")
object_path = os.path.join(output_dir, f"object_id{camera_id}{frame_suffix}.png")
material_path = os.path.join(output_dir, f"material_id{camera_id}{frame_suffix}.png")
- depth_image = cv2.imread(depth_path, cv2.IMREAD_GRAYSCALE)
+ depth_image = cv2.imread(depth_path, cv2.IMREAD_GRAYSCALE) if use_projection_guard else None
if use_projection_guard and depth_image is not None:
object_image = (
cv2.imread(object_path, cv2.IMREAD_GRAYSCALE)
@@ -2512,28 +2539,26 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
)
cv2.imwrite(guard_path, guard)
feather_width = getattr(context.scene, 'sg_silhouette_feather', 0)
- feather = feather_projection_guard(guard, feather_width * guard_scale)
+ feather, guard_influence = feather_projection_guard(
+ guard, feather_width * guard_scale, return_influence=True)
# Keep this field continuous through the rejected band. A white
# core creates a second edge under bilinear sampling and a halo.
# A constant shader floor preserves last-source coverage without
# introducing another mask boundary.
feather_path = os.path.join(guard_dir, f"projection_guard_feather{camera_id}.png")
cv2.imwrite(feather_path, feather)
- occlusion, occlusion_influence = projection_occlusion_fields(
- depth_image, feather_width * guard_scale,
- max(1, round(float(getattr(context.scene, 'sg_silhouette_depth', 0.01)) * 255)),
- object_ids=object_image if not guard_diagnostics['fallback_triggered'] else None)
distance_path = os.path.join(output_dir, f'projection_distance{camera_id}{frame_suffix}.png')
distance_image = cv2.imread(distance_path, cv2.IMREAD_UNCHANGED)
if distance_image is None or distance_image.dtype != np.uint16:
raise RuntimeError('Projection occlusion distance pass is missing or not 16-bit')
if distance_image.ndim == 3:
distance_image = distance_image[:, :, 0]
- ramp, influence, reference = projection_occlusion_fields(
+ occlusion, occlusion_influence, ramp, influence, reference = projection_occlusion_fields(
depth_image, feather_width * guard_scale,
max(1, round(float(getattr(context.scene, 'sg_silhouette_depth', 0.01)) * 255)),
object_ids=object_image if not guard_diagnostics['fallback_triggered'] else None,
- surface_distance=distance_image.astype(np.float32) / 65535.0)
+ surface_distance=distance_image.astype(np.float32) / 65535.0,
+ include_legacy=True)
# PNG channels R/G/B: continuous ramp, sharpening influence,
# midpoint distance separating the occluder and receiving surface.
geometry_path = os.path.join(guard_dir, f'projection_occlusion_geometry{camera_id}.png')
@@ -2545,8 +2570,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
occlusion_path = os.path.join(guard_dir, f"projection_occlusion_feather{camera_id}.png")
cv2.imwrite(occlusion_path, occlusion)
cv2.imwrite(os.path.join(guard_dir, f"projection_guard_influence{camera_id}.png"),
- np.maximum(projection_guard_feather_influence(guard, feather_width * guard_scale),
- occlusion_influence))
+ np.maximum(guard_influence, occlusion_influence))
self._record_confidence_artifacts(
context,
camera_id,
@@ -2589,22 +2613,46 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
print(f"[StableGen] Depth map saved to: {os.path.join(output_dir, output_file)}.png")
# Restore original settings
- bpy.context.scene.render.engine = original_engine
+ if bpy.context.scene.render.engine != original_engine:
+ bpy.context.scene.render.engine = original_engine
bpy.context.scene.view_settings.view_transform = original_view_transform
bpy.context.scene.render.film_transparent = original_film_transparent
bpy.context.scene.render.use_compositing = original_use_compositing
bpy.context.scene.render.filepath = original_filepath
view_layer.use_pass_z = original_pass_z
+ @operation
+ def export_depthmaps(self, context, cameras, stage='Rendering Depth Maps'):
+ """Keep the renderer stable across guidance views, with one job dispatch.
+
+ Each view still has its own render suspension and transaction. The
+ outer transaction restores the original engine on success, failure or
+ cancellation; native job teardown remains managed by jobs.py.
+ """
+ with _RenderStateTransaction(context):
+ use_part_ids = (getattr(context.scene, 'sg_rework_projection_guard', False)
+ or experimental_enabled(context.scene, 'qwen_regularize_context_mask'))
+ engine = 'CYCLES' if use_part_ids else 'BLENDER_WORKBENCH'
+ if context.scene.render.engine != engine:
+ context.scene.render.engine = engine
+ for i, camera in enumerate(cameras):
+ self._stage = f'{stage} ({i+1}/{len(cameras)})'
+ self._activate_view(context, i)
+ with _SGCameraResolution(context, camera):
+ yield from self.export_depthmap(context, camera_id=i, _steps=True)
+
+ @operation
def export_depthmap(self, context, camera_id=None):
- meshes = [obj for obj in context.view_layer.objects if obj.type == 'MESH']
- with _RenderStateTransaction(context, meshes):
+ # Depth/index exports never edit slots or polygon assignments. Avoid
+ # clearing/restoring them: that invalidates every material/viewport.
+ with _RenderStateTransaction(context):
scale = (int(getattr(context.scene, 'sg_projection_guard_scale', '1'))
if getattr(context.scene, 'sg_rework_projection_guard', False) else 1)
context.scene.render.resolution_x *= scale
context.scene.render.resolution_y *= scale
- return self._export_depthmap_impl(context, camera_id=camera_id, guard_scale=scale)
+ return (yield from self._export_depthmap_impl(context, camera_id=camera_id, guard_scale=scale, _steps=True))
+ @operation
def export_normal(self, context, camera_id=None):
"""
Exports the normal map of the scene.
@@ -2615,8 +2663,9 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
"""
meshes = [obj for obj in context.view_layer.objects if obj.type == 'MESH']
with _RenderStateTransaction(context, meshes):
- return self._export_normal_impl(context, camera_id=camera_id)
+ return (yield from self._export_normal_impl(context, camera_id=camera_id, _steps=True))
+ @operation
def _export_normal_impl(self, context, camera_id=None):
"""Render the evaluated normal pass without mutating production state."""
print("[StableGen] Exporting normal map")
@@ -2675,7 +2724,7 @@ class ComfyUIGenerate(_PBRMixin, bpy.types.Operator):
links.new(render_layers_node.outputs["Alpha"], alpha_over_node.inputs[0])
try:
- bpy.ops.render.render(write_still=True)
+ yield RenderRequest()
finally:
_finish_compositor_render(node_tree, compositor_state)
diff --git a/stablegen/texturing/jobs.py b/stablegen/texturing/jobs.py
new file mode 100644
index 0000000..f7929b3
--- /dev/null
+++ b/stablegen/texturing/jobs.py
@@ -0,0 +1,148 @@
+"""Cooperative main-thread operations, with synchronous script compatibility."""
+
+from functools import wraps
+from types import GeneratorType
+from time import perf_counter
+import bpy
+
+
+class RenderRequest:
+ """Suspend a render transaction until Blender's native render job ends."""
+
+ def __init__(self, *, write_still=True):
+ # Compositor File Output nodes write independently of the main colour
+ # result. Guidance exports can omit that otherwise unused extra image.
+ self.write_still = write_still
+
+
+def operation(function):
+ @wraps(function)
+ def run(*args, **kwargs):
+ steps = function(*args, **kwargs)
+ return run_sync(steps)
+
+ @wraps(function)
+ def dispatch(*args, **kwargs):
+ if kwargs.pop('_steps', False):
+ return function(*args, **kwargs)
+ return run(*args, **kwargs)
+ return dispatch
+
+
+def run_sync(steps):
+ """Existing background, debug and script callers retain blocking semantics."""
+ if not isinstance(steps, GeneratorType):
+ return steps
+ try:
+ while True:
+ request = next(steps)
+ if isinstance(request, RenderRequest):
+ result = bpy.ops.render.render(write_still=request.write_still)
+ if 'CANCELLED' in result:
+ raise RuntimeError('StableGen render cancelled')
+ except StopIteration as complete:
+ return complete.value
+ finally:
+ steps.close()
+
+
+class MainThreadOperation:
+ """Advance transactions only on the main thread and after render teardown.
+
+ Handlers only mark completion. The timer waits until the RENDER job has
+ actually released its scene before resuming or unwinding the transaction.
+ """
+
+ def __init__(self, steps, done, cancelled=lambda: False):
+ self.steps = steps
+ self.done = done
+ self.cancelled = cancelled
+ self.rendering = False
+ self.outcome = None
+ self.scene = None
+ self.handlers = []
+
+ def _detach(self):
+ for collection, callback in self.handlers:
+ if callback in collection:
+ collection.remove(callback)
+ self.handlers.clear()
+
+ def _start_render(self, request):
+ self.scene = bpy.context.scene
+ self.outcome = None
+ def complete(scene, *args):
+ if scene == self.scene:
+ self.outcome = 'complete'
+ def cancel(scene, *args):
+ if scene == self.scene:
+ self.outcome = 'cancel'
+ for collection, callback in ((bpy.app.handlers.render_complete, complete),
+ (bpy.app.handlers.render_cancel, cancel)):
+ collection.append(callback)
+ self.handlers.append((collection, callback))
+ try:
+ # Guidance exports should not open a Render Result window or steal
+ # the panel's context. Restore the preference immediately after
+ # invoking; it is only consulted when the job is started.
+ view = bpy.context.preferences.view
+ display = view.render_display_type
+ try:
+ view.render_display_type = 'NONE'
+ result = bpy.ops.render.render('INVOKE_DEFAULT', write_still=request.write_still)
+ finally:
+ view.render_display_type = display
+ if 'CANCELLED' in result:
+ raise RuntimeError('StableGen render could not start')
+ if 'FINISHED' in result and self.outcome is None:
+ self.outcome = 'complete'
+ self.rendering = True
+ except Exception:
+ self._detach()
+ raise
+
+ def tick(self):
+ try:
+ # Do not enter or unwind a scene transaction while another render
+ # owns Blender's scene. Completion handlers alone are insufficient.
+ if not self.rendering and bpy.app.is_job_running('RENDER'):
+ return 0.05
+ if self.rendering:
+ if bpy.app.is_job_running('RENDER'):
+ # Completion precedes native job teardown. Poll more
+ # closely only during that short tail, without touching
+ # the scene before Blender has actually released it.
+ return 0.01 if self.outcome is not None else 0.05
+ if self.outcome is None:
+ raise RuntimeError('StableGen render ended without completion')
+ self.rendering = False
+ self._detach()
+ if self.outcome == 'cancel':
+ raise RuntimeError('StableGen render cancelled')
+ # Tiny graph/layout steps should share a bounded turn instead of
+ # each paying a timer delay. A render always ends the batch; never
+ # resume its transaction until the native job has released it.
+ deadline = perf_counter() + 0.005
+ while True:
+ if self.cancelled():
+ raise RuntimeError('Generation cancelled')
+ request = next(self.steps)
+ if isinstance(request, RenderRequest):
+ if bpy.app.is_job_running('RENDER'):
+ raise RuntimeError('Another render job is already running')
+ self._start_render(request)
+ break
+ if perf_counter() >= deadline:
+ break
+ return 0.01
+ except StopIteration:
+ self._detach()
+ self.done(None)
+ except Exception as error:
+ self._detach()
+ try:
+ self.steps.close()
+ except Exception as cleanup_error:
+ error = RuntimeError(f'{error}; cleanup failed: {cleanup_error}')
+ self.done(error)
+ return None
diff --git a/stablegen/texturing/node_layout.py b/stablegen/texturing/node_layout.py
index ac06962..ecf5453 100644
--- a/stablegen/texturing/node_layout.py
+++ b/stablegen/texturing/node_layout.py
@@ -6,6 +6,7 @@ construction order, including incremental sequential and appended edit passes.
"""
from collections import defaultdict
+from .jobs import operation
_VIEW_STAGES = (
@@ -191,11 +192,11 @@ def _arrange_group(nodes, links, items, title, parent, x, y):
members = {node.as_pointer(): node for node in items}
parents = defaultdict(list)
children = defaultdict(list)
- for link in links:
- source, target = link.from_node.as_pointer(), link.to_node.as_pointer()
- if source in members and target in members and source != target:
- parents[target].append(source)
- children[source].append(target)
+ for source in members:
+ for target in links.get(source, ()):
+ if target in members and source != target:
+ parents[target].append(source)
+ children[source].append(target)
depth = {pointer: 0 for pointer in members}
indegree = {pointer: len(set(parents[pointer])) for pointer in members}
@@ -231,6 +232,7 @@ def _arrange_group(nodes, links, items, title, parent, x, y):
return width, height
+@operation
def _arrange_crossfade(nodes, links, items, parent, x, y):
"""Separate the six threshold levels of the exact crossfade graph."""
levels = defaultdict(list)
@@ -256,12 +258,14 @@ def _arrange_crossfade(nodes, links, items, parent, x, y):
left_title, left_items = sections[offset]
left_width, left_height = _arrange_group(
nodes, links, left_items, left_title, frame, 35, top)
+ yield None
row_width, row_height = left_width, left_height
if offset + 1 < len(sections):
right_title, right_items = sections[offset + 1]
right_width, right_height = _arrange_group(
nodes, links, right_items, right_title,
frame, 35 + left_width + 55, top)
+ yield None
row_width += 55 + right_width
row_height = max(row_height, right_height)
width = max(width, row_width)
@@ -271,6 +275,7 @@ def _arrange_crossfade(nodes, links, items, parent, x, y):
return frame.width, frame.height
+@operation
def organize_projection_material(material, new_nodes, camera_nodes, cameras, stop_index,
local_edit=False, feather_method='ORIGINAL'):
"""Arrange only nodes made by this build, preserving earlier edit passes."""
@@ -278,7 +283,12 @@ def organize_projection_material(material, new_nodes, camera_nodes, cameras, sto
if not items:
return
tree = material.node_tree
- nodes, links = tree.nodes, tree.links
+ nodes = tree.nodes
+ # Index once for the whole layout pass. Feature frames only need edges
+ # leaving their own members, rather than rescanning the complete graph.
+ links = defaultdict(list)
+ for link in tree.links:
+ links[link.from_node.as_pointer()].append(link.to_node.as_pointer())
view_items = defaultdict(list)
global_items = []
for node in items:
@@ -351,6 +361,7 @@ def organize_projection_material(material, new_nodes, camera_nodes, cameras, sto
continue
width, height = _arrange_group(nodes, links, members, stage,
view_frame, section_x, -70)
+ yield None
section_x += width + 45
section_height = max(section_height, height)
view_frame.width = section_x + 10
@@ -368,13 +379,14 @@ def organize_projection_material(material, new_nodes, camera_nodes, cameras, sto
if not members:
continue
if stage == 'Boundary crossfade':
- width, height = _arrange_crossfade(
- nodes, links, members, pass_frame, blend_x, -70)
+ width, height = yield from _arrange_crossfade(
+ nodes, links, members, pass_frame, blend_x, -70, _steps=True)
else:
width, height = _arrange_group(
nodes, links, members, stage, pass_frame, blend_x, -70)
blend_x += width + 60
blend_height = max(blend_height, height)
+ yield None
if output:
width, height = _arrange_group(nodes, links, output, 'Material output',
pass_frame, blend_x, -70)
diff --git a/stablegen/texturing/projection.py b/stablegen/texturing/projection.py
index 0a74f0c..6c8f9a9 100644
--- a/stablegen/texturing/projection.py
+++ b/stablegen/texturing/projection.py
@@ -7,6 +7,8 @@ from ..utils import get_last_material_index, get_file_path, get_dir_path
from .rendering import prepare_baking, bake_texture, unwrap
from ..cameras.geometry import _get_camera_resolution
from mathutils import Matrix, Vector
+from .jobs import operation
+from .projection_cache import eligible as _cache_eligible, configuration as _cache_configuration, graph_state as _graph_state, asset_state as _asset_state
from .svm_budget import SVM_STACK_SIZE, estimate_svm_stack
@@ -1588,6 +1590,7 @@ def _apply_guard_coverage_rescue(nodes, links, guarded, raw, guards, feathers, t
return outputs, protect_occlusions(fades)
+@operation
def _crossfade_projection_feathers(nodes, links, weights, feathers, bases):
"""Integrate normalized camera combinations over feather thresholds.
@@ -1637,10 +1640,12 @@ def _crossfade_projection_feathers(nodes, links, weights, feathers, bases):
share = math('DIVIDE', f'CrossfadeShare-{i}-{j}', power, total)
result[j] = math('ADD', f'CrossfadeAccumulated-{i}-{j}', result[j],
math('MULTIPLY', f'CrossfadeContribution-{i}-{j}', interval, share))
+ yield None
return [_projection_feather_math(nodes, links, 'MULTIPLY', f'PostExponentFeather-{i}', value, 1.0)
for i, value in enumerate(result)]
+@operation
def _apply_projection_feathers(nodes, links, weights, feathers, bases, influences, method):
"""Apply a continuous, post-exponent fade without replacing view preference.
@@ -1650,7 +1655,7 @@ def _apply_projection_feathers(nodes, links, weights, feathers, bases, influence
if not any(feather is not None for feather in feathers):
return weights
if method == 'BOUNDARY_CROSSFADE':
- return _crossfade_projection_feathers(nodes, links, weights, feathers, bases)
+ return (yield from _crossfade_projection_feathers(nodes, links, weights, feathers, bases, _steps=True))
if method == 'LOCAL_UNSHARPEN':
def math(op, label, a, b):
return _projection_feather_math(nodes, links, op, label, a, b).outputs[0]
@@ -1686,6 +1691,7 @@ def _apply_projection_feathers(nodes, links, weights, feathers, bases, influence
return result
+@operation
def project_image(
context,
to_project,
@@ -1695,6 +1701,7 @@ def project_image(
view_plan=None,
force_rebuild=False,
reuse_projection_uv=False,
+ incremental_cache=None,
):
"""
Projects an image onto all mesh objects using UV Project Modifier.
@@ -1710,6 +1717,7 @@ def project_image(
# A newly generated view changes the joint solution. Do not reuse a
# sequential material containing the previous camera set's field.
force_rebuild = True
+ @operation
def build_mix_tree(shaders, weight_nodes, nodes_collection, links, last_node=None, level=0, stop_index=1000000):
"""
Recursively builds a binary tree of mix shader nodes.
@@ -1750,8 +1758,8 @@ def project_image(
f'SurfaceBlendVisible-{idx}', raw.outputs[0], 0.)
solved.append(_projection_feather_math(nodes_collection, links, 'MULTIPLY',
f'SurfaceBlendFinal-{idx}', field.outputs['Fac'], valid.outputs[0]))
- return build_mix_tree(shaders, solved, nodes_collection, links, last_node, level=1,
- stop_index=stop_index)
+ return (yield from build_mix_tree(shaders, solved, nodes_collection, links, last_node, level=1,
+ stop_index=stop_index, _steps=True))
def _combine_angle_feather(nodes_col, lnk, cr_angle_out, cr_feather_out, loc):
"""Combine angle-ramp and feather-ramp outputs into a single
@@ -2124,10 +2132,10 @@ def project_image(
normalized_outputs.append(div.outputs[0])
# Replace weight_nodes with sharpened-normalised nodes
- weight_nodes = _apply_projection_feathers(
+ weight_nodes = yield from _apply_projection_feathers(
nodes_collection, links, powered_nodes, feather_outputs,
normalized_outputs, influence_outputs,
- getattr(context.scene, 'sg_guard_feather_method', 'ORIGINAL'))
+ getattr(context.scene, 'sg_guard_feather_method', 'ORIGINAL'), _steps=True)
new_shaders = []
new_weight_nodes = []
@@ -2237,7 +2245,7 @@ def project_image(
new_shaders.append(shaders[i])
new_weight_nodes.append(weight_nodes[i])
i += 1
- return build_mix_tree(new_shaders, new_weight_nodes, nodes_collection, links, last_node, level+1)
+ return (yield from build_mix_tree(new_shaders, new_weight_nodes, nodes_collection, links, last_node, level+1, _steps=True))
if view_plan is not None:
cameras = []
@@ -2257,6 +2265,15 @@ def project_image(
else:
cameras = list(cameras)
+ stable_sequential = incremental_cache is not None and _cache_eligible(context.scene)
+ guard_dir = get_dir_path(context, 'inpaint')['visibility']
+ assets = [os.path.join(guard_dir, f'{prefix}{i}.png')
+ for i in range(len(cameras)) for prefix in
+ ('projection_guard', 'projection_guard_feather',
+ 'projection_guard_influence', 'projection_occlusion_geometry')]
+ assets += [os.path.join(guard_dir, f'projection_occlusion_geometry{i}.json')
+ for i in range(len(cameras))]
+
if surface_blend:
from .surface_blend import validate
validate(context, to_project, cameras)
@@ -2351,6 +2368,8 @@ def project_image(
if original_uv_map.name == _SG_BUFFER_UV_NAME and len(obj.data.uv_layers) > 1:
original_uv_map = obj.data.uv_layers[1]
obj.data.uv_layers.active = original_uv_map
+ if not reuse_projection_uv and stop_index == 0:
+ yield None
# Clean up the temporary buffer only when this call created projection UVs.
if not reuse_projection_uv:
@@ -2408,6 +2427,8 @@ def project_image(
processed_materials = set()
svm_estimates = {}
+ cache_key = (_cache_configuration(context.scene, cameras, mat_id, assets, to_project)
+ if stable_sequential else None)
for obj in to_project:
@@ -2446,7 +2467,11 @@ def project_image(
if original_mat != mat:
obj.data.materials.append(original_mat)
- if surface_blend and mat.name in processed_materials:
+ # UV and baked-visibility attributes were prepared on every mesh above.
+ # The shader reads those named attributes at the shading point, so its
+ # topology is independent of the representative object. Local edits
+ # retain their existing append/copy behavior.
+ if not is_local_edit and mat.name in processed_materials:
continue
# Enable use of nodes
@@ -2470,8 +2495,42 @@ def project_image(
original_uv_map = obj.data.uv_layers[1]
+ cache_entry = (incremental_cache.get(mat.as_pointer())
+ if stable_sequential else None)
+ if (not force_rebuild and cache_entry and cache_entry['key'] == cache_key
+ and stop_index == cache_entry['stop'] + 1
+ and cache_entry['assets'] == _asset_state(cache_entry['asset_paths'])
+ and cache_entry['state'] == _graph_state(mat)):
+ image_path = get_file_path(context, 'generated', camera_id=stop_index, material_id=mat_id)
+ image = get_or_load_image(image_path, force_reload=context.scene.overwrite_material)
+ if image:
+ nodes[cache_entry['images'][stop_index]].image = image
+ write = cache_entry['writes'].get(stop_index)
+ if write:
+ texture_name, bypass_name, mask_path = write
+ mask = (get_or_load_image(mask_path, force_reload=context.scene.overwrite_material)
+ if os.path.isfile(mask_path) else None)
+ if mask:
+ mask.colorspace_settings.name = 'Non-Color'
+ nodes[texture_name].image = mask
+ nodes[bypass_name].inputs[1].default_value = 0.0 if mask else 1.0
+ nodes[cache_entry['gates'][stop_index]].inputs[1].default_value = 1.0
+ for frame in nodes:
+ if frame.type == 'FRAME' and frame.label.startswith(f'View {stop_index + 1:02d} |'):
+ frame.label = frame.label.removesuffix(' | pending')
+ cache_entry['stop'] = stop_index
+ cache_entry['asset_paths'].append(image_path)
+ if write:
+ cache_entry['asset_paths'].append(mask_path)
+ cache_entry['assets'] = _asset_state(cache_entry['asset_paths'])
+ cache_entry['state'] = _graph_state(mat)
+ processed_materials.add(mat.name)
+ yield None
+ continue
+
rework_requires_rebuild = any((
force_rebuild,
+ stable_sequential,
getattr(context.scene, 'sg_rework_projection_guard', False),
getattr(context.scene, 'qwen_use_confidence_context', False),
experimental_enabled(context.scene, 'qwen_use_projection_write_masks'),
@@ -2541,6 +2600,7 @@ def project_image(
if image:
node.image = image
break
+ processed_materials.add(mat.name)
continue
elif not is_local_edit:
# Rebuild mode is required for per-view assets that only exist
@@ -2580,6 +2640,7 @@ def project_image(
camera_aspect_ratio_nodes = []
camera_direction_nodes = []
camera_up_nodes = []
+ sequential_writes = {}
def _create_edge_feather_weight(cam_index, mat_id_inner):
"""Load the edge-feathered visibility mask for camera *cam_index*
@@ -2631,15 +2692,16 @@ def project_image(
subtype="visibility",
camera_id=cam_index,
)
- if not os.path.exists(mask_path):
+ if not os.path.exists(mask_path) and not stable_sequential:
return None
mask_image = get_or_load_image(
mask_path,
force_reload=context.scene.overwrite_material,
- )
- if mask_image is None:
+ ) if os.path.isfile(mask_path) and cam_index <= stop_index else None
+ if mask_image is None and not stable_sequential:
return None
- mask_image.colorspace_settings.name = 'Non-Color'
+ if mask_image:
+ mask_image.colorspace_settings.name = 'Non-Color'
mask_tex = nodes.new("ShaderNodeTexImage")
mask_tex.image = mask_image
@@ -2657,6 +2719,16 @@ def project_image(
to_float.location = (-300, -200 * cam_index - 300)
to_float.label = f"{label_prefix}Weight-{cam_index}-{mat_id_inner}"
links.new(_mask_value(nodes, links, mask_tex).outputs[0], to_float.inputs[0])
+ if stable_sequential:
+ # A missing mask is a neutral multiplier, matching a rebuild.
+ # Bind its resource before opening the camera's active gate.
+ bypass = nodes.new('ShaderNodeMath')
+ bypass.operation = 'MAXIMUM'
+ bypass.label = f'{label_prefix}Available-{cam_index}-{mat_id_inner}'
+ bypass.inputs[1].default_value = 0.0 if mask_image else 1.0
+ links.new(to_float.outputs[0], bypass.inputs[0])
+ sequential_writes[cam_index] = (mask_tex.name, bypass.name, mask_path)
+ return bypass
return to_float
def _create_projection_guard_weight(cam_index, mat_id_inner):
@@ -2800,7 +2872,7 @@ def project_image(
script_nodes.append(scripts_to_connect)
- if i > stop_index and not voronoi_active:
+ if i > stop_index and not voronoi_active and not stable_sequential:
less_than = nodes.new("ShaderNodeMath")
less_than.operation = 'LESS_THAN'
less_than.label = f"StopGate-{i}-{mat_id}"
@@ -2875,7 +2947,7 @@ def project_image(
script_nodes.append(scripts_to_connect)
- if i > stop_index and not voronoi_active:
+ if i > stop_index and not voronoi_active and not stable_sequential:
less_than = nodes.new("ShaderNodeMath")
less_than.operation = 'LESS_THAN'
less_than.label = f"StopGate-{i}-{mat_id}"
@@ -3011,7 +3083,7 @@ def project_image(
script_nodes.append(scripts_to_connect)
- if i > stop_index and not voronoi_active:
+ if i > stop_index and not voronoi_active and not stable_sequential:
# Connect a temporary less than node to the script node
less_than = nodes.new("ShaderNodeMath")
less_than.operation = 'LESS_THAN'
@@ -3088,7 +3160,7 @@ def project_image(
use_qwen_write_mask = (
i > 0
- and i <= stop_index
+ and (i <= stop_index or stable_sequential)
and context.scene.model_architecture.startswith('qwen')
and context.scene.generation_method == 'sequential'
and context.scene.qwen_generation_method == 'generate'
@@ -3097,10 +3169,10 @@ def project_image(
)
qwen_write_weight = None
use_sdxl_write_mask = (
- i > 0 and i <= stop_index and (context.scene.model_architecture == 'sdxl') and (context.scene.generation_method == 'sequential')
+ i > 0 and (i <= stop_index or stable_sequential) and (context.scene.model_architecture == 'sdxl') and (context.scene.generation_method == 'sequential')
)
use_qwen21_generated_alpha = (
- i <= stop_index
+ (i <= stop_index or stable_sequential)
and context.scene.model_architecture == 'qwen_image_21'
)
if (use_qwen_write_mask or use_sdxl_write_mask) and script_nodes_outputs:
@@ -3135,6 +3207,15 @@ def project_image(
),
)
+ if stable_sequential:
+ # Keep validity/alpha before DPPG and normalization. A pending
+ # camera is exactly zero, including in coverage rescue and the
+ # crossfade subsets; it cannot become an untrusted competitor.
+ gate = _projection_feather_math(nodes, links, 'MULTIPLY',
+ f'SequentialActive-{i}-{mat_id}', script_nodes_outputs[-1].outputs[0],
+ float(i <= stop_index))
+ script_nodes_outputs[-1] = gate
+
if script_nodes_outputs and not isinstance(script_nodes_outputs[-1], tuple):
guard_weight = _create_projection_guard_weight(i, mat_id)
if guard_weight is not None:
@@ -3324,6 +3405,7 @@ def project_image(
pointer = camera_node.as_pointer()
if pointer not in camera_node_start:
layout_camera_nodes[pointer] = i
+ yield None
# Expose a confidence-only max chain for context rendering. This
# node does not mix RGB. Continuous weighted blending retains a separate
@@ -3353,7 +3435,7 @@ def project_image(
# The mix node is connected to the output node
mix_node = output.inputs["Surface"].links[0].from_node
else:
- mix_node, _ = build_mix_tree(tex_image_nodes, script_nodes_outputs, nodes, links, previous_node, stop_index=stop_index)
+ mix_node, _ = yield from build_mix_tree(tex_image_nodes, script_nodes_outputs, nodes, links, previous_node, stop_index=stop_index, _steps=True)
links.new(mix_node.outputs[0], output.inputs["Surface"])
processed_materials.add(mat.name)
@@ -3425,12 +3507,25 @@ def project_image(
# The output is reused by appended edit passes. Move its frame to
# the latest pass so the visible graph still ends at the output.
layout_nodes.append(output)
- organize_projection_material(
+ yield from organize_projection_material(
mat, layout_nodes,
layout_camera_nodes, cameras, stop_index, local_edit=is_local_edit,
feather_method=getattr(context.scene, 'sg_guard_feather_method', 'ORIGINAL'),
+ _steps=True,
)
check_projection_svm_stack(mat, svm_estimates)
+ if stable_sequential:
+ active_assets = [n.image.filepath for i, n in enumerate(tex_image_nodes)
+ if i <= stop_index and n.image]
+ active_assets += [v[2] for i, v in sequential_writes.items() if i <= stop_index]
+ incremental_cache[mat.as_pointer()] = dict(
+ key=cache_key, stop=stop_index, state=_graph_state(mat),
+ writes=sequential_writes, asset_paths=active_assets, assets=_asset_state(active_assets),
+ images=[n.name for n in tex_image_nodes],
+ gates=[next(n.name for n in nodes if n.label == f'SequentialActive-{i}-{mat_id}')
+ for i in range(len(cameras))],
+ )
+ yield None
return True
diff --git a/stablegen/texturing/projection_cache.py b/stablegen/texturing/projection_cache.py
new file mode 100644
index 0000000..90ce10d
--- /dev/null
+++ b/stablegen/texturing/projection_cache.py
@@ -0,0 +1,127 @@
+"""Generation-scoped shader reuse; never trust a saved or edited topology."""
+
+import os
+import hashlib
+from array import array
+import bpy
+
+
+def _plain(value):
+ if isinstance(value, (str, int, float, bool)) or value is None:
+ return value
+ if isinstance(value, bpy.types.ID):
+ return value.as_pointer()
+ try:
+ return tuple(value)
+ except TypeError:
+ return str(value)
+
+
+def graph_state(material):
+ """Include functional edits, socket values and links, but not UI layout."""
+ return _tree_state(material.node_tree, set())
+
+
+def _tree_state(tree, visited):
+ if tree.as_pointer() in visited:
+ return tree.as_pointer()
+ visited.add(tree.as_pointer())
+ nodes = []
+ for node in tree.nodes:
+ if node.type == 'FRAME':
+ continue
+ properties = []
+ for prop in node.bl_rna.properties:
+ name = prop.identifier
+ if (prop.type in {'BOOLEAN', 'INT', 'FLOAT', 'ENUM', 'STRING'}
+ and name not in {'name', 'label', 'select', 'hide', 'show_options',
+ 'show_preview', 'show_texture', 'width', 'height',
+ 'location', 'location_absolute', 'dimensions',
+ 'color', 'use_custom_color',
+ 'bl_label', 'bl_description', 'bl_icon'}):
+ properties.append((name, _plain(getattr(node, name))))
+ defaults = tuple(_plain(s.default_value) if hasattr(s, 'default_value') else None
+ for s in node.inputs)
+ outputs = (tuple(_plain(s.default_value) for s in node.outputs)
+ if node.type in {'VALUE', 'RGB'} else ())
+ image = getattr(node, 'image', None)
+ image_state = ((image.as_pointer(), image.filepath,
+ image.colorspace_settings.name, image.alpha_mode,
+ tuple(image.size)) if image else None)
+ group = getattr(node, 'node_tree', None)
+ group_state = _tree_state(group, visited) if group else None
+ ramp = getattr(node, 'color_ramp', None)
+ ramp_state = ((ramp.interpolation, ramp.color_mode, ramp.hue_interpolation,
+ tuple((e.position, tuple(e.color)) for e in ramp.elements))
+ if ramp else None)
+ nodes.append((node.as_pointer(), node.name, node.label, tuple(properties), defaults,
+ outputs, image_state, group_state, ramp_state))
+ edges = tuple((l.from_node.as_pointer(), l.from_socket.identifier,
+ l.to_node.as_pointer(), l.to_socket.identifier) for l in tree.links)
+ return tuple(nodes), edges
+
+
+def eligible(scene):
+ # Edit passes, baked fields and joint surface solves change topology. Base
+ # sequential projection is shared by all texturing architectures; late
+ # write-mask/image resources are bound before activating their view.
+ qwen = scene.model_architecture.startswith('qwen')
+ confidence = (qwen and scene.qwen_use_confidence_context
+ and scene.qwen_context_render_mode in {'REPLACE_STYLE', 'ADDITIONAL'})
+ voronoi = (scene.model_architecture in {'qwen_image_edit', 'qwen_image_21', 'flux2_klein'}
+ and scene.qwen_voronoi_mode)
+ surface = (scene.sg_guard_feather_method == 'SURFACE_BLEND'
+ and scene.sg_rework_projection_guard and scene.sg_silhouette_feather > 0)
+ return (bpy.app.version >= (5, 1, 0)
+ and scene.generation_method == 'sequential'
+ and scene.generation_mode == 'standard'
+ and (not qwen or scene.qwen_generation_method == 'generate')
+ and not scene.bake_visibility_weights
+ and not confidence and not surface and not voronoi)
+
+
+def asset_state(paths):
+ """Track missing files too, so later arrival cannot silently change a view."""
+ return tuple((path, os.stat(path).st_mtime_ns, os.stat(path).st_size)
+ if os.path.isfile(path) else (path, None, None) for path in paths)
+
+
+def configuration(scene, cameras, mat_id, assets, meshes):
+ # Read the StableGen property groups that can affect projection. Progress,
+ # the active camera is runtime state. Include each effective projection
+ # resolution, including the scene fallback for cameras without overrides.
+ from ..cameras.geometry import _get_camera_resolution
+ from ..feature_flags import experimental_enabled
+ prefixes = ('sg_guard_', 'sg_rework_', 'sg_silhouette_', 'sg_projection_',
+ 'discard_', 'weight_', 'early_', 'visibility_', 'refine_',
+ 'qwen_', 'bake_visibility', 'sg_allow_backface', 'sequential_', 'use_ipadapter',
+ 'fallback_color', 'view_blend_')
+ modes = {'model_architecture', 'generation_method', 'generation_mode'}
+ settings = tuple((p.identifier, _plain(getattr(scene, p.identifier)))
+ for p in scene.bl_rna.properties
+ if (p.identifier in modes or p.identifier.startswith(prefixes))
+ and p.type in {'BOOLEAN', 'INT', 'FLOAT', 'ENUM', 'STRING'})
+ views = tuple((c.as_pointer(), _get_camera_resolution(c, scene),
+ tuple(v for row in c.matrix_world for v in row),
+ tuple((p.identifier, _plain(getattr(c.data, p.identifier)))
+ for p in c.data.bl_rna.properties
+ if p.type in {'BOOLEAN', 'INT', 'FLOAT', 'ENUM', 'STRING'}),
+ tuple(sorted((k, _plain(v)) for k, v in c.items()))) for c in cameras)
+ files = asset_state(assets)
+ geometry = []
+ for obj in meshes:
+ coordinates = array('f', [0.0]) * (len(obj.data.vertices) * 3)
+ obj.data.vertices.foreach_get('co', coordinates)
+ topology = array('i', [0]) * len(obj.data.loops)
+ obj.data.loops.foreach_get('vertex_index', topology)
+ modifiers = tuple((m.type, tuple(
+ (p.identifier, _plain(getattr(m, p.identifier)))
+ for p in m.bl_rna.properties
+ if p.type in {'BOOLEAN', 'INT', 'FLOAT', 'ENUM', 'STRING', 'POINTER'}
+ and p.identifier != 'rna_type')) for m in obj.modifiers)
+ geometry.append((obj.data.as_pointer(), tuple(v for row in obj.matrix_world for v in row),
+ hashlib.blake2b(coordinates.tobytes() + topology.tobytes()).digest(),
+ modifiers))
+ # Saved flags alone do not determine behavior: preferences gate experiments.
+ write_masks = experimental_enabled(scene, 'qwen_use_projection_write_masks')
+ return mat_id, settings, write_masks, views, files, tuple(geometry)
diff --git a/stablegen/texturing/quality_masks.py b/stablegen/texturing/quality_masks.py
index c8de872..77f2ea6 100644
--- a/stablegen/texturing/quality_masks.py
+++ b/stablegen/texturing/quality_masks.py
@@ -254,16 +254,21 @@ def residual_fallback_percentage(write_mask, generated_bgr, fallback_rgb, tolera
return float(np.count_nonzero(editable & sentinel) * 100.0 / np.count_nonzero(editable))
-def feather_projection_guard(guard, width):
+def feather_projection_guard(guard, width, *, return_influence=False):
"""Fade into safe pixels without admitting any rejected silhouette pixels."""
width = max(0.0, float(width))
if width == 0:
- return guard.copy()
+ return (guard.copy(), np.zeros_like(guard)) if return_influence else guard.copy()
allowed = (guard > 0).astype(np.uint8)
distance = cv2.distanceTransform(allowed, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
ramp = np.clip(distance / width, 0.0, 1.0)
ramp = ramp * ramp * (3.0 - 2.0 * ramp)
- return np.rint(ramp * 255).astype(np.uint8)
+ feather = np.rint(ramp * 255).astype(np.uint8)
+ if not return_influence:
+ return feather
+ shoulder = np.clip((distance - width) / width, 0.0, 1.0)
+ shoulder = shoulder * shoulder * (3.0 - 2.0 * shoulder)
+ return feather, np.rint((1.0 - shoulder) * 255).astype(np.uint8)
def projection_guard_feather_influence(guard, width):
@@ -277,7 +282,7 @@ def projection_guard_feather_influence(guard, width):
return np.rint((1.0 - ramp) * 255).astype(np.uint8)
-def projection_occlusion_fields(depth, width, depth_threshold, object_ids=None, surface_distance=None):
+def projection_occlusion_fields(depth, width, depth_threshold, object_ids=None, surface_distance=None, *, include_legacy=False):
"""Fade only the far surface approaching a resolved depth discontinuity.
Unlike a part/margin guard, this field must survive coverage rescue. Carry
@@ -285,15 +290,25 @@ def projection_occlusion_fields(depth, width, depth_threshold, object_ids=None,
near surface (e.g. from the neck onto the visible chin). Object boundaries
with a resolved depth order catch thin nearby occluders below the general
depth threshold. Flat ID seams and foreground margins do not seed it.
+
+ With surface distance and include_legacy, also return the two legacy
+ fields before the geometry fields, sharing the same boundary transforms.
"""
+ if include_legacy and surface_distance is None:
+ raise ValueError('Combined occlusion fields require surface distance')
if depth.ndim == 3:
depth = cv2.cvtColor(depth, cv2.COLOR_BGR2GRAY)
depth = depth.astype(np.uint8)
width = max(0.0, float(width))
white = np.full(depth.shape, 255, dtype=np.uint8)
empty = np.zeros_like(depth)
+ def empty_fields():
+ geometry = (white, empty, np.zeros(depth.shape, np.float32))
+ if surface_distance is None:
+ return white, empty
+ return (white, empty) + geometry if include_legacy else geometry
if width == 0:
- return (white, empty, np.zeros(depth.shape, np.float32)) if surface_distance is not None else (white, empty)
+ return empty_fields()
threshold = max(0, int(depth_threshold))
near = cv2.dilate(depth, np.ones((3, 3), np.uint8))
seeds = near.astype(np.int16) - depth > threshold
@@ -306,20 +321,21 @@ def projection_occlusion_fields(depth, width, depth_threshold, object_ids=None,
interpolation=cv2.INTER_NEAREST)
padded_depth = np.pad(depth, 1, mode='edge')
padded_ids = np.pad(object_ids, 1, mode='edge')
+ depth_signed = depth.astype(np.int16)
object_near = depth.copy()
for dy, dx in ((dy, dx) for dy in range(3) for dx in range(3) if (dy, dx) != (1, 1)):
neighbor = padded_depth[dy:dy+depth.shape[0], dx:dx+depth.shape[1]]
ids = padded_ids[dy:dy+depth.shape[0], dx:dx+depth.shape[1]]
# Ignore a single quantization step and never infer ordering from
# IDs alone. Compare against the OTHER object's depth explicitly.
- ordered = (ids != object_ids) & (neighbor.astype(np.int16) - depth > 1)
- object_near = np.maximum(object_near, np.where(ordered, neighbor, 0))
+ ordered = (ids != object_ids) & (neighbor.astype(np.int16) - depth_signed > 1)
+ np.maximum(object_near, neighbor, out=object_near, where=ordered)
object_seeds = object_near.astype(np.int16) - depth > 1
near = np.where(object_seeds, object_near, near)
seed_threshold[object_seeds] = 1
seeds |= object_seeds
if not np.any(seeds):
- return (white, empty, np.zeros(depth.shape, np.float32)) if surface_distance is not None else (white, empty)
+ return empty_fields()
allowed = (~seeds).astype(np.uint8)
# Labels identify the occluder belonging to the nearest boundary pixel.
# Use the precise transform separately for the continuous feather width.
@@ -344,8 +360,12 @@ def projection_occlusion_fields(depth, width, depth_threshold, object_ids=None,
midpoint = (nearest + surface_distance) * 0.5
reference = np.zeros(int(labels.max()) + 1, dtype=np.float32)
reference[labels[seeds]] = midpoint[seeds]
- return (np.rint(fade * 255).astype(np.uint8),
- np.rint(influence * 255).astype(np.uint8), reference[labels])
+ geometry = (np.rint(fade * 255).astype(np.uint8),
+ np.rint(influence * 255).astype(np.uint8), reference[labels])
+ if not include_legacy:
+ return geometry
+ return (np.where(far_side, geometry[0], 255).astype(np.uint8),
+ np.where(far_side, geometry[1], 0).astype(np.uint8)) + geometry
return (np.where(far_side, np.rint(fade * 255), 255).astype(np.uint8),
np.where(far_side, np.rint(influence * 255), 0).astype(np.uint8))
diff --git a/stablegen/texturing/rendering.py b/stablegen/texturing/rendering.py
index 24be648..40aa93b 100644
--- a/stablegen/texturing/rendering.py
+++ b/stablegen/texturing/rendering.py
@@ -9,6 +9,7 @@ import mathutils
from ..utils import get_file_path, get_dir_path, get_compositor_node_tree, configure_output_node_paths, get_eevee_engine_id, sg_modal_active, remove_empty_dirs
from ..mesh_gen.rig_display import configure_gltf_not_exported_visibility
from .pose_lock import ensure_texturing_frame
+from .jobs import operation, RenderRequest
from PIL import Image
import cv2
@@ -168,6 +169,7 @@ class _RenderStateTransaction:
"exposure": scene.view_settings.exposure,
"gamma": scene.view_settings.gamma,
"cycles_samples": getattr(scene.cycles, "samples", None),
+ "cycles_use_denoising": getattr(scene.cycles, "use_denoising", None),
"cycles_device": getattr(scene.cycles, "device", None),
"cycles_shading_system": getattr(scene.cycles, "shading_system", None),
"cycles_use_osl": getattr(scene.cycles, "use_osl", None),
@@ -193,6 +195,10 @@ class _RenderStateTransaction:
self.compositor_ptrs = (
{node.as_pointer() for node in node_tree.nodes} if node_tree else set()
)
+ self.compositor_mutes = (
+ {node.as_pointer(): bool(node.mute) for node in node_tree.nodes}
+ if node_tree else {}
+ )
return self
def __exit__(self, exc_type, exc_value, traceback):
@@ -232,6 +238,10 @@ class _RenderStateTransaction:
for node in list(compositor_tree.nodes):
if node.as_pointer() not in self.compositor_ptrs:
compositor_tree.nodes.remove(node)
+ else:
+ # Setup can fail before the export enters its render
+ # finally block; restore outputs muted during preparation.
+ node.mute = self.compositor_mutes[node.as_pointer()]
scene.world = self.world
if self.world and self.world_state:
@@ -244,7 +254,8 @@ class _RenderStateTransaction:
node.inputs["Strength"].default_value = strength
scene.camera = state["camera"]
- scene.render.engine = state["engine"]
+ if scene.render.engine != state["engine"]:
+ scene.render.engine = state["engine"]
scene.render.film_transparent = state["film_transparent"]
scene.render.use_compositing = state["use_compositing"]
scene.render.filepath = state["filepath"]
@@ -261,6 +272,8 @@ class _RenderStateTransaction:
scene.view_settings.gamma = state["gamma"]
if state["cycles_samples"] is not None:
scene.cycles.samples = state["cycles_samples"]
+ if state["cycles_use_denoising"] is not None:
+ scene.cycles.use_denoising = state["cycles_use_denoising"]
if state["cycles_device"] is not None:
scene.cycles.device = state["cycles_device"]
if state["cycles_shading_system"] is not None:
@@ -892,7 +905,8 @@ def flatten_projection_material_for_refine(context, obj, baked_image_path):
print(f"[StableGen] Flattened ProjectionMaterial for '{obj.name}' to baked texture '{os.path.basename(baked_image_path)}'")
-def _export_emit_image_impl(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.5), view_transform='Standard', fallback_color=(0,0,0)):
+@operation
+def _export_emit_image_impl(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.5), view_transform='Standard', fallback_color=(0,0,0), restore_materials=True):
"""
Exports a emit-only render of the scene from a camera's perspective.
:param context: Blender context.
@@ -904,6 +918,7 @@ def _export_emit_image_impl(context, to_export, camera_id=None, bg_color=(0.5, 0
# Store original materials and create temporary ones
original_materials = {}
temporary_materials = {}
+ emission_materials = {}
# Check if there is BSDF applied
@@ -911,13 +926,21 @@ def _export_emit_image_impl(context, to_export, camera_id=None, bg_color=(0.5, 0
for obj in to_export:
# Store original materials
- original_materials[obj] = _snapshot_material_state(obj)
+ if restore_materials:
+ original_materials[obj] = _snapshot_material_state(obj)
# Copy active material and switch to it
mat = obj.active_material
if not mat:
continue
+ source_pointer = mat.as_pointer()
+ if source_pointer in emission_materials:
+ obj.data.materials.clear()
+ obj.data.materials.append(emission_materials[source_pointer])
+ yield None
+ continue
mat_copy = mat.copy()
+ emission_materials[source_pointer] = mat_copy
# Clear materials and assign temp material
obj.data.materials.clear()
@@ -1030,15 +1053,17 @@ def _export_emit_image_impl(context, to_export, camera_id=None, bg_color=(0.5, 0
bpy.context.scene.display_settings.display_device = 'sRGB'
bpy.context.scene.view_settings.view_transform = view_transform
context.scene.cycles.samples = 1 # Minimum samples for speed
- # Configure view layer settings for diffuse-only
+ # Only Emit and Environment feed this export. Denoising the unused
+ # Combined image is expensive and does not change either consumed pass.
+ # The enclosing render transaction restores the user's denoising flag.
+ context.scene.cycles.use_denoising = False
+ # Request only the two passes consumed below. In particular, saved
+ # Combined, Z, normal or index passes must not add work to every
+ # context/visibility render. The transaction restores each flag.
view_layer = context.view_layer
- view_layer.use_pass_diffuse_color = False
- view_layer.use_pass_diffuse_direct = False
- view_layer.use_pass_diffuse_indirect = False
-
- # Disable all other passes
- view_layer.use_pass_ambient_occlusion = False
- view_layer.use_pass_shadow = False
+ for name in _RenderStateTransaction._PASS_NAMES:
+ if hasattr(view_layer, name):
+ setattr(view_layer, name, name in {'use_pass_emit', 'use_pass_environment'})
view_layer.use_pass_emit = True
view_layer.use_pass_environment = True
@@ -1073,8 +1098,9 @@ def _export_emit_image_impl(context, to_export, camera_id=None, bg_color=(0.5, 0
links.new(mix_node.outputs[0], output_node.inputs[0])
# Render
+ yield None
try:
- bpy.ops.render.render(write_still=True)
+ yield RenderRequest()
finally:
_finish_compositor_render(node_tree, compositor_state)
@@ -1129,19 +1155,25 @@ def _export_emit_image_impl(context, to_export, camera_id=None, bg_color=(0.5, 0
print(f"[StableGen] Emmision render saved to: {os.path.join(output_dir, output_file)}.png")
+@operation
def export_emit_image(context, to_export, camera_id=None, bg_color=(0.5, 0.5, 0.5), view_transform='Standard', fallback_color=(0,0,0)):
- with _RenderStateTransaction(context, to_export):
+ # All material/mesh edits happen on isolated copies, so production slots
+ # must not be cleared and restored at the end of the transaction.
+ with _RenderStateTransaction(context):
with _temporary_render_copies(context, to_export) as (copies, _mapping):
- return _export_emit_image_impl(
+ return (yield from _export_emit_image_impl(
context,
copies,
camera_id=camera_id,
bg_color=bg_color,
view_transform=view_transform,
fallback_color=fallback_color,
- )
+ restore_materials=False,
+ _steps=True,
+ ))
+@operation
def _export_render_impl(
context, camera_id=None, output_dir=None, filename=None,
background_color=None,
@@ -1217,7 +1249,7 @@ def _export_render_impl(
# Render
try:
- bpy.ops.render.render(write_still=True)
+ yield RenderRequest()
finally:
_finish_compositor_render(node_tree, compositor_state)
@@ -1236,16 +1268,17 @@ def _export_render_impl(
context.scene.render.use_compositing = original_use_compositing
+@operation
def export_render(
context, camera_id=None, output_dir=None, filename=None,
background_color=None,
):
objects = [obj for obj in context.view_layer.objects if obj.type == 'MESH']
with _RenderStateTransaction(context, objects):
- return _export_render_impl(
+ return (yield from _export_render_impl(
context, camera_id=camera_id, output_dir=output_dir,
- filename=filename, background_color=background_color,
- )
+ filename=filename, background_color=background_color, _steps=True,
+ ))
def _export_viewport_impl(
@@ -1371,6 +1404,7 @@ def export_viewport(
filename=filename, background_color=background_color,
)
+@operation
def export_canny(context, camera_id=None, low_threshold=0, high_threshold=80):
"""
Uses export_render and openCV to generate a Canny edge detection image.
@@ -1381,7 +1415,7 @@ def export_canny(context, camera_id=None, low_threshold=0, high_threshold=80):
:return: None
"""
# Render the scene
- export_render(context, camera_id)
+ yield from export_render(context, camera_id, _steps=True)
# Load the rendered image
output_dir_render = get_dir_path(context, "misc")
@@ -1482,9 +1516,10 @@ def expand_mask_to_blocks(mask_file_path, block_size=8):
return None
+@operation
def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None,
prepare_only=False, camera_render=None, camera_id=None,
- output_object_name=None):
+ output_object_name=None, restore_materials=True):
"""
Exports the visibility of the mesh by temporarily altering the shading nodes.
:param context: Blender context.
@@ -1499,11 +1534,12 @@ def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None
# Store original materials and create temporary ones
original_materials = {}
temporary_materials = {}
+ prepared_materials = {}
def restore_prepared_materials():
for prepared_obj, material_state in original_materials.items():
_restore_material_state(prepared_obj, material_state)
- for temp_mat in temporary_materials.values():
+ for temp_mat in set(temporary_materials.values()):
if temp_mat and temp_mat.name in bpy.data.materials:
bpy.data.materials.remove(temp_mat)
@@ -1513,7 +1549,14 @@ def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None
return False
# Store original materials
- original_materials[obj] = _snapshot_material_state(obj)
+ if restore_materials:
+ original_materials[obj] = _snapshot_material_state(obj)
+ source_pointer = mat.as_pointer()
+ if not prepare_only and source_pointer in prepared_materials:
+ obj.data.materials.clear()
+ obj.data.materials.append(prepared_materials[source_pointer])
+ temporary_materials[obj] = prepared_materials[source_pointer]
+ return True
# Store original active mat
@@ -1574,6 +1617,7 @@ def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None
obj.data.materials.clear()
obj.data.materials.append(mat_copy)
temporary_materials[obj] = mat_copy
+ prepared_materials[source_pointer] = mat_copy
return True
# Determine which input to used based on existence of BSDF node before the output
@@ -1595,6 +1639,7 @@ def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None
obj.data.materials.clear()
obj.data.materials.append(mat_copy)
temporary_materials[obj] = mat_copy
+ prepared_materials[source_pointer] = mat_copy
# Add equal node between color mix and bsdf
equal = nodes.new("ShaderNodeMath")
@@ -1756,6 +1801,7 @@ def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None
if not prepare_material(obj):
restore_prepared_materials()
return False
+ yield None
if prepare_only:
# Debug mode: leave the visibility material applied for viewport inspection
@@ -1801,7 +1847,7 @@ def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None
else:
camera_render_index = camera_id
context.scene.camera = camera_render
- _export_emit_image_impl(context, to_export, camera_id=f"{camera_render_index}_visibility", bg_color=(1, 1, 1), view_transform='Raw', fallback_color=(1,1,1))
+ yield from _export_emit_image_impl(context, to_export, camera_id=f"{camera_render_index}_visibility", bg_color=(1, 1, 1), view_transform='Raw', fallback_color=(1,1,1), restore_materials=restore_materials, _steps=True)
# Restore original materials
restore_prepared_materials()
@@ -1809,24 +1855,28 @@ def _export_visibility_impl(context, to_export, obj=None, camera_visibility=None
return True
+@operation
def export_visibility(context, to_export, obj=None, camera_visibility=None,
prepare_only=False, camera_render=None, camera_id=None):
objects = [obj] if obj is not None else list(to_export or ())
if prepare_only:
- return _export_visibility_impl(
+ return (yield from _export_visibility_impl(
context, to_export, obj=obj, camera_visibility=camera_visibility,
prepare_only=True, camera_render=camera_render, camera_id=camera_id,
- )
- with _RenderStateTransaction(context, objects):
+ _steps=True,
+ ))
+ with _RenderStateTransaction(context):
with _temporary_render_copies(context, objects) as (copies, mapping):
mapped_obj = mapping.get(obj) if obj is not None else None
- return _export_visibility_impl(
+ return (yield from _export_visibility_impl(
context, copies, obj=mapped_obj,
camera_visibility=camera_visibility,
prepare_only=False, camera_render=camera_render,
camera_id=camera_id,
output_object_name=obj.name if obj is not None else None,
- )
+ restore_materials=False,
+ _steps=True,
+ ))
# =========================================================