Skip to content

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

pbr material decomposition - Marigold IID / StableDelight PBR extraction with batched model-first execution - NxN tiled super-resolution with cosine-blended stitching and selective albedo-only mode - Camera→world normal conversion - Visibility-weighted PBR projection onto Principled BSDF via cloned mix-trees - Orbit GIF: HDRI environment (fixed/counter-rotate/env-only), shadow catcher, denoiser - AddHDRI strength property, shared studio lighting helper

sakalond02c04c88aadb
diff --git a/installer.py b/installer.py
index f71b4c2..cc841da 100644
--- a/installer.py
+++ b/installer.py
@@ -485,6 +485,41 @@ DEPENDENCIES: Dict[str, Dict[str, Any]] = {
"target_path_relative": "models/diffusion_models", "filename": "svdq-int4_r128-qwen-image-edit-2509-lightning-4steps-251115.safetensors",
"license": "Apache 2.0", "size_mb": 12700, "packages": ["qwen_nunchaku"]
},
+ # --- PBR Decomposition (Marigold IID) ---
+ "cn_comfyui_marigold": {
+ "id": "cn_comfyui_marigold", "type": "node", "name": "ComfyUI-Marigold (IID/Depth/Normal)",
+ "git_url": "https://github.com/kijai/ComfyUI-Marigold.git",
+ "target_dir_relative": "custom_nodes",
+ "repo_name": "ComfyUI-Marigold",
+ "license": "GPL-3.0", "packages": ["pbr_marigold"],
+ "pip_packages": ["diffusers>=0.28"],
+ "run_install_script": True,
+ },
+ # --- StableDelight (specular-free albedo) ---
+ "cn_comfyui_stabledelight": {
+ "id": "cn_comfyui_stabledelight", "type": "node", "name": "ComfyUI StableDelight (Delighting)",
+ "git_url": "https://github.com/lldacing/ComfyUI_StableDelight_ll.git",
+ "target_dir_relative": "custom_nodes",
+ "repo_name": "ComfyUI_StableDelight_ll",
+ "license": "Apache-2.0", "packages": ["pbr_stabledelight"],
+ "post_clone_patches": [
+ {
+ "file": "nodes/BaseNode.py",
+ "marker": "local_files_only=False",
+ "anchor": "local_files_only=True",
+ "patch": "local_files_only=False",
+ "mode": "replace",
+ },
+ ],
+ },
+ "model_stabledelight": {
+ "id": "model_stabledelight", "type": "hf_model",
+ "name": "StableDelight Model (yoso-delight-v0-4-base)",
+ "hf_repo": "Stable-X/yoso-delight-v0-4-base",
+ "local_dir": "Stable-X--yoso-delight-v0-4-base",
+ "license": "Apache-2.0", "size_mb": 3300,
+ "packages": ["pbr_stabledelight"],
+ },
# --- TRELLIS.2 ---
"cn_trellis2": {
"id": "cn_trellis2", "type": "node", "name": "ComfyUI TRELLIS.2",
@@ -598,6 +633,19 @@ MENU_PACKAGES: Dict[str, Dict[str, Any]] = {
"description_suffix": "*Installs ComfyUI-TRELLIS2 custom node. Models are downloaded automatically on first use by the node.*\n"
" *LICENSE NOTICE: Only the 'Native (TRELLIS.2)' texture mode uses nvdiffrast/nvdiffrec*\n"
" *(NVIDIA Source Code License — non-commercial use only). See README.md for full details.*",
+ },
+ '9': {"name": "[PBR DECOMPOSITION] Marigold IID Node",
+ "tags": ["pbr_marigold"],
+ "size_gb": 0.01,
+ "description_suffix": "*Installs ComfyUI-Marigold custom node for PBR decomposition (albedo, roughness, metallic).*\n"
+ " *IID models (~2GB each) are downloaded automatically from HuggingFace on first use.*",
+ },
+ '10': {"name": "[PBR DECOMPOSITION] StableDelight Node + Model",
+ "tags": ["pbr_marigold", "pbr_stabledelight"],
+ "size_gb": 3.3,
+ "description_suffix": "*Installs ComfyUI_StableDelight_ll custom node + downloads the*\n"
+ " *Stable-X/yoso-delight-v0-4-base model (~3.3GB fp16) for specular-free albedo.*",
+ },
}
# --- Helper Functions ---
@@ -832,6 +880,70 @@ def download_file(item_details: Dict[str, Any], comfyui_path: Path):
print(f" An unexpected error occurred during download of {item_details['name']}: {e}")
+def download_hf_model(item_details: Dict[str, Any], comfyui_path: Path):
+ """Download a HuggingFace diffusers model repo into ComfyUI/models/diffusers/.
+
+ Uses ``huggingface_hub.snapshot_download`` if available, otherwise falls
+ back to ``huggingface-cli download``.
+
+ Required keys in *item_details*:
+ hf_repo – e.g. ``'Stable-X/yoso-delight-v0-4-base'``
+ local_dir – directory name under ``models/diffusers/``,
+ e.g. ``'Stable-X--yoso-delight-v0-4-base'``
+ """
+ hf_repo = item_details["hf_repo"]
+ local_dir_name = item_details["local_dir"]
+ target_dir = comfyui_path / "models" / "diffusers" / local_dir_name
+
+ if target_dir.is_dir() and (target_dir / "model_index.json").exists():
+ print(f"INFO: HuggingFace model '{hf_repo}' already exists at '{target_dir}'. Skipping.")
+ return
+
+ create_dir_if_not_exists(target_dir.parent)
+ print(f" Downloading HuggingFace model: {hf_repo} (~{item_details.get('size_mb', '?')} MB)")
+ print(f" License: {item_details.get('license', 'unknown')}")
+ print(f" Destination: {target_dir}")
+
+ # Try Python API first
+ try:
+ from huggingface_hub import snapshot_download
+ snapshot_download(
+ repo_id=hf_repo,
+ local_dir=str(target_dir),
+ local_dir_use_symlinks=False,
+ # Download config files + fp16 weight variant only (skip fp32)
+ allow_patterns=[
+ "*.json", "*.txt", "*.yaml", "*.md", "*.py", "*.model",
+ "*.fp16.safetensors", "*.fp16.bin",
+ "tokenizer/**",
+ ],
+ )
+ print(f" HuggingFace model download complete: {local_dir_name}")
+ return
+ except ImportError:
+ print(" huggingface_hub not available, trying CLI fallback...")
+ except Exception as e:
+ print(f" snapshot_download failed: {e}. Trying CLI fallback...")
+
+ # Fallback: huggingface-cli
+ try:
+ python_exe = find_comfyui_python(comfyui_path)
+ cmd = [
+ str(python_exe), "-m", "huggingface_hub.commands.huggingface_cli",
+ "download", hf_repo,
+ "--local-dir", str(target_dir),
+ ]
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=600)
+ if result.returncode == 0:
+ print(f" HuggingFace model download complete: {local_dir_name}")
+ else:
+ print(f" ERROR downloading HuggingFace model: {result.stderr}")
+ except Exception as e:
+ print(f" ERROR downloading HuggingFace model '{hf_repo}': {e}")
+ print(f" You can manually download it with:")
+ print(f" huggingface-cli download {hf_repo} --local-dir {target_dir}")
+
+
def clone_git_repo(item_details: Dict[str, Any], comfyui_path: Path):
git_url = item_details["git_url"]
target_parent_dir = comfyui_path / item_details["target_dir_relative"]
@@ -938,7 +1050,7 @@ def main():
while True:
display_menu(comfyui_base_path)
- choice = input("Enter your choice (1-8, or q to quit): ").strip().lower()
+ choice = input("Enter your choice (1-9, or q to quit): ").strip().lower()
if choice == 'q':
print("Exiting installer.")
@@ -1033,6 +1145,9 @@ def main():
elif item_details["type"] == "model":
target_full_path = comfyui_base_path / item_details["target_path_relative"] / item_details["filename"]
download_file(item_details, comfyui_base_path)
+ elif item_details["type"] == "hf_model":
+ target_full_path = comfyui_base_path / "models" / "diffusers" / item_details["local_dir"]
+ download_hf_model(item_details, comfyui_base_path)
processed_during_this_session.add(item_details["id"])
diff --git a/stablegen/__init__.py b/stablegen/__init__.py
index 026cc13..eb4c194 100644
--- a/stablegen/__init__.py
+++ b/stablegen/__init__.py
@@ -3076,6 +3076,192 @@ def register():
update=update_parameters
)
+ # ── PBR Decomposition (Marigold IID) ──────────────────────────────
+ bpy.types.Scene.pbr_decomposition = bpy.props.BoolProperty(
+ name="PBR Decomposition",
+ description="Run Marigold decomposition on each generated image to "
+ "produce PBR material maps (albedo, roughness, metallic, normal, depth)",
+ default=False,
+ update=update_parameters
+ )
+
+ # ── Albedo source ─────────────────────────────────────────────────
+ bpy.types.Scene.pbr_albedo_source = bpy.props.EnumProperty(
+ name="Albedo Source",
+ description="Model to use for extracting the Base Color / albedo map",
+ items=[
+ ('marigold', "Marigold IID (Flat Albedo)",
+ "True albedo from Marigold IID-Appearance — removes all lighting "
+ "but may lose texture detail"),
+ ('delight', "StableDelight (Delighted)",
+ "Specular-removed image via StableDelight — preserves diffuse "
+ "shading and texture detail, only strips highlights"),
+ ],
+ default='marigold',
+ update=update_parameters,
+ )
+
+ # ── Per-map enable toggles ────────────────────────────────────────
+ bpy.types.Scene.pbr_map_albedo = bpy.props.BoolProperty(
+ name="Albedo",
+ description="Extract albedo (diffuse colour without lighting) and use it as Base Color",
+ default=True,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_roughness = bpy.props.BoolProperty(
+ name="Roughness",
+ description="Extract roughness map (0 = mirror, 1 = rough)",
+ default=True,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_metallic = bpy.props.BoolProperty(
+ name="Metallic",
+ description="Extract metallic map (0 = dielectric, 1 = metal)",
+ default=True,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_normal = bpy.props.BoolProperty(
+ name="Normal",
+ description="Extract surface normal map for detail and bump. "
+ "Warning: may cause triangle artifacts on voxel-remeshed geometry",
+ default=True,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_normal_mode = bpy.props.EnumProperty(
+ name="Normal Mode",
+ description="How to apply the predicted normal map to the mesh",
+ items=[
+ ('world', "World Space",
+ "Camera-space normals are converted to world space. "
+ "Works on any geometry (no tangent frame needed). "
+ "Best general-purpose mode"),
+ ('bump', "Bump from Normal",
+ "Convert the normal map to a bump/height map. "
+ "Works on any geometry including voxel remesh, "
+ "but loses directional detail"),
+ ('tangent', "Tangent Space",
+ "Standard tangent-space normal map. "
+ "Best quality on properly UV-unwrapped meshes, "
+ "may show triangle artifacts on voxel-remeshed geometry"),
+ ],
+ default='world',
+ update=update_parameters,
+ )
+ bpy.types.Scene.pbr_normal_strength = bpy.props.FloatProperty(
+ name="Normal Strength",
+ description="How strongly the normal map perturbs the surface shading. "
+ "Lower values reduce triangle artifacts on poor topology",
+ default=1.0,
+ min=0.0,
+ max=2.0,
+ step=0.05,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_delight_strength = bpy.props.FloatProperty(
+ name="Delight Strength",
+ description="How strongly StableDelight removes specular reflections. "
+ "Lower values preserve more original texture detail. "
+ "1.0 = full delighting, 0.5 = subtle",
+ default=1.0,
+ min=0.01,
+ max=5.0,
+ step=0.1,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_map_depth = bpy.props.BoolProperty(
+ name="Depth / Height",
+ description="Extract depth map for displacement/height",
+ default=False,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_use_native_resolution = bpy.props.BoolProperty(
+ name="Use Native Resolution",
+ description="Process PBR maps at the image's native resolution (longest "
+ "edge, rounded to 64px). Produces sharper results but uses "
+ "more VRAM. When disabled, the fixed Processing Resolution is used",
+ default=True,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_tiling = bpy.props.EnumProperty(
+ name="Tiling",
+ description="Tile-based super-resolution for PBR maps. Each tile "
+ "is upscaled to the full image resolution before "
+ "processing, producing N\u00b2\u00d7 the effective detail",
+ items=[
+ ('off', "Off", "No tiling — process the full image in one pass"),
+ ('selective', "Selective", "Tile albedo only (StableDelight and/or "
+ "IID albedo). Normals, roughness, metallic "
+ "and depth are processed normally"),
+ ('all', "All", "Tile every PBR model including normals and depth"),
+ ],
+ default='selective',
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_grid = bpy.props.IntProperty(
+ name="Tile Grid",
+ description="N\u00d7N grid size for tiling. 2 = 4 tiles (4\u00d7 detail), "
+ "3 = 9 tiles (9\u00d7 detail), 4 = 16 tiles (16\u00d7 detail). "
+ "Processing time scales with N\u00b2",
+ default=2,
+ min=2,
+ max=4,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_tile_superres = bpy.props.BoolProperty(
+ name="Super Resolution",
+ description="When enabled, the stitched PBR maps are kept at the "
+ "upscaled tile resolution (~N\u00d7 the original image size). "
+ "When disabled (default), tiles are scaled back to the "
+ "original image resolution — still higher detail from "
+ "tiled processing, but matching the source texture size",
+ default=True,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_processing_resolution = bpy.props.IntProperty(
+ name="Processing Resolution",
+ description="Internal processing resolution for the Marigold model. "
+ "Output is upscaled back to the original resolution. "
+ "768 is the default for the model's training resolution",
+ default=768,
+ min=256,
+ max=2048,
+ step=64,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_denoise_steps = bpy.props.IntProperty(
+ name="Denoise Steps",
+ description="Number of denoising steps. "
+ "More steps = better quality but slower. 4 is a good balance",
+ default=4,
+ min=1,
+ max=50,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_ensemble_size = bpy.props.IntProperty(
+ name="Ensemble Size",
+ description="Number of ensemble predictions to average. "
+ "Higher = more stable results but linearly slower. "
+ "1 is usually sufficient",
+ default=1,
+ min=1,
+ max=10,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_replace_color_with_albedo = bpy.props.BoolProperty(
+ name="Use Albedo as Base Color",
+ description="Replace the projected colour texture with the albedo map. "
+ "This effectively delights the texture",
+ default=True,
+ update=update_parameters
+ )
+ bpy.types.Scene.pbr_auto_lighting = bpy.props.BoolProperty(
+ name="Studio Lighting",
+ description="Create a three-point studio lighting rig (key, fill, rim) "
+ "after PBR projection to showcase PBR materials",
+ default=False,
+ update=update_parameters
+ )
+
# Artifact Filtering
bpy.types.Scene.trellis2_artifact_laplacian_sigma = bpy.props.FloatProperty(
name="Laplacian Sigma",
@@ -3231,6 +3417,22 @@ def unregister():
del bpy.types.Scene.qwen_context_fallback_dilation
del bpy.types.Scene.qwen_timestep_zero_ref
+ # --- PBR Decomposition Properties ---
+ pbr_props = [
+ 'pbr_decomposition', 'pbr_albedo_source',
+ 'pbr_map_albedo', 'pbr_map_roughness', 'pbr_map_metallic',
+ 'pbr_map_normal', 'pbr_map_depth',
+ 'pbr_normal_mode', 'pbr_normal_strength',
+ 'pbr_delight_strength',
+ 'pbr_use_native_resolution', 'pbr_tiling', 'pbr_tile_grid', 'pbr_tile_superres',
+ 'pbr_processing_resolution', 'pbr_denoise_steps', 'pbr_ensemble_size',
+ 'pbr_replace_color_with_albedo', 'pbr_auto_lighting',
+ 'pbr_model_variant', # legacy, kept for compat
+ ]
+ for prop in pbr_props:
+ if hasattr(bpy.types.Scene, prop):
+ delattr(bpy.types.Scene, prop)
+
# --- TRELLIS.2 Properties ---
trellis2_props = [
'trellis2_available', 'show_trellis2_params', 'show_trellis2_advanced',
diff --git a/stablegen/generator.py b/stablegen/generator.py
index bba6bd2..b369dcc 100644
--- a/stablegen/generator.py
+++ b/stablegen/generator.py
@@ -37,6 +37,67 @@ def redraw_ui(context):
for area in context.screen.areas:
area.tag_redraw()
+
+def setup_studio_lighting(context, scale=1.0):
+ """Create a three-point studio lighting rig (key, fill, rim).
+
+ Re-usable from any generation mode (TRELLIS.2, PBR decomposition, etc.).
+
+ Args:
+ context: Blender context.
+ scale: Scene scale factor — lights are placed at ``scale * 2.5`` distance.
+ """
+ S = max(scale, 0.5)
+ dist = S * 2.5
+
+ light_defs = [
+ ("SG_Key", 200, 1.5 * S, (1.0, 0.96, 0.90), 45, 40),
+ ("SG_Fill", 80, 2.5 * S, (0.90, 0.94, 1.0), -60, 15),
+ ("SG_Rim", 120, 0.8 * S, (1.0, 1.0, 1.0), 170, 55),
+ ]
+
+ collection = context.collection
+ created = []
+
+ for name, power, size, color, az_deg, el_deg in light_defs:
+ old = bpy.data.objects.get(name)
+ if old:
+ bpy.data.objects.remove(old, do_unlink=True)
+
+ az = math.radians(az_deg)
+ el = math.radians(el_deg)
+ x = dist * math.cos(el) * math.sin(az)
+ y = -dist * math.cos(el) * math.cos(az)
+ z = dist * math.sin(el)
+
+ light_data = bpy.data.lights.new(name=name, type='AREA')
+ light_data.energy = power
+ light_data.size = size
+ light_data.color = color
+
+ light_obj = bpy.data.objects.new(name=name, object_data=light_data)
+ collection.objects.link(light_obj)
+
+ light_obj.location = (x, y, z)
+ direction = mathutils.Vector((0, 0, 0)) - mathutils.Vector((x, y, z))
+ rot = direction.to_track_quat('-Z', 'Y')
+ light_obj.rotation_euler = rot.to_euler()
+
+ created.append(light_obj)
+
+ print(f"[StableGen] Studio lighting created: {[o.name for o in created]}")
+ return created
+
+
+def _pbr_setup_studio_lights(context, to_texture):
+ """Calculate scene scale from target objects and set up studio lights."""
+ max_dim = 1.0
+ for obj in to_texture:
+ if hasattr(obj, 'dimensions'):
+ max_dim = max(max_dim, *obj.dimensions)
+ setup_studio_lighting(context, scale=max_dim)
+
+
class Regenerate(bpy.types.Operator):
"""Regenerate textures for selected cameras / viewpoints
- Works for sequential and separate generation modes
@@ -1057,6 +1118,7 @@ class ComfyUIGenerate(bpy.types.Operator):
:return: None
"""
self._error = None
+ self._pbr_maps = {} # camera_key → {map_name: file_path}
try:
# --- Render ControlNet / refine maps with live progress ---
self._prepare_maps(context)
@@ -1291,7 +1353,9 @@ class ComfyUIGenerate(bpy.types.Operator):
image_path = image_path.replace(".png", "_ipadapter.png")
with open(image_path, 'wb') as f:
f.write(image)
-
+
+ # ── PBR decomposition is deferred until after ALL cameras ──
+
# Sequential mode callback
if context.scene.generation_method == 'sequential':
self._stage = "Projecting Image"
@@ -1367,8 +1431,47 @@ class ComfyUIGenerate(bpy.types.Operator):
traceback.print_exc()
return
+ # ── PBR Decomposition (runs after ALL cameras are generated) ──
+ if getattr(context.scene, 'pbr_decomposition', False):
+ self._pbr_maps = {}
+ # Collect camera images that need PBR decomposition
+ camera_images = {} # cam_idx → image_path
+ num_cameras = len(self._cameras)
+ for cam_idx in range(num_cameras):
+ cam_image_path = get_file_path(
+ context, "generated", camera_id=cam_idx,
+ material_id=self._material_id,
+ )
+ if os.path.exists(cam_image_path):
+ # In project_only (reproject) mode, reuse existing PBR
+ # maps when all enabled maps are already on disk. This
+ # avoids re-running the slow ComfyUI decomposition.
+ if context.scene.generation_mode == 'project_only':
+ existing = self._find_existing_pbr_maps(
+ context, cam_idx)
+ if existing:
+ self._pbr_maps[cam_idx] = existing
+ print(f"[StableGen] Reusing existing PBR maps "
+ f"for camera {cam_idx}")
+ continue
+ print(f"[StableGen] PBR maps missing for camera "
+ f"{cam_idx}, running decomposition…")
+ camera_images[cam_idx] = cam_image_path
+ if camera_images:
+ self._run_pbr_decomposition_batched(context, camera_images)
+
def image_project_callback():
if context.scene.generation_method == 'sequential':
+ # In sequential mode, projection happened per-camera inside the loop.
+ # PBR is projected onto the existing material after all cameras.
+ if getattr(context.scene, 'pbr_decomposition', False) and hasattr(self, '_pbr_maps') and self._pbr_maps:
+ from .project import project_pbr_to_bsdf
+ project_pbr_to_bsdf(
+ context, self._to_texture, self._pbr_maps,
+ material_id=self._material_id
+ )
+ if getattr(context.scene, 'pbr_auto_lighting', False):
+ _pbr_setup_studio_lights(context, self._to_texture)
return None
self._stage = "Projecting Image"
redraw_ui(context)
@@ -1382,6 +1485,17 @@ class ComfyUIGenerate(bpy.types.Operator):
context, "generated_baked", object_name=obj.name, material_id=self._material_id
)
apply_uv_inpaint_texture(context, obj, texture_path)
+
+ # Project PBR maps onto material after color projection
+ if getattr(context.scene, 'pbr_decomposition', False) and hasattr(self, '_pbr_maps') and self._pbr_maps:
+ from .project import project_pbr_to_bsdf
+ project_pbr_to_bsdf(
+ context, self._to_texture, self._pbr_maps,
+ material_id=self._material_id
+ )
+ if getattr(context.scene, 'pbr_auto_lighting', False):
+ _pbr_setup_studio_lights(context, self._to_texture)
+
return None
if context.scene.view_blend_use_color_match and self._to_texture:
@@ -1478,6 +1592,625 @@ class ComfyUIGenerate(bpy.types.Operator):
if hasattr(self, '_uploaded_images_cache') and self._uploaded_images_cache is not None:
self._uploaded_images_cache.pop(os.path.abspath(image_path), None)
+ # ── PBR Decomposition ──────────────────────────────────────────────
+ def _find_existing_pbr_maps(self, context, camera_id):
+ """Check if all enabled PBR maps already exist on disk for a camera.
+
+ Returns a dict ``{map_name: file_path}`` when every required map
+ is present, or ``None`` if any enabled map is missing (the caller
+ should run decomposition in that case).
+ """
+ scene = context.scene
+ existing = {}
+
+ map_checks = []
+ if getattr(scene, 'pbr_map_albedo', True):
+ map_checks.append('albedo')
+ if getattr(scene, 'pbr_map_roughness', True):
+ map_checks.append('roughness')
+ if getattr(scene, 'pbr_map_metallic', True):
+ map_checks.append('metallic')
+ if getattr(scene, 'pbr_map_normal', True):
+ map_checks.append('normal')
+ if getattr(scene, 'pbr_map_depth', False):
+ map_checks.append('depth')
+
+ if not map_checks:
+ return None
+
+ for map_name in map_checks:
+ path = get_file_path(
+ context, "pbr", subtype=map_name,
+ camera_id=camera_id, material_id=self._material_id
+ )
+ if os.path.exists(path):
+ existing[map_name] = path
+ else:
+ return None # At least one required map is missing
+
+ return existing
+
+ # ── Tiled PBR processing ──────────────────────────────────────────
+
+ @staticmethod
+ def _create_tile_blend_mask(h, w, overlap_x, overlap_y,
+ fade_left, fade_right,
+ fade_top, fade_bottom):
+ """Return a float32 [H, W, 1] weight mask with cosine fade on inner edges.
+
+ Only the edges that border a neighbouring tile (indicated by the
+ ``fade_*`` booleans) get a cosine-weighted ramp.
+ ``overlap_x`` controls left/right ramp length,
+ ``overlap_y`` controls top/bottom ramp length.
+ Image-boundary edges stay at full weight (1.0).
+ """
+ mask = np.ones((h, w, 1), dtype=np.float32)
+
+ if overlap_x > 0:
+ ramp_x = np.linspace(0.0, np.pi, overlap_x, dtype=np.float32)
+ ramp_x = (1.0 - np.cos(ramp_x)) * 0.5 # 0 → 1 cosine ease
+ if fade_left and overlap_x <= w:
+ mask[:, :overlap_x, 0] *= ramp_x[np.newaxis, :]
+ if fade_right and overlap_x <= w:
+ mask[:, -overlap_x:, 0] *= ramp_x[np.newaxis, ::-1]
+
+ if overlap_y > 0:
+ ramp_y = np.linspace(0.0, np.pi, overlap_y, dtype=np.float32)
+ ramp_y = (1.0 - np.cos(ramp_y)) * 0.5
+ if fade_top and overlap_y <= h:
+ mask[:overlap_y, :, 0] *= ramp_y[:, np.newaxis]
+ if fade_bottom and overlap_y <= h:
+ mask[-overlap_y:, :, 0] *= ramp_y[::-1, np.newaxis]
+
+ return mask
+
+ def _process_model_tiled(self, context, image_path, model_name=None,
+ process_fn=None):
+ """Run a model on an N×N grid of overlapping tiles.
+
+ Each tile is **upscaled to the full image's longest edge** before
+ processing, so the model spends its full resolution budget on
+ only 1/N² of the spatial area — effectively N²× the detail.
+ The stitched output is at the **upscaled** resolution (~N× the
+ original), producing super-resolution PBR maps.
+
+ Args:
+ model_name: Passed to ``generate_pbr_maps()``.
+ process_fn: Optional callable ``(context, tile_path) → result``.
+ If given, called instead of ``generate_pbr_maps``.
+ May return ``bytes`` (single image) or ``list[bytes]``.
+
+ Returns the same ``list[bytes]`` as ``generate_pbr_maps()``.
+ """
+ import tempfile
+
+ OVERLAP = 64 # pixels each tile extends beyond its boundary
+ MIN_DIM = 256 # skip tiling if either dimension is too small
+
+ scene = context.scene
+ N = getattr(scene, 'pbr_tile_grid', 2)
+
+ src = Image.open(image_path)
+ W, H = src.size
+ longest = max(W, H)
+
+ if W < MIN_DIM or H < MIN_DIM:
+ src.close()
+ print(f"[StableGen] Image {W}×{H} too small for tiling, "
+ f"processing normally")
+ if process_fn is not None:
+ fb = process_fn(context, image_path)
+ return [fb] if isinstance(fb, bytes) else fb
+ return self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+
+ # Compute tile boundaries in original-image coordinates
+ x_bounds = [W * c // N for c in range(N + 1)] # [0, W/N, 2W/N, …, W]
+ y_bounds = [H * r // N for r in range(N + 1)]
+
+ tile_info = [] # list of (l, t, r, b, fade_left, fade_right, fade_top, fade_bottom)
+ for row in range(N):
+ for col in range(N):
+ l = x_bounds[col] - (OVERLAP if col > 0 else 0)
+ r = x_bounds[col+1] + (OVERLAP if col < N - 1 else 0)
+ t = y_bounds[row] - (OVERLAP if row > 0 else 0)
+ b = y_bounds[row+1] + (OVERLAP if row < N - 1 else 0)
+ # Clamp to image bounds
+ l = max(l, 0); r = min(r, W)
+ t = max(t, 0); b = min(b, H)
+ tile_info.append((
+ l, t, r, b,
+ col > 0, # fade_left
+ col < N - 1, # fade_right
+ row > 0, # fade_top
+ row < N - 1, # fade_bottom
+ ))
+
+ total_tiles = len(tile_info)
+
+ # Uniform scale factor — upscale tiles so longest edge ≈ full image
+ sample_tw = tile_info[0][2] - tile_info[0][0]
+ sample_th = tile_info[0][3] - tile_info[0][1]
+ sample_longest = max(sample_tw, sample_th)
+ scale = longest / sample_longest if sample_longest < longest else 1.0
+
+ # ── Process each tile ─────────────────────────────────
+ all_tile_results = [] # list of list[bytes]
+ tile_up_sizes = [] # (up_w, up_h) per tile
+ tmp_dir = tempfile.gettempdir()
+
+ for i, (l, t, r, b, fl, fr, ft, fb) in enumerate(tile_info):
+ tw, th = r - l, b - t
+ tile_img = src.crop((l, t, r, b))
+
+ tile_longest = max(tw, th)
+ if tile_longest < longest:
+ up_w = int(round(tw * scale))
+ up_h = int(round(th * scale))
+ tile_img = tile_img.resize(
+ (up_w, up_h), Image.LANCZOS)
+ print(f"[StableGen] Tile {i+1}/{total_tiles}: "
+ f"{tw}×{th} → {up_w}×{up_h} "
+ f"region ({l},{t})→({r},{b})")
+ else:
+ up_w, up_h = tw, th
+ print(f"[StableGen] Tile {i+1}/{total_tiles}: "
+ f"{tw}×{th} region ({l},{t})→({r},{b})")
+ tile_up_sizes.append((up_w, up_h))
+
+ tile_path = os.path.join(tmp_dir, f"sg_tile_{i}.png")
+ tile_img.save(tile_path)
+
+ if process_fn is not None:
+ result = process_fn(context, tile_path)
+ # Normalise to list[bytes] (StableDelight returns bytes)
+ if isinstance(result, bytes):
+ result = [result]
+ else:
+ result = self.workflow_manager.generate_pbr_maps(
+ context, tile_path, model_name=model_name,
+ force_native_resolution=True)
+
+ try:
+ os.remove(tile_path)
+ except OSError:
+ pass
+
+ if isinstance(result, dict):
+ print(f"[StableGen] Tile {i+1} failed, falling back "
+ f"to full-image processing")
+ src.close()
+ if process_fn is not None:
+ fb = process_fn(context, image_path)
+ return [fb] if isinstance(fb, bytes) else fb
+ return self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+
+ # Resize model output to expected upscaled tile dims
+ resized = []
+ for map_bytes in result:
+ map_img = Image.open(io.BytesIO(map_bytes))
+ if map_img.size != (up_w, up_h):
+ map_img = map_img.resize((up_w, up_h), Image.LANCZOS)
+ buf = io.BytesIO()
+ map_img.save(buf, format='PNG')
+ resized.append(buf.getvalue())
+ all_tile_results.append(resized)
+
+ src.close()
+
+ # ── Stitch tiles ──────────────────────────────────────
+ superres = getattr(scene, 'pbr_tile_superres', False)
+
+ if superres:
+ # Super-resolution: stitch at the upscaled tile size (~N× original)
+ out_W = int(round(W * scale))
+ out_H = int(round(H * scale))
+ scaled_overlap = int(round(OVERLAP * scale))
+ else:
+ # Original resolution: downscale tiles back before stitching
+ out_W, out_H = W, H
+ scaled_overlap = OVERLAP # overlap in original coords
+
+ num_maps = len(all_tile_results[0])
+ stitched = []
+
+ for map_idx in range(num_maps):
+ canvas = np.zeros((out_H, out_W, 3), dtype=np.float32)
+ weight = np.zeros((out_H, out_W, 1), dtype=np.float32)
+
+ for i, (l, t, r, b, fl, fr, ft, fb) in enumerate(tile_info):
+ tile_bytes = all_tile_results[i][map_idx]
+ tile_img = Image.open(io.BytesIO(tile_bytes)).convert('RGB')
+
+ if superres:
+ up_w, up_h = tile_up_sizes[i]
+ sl = int(round(l * scale))
+ st = int(round(t * scale))
+ sr = min(sl + up_w, out_W)
+ sb = min(st + up_h, out_H)
+ else:
+ # Downscale tile back to original crop dimensions
+ orig_w, orig_h = r - l, b - t
+ if tile_img.size != (orig_w, orig_h):
+ tile_img = tile_img.resize(
+ (orig_w, orig_h), Image.LANCZOS)
+ sl, st, sr, sb = l, t, r, b
+
+ tile_arr = np.asarray(tile_img, dtype=np.float32)
+ tile_arr = tile_arr[:sb - st, :sr - sl]
+
+ overlap_px = 2 * scaled_overlap
+ mask = self._create_tile_blend_mask(
+ sb - st, sr - sl,
+ overlap_x=overlap_px,
+ overlap_y=overlap_px,
+ fade_left=fl, fade_right=fr,
+ fade_top=ft, fade_bottom=fb)
+
+ canvas[st:sb, sl:sr] += tile_arr * mask
+ weight[st:sb, sl:sr] += mask
+
+ canvas /= np.maximum(weight, 1e-6)
+ canvas = np.clip(canvas, 0, 255).astype(np.uint8)
+
+ out_img = Image.fromarray(canvas, mode='RGB')
+ buf = io.BytesIO()
+ out_img.save(buf, format='PNG')
+ stitched.append(buf.getvalue())
+
+ sr_label = " (super-res)" if superres else ""
+ print(f"[StableGen] Stitched {N}×{N} output: {out_W}×{out_H}"
+ f"{sr_label}")
+
+ return stitched
+
+ def _convert_normals_cam_to_world(self, map_bytes, camera_id):
+ """Convert a Marigold camera-space normal map to world space.
+
+ Marigold normals are in camera space (X = right, Y = up,
+ Z = toward the camera). The PNG encodes them as
+ ``(N + 1) / 2``, mapping [-1, 1] to [0, 1].
+
+ This method:
+ 1. Decodes the PNG into an RGB float array.
+ 2. Converts [0, 1] → [-1, 1].
+ 3. Rotates each normal by the camera's world rotation
+ (Marigold OpenGL convention and Blender camera-local
+ convention agree: X-right, Y-up, Z-toward-viewer).
+ 4. Re-normalises and re-encodes to [0, 1] PNG bytes.
+
+ Returns PNG bytes, or ``None`` on failure.
+ """
+ try:
+ img = Image.open(io.BytesIO(map_bytes)).convert('RGB')
+ arr = np.asarray(img, dtype=np.float32) / 255.0 # [H, W, 3] in [0,1]
+
+ # Decode to [-1, 1]
+ normals = arr * 2.0 - 1.0 # [H, W, 3] (X, Y, Z) camera-space
+
+ # Marigold (OpenGL) convention:
+ # X = right, Y = up, Z = toward camera (out of screen)
+ # Blender camera-local convention:
+ # X = right, Y = up, Z = behind camera (camera looks -Z)
+ #
+ # These AGREE: a surface facing the camera has Z = +1 in
+ # both systems (+Z_local points toward the viewer).
+ # No axis flip is needed.
+
+ # Get camera rotation (camera-local → world)
+ cam = self._cameras[camera_id]
+ cam_rot = np.array(cam.matrix_world.to_3x3(), dtype=np.float32) # 3×3
+
+ # Rotate all normals: N_world = cam_rot @ N_local
+ # Reshape to [H*W, 3] for batch matrix multiply
+ H, W, _ = normals.shape
+ flat = normals.reshape(-1, 3) # [H*W, 3]
+ flat_world = (cam_rot @ flat.T).T # [H*W, 3]
+
+ # Re-normalise (avoid div-by-zero)
+ norms = np.linalg.norm(flat_world, axis=1, keepdims=True)
+ norms = np.maximum(norms, 1e-6)
+ flat_world /= norms
+
+ # Encode back to [0, 1] PNG
+ world_normals = flat_world.reshape(H, W, 3)
+ encoded = np.clip((world_normals + 1.0) / 2.0, 0.0, 1.0)
+ encoded_u8 = (encoded * 255.0).astype(np.uint8)
+
+ out_img = Image.fromarray(encoded_u8, mode='RGB')
+ buf = io.BytesIO()
+ out_img.save(buf, format='PNG')
+ print(f" Converted normal map to world space for camera {camera_id}")
+ return buf.getvalue()
+ except Exception as err:
+ print(f" Warning: camera→world normal conversion failed: {err}")
+ return None
+
+ def _run_pbr_decomposition_batched(self, context, camera_images):
+ """Run PBR decomposition **model-first** across all cameras.
+
+ Instead of iterating cameras → models (which forces repeated model
+ loading/unloading), this iterates models → cameras so each model's
+ weights are loaded once and reused for every camera image.
+
+ Args:
+ context: Blender context.
+ camera_images: ``{cam_idx: image_path}`` for cameras that need
+ processing.
+ """
+ scene = context.scene
+ self._stage = "PBR Decomposition"
+ self._progress = 0
+ num_cams = len(camera_images)
+ cam_ids = sorted(camera_images.keys())
+ print(f"[StableGen] Running batched PBR decomposition on "
+ f"{num_cams} camera(s)…")
+
+ # Free VRAM before loading PBR models — the previous generation
+ # model may still be cached, and Marigold/StableDelight need
+ # GPU memory.
+ try:
+ server_address = context.preferences.addons[
+ __package__].preferences.server_address
+ self.workflow_manager._flush_comfyui_vram(
+ server_address, retries=1, label="PBR pre-load")
+ except Exception as e:
+ print(f"[StableGen] VRAM flush before PBR failed (non-fatal): {e}")
+
+ want_albedo = getattr(scene, 'pbr_map_albedo', True)
+ want_roughness = getattr(scene, 'pbr_map_roughness', True)
+ want_metallic = getattr(scene, 'pbr_map_metallic', True)
+ want_normal = getattr(scene, 'pbr_map_normal', True)
+ want_depth = getattr(scene, 'pbr_map_depth', False)
+ albedo_source = getattr(scene, 'pbr_albedo_source', 'delight')
+
+ use_delight = (want_albedo and albedo_source == 'delight')
+
+ # ── Determine which Marigold models to run ────────────────────
+ need_appearance = (
+ (want_albedo and albedo_source == 'marigold')
+ or want_roughness
+ or want_metallic
+ )
+ need_normals = want_normal
+ need_depth = want_depth
+
+ models_to_run = []
+ if need_appearance:
+ models_to_run.append((
+ 'prs-eth/marigold-iid-appearance-v1-1',
+ ['albedo', 'material'],
+ ))
+ if need_normals:
+ models_to_run.append((
+ 'prs-eth/marigold-normals-lcm-v0-1',
+ ['normal'],
+ ))
+ if need_depth:
+ models_to_run.append((
+ 'prs-eth/marigold-depth-lcm-v1-0',
+ ['depth'],
+ ))
+
+ total_model_steps = len(models_to_run) + (1 if use_delight else 0)
+ if total_model_steps == 0:
+ print("[StableGen] No PBR maps enabled, skipping decomposition")
+ return
+
+ # Ensure every camera has an entry in _pbr_maps
+ for cam_idx in cam_ids:
+ if cam_idx not in self._pbr_maps:
+ self._pbr_maps[cam_idx] = {}
+
+ # ── Tiling settings ───────────────────────────────────────────
+ tiling_mode = getattr(scene, 'pbr_tiling', 'off')
+ tile_grid = getattr(scene, 'pbr_tile_grid', 2)
+
+ tile_model_keys = set()
+ if tiling_mode == 'selective':
+ tile_model_keys = {'appearance'}
+ elif tiling_mode == 'all':
+ tile_model_keys = {'appearance', 'normals', 'depth'}
+
+ model_step = 0
+
+ # ── StableDelight pass (all cameras) ──────────────────────────
+ if use_delight:
+ model_step += 1
+ tile_delight = tiling_mode in ('selective', 'all')
+ tile_label = f" (tiled {tile_grid}×{tile_grid})" if tile_delight else ""
+ print(f"[StableGen] Model {model_step}/{total_model_steps}: "
+ f"StableDelight{tile_label}")
+
+ for ci, cam_idx in enumerate(cam_ids):
+ image_path = camera_images[cam_idx]
+ self._stage = (f"PBR: StableDelight "
+ f"(cam {ci+1}/{num_cams})")
+ self._progress = 0
+ print(f" Camera {cam_idx} ({ci+1}/{num_cams})…")
+
+ if tile_delight:
+ result = self._process_model_tiled(
+ context, image_path,
+ process_fn=self.workflow_manager.generate_delight_map)
+ if isinstance(result, list) and result:
+ result = result[0]
+ else:
+ result = self.workflow_manager.generate_delight_map(
+ context, image_path
+ )
+
+ if isinstance(result, dict) and "error" in result:
+ print(f" StableDelight error: {result['error']}")
+ self._warning = f"StableDelight failed: {result['error']}"
+ elif isinstance(result, bytes):
+ pbr_path = get_file_path(
+ context, "pbr", subtype="albedo",
+ camera_id=cam_idx,
+ material_id=self._material_id)
+ try:
+ with open(pbr_path, 'wb') as f:
+ f.write(result)
+ self._pbr_maps[cam_idx]['albedo'] = pbr_path
+ print(f" Saved albedo (delight) → "
+ f"{os.path.basename(pbr_path)}")
+ except Exception as err:
+ print(f" Failed to save delight albedo: {err}")
+
+ # ── Marigold model passes (all cameras per model) ─────────────
+ for model_name, map_names in models_to_run:
+ model_step += 1
+ model_short = model_name.split('/')[-1]
+ should_tile = any(k in model_short for k in tile_model_keys)
+ is_iid = 'appearance' in model_short
+
+ tile_label = f" (tiled {tile_grid}×{tile_grid})" if should_tile else ""
+ print(f"[StableGen] Model {model_step}/{total_model_steps}: "
+ f"{model_name}{tile_label}")
+
+ for ci, cam_idx in enumerate(cam_ids):
+ image_path = camera_images[cam_idx]
+ self._stage = (f"PBR: {model_short} "
+ f"(cam {ci+1}/{num_cams})")
+ self._progress = 0
+
+ # ── Selective IID dual-run ─────────────────────────
+ dual_run_iid = (
+ should_tile and is_iid
+ and tiling_mode == 'selective'
+ and not use_delight
+ )
+
+ extra = " + untiled material" if dual_run_iid else ""
+ print(f" Camera {cam_idx} ({ci+1}/{num_cams}){extra}…")
+
+ if dual_run_iid:
+ result_tiled = self._process_model_tiled(
+ context, image_path, model_name)
+ result_untiled = self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+ if (isinstance(result_tiled, list)
+ and len(result_tiled) >= 2
+ and isinstance(result_untiled, list)
+ and len(result_untiled) >= 2):
+ result = [result_tiled[0], result_untiled[1]]
+ elif isinstance(result_tiled, dict):
+ result = result_tiled
+ elif isinstance(result_untiled, dict):
+ result = result_untiled
+ else:
+ result = (result_tiled
+ if isinstance(result_tiled, list)
+ else result_untiled)
+ elif should_tile and not (is_iid and use_delight):
+ result = self._process_model_tiled(
+ context, image_path, model_name)
+ else:
+ result = self.workflow_manager.generate_pbr_maps(
+ context, image_path, model_name=model_name)
+
+ if isinstance(result, dict) and "error" in result:
+ print(f" PBR model error: {result['error']}")
+ self._warning = (f"PBR decomposition failed: "
+ f"{result['error']}")
+ continue
+
+ # ── Save each output component ────────────────────
+ self._save_pbr_map_outputs(
+ context, result, map_names, cam_idx,
+ want_roughness=want_roughness,
+ want_metallic=want_metallic,
+ use_delight=use_delight)
+
+ print(f"[StableGen] PBR decomposition complete for "
+ f"{num_cams} camera(s)")
+
+ def _save_pbr_map_outputs(self, context, result, map_names, camera_id,
+ want_roughness=True, want_metallic=True,
+ use_delight=False):
+ """Save the output images from a single model run to disk.
+
+ Handles the IID material channel split (R=roughness, G=metallic)
+ and the camera→world normal conversion.
+
+ Args:
+ result: list of bytes (one per output map).
+ map_names: list of names corresponding to each output.
+ camera_id: Camera index.
+ want_roughness / want_metallic: Whether to save those channels.
+ use_delight: Whether StableDelight handles albedo (skip IID albedo).
+ """
+ scene = context.scene
+ for i, map_bytes in enumerate(result):
+ map_name = map_names[i] if i < len(map_names) else f"component_{i}"
+
+ # ── IID-Appearance "material" channel split ───────────
+ if map_name == 'material':
+ try:
+ mat_img = Image.open(io.BytesIO(map_bytes)).convert('RGB')
+ except Exception as err:
+ print(f" Failed to decode material image: {err}")
+ continue
+
+ if want_roughness:
+ rough_img = mat_img.getchannel('R').convert('L')
+ rough_path = get_file_path(
+ context, "pbr", subtype="roughness",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ try:
+ rough_img.save(rough_path)
+ self._pbr_maps[camera_id]['roughness'] = rough_path
+ print(f" Saved roughness (material R) → "
+ f"{os.path.basename(rough_path)}")
+ except Exception as err:
+ print(f" Failed to save roughness: {err}")
+
+ if want_metallic:
+ metal_img = mat_img.getchannel('G').convert('L')
+ metal_path = get_file_path(
+ context, "pbr", subtype="metallic",
+ camera_id=camera_id,
+ material_id=self._material_id)
+ try:
+ metal_img.save(metal_path)
+ self._pbr_maps[camera_id]['metallic'] = metal_path
+ print(f" Saved metallic (material G) → "
+ f"{os.path.basename(metal_path)}")
+ except Exception as err:
+ print(f" Failed to save metallic: {err}")
+ continue
+
+ # Skip maps the user didn't enable
+ toggle_attr = f"pbr_map_{map_name}"
+ if hasattr(scene, toggle_attr) and not getattr(scene, toggle_attr):
+ continue
+
+ # Skip Marigold albedo when StableDelight is handling it
+ if map_name == 'albedo' and use_delight:
+ continue
+
+ pbr_path = get_file_path(
+ context, "pbr", subtype=map_name,
+ camera_id=camera_id,
+ material_id=self._material_id)
+ try:
+ # ── Camera→world-space conversion for normals ─────
+ if map_name == 'normal':
+ converted = self._convert_normals_cam_to_world(
+ map_bytes, camera_id)
+ if converted is not None:
+ map_bytes = converted
+
+ with open(pbr_path, 'wb') as f:
+ f.write(map_bytes)
+ self._pbr_maps[camera_id][map_name] = pbr_path
+ print(f" Saved {map_name} → "
+ f"{os.path.basename(pbr_path)}")
+ except Exception as err:
+ print(f" Failed to save PBR map {map_name}: {err}")
+
def _apply_qwen_context_cleanup(self, context, image_bytes):
hue_tolerance = max(context.scene.qwen_context_cleanup_hue_tolerance, 0.0)
value_adjust = context.scene.qwen_context_cleanup_value_adjust
@@ -2644,49 +3377,7 @@ class Trellis2Generate(bpy.types.Operator):
# -----------------------------------------------------------------
def _setup_studio_lighting(self, context, import_scale):
"""Create a three-point studio lighting setup around the imported mesh."""
- S = max(import_scale, 0.5) # Avoid degenerate positions
- dist = S * 2.5 # Distance from origin
-
- # (name, watts, size, color_rgb, azimuth_deg, elevation_deg)
- light_defs = [
- ("SG_Key", 200, 1.5 * S, (1.0, 0.96, 0.90), 45, 40),
- ("SG_Fill", 80, 2.5 * S, (0.90, 0.94, 1.0), -60, 15),
- ("SG_Rim", 120, 0.8 * S, (1.0, 1.0, 1.0), 170, 55),
- ]
-
- collection = context.collection
- created = []
-
- for name, power, size, color, az_deg, el_deg in light_defs:
- # Remove any stale light with the same name
- old = bpy.data.objects.get(name)
- if old:
- bpy.data.objects.remove(old, do_unlink=True)
-
- az = math.radians(az_deg)
- el = math.radians(el_deg)
- x = dist * math.cos(el) * math.sin(az)
- y = -dist * math.cos(el) * math.cos(az) # -Y = Blender front
- z = dist * math.sin(el)
-
- light_data = bpy.data.lights.new(name=name, type='AREA')
- light_data.energy = power
- light_data.size = size
- light_data.color = color
-
- light_obj = bpy.data.objects.new(name=name, object_data=light_data)
- collection.objects.link(light_obj)
-
- light_obj.location = (x, y, z)
- # Aim at origin
- direction = mathutils.Vector((0, 0, 0)) - mathutils.Vector((x, y, z))
- rot = direction.to_track_quat('-Z', 'Y')
- light_obj.rotation_euler = rot.to_euler()
-
- created.append(light_obj)
-
- print(f"[TRELLIS2] Studio lighting created: {[o.name for o in created]}")
- return created
+ return setup_studio_lighting(context, scale=import_scale)
def _schedule_texture_generation(self, context):
"""Defer texture generation via a timer so Blender can digest the new cameras.
diff --git a/stablegen/project.py b/stablegen/project.py
index 7dcd157..dbeda14 100644
--- a/stablegen/project.py
+++ b/stablegen/project.py
@@ -1673,3 +1673,457 @@ def reinstate_compare_nodes(context, to_project, stop_id_mat_id_pairs):
# Connect script_node -> less_than_node -> original destination
links.new(from_socket, less_than_node.inputs[0])
links.new(less_than_node.outputs[0], to_socket)
+
+
+# ── PBR Projection ────────────────────────────────────────────────────
+
+
+def _clone_color_tree_for_pbr(nodes, links, source_node, cam_tex_to_pbr,
+ channel, y_offset):
+ """Recursively clone the colour mix-tree, sharing *Fac* connections and
+ swapping Image Texture leaves for their PBR counterparts.
+
+ Args:
+ nodes / links: ``mat.node_tree.nodes / links``.
+ source_node: current node being cloned (walk from BSDF backwards).
+ cam_tex_to_pbr: ``{color_tex_node: pbr_tex_node}``.
+ channel: PBR channel name (for default values).
+ y_offset: vertical pixel shift for cloned nodes.
+
+ Returns:
+ Output socket of the cloned sub-tree, or *None*.
+ """
+ # ── Leaf: Image Texture → swap for PBR variant ────────────────────
+ if source_node.type == 'TEX_IMAGE':
+ pbr_node = cam_tex_to_pbr.get(source_node)
+ return pbr_node.outputs[0] if pbr_node else None
+
+ # ── Resolve input names: ShaderNodeMixRGB vs ShaderNodeMix ────────
+ # Blender 4.0+ may use "A"/"B"/"Factor" instead of legacy names.
+ fac_name, c1_name, c2_name = "Fac", "Color1", "Color2"
+ if "Factor" in source_node.inputs and "A" in source_node.inputs:
+ fac_name, c1_name, c2_name = "Factor", "A", "B"
+
+ # ── MixRGB / Mix node → clone with same Fac ──────────────────────
+ is_mix = (source_node.type in ('MIX_RGB', 'MIX')
+ or (hasattr(source_node, 'bl_idname')
+ and source_node.bl_idname in (
+ 'ShaderNodeMixRGB', 'ShaderNodeMix'))
+ or (c1_name in source_node.inputs
+ and c2_name in source_node.inputs
+ and fac_name in source_node.inputs))
+ if is_mix:
+ c1_link = source_node.inputs[c1_name].links
+ c2_link = source_node.inputs[c2_name].links
+ c1_src = c1_link[0].from_node if c1_link else None
+ c2_src = c2_link[0].from_node if c2_link else None
+
+ c1_pbr = (_clone_color_tree_for_pbr(
+ nodes, links, c1_src, cam_tex_to_pbr, channel, y_offset)
+ if c1_src else None)
+ c2_pbr = (_clone_color_tree_for_pbr(
+ nodes, links, c2_src, cam_tex_to_pbr, channel, y_offset)
+ if c2_src else None)
+
+ if c1_pbr is None and c2_pbr is None:
+ return None
+
+ new_mix = nodes.new("ShaderNodeMixRGB")
+ new_mix.location = (source_node.location[0],
+ source_node.location[1] + y_offset)
+ new_mix.label = f"PBR_{channel}"
+
+ # Share the Fac (visibility weight) connection
+ fac_links = source_node.inputs[fac_name].links
+ if fac_links:
+ links.new(fac_links[0].from_socket, new_mix.inputs["Fac"])
+ else:
+ new_mix.inputs["Fac"].default_value = (
+ source_node.inputs[fac_name].default_value)
+
+ # Connect children
+ if c1_pbr:
+ links.new(c1_pbr, new_mix.inputs["Color1"])
+ else:
+ # Sensible defaults for uncovered areas
+ defaults = {
+ 'roughness': (0.5, 0.5, 0.5, 1.0),
+ 'metallic': (0.0, 0.0, 0.0, 1.0),
+ 'albedo': (0.5, 0.5, 0.5, 1.0),
+ 'residual': (0.0, 0.0, 0.0, 1.0),
+ 'shading': (0.5, 0.5, 0.5, 1.0),
+ }
+ new_mix.inputs["Color1"].default_value = defaults.get(
+ channel, (0.0, 0.0, 0.0, 1.0))
+
+ if c2_pbr:
+ links.new(c2_pbr, new_mix.inputs["Color2"])
+ else:
+ # For Color2 (the "fallback" side of the mix) we inherit the
+ # original node's default so that areas not covered by any
+ # camera projection keep the scene's fallback colour. Only
+ # data channels (roughness, metallic, …) use a hardcoded
+ # neutral, because the original tree's fallback colour would
+ # be nonsensical for those channels.
+ defaults = {
+ 'roughness': (0.5, 0.5, 0.5, 1.0),
+ 'metallic': (0.0, 0.0, 0.0, 1.0),
+ 'residual': (0.0, 0.0, 0.0, 1.0),
+ 'shading': (0.5, 0.5, 0.5, 1.0),
+ }
+ new_mix.inputs["Color2"].default_value = defaults.get(
+ channel, source_node.inputs[c2_name].default_value)
+
+ return new_mix.outputs[0]
+
+ # ── Unknown node → follow first connected input ───────────────────
+ for inp in source_node.inputs:
+ if inp.links:
+ result = _clone_color_tree_for_pbr(
+ nodes, links, inp.links[0].from_node,
+ cam_tex_to_pbr, channel, y_offset)
+ if result:
+ return result
+ return None
+
+
+def project_pbr_to_bsdf(context, to_texture, pbr_maps, material_id=None):
+ """Project PBR decomposition maps through the same camera UV projections
+ as the colour texture and wire them to the Principled BSDF.
+
+ This function is called **after** ``project_image()`` has finished
+ building the full colour projection material (including UV Project
+ attributes and the visibility-weighted mix tree).
+
+ For each PBR channel it:
+
+ 1. Creates Image Texture nodes that re-use the per-camera UV attributes
+ already baked by ``project_image()``.
+ 2. Clones the colour MixRGB binary tree, sharing the **Fac** (weight)
+ connections so visibility blending is identical.
+ 3. Wires the cloned tree's output to the corresponding Principled BSDF
+ input (Roughness, Metallic, Base Color, etc.).
+
+ Args:
+ context: Blender context.
+ to_texture: List of mesh objects that were textured.
+ pbr_maps: ``{camera_idx: {map_name: file_path}, …}``.
+ material_id: Material slot index used during generation.
+ """
+ scene = context.scene
+
+ if not pbr_maps:
+ return
+
+ # Build channel → BSDF input map from enabled per-map toggles
+ channel_wiring = {} # map_name → BSDF input name
+ special_wiring = {} # map_name → special handler ('normal' | 'displacement')
+
+ if getattr(scene, 'pbr_replace_color_with_albedo', True) and getattr(scene, 'pbr_map_albedo', True):
+ channel_wiring['albedo'] = 'Base Color'
+ if getattr(scene, 'pbr_map_roughness', True):
+ channel_wiring['roughness'] = 'Roughness'
+ if getattr(scene, 'pbr_map_metallic', True):
+ channel_wiring['metallic'] = 'Metallic'
+ if getattr(scene, 'pbr_map_normal', True):
+ special_wiring['normal'] = 'normal'
+ if getattr(scene, 'pbr_map_depth', False):
+ special_wiring['depth'] = 'displacement'
+
+ processed_materials = set()
+
+ for obj in to_texture:
+ if not hasattr(obj, 'active_material') or not obj.active_material:
+ continue
+ mat = obj.active_material
+ if mat.name in processed_materials:
+ continue
+ if not mat.use_nodes:
+ continue
+
+ nodes = mat.node_tree.nodes
+ links = mat.node_tree.links
+
+ # ── Find or create the Principled BSDF ────────────────────────────
+ principled = None
+ for node in nodes:
+ if node.type == 'BSDF_PRINCIPLED':
+ principled = node
+ break
+
+ if principled is None:
+ # No BSDF exists (apply_bsdf is probably False). Create one
+ # and rewire the existing colour output through it so that PBR
+ # channels have somewhere to connect.
+ output_node = None
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output_node = node
+ break
+ if output_node is None:
+ continue
+
+ principled = nodes.new("ShaderNodeBsdfPrincipled")
+ principled.inputs["Roughness"].default_value = 1.0
+
+ # Rewire: existing surface source → BSDF Base Color → Output
+ surface_links = output_node.inputs["Surface"].links
+ if surface_links:
+ old_source = surface_links[0].from_socket
+ # Position BSDF between old source and output
+ principled.location = (
+ output_node.location[0] - 300,
+ output_node.location[1])
+ links.new(old_source, principled.inputs["Base Color"])
+ else:
+ principled.location = (
+ output_node.location[0] - 300,
+ output_node.location[1])
+
+ links.new(principled.outputs[0],
+ output_node.inputs["Surface"])
+
+ # ── Discover existing camera Image Texture nodes ──────────────
+ # Labels follow the pattern "{cam_idx}-{mat_id}" set by
+ # project_image().
+ camera_tex_nodes = {} # cam_idx → tex_node
+ for node in nodes:
+ if node.type == 'TEX_IMAGE' and node.label and '-' in node.label:
+ parts = node.label.split('-')
+ try:
+ cam_idx = int(parts[0])
+ camera_tex_nodes[cam_idx] = node
+ except ValueError:
+ continue
+
+ if not camera_tex_nodes:
+ continue
+
+ # ── Find the root of the colour mix tree ─────────────────────
+ color_root = None
+ if principled.inputs["Base Color"].links:
+ color_root = principled.inputs["Base Color"].links[0].from_node
+
+ # ── For each PBR channel, create textures and clone the tree ──
+ channel_idx = 0
+ for channel, bsdf_input in channel_wiring.items():
+ if bsdf_input not in principled.inputs:
+ continue
+
+ # Build mapping: colour_tex_node → PBR_tex_node
+ cam_tex_to_pbr = {}
+ for cam_idx, color_tex in camera_tex_nodes.items():
+ if cam_idx not in pbr_maps:
+ continue
+ cam_pbr = pbr_maps[cam_idx]
+ if channel not in cam_pbr:
+ continue
+
+ pbr_path = cam_pbr[channel]
+ if not os.path.exists(pbr_path):
+ continue
+
+ pbr_img = get_or_load_image(pbr_path, force_reload=True)
+ if not pbr_img:
+ continue
+
+ # Non-Color for data maps
+ if channel in ('roughness', 'metallic'):
+ pbr_img.colorspace_settings.name = 'Non-Color'
+
+ pbr_tex = nodes.new("ShaderNodeTexImage")
+ pbr_tex.image = pbr_img
+ pbr_tex.extension = 'CLIP'
+ pbr_tex.label = (
+ f"PBR_{channel}_{cam_idx}_{material_id}")
+ pbr_tex.location = (
+ color_tex.location[0],
+ color_tex.location[1]
+ - 200 * (1 + channel_idx) * len(camera_tex_nodes))
+
+ # Re-use the same camera UV attribute
+ if color_tex.inputs["Vector"].links:
+ uv_socket = (
+ color_tex.inputs["Vector"].links[0].from_socket)
+ links.new(uv_socket, pbr_tex.inputs["Vector"])
+
+ cam_tex_to_pbr[color_tex] = pbr_tex
+
+ if not cam_tex_to_pbr:
+ continue
+
+ # Clone the colour mix tree (or fall back to direct connection)
+ pbr_final_output = None
+ if color_root:
+ y_off = -500 * (1 + channel_idx)
+ pbr_output = _clone_color_tree_for_pbr(
+ nodes, links, color_root, cam_tex_to_pbr,
+ channel, y_off)
+ if pbr_output:
+ pbr_final_output = pbr_output
+ else:
+ # No colour tree exists; connect first PBR texture directly
+ first_pbr = next(iter(cam_tex_to_pbr.values()))
+ pbr_final_output = first_pbr.outputs["Color"]
+
+ if pbr_final_output:
+ links.new(pbr_final_output,
+ principled.inputs[bsdf_input])
+
+ channel_idx += 1
+
+ # ── Special wiring: Normal Map + Displacement ─────────────────
+ for sp_channel, sp_type in special_wiring.items():
+ # Build PBR texture nodes for this special channel
+ sp_tex_map = {} # color_tex_node → pbr_tex_node
+ for cam_idx, color_tex in camera_tex_nodes.items():
+ if cam_idx not in pbr_maps:
+ continue
+ cam_pbr = pbr_maps[cam_idx]
+ if sp_channel not in cam_pbr:
+ continue
+ sp_path = cam_pbr[sp_channel]
+ if not os.path.exists(sp_path):
+ continue
+ sp_img = get_or_load_image(sp_path, force_reload=True)
+ if not sp_img:
+ continue
+ sp_img.colorspace_settings.name = 'Non-Color'
+
+ sp_tex = nodes.new("ShaderNodeTexImage")
+ sp_tex.image = sp_img
+ sp_tex.extension = 'CLIP'
+ sp_tex.label = f"PBR_{sp_channel}_{cam_idx}_{material_id}"
+ sp_tex.location = (
+ color_tex.location[0],
+ color_tex.location[1]
+ - 200 * (2 + channel_idx) * len(camera_tex_nodes))
+ if color_tex.inputs["Vector"].links:
+ uv_socket = color_tex.inputs["Vector"].links[0].from_socket
+ links.new(uv_socket, sp_tex.inputs["Vector"])
+ sp_tex_map[color_tex] = sp_tex
+
+ if not sp_tex_map:
+ continue
+
+ # Clone the colour tree for this special channel
+ sp_output = None
+ if color_root:
+ y_off = -500 * (2 + channel_idx)
+ sp_output = _clone_color_tree_for_pbr(
+ nodes, links, color_root, sp_tex_map,
+ sp_channel, y_off)
+ else:
+ first_sp = next(iter(sp_tex_map.values()))
+ sp_output = first_sp.outputs["Color"]
+
+ if sp_output is None:
+ continue
+
+ if sp_type == 'normal':
+ normal_strength = getattr(scene, 'pbr_normal_strength', 0.5)
+ normal_mode = getattr(scene, 'pbr_normal_mode', 'world')
+
+ if normal_mode == 'world':
+ # ── World-space Normal Map ────────────────────────
+ # The normal images were already converted from
+ # camera-space to world-space in
+ # _convert_normals_cam_to_world(). Using the
+ # WORLD space mode of the Normal Map node means
+ # we don't rely on tangent frames, so this works
+ # correctly on voxel-remeshed geometry.
+ normal_map_node = nodes.new("ShaderNodeNormalMap")
+ normal_map_node.space = 'WORLD'
+ normal_map_node.location = (
+ principled.location[0] - 300,
+ principled.location[1] - 400)
+ normal_map_node.label = "PBR Normal Map (World)"
+ normal_map_node.inputs["Strength"].default_value = (
+ normal_strength)
+ links.new(sp_output, normal_map_node.inputs["Color"])
+ if "Normal" in principled.inputs:
+ links.new(normal_map_node.outputs["Normal"],
+ principled.inputs["Normal"])
+
+ elif normal_mode == 'bump':
+ # ── Bump from Normal ──────────────────────────────
+ # Convert the normal map to a height signal by
+ # extracting luminance, then drive a Bump node.
+ # This avoids tangent-space dependency so it works
+ # on voxel-remeshed geometry without triangle seams.
+ sep_node = nodes.new("ShaderNodeSeparateColor")
+ sep_node.location = (
+ principled.location[0] - 600,
+ principled.location[1] - 400)
+ sep_node.label = "PBR Normal → Channels"
+ links.new(sp_output, sep_node.inputs["Color"])
+
+ bump_node = nodes.new("ShaderNodeBump")
+ bump_node.location = (
+ principled.location[0] - 300,
+ principled.location[1] - 400)
+ bump_node.label = "PBR Bump from Normal"
+ bump_node.inputs["Strength"].default_value = normal_strength
+ bump_node.inputs["Distance"].default_value = 0.02
+ # Use the Blue channel (Z/facing component) inverted
+ # as a height proxy: flat areas → high blue → low
+ # height; angled areas → low blue → high height.
+ invert_z = nodes.new("ShaderNodeMath")
+ invert_z.operation = 'SUBTRACT'
+ invert_z.inputs[0].default_value = 1.0
+ invert_z.location = (
+ principled.location[0] - 450,
+ principled.location[1] - 400)
+ invert_z.label = "Invert Z"
+ links.new(sep_node.outputs["Blue"],
+ invert_z.inputs[1])
+ links.new(invert_z.outputs["Value"],
+ bump_node.inputs["Height"])
+
+ if "Normal" in principled.inputs:
+ links.new(bump_node.outputs["Normal"],
+ principled.inputs["Normal"])
+ else:
+ # ── Tangent-space Normal Map ──────────────────────
+ normal_map_node = nodes.new("ShaderNodeNormalMap")
+ normal_map_node.location = (
+ principled.location[0] - 300,
+ principled.location[1] - 400)
+ normal_map_node.label = "PBR Normal Map"
+ normal_map_node.inputs["Strength"].default_value = (
+ normal_strength)
+ links.new(sp_output, normal_map_node.inputs["Color"])
+ if "Normal" in principled.inputs:
+ links.new(normal_map_node.outputs["Normal"],
+ principled.inputs["Normal"])
+
+ elif sp_type == 'displacement':
+ # Create Displacement node → Material Output Displacement
+ output_node = None
+ for node in nodes:
+ if node.type == 'OUTPUT_MATERIAL':
+ output_node = node
+ break
+ if output_node and "Displacement" in output_node.inputs:
+ disp_node = nodes.new("ShaderNodeDisplacement")
+ disp_node.location = (
+ output_node.location[0] - 300,
+ output_node.location[1] - 200)
+ disp_node.label = "PBR Displacement"
+ disp_node.inputs["Scale"].default_value = 0.1
+ disp_node.inputs["Midlevel"].default_value = 0.5
+ links.new(sp_output, disp_node.inputs["Height"])
+ links.new(disp_node.outputs["Displacement"],
+ output_node.inputs["Displacement"])
+
+ channel_idx += 1
+
+ # ── Set BSDF default values ───────────────────────────────────
+ if 'roughness' in channel_wiring:
+ principled.inputs["Roughness"].default_value = 1.0
+ if 'metallic' in channel_wiring:
+ principled.inputs["Metallic"].default_value = 0.0
+
+ processed_materials.add(mat.name)
+
+
diff --git a/stablegen/render_tools.py b/stablegen/render_tools.py
index f27008f..ad62633 100644
--- a/stablegen/render_tools.py
+++ b/stablegen/render_tools.py
@@ -5007,6 +5007,51 @@ class ExportOrbitGIF(bpy.types.Operator):
default='LINEAR'
) # type: ignore
+ # ── PBR showcase settings ─────────────────────────────────────────
+ use_hdri: bpy.props.BoolProperty(
+ name="HDRI Environment",
+ description="Use the scene's existing HDRI world (set up via the "
+ "Add HDRI operator) for realistic environment lighting",
+ default=False
+ ) # type: ignore
+
+ hdri_rotation: bpy.props.FloatProperty(
+ name="Environment Rotation",
+ description="Initial Z-rotation offset for the HDRI (degrees)",
+ default=0.0,
+ min=0.0,
+ max=360.0,
+ subtype='ANGLE'
+ ) # type: ignore
+
+ env_mode: bpy.props.EnumProperty(
+ name="Environment Mode",
+ description="How the environment lighting interacts with the orbit",
+ items=[
+ ('FIXED', "Fixed",
+ "Camera orbits, HDRI stays fixed — reflections shift naturally"),
+ ('COUNTER', "Counter-Rotate",
+ "HDRI rotates opposite to camera — 2× apparent light change"),
+ ('ENV_ONLY', "Environment Only",
+ "Camera stays fixed, only the HDRI rotates around the model"),
+ ],
+ default='FIXED'
+ ) # type: ignore
+
+ use_shadow_plane: bpy.props.BoolProperty(
+ name="Ground Shadow",
+ description="Add a temporary shadow-catcher plane beneath the "
+ "model for a grounded appearance (Cycles only)",
+ default=False
+ ) # type: ignore
+
+ use_denoiser: bpy.props.BoolProperty(
+ name="Denoise",
+ description="Enable Cycles denoising for cleaner specular "
+ "highlights with fewer samples",
+ default=True
+ ) # type: ignore
+
_timer = None
_rendering = False
_cancelled = False # Added flag to track cancellation
@@ -5014,6 +5059,9 @@ class ExportOrbitGIF(bpy.types.Operator):
_handle_cancel = None
_initial_settings = {}
_temp_empty = None # Added for the pivot empty
+ _temp_shadow_plane = None # Added for shadow catcher
+ _env_mapping_node = None # For HDRI rotation animation
+ _original_world_data = None # Store entire world setup for restore
_output_path = "" # Internal variable to store the final GIF path
_output_path_mp4 = "" # Internal variable to store the final MP4 path
_temp_dir = "" # Temporary directory for frames
@@ -5114,6 +5162,198 @@ class ExportOrbitGIF(bpy.types.Operator):
for kf in fc.keyframe_points:
kf.interpolation = interpolation_type
+ # ── PBR Showcase helpers ──────────────────────────────────────────
+
+ def _setup_hdri_environment(self, context):
+ """Prepare the scene's existing HDRI world for orbit rendering.
+
+ Assumes the world was already set up by the AddHDRI operator
+ (or any setup with an Environment Texture node). This method:
+
+ 1. Stores the original world state for ``cleanup()``.
+ 2. Injects a Mapping node before the Environment Texture to
+ allow rotation animation (counter-rotate / env-only modes).
+ 3. Applies the user's initial rotation offset.
+
+ If no Environment Texture node is found, falls back to creating
+ a procedural Nishita sky with a Mapping node.
+ """
+ world = context.scene.world
+ if not world:
+ world = bpy.data.worlds.new("SG_OrbitWorld")
+ context.scene.world = world
+
+ # Store original state for restore
+ self._original_world_data = {
+ 'world_ref': world,
+ 'use_nodes': world.use_nodes,
+ 'color': world.color.copy(),
+ }
+
+ world.use_nodes = True
+ tree = world.node_tree
+
+ # Find the existing Environment Texture node
+ env_tex = None
+ for node in tree.nodes:
+ if node.type == 'TEX_ENVIRONMENT':
+ env_tex = node
+ break
+
+ if env_tex is None:
+ # No HDRI world set up — create a procedural sky fallback
+ print("[StableGen] Orbit GIF: No HDRI found, creating sky fallback")
+ tree.nodes.clear()
+
+ tex_coord = tree.nodes.new("ShaderNodeTexCoord")
+ tex_coord.location = (-800, 300)
+
+ mapping = tree.nodes.new("ShaderNodeMapping")
+ mapping.location = (-600, 300)
+ mapping.inputs["Rotation"].default_value[2] = self.hdri_rotation
+ self._env_mapping_node = mapping
+
+ sky_tex = tree.nodes.new("ShaderNodeTexSky")
+ sky_tex.location = (-300, 300)
+ sky_tex.sky_type = 'NISHITA'
+ sky_tex.sun_elevation = math.radians(30)
+ sky_tex.sun_rotation = math.radians(45)
+
+ bg_node = tree.nodes.new("ShaderNodeBackground")
+ bg_node.location = (0, 300)
+
+ output_node = tree.nodes.new("ShaderNodeOutputWorld")
+ output_node.location = (200, 300)
+
+ tree.links.new(tex_coord.outputs["Generated"],
+ mapping.inputs["Vector"])
+ tree.links.new(mapping.outputs["Vector"],
+ sky_tex.inputs["Vector"])
+ tree.links.new(sky_tex.outputs["Color"],
+ bg_node.inputs["Color"])
+ tree.links.new(bg_node.outputs["Background"],
+ output_node.inputs["Surface"])
+
+ # Track that we created the fallback so cleanup can remove it
+ self._original_world_data['created_fallback'] = True
+ return
+
+ # ── Inject Mapping node before the Environment Texture ────────
+ # Store original Vector input link/socket for restore
+ existing_vec_link = None
+ if env_tex.inputs["Vector"].links:
+ existing_vec_link = env_tex.inputs["Vector"].links[0]
+ self._original_world_data['env_vec_from_socket'] = (
+ existing_vec_link.from_socket)
+
+ # Create TexCoord + Mapping chain
+ tex_coord = tree.nodes.new("ShaderNodeTexCoord")
+ tex_coord.name = "SG_OrbitTexCoord"
+ tex_coord.location = (env_tex.location[0] - 500,
+ env_tex.location[1])
+
+ mapping = tree.nodes.new("ShaderNodeMapping")
+ mapping.name = "SG_OrbitMapping"
+ mapping.location = (env_tex.location[0] - 250,
+ env_tex.location[1])
+ mapping.inputs["Rotation"].default_value[2] = self.hdri_rotation
+ self._env_mapping_node = mapping
+
+ tree.links.new(tex_coord.outputs["Generated"],
+ mapping.inputs["Vector"])
+
+ # Remove existing vector link and insert mapping before env tex
+ if existing_vec_link:
+ # Rewire: old_source → Mapping.Vector, Mapping.out → EnvTex.Vector
+ old_source = existing_vec_link.from_socket
+ tree.links.remove(existing_vec_link)
+ tree.links.new(old_source, mapping.inputs["Vector"])
+ # else: TexCoord → Mapping is already connected above
+
+ tree.links.new(mapping.outputs["Vector"],
+ env_tex.inputs["Vector"])
+
+ print("[StableGen] Orbit GIF: using existing HDRI world"
+ f" (rotation offset={math.degrees(self.hdri_rotation):.0f}°)")
+
+ def _setup_shadow_plane(self, context):
+ """Create a temporary shadow-catcher plane beneath the model."""
+ obj = context.active_object
+ if not obj:
+ return
+
+ # Determine the bottom of the object's bounding box in world space
+ bbox_corners = [obj.matrix_world @ mathutils.Vector(c)
+ for c in obj.bound_box]
+ min_z = min(c.z for c in bbox_corners)
+ center_x = sum(c.x for c in bbox_corners) / 8
+ center_y = sum(c.y for c in bbox_corners) / 8
+
+ # Determine a reasonable plane size (2× the bounding box extent)
+ extent_x = max(c.x for c in bbox_corners) - min(c.x for c in bbox_corners)
+ extent_y = max(c.y for c in bbox_corners) - min(c.y for c in bbox_corners)
+ plane_size = max(extent_x, extent_y) * 3
+
+ bpy.ops.mesh.primitive_plane_add(
+ size=plane_size,
+ location=(center_x, center_y, min_z))
+ plane = context.active_object
+ plane.name = "SG_ShadowPlane"
+
+ # Set as shadow catcher (Cycles)
+ plane.is_shadow_catcher = True
+
+ # Create a holdout material so the plane is invisible except
+ # for the shadow it catches.
+ mat = bpy.data.materials.new("SG_ShadowCatcher")
+ mat.use_nodes = True
+ mat.node_tree.nodes.clear()
+ output = mat.node_tree.nodes.new("ShaderNodeOutputMaterial")
+ holdout = mat.node_tree.nodes.new("ShaderNodeHoldout")
+ holdout.location = (-200, 0)
+ mat.node_tree.links.new(holdout.outputs[0],
+ output.inputs["Surface"])
+ plane.data.materials.append(mat)
+
+ self._temp_shadow_plane = plane
+ print("[StableGen] Orbit GIF: shadow catcher plane added")
+
+ def _animate_hdri_rotation(self, context, total_frames):
+ """Animate the HDRI Mapping node rotation for counter-rotate
+ or environment-only modes."""
+ if not self._env_mapping_node:
+ return
+
+ mapping = self._env_mapping_node
+ prefs = bpy.context.preferences.edit
+ prev_interp = prefs.keyframe_new_interpolation_type
+ prefs.keyframe_new_interpolation_type = self.interpolation
+ try:
+ # Start rotation = current value (may include user offset)
+ start_z = mapping.inputs["Rotation"].default_value[2]
+ mapping.inputs["Rotation"].default_value[2] = start_z
+ mapping.inputs["Rotation"].keyframe_insert(
+ data_path="default_value", index=2, frame=1)
+
+ # For counter-rotate: rotate in opposite direction to camera
+ # For env-only: rotate in positive direction
+ if self.env_mode == 'COUNTER':
+ end_z = start_z - math.radians(360)
+ else:
+ end_z = start_z + math.radians(360)
+
+ mapping.inputs["Rotation"].default_value[2] = end_z
+ mapping.inputs["Rotation"].keyframe_insert(
+ data_path="default_value", index=2, frame=total_frames + 1)
+ finally:
+ prefs.keyframe_new_interpolation_type = prev_interp
+
+ # Force interpolation on the world node tree's fcurves
+ world = context.scene.world
+ if world and world.node_tree:
+ self._force_fcurve_interpolation(
+ world.node_tree, self.interpolation)
+
def setup_animation(self, context):
obj = context.active_object
scene = context.scene
@@ -5161,24 +5401,29 @@ class ExportOrbitGIF(bpy.types.Operator):
scene.frame_end = total_frames
scene.render.fps = self.frame_rate
- # Animate Empty's Rotation
- # Set keyframe interpolation type before inserting so keyframes
- # are created with the correct curve.
- prefs = bpy.context.preferences.edit
- prev_interp = prefs.keyframe_new_interpolation_type
- prefs.keyframe_new_interpolation_type = self.interpolation
- try:
- self._temp_empty.rotation_euler = (0, 0, 0)
- self._temp_empty.keyframe_insert(data_path="rotation_euler", index=2, frame=1)
+ # Animate Empty's Rotation (skip if ENV_ONLY — camera stays fixed)
+ if self.env_mode != 'ENV_ONLY':
+ # Set keyframe interpolation type before inserting so keyframes
+ # are created with the correct curve.
+ prefs = bpy.context.preferences.edit
+ prev_interp = prefs.keyframe_new_interpolation_type
+ prefs.keyframe_new_interpolation_type = self.interpolation
+ try:
+ self._temp_empty.rotation_euler = (0, 0, 0)
+ self._temp_empty.keyframe_insert(data_path="rotation_euler", index=2, frame=1)
- self._temp_empty.rotation_euler = (0, 0, math.radians(360))
- self._temp_empty.keyframe_insert(data_path="rotation_euler", index=2, frame=total_frames + 1)
- finally:
- prefs.keyframe_new_interpolation_type = prev_interp
+ self._temp_empty.rotation_euler = (0, 0, math.radians(360))
+ self._temp_empty.keyframe_insert(data_path="rotation_euler", index=2, frame=total_frames + 1)
+ finally:
+ prefs.keyframe_new_interpolation_type = prev_interp
- # Post-insert: force the chosen interpolation on all keyframe points.
- # The preference alone doesn't always take effect on Blender 5.x.
- self._force_fcurve_interpolation(self._temp_empty, self.interpolation)
+ # Post-insert: force the chosen interpolation on all keyframe points.
+ # The preference alone doesn't always take effect on Blender 5.x.
+ self._force_fcurve_interpolation(self._temp_empty, self.interpolation)
+
+ # Animate HDRI environment rotation (counter-rotate or env-only)
+ if self.use_hdri and self.env_mode in ('COUNTER', 'ENV_ONLY'):
+ self._animate_hdri_rotation(context, total_frames)
def execute(self, context):
@@ -5229,6 +5474,25 @@ class ExportOrbitGIF(bpy.types.Operator):
render.film_transparent = True
cycles.samples = self.samples # Set samples for rendering
+ # ── PBR Showcase: HDRI Environment ────────────────────────────
+ if self.use_hdri:
+ self._setup_hdri_environment(context)
+
+ # ── PBR Showcase: Cycles Denoiser ─────────────────────────────
+ if self.use_denoiser and scene.render.engine == 'CYCLES':
+ self._initial_settings['use_denoising'] = cycles.use_denoising
+ self._initial_settings['denoiser'] = cycles.denoiser
+ cycles.use_denoising = True
+ # Prefer OpenImageDenoise (CPU-based, always available)
+ try:
+ cycles.denoiser = 'OPENIMAGEDENOISE'
+ except TypeError:
+ pass # Fallback to whatever is default
+
+ # ── PBR Showcase: Ground Shadow Plane ─────────────────────────
+ if self.use_shadow_plane and scene.render.engine == 'CYCLES':
+ self._setup_shadow_plane(context)
+
# Setup Animation
try:
self.setup_animation(context)
@@ -5335,7 +5599,7 @@ class ExportOrbitGIF(bpy.types.Operator):
setattr(render.image_settings, 'file_format', value)
elif key == 'color_mode':
setattr(render.image_settings, 'color_mode', value)
- elif key == 'samples': # Restore samples
+ elif key in ('samples', 'use_denoising', 'denoiser'):
setattr(cycles, key, value)
elif key == 'engine': # Restore engine
setattr(render, key, value)
@@ -5369,6 +5633,76 @@ class ExportOrbitGIF(bpy.types.Operator):
bpy.data.objects.remove(self._temp_empty, do_unlink=True)
self._temp_empty = None
+ # Remove temporary shadow-catcher plane
+ if self._temp_shadow_plane:
+ try:
+ # Remove the material
+ for mat_slot in self._temp_shadow_plane.data.materials:
+ if mat_slot and mat_slot.name in bpy.data.materials:
+ bpy.data.materials.remove(mat_slot)
+ # Remove the mesh data
+ mesh_data = self._temp_shadow_plane.data
+ if self._temp_shadow_plane.name in context.scene.collection.objects:
+ context.scene.collection.objects.unlink(self._temp_shadow_plane)
+ if self._temp_shadow_plane.name in bpy.data.objects:
+ bpy.data.objects.remove(self._temp_shadow_plane, do_unlink=True)
+ if mesh_data and mesh_data.name in bpy.data.meshes:
+ bpy.data.meshes.remove(mesh_data)
+ except Exception as e:
+ print(f"Warning: Could not remove shadow plane: {e}")
+ self._temp_shadow_plane = None
+
+ # Restore the world environment (remove injected mapping nodes)
+ if self._original_world_data:
+ try:
+ world = self._original_world_data['world_ref']
+ if world and world.name in bpy.data.worlds:
+ # Remove HDRI animation keyframes
+ if world.node_tree and world.node_tree.animation_data:
+ action = world.node_tree.animation_data.action
+ if action and action.name in bpy.data.actions:
+ bpy.data.actions.remove(action)
+ world.node_tree.animation_data_clear()
+
+ if self._original_world_data.get('created_fallback'):
+ # We created a whole sky fallback — restore original
+ world.use_nodes = self._original_world_data['use_nodes']
+ world.color = self._original_world_data['color']
+ if not self._original_world_data['use_nodes']:
+ if world.node_tree:
+ world.node_tree.nodes.clear()
+ else:
+ # We injected SG_OrbitMapping + SG_OrbitTexCoord
+ # into the existing tree. Remove them and restore
+ # the original vector link.
+ tree = world.node_tree
+ if tree:
+ # Find the env tex and restore its vector input
+ env_tex = None
+ for node in tree.nodes:
+ if node.type == 'TEX_ENVIRONMENT':
+ env_tex = node
+ break
+
+ orig_socket = self._original_world_data.get(
+ 'env_vec_from_socket')
+ if env_tex and orig_socket:
+ tree.links.new(orig_socket,
+ env_tex.inputs["Vector"])
+ elif env_tex:
+ # Had no vector link originally — just remove
+ for link in list(env_tex.inputs["Vector"].links):
+ tree.links.remove(link)
+
+ # Remove injected nodes
+ for name in ("SG_OrbitMapping", "SG_OrbitTexCoord"):
+ if name in tree.nodes:
+ tree.nodes.remove(tree.nodes[name])
+ except Exception as e:
+ print(f"Warning: Could not restore world environment: {e}")
+ self._original_world_data = None
+ self._env_mapping_node = None
+
self._rendering = False # Ensure rendering flag is reset
self._cancelled = False # Reset cancellation flag
diff --git a/stablegen/stablegen.py b/stablegen/stablegen.py
index 5683fed..77362a7 100644
--- a/stablegen/stablegen.py
+++ b/stablegen/stablegen.py
@@ -1044,6 +1044,55 @@ class StableGenPanel(bpy.types.Panel):
row = content_box.row()
row.prop(scene, "overwrite_material", text="Overwrite Material", toggle=True, icon="FILE_REFRESH")
+ # ── PBR Decomposition ──
+ content_box.separator()
+ row = content_box.row()
+ row.prop(scene, "pbr_decomposition", text="PBR Decomposition", toggle=True, icon="NODE_MATERIAL")
+ if scene.pbr_decomposition:
+ sub = content_box.box()
+ # ── Map toggles ──
+ sub.label(text="Maps to Extract:", icon="IMAGE_DATA")
+ grid = sub.grid_flow(row_major=True, columns=3, even_columns=True, align=True)
+ grid.prop(scene, "pbr_map_albedo", toggle=True, icon="SHADING_SOLID")
+ grid.prop(scene, "pbr_map_roughness", toggle=True, icon="MATFLUID")
+ grid.prop(scene, "pbr_map_metallic", toggle=True, icon="META_BALL")
+ grid.prop(scene, "pbr_map_normal", toggle=True, icon="NORMALS_FACE")
+ grid.prop(scene, "pbr_map_depth", toggle=True, icon="MOD_DISPLACE")
+ # ── Per-map adjustment controls ──
+ if scene.pbr_map_normal:
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_normal_mode", text="Normal Mode")
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_normal_strength", text="Normal Strength", slider=True)
+ # ── Albedo source selector (only when albedo enabled) ──
+ if scene.pbr_map_albedo:
+ sub.separator()
+ row = sub.row()
+ row.prop(scene, "pbr_albedo_source", text="Albedo Source")
+ if scene.pbr_albedo_source == 'delight':
+ adj_row = sub.row()
+ adj_row.prop(scene, "pbr_delight_strength", text="Delight Strength", slider=True)
+ sub.separator()
+ # ── Quality settings ──
+ row = sub.row(align=True)
+ row.prop(scene, "pbr_use_native_resolution", text="Native Resolution", toggle=True, icon="FULLSCREEN_ENTER")
+ row.prop(scene, "pbr_tiling", text="")
+ if scene.pbr_tiling != 'off':
+ row = sub.row(align=True)
+ row.prop(scene, "pbr_tile_grid", text="Tile Grid (N×N)")
+ row.prop(scene, "pbr_tile_superres", text="Super Res", toggle=True, icon="IMAGE_PLANE")
+ if not scene.pbr_use_native_resolution:
+ row = sub.row()
+ row.prop(scene, "pbr_processing_resolution", text="Processing Resolution")
+ row = sub.row(align=True)
+ row.prop(scene, "pbr_denoise_steps", text="Denoise Steps")
+ row.prop(scene, "pbr_ensemble_size", text="Ensemble Size")
+ sub.separator()
+ row = sub.row()
+ row.prop(scene, "pbr_replace_color_with_albedo", text="Use Albedo as Base Color", toggle=True, icon="SHADING_SOLID")
+ row = sub.row()
+ row.prop(scene, "pbr_auto_lighting", text="Studio Lighting", toggle=True, icon="LIGHT_AREA")
+
# --- Image Guidance (IPAdapter & ControlNet) ---
if _show_diffusion_sections:
if scene.model_architecture in ['sdxl', 'flux1']:
diff --git a/stablegen/utils.py b/stablegen/utils.py
index 8c6c000..d328c9d 100644
--- a/stablegen/utils.py
+++ b/stablegen/utils.py
@@ -15,6 +15,14 @@ class AddHDRI(bpy.types.Operator):
subtype='FILE_PATH'
) # type: ignore
+ strength: bpy.props.FloatProperty(
+ name="Strength",
+ description="Brightness of the HDRI environment lighting",
+ default=1.0,
+ min=0.01,
+ max=10.0
+ ) # type: ignore
+
def execute(self, context):
world = context.scene.world
if not world:
@@ -50,6 +58,7 @@ class AddHDRI(bpy.types.Operator):
bg_hdri = nodes.new("ShaderNodeBackground")
bg_hdri.name = "HDRI Background"
bg_hdri.location = (0, 100)
+ bg_hdri.inputs['Strength'].default_value = self.strength
bg_solid = nodes.new("ShaderNodeBackground")
bg_solid.name = "Solid Background"
@@ -67,7 +76,7 @@ class AddHDRI(bpy.types.Operator):
links.new(bg_solid.outputs['Background'], mix_shader.inputs[2])
links.new(mix_shader.outputs['Shader'], output_node.inputs['Surface'])
- self.report({'INFO'}, "HDRI lighting added (background hidden).")
+ self.report({'INFO'}, f"HDRI lighting added (strength={self.strength:.2f}, background hidden).")
return {'FINISHED'}
def invoke(self, context, event):
@@ -253,6 +262,7 @@ def get_generation_dirs(context):
},
"uv_inpaint_root": os.path.join(revision_dir, "uv_inpaint"),
"misc" : os.path.join(revision_dir, "misc"),
+ "pbr": os.path.join(revision_dir, "pbr"),
}
def ensure_dirs_exist(dirs_dict):
@@ -281,6 +291,8 @@ def ensure_dirs_exist(dirs_dict):
os.makedirs(path, exist_ok=True)
# Create misc directory
os.makedirs(dirs_dict["misc"], exist_ok=True)
+ # Create pbr directory
+ os.makedirs(dirs_dict["pbr"], exist_ok=True)
def get_file_path(context, file_type, subtype=None, filename=None, camera_id=None, object_name=None, material_id=None, legacy=False):
"""
@@ -350,7 +362,16 @@ def get_file_path(context, file_type, subtype=None, filename=None, camera_id=Non
if subtype == "visibility":
filename = f"{object_name}_baked_visibility" if not filename else filename
return os.path.join(base_dir, f"{filename}.png")
-
+
+ elif file_type == "pbr" and subtype:
+ # PBR decomposition maps: subtype is the map name (albedo, roughness, metallic, etc.)
+ base_dir = dirs["pbr"]
+ material_suffix = f"-{material_id}" if material_id is not None else ""
+ if camera_id is not None:
+ return os.path.join(base_dir, f"pbr_{subtype}_cam{camera_id}{material_suffix}.png")
+ else:
+ return os.path.join(base_dir, f"pbr_{subtype}{material_suffix}.png")
+
# Fallback to revision directory
return os.path.join(dirs["revision"], f"{filename or 'file'}.png")
diff --git a/stablegen/workflows.py b/stablegen/workflows.py
index 6fbec3b..d7734a5 100644
--- a/stablegen/workflows.py
+++ b/stablegen/workflows.py
@@ -983,6 +983,325 @@ class WorkflowManager:
return prompt, "7"
+ # ── PBR Decomposition (Marigold IID) ───────────────────────────────
+ def generate_pbr_maps(self, context, input_image_path, model_name=None,
+ force_native_resolution=False):
+ """Run a Marigold model on a generated texture and collect the output.
+
+ Uploads *input_image_path* to the ComfyUI server, runs it through the
+ ``MarigoldModelLoader`` + ``MarigoldDepthEstimation_v2`` pipeline,
+ and collects the resulting map(s) via ``SaveImageWebsocket``.
+
+ Different models produce different outputs:
+
+ * **IID-Appearance** → 3 images: [Albedo, Roughness, Metallicity]
+ * **IID-Lighting** → 3 images: [Albedo, Shading, Residual]
+ * **Normals** → 1 image: [Normal map]
+ * **Depth** → 1 image: [Depth map]
+
+ Args:
+ input_image_path: Absolute path to the generated texture PNG.
+ model_name: HuggingFace model repo name to use, e.g.
+ ``'prs-eth/marigold-iid-appearance-v1-1'``. If *None*,
+ defaults to the IID-Appearance model.
+ force_native_resolution: If *True*, always process at the
+ image's native longest edge (rounded to 64) regardless
+ of the user-configured resolution settings. Used by
+ the tiling path so each tile gets full-detail processing.
+
+ Returns:
+ list[bytes] | dict: A list of raw-PNG byte buffers on success,
+ or ``{"error": "..."}`` on failure.
+ """
+ server_address = context.preferences.addons[__package__].preferences.server_address
+ client_id = str(uuid.uuid4())
+ scene = context.scene
+
+ if model_name is None:
+ model_name = 'prs-eth/marigold-iid-appearance-v1-1'
+
+ # Pre-flight: verify that ComfyUI-Marigold nodes are installed
+ required_nodes = ['MarigoldModelLoader', 'MarigoldDepthEstimation_v2']
+ for node_class in required_nodes:
+ try:
+ resp = urllib.request.urlopen(
+ f"http://{server_address}/object_info/{node_class}",
+ timeout=get_timeout('api')
+ )
+ data = json.loads(resp.read())
+ if node_class not in data:
+ return {"error": f"ComfyUI-Marigold node '{node_class}' not found. "
+ f"Install ComfyUI-Marigold (run installer option 9 "
+ f"or git clone https://github.com/kijai/ComfyUI-Marigold "
+ f"into ComfyUI/custom_nodes/) and ensure 'diffusers>=0.28' "
+ f"is installed in ComfyUI's Python, then restart ComfyUI."}
+ except Exception:
+ return {"error": f"ComfyUI-Marigold node '{node_class}' not found. "
+ f"Install ComfyUI-Marigold (run installer option 9 "
+ f"or git clone https://github.com/kijai/ComfyUI-Marigold "
+ f"into ComfyUI/custom_nodes/) and ensure 'diffusers>=0.28' "
+ f"is installed in ComfyUI's Python, then restart ComfyUI."}
+
+ # Upload the source image to ComfyUI's input folder
+ from .generator import upload_image_to_comfyui
+ image_info = upload_image_to_comfyui(server_address, input_image_path)
+ if image_info is None:
+ return {"error": f"Failed to upload image for PBR decomposition: {input_image_path}"}
+
+ uploaded_name = image_info.get("name", os.path.basename(input_image_path))
+
+ # Determine the processing resolution.
+ # When 'Native Resolution' is enabled (or forced by tiling), use
+ # the image's own longest edge (rounded to 64) so Marigold
+ # processes at full detail.
+ # Otherwise fall back to the user-specified fixed resolution.
+ use_native = force_native_resolution or getattr(
+ scene, 'pbr_use_native_resolution', False)
+ if use_native:
+ try:
+ from PIL import Image as _PILImage
+ with _PILImage.open(input_image_path) as _img:
+ _w, _h = _img.size
+ proc_res = max(_w, _h)
+ proc_res = ((proc_res + 63) // 64) * 64
+ print(f"[StableGen] Marigold: native resolution {proc_res}px "
+ f"(input {_w}\u00d7{_h})")
+ except Exception:
+ proc_res = scene.pbr_processing_resolution
+ else:
+ proc_res = scene.pbr_processing_resolution
+
+ # Build the minimal ComfyUI workflow
+ prompt = {
+ "1": {
+ "class_type": "LoadImage",
+ "inputs": {"image": uploaded_name}
+ },
+ "2": {
+ "class_type": "MarigoldModelLoader",
+ "inputs": {
+ "model": model_name,
+ }
+ },
+ "3": {
+ "class_type": "MarigoldDepthEstimation_v2",
+ "inputs": {
+ "marigold_model": ["2", 0],
+ "image": ["1", 0],
+ "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
+ "denoise_steps": scene.pbr_denoise_steps,
+ "ensemble_size": scene.pbr_ensemble_size,
+ "processing_resolution": proc_res,
+ "scheduler": "LCMScheduler",
+ "use_taesd_vae": False,
+ # Keep loaded to avoid offloading to CPU — newer diffusers
+ # raises ValueError when moving a float16 pipeline to CPU.
+ "keep_model_loaded": True,
+ }
+ },
+ "4": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["3", 0]}
+ }
+ }
+
+ NODES = {"save_image": "4"}
+
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "WebSocket connection failed for PBR decomposition"}
+
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = ws
+ type(self.operator)._active_ws = ws
+
+ try:
+ images = self._execute_prompt_and_get_images(
+ ws, prompt, client_id, server_address, NODES
+ )
+ finally:
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = None
+ type(self.operator)._active_ws = None
+ try:
+ ws.close()
+ except Exception:
+ pass
+
+ if images is None or not isinstance(images, dict) or not images:
+ return {"error": "PBR decomposition failed — no output received"}
+
+ save_node = NODES['save_image']
+ if save_node not in images or not images[save_node]:
+ return {"error": "PBR decomposition failed — no images from save node"}
+
+ pbr_maps = images[save_node] # list of raw PNG bytes
+ print(f"[StableGen] Marigold '{model_name}' returned {len(pbr_maps)} image(s)")
+
+ return pbr_maps
+
+ def generate_delight_map(self, context, input_image_path):
+ """Run StableDelight on an image to produce a specular-free (delighted) version.
+
+ Uses the ``LoadStableDelightModel`` + ``ApplyStableDelight`` nodes
+ from the *ComfyUI_StableDelight_ll* custom-node package.
+
+ The delighted image preserves diffuse shading and texture detail
+ while removing specular highlights — making it a high-quality
+ alternative to Marigold IID's flat albedo for the Base Color slot.
+
+ Args:
+ input_image_path: Absolute path to the generated texture PNG.
+
+ Returns:
+ bytes | dict: Raw PNG bytes of the delighted image on success,
+ or ``{"error": "..."}`` on failure.
+ """
+ server_address = context.preferences.addons[__package__].preferences.server_address
+ client_id = str(uuid.uuid4())
+ scene = context.scene
+
+ # Pre-flight: verify that StableDelight nodes are installed
+ required_nodes = ['LoadStableDelightModel', 'ApplyStableDelight']
+ for node_class in required_nodes:
+ try:
+ resp = urllib.request.urlopen(
+ f"http://{server_address}/object_info/{node_class}",
+ timeout=get_timeout('api')
+ )
+ data = json.loads(resp.read())
+ if node_class not in data:
+ return {"error": f"StableDelight node '{node_class}' not found. "
+ f"Install ComfyUI_StableDelight_ll (run installer "
+ f"option 10 or git clone "
+ f"https://github.com/lldacing/ComfyUI_StableDelight_ll "
+ f"into ComfyUI/custom_nodes/) and download the model "
+ f"'Stable-X/yoso-delight-v0-4-base' into "
+ f"ComfyUI/models/diffusers/, then restart ComfyUI."}
+ except Exception:
+ return {"error": f"StableDelight node '{node_class}' not found. "
+ f"Install ComfyUI_StableDelight_ll (run installer "
+ f"option 10 or git clone "
+ f"https://github.com/lldacing/ComfyUI_StableDelight_ll "
+ f"into ComfyUI/custom_nodes/) and download the model "
+ f"'Stable-X/yoso-delight-v0-4-base' into "
+ f"ComfyUI/models/diffusers/, then restart ComfyUI."}
+
+ # Resolve model path — the node expects a relative path under
+ # ComfyUI/models/diffusers/ containing model_index.json.
+ delight_model_path = "Stable-X--yoso-delight-v0-4-base"
+
+ # Upload the source image to ComfyUI's input folder
+ from .generator import upload_image_to_comfyui
+ image_info = upload_image_to_comfyui(server_address, input_image_path)
+ if image_info is None:
+ return {"error": f"Failed to upload image for StableDelight: {input_image_path}"}
+
+ uploaded_name = image_info.get("name", os.path.basename(input_image_path))
+
+ # Determine the processing resolution.
+ #
+ # The official Stable-X Predictor uses processing_resolution=2048,
+ # while the ComfyUI node defaults to 1024. Both use
+ # MarigoldImageProcessor.preprocess() → resize_to_max_edge() which
+ # scales the image so its *longest edge* equals processing_resolution,
+ # then pads to a multiple of vae_scale_factor (8).
+ #
+ # Problems with a fixed resolution:
+ # - Too small (e.g. 1024): images larger than 1024px get downscaled
+ # then bilinearly upscaled back → blurry output.
+ # - Too large: small images get massively upscaled → wastes VRAM
+ # and can exceed available memory.
+ # - resolution=0: triggers skip_preprocess=True in the node, which
+ # has a 3D/4D tensor bug in the pipeline's VAE encoder.
+ #
+ # Solution: use max(native_longest_edge, 1024). This ensures:
+ # - Large images (>1024) are processed at native res (no downscale).
+ # - Small images (<1024) are upscaled to 1024 — giving the SD1.5-
+ # based model enough spatial tokens to produce clean output.
+ # The value is rounded up to the nearest multiple of 64 (not just 8)
+ # to match the official Stable-X resize_image() convention, which
+ # aligns to 64 for clean integer dimensions through every UNet level
+ # (VAE /8, then 3 downsampling stages at /2 each = 64× total).
+ try:
+ from PIL import Image as _PILImage
+ with _PILImage.open(input_image_path) as _img:
+ _w, _h = _img.size
+ # Floor: at least 1024 (the model's ComfyUI default)
+ # Ceiling: native resolution (avoid downscaling)
+ proc_res = max(max(_w, _h), 1024)
+ # Round up to next multiple of 64 (official alignment)
+ proc_res = ((proc_res + 63) // 64) * 64
+ print(f"[StableGen] StableDelight: processing at {proc_res}px "
+ f"(input {_w}×{_h})")
+ except Exception:
+ proc_res = 1024 # safe fallback
+
+ # Build the ComfyUI workflow
+ prompt = {
+ "1": {
+ "class_type": "LoadImage",
+ "inputs": {"image": uploaded_name}
+ },
+ "2": {
+ "class_type": "LoadStableDelightModel",
+ "inputs": {
+ "model": delight_model_path,
+ "device": "AUTO",
+ }
+ },
+ "3": {
+ "class_type": "ApplyStableDelight",
+ "inputs": {
+ "model": ["2", 0],
+ "images": ["1", 0],
+ "strength": getattr(scene, 'pbr_delight_strength', 1.0),
+ "resolution": proc_res,
+ "upscale_method": "bilinear",
+ }
+ },
+ "4": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["3", 0]}
+ }
+ }
+
+ NODES = {"save_image": "4"}
+
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "WebSocket connection failed for StableDelight"}
+
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = ws
+ type(self.operator)._active_ws = ws
+
+ try:
+ images = self._execute_prompt_and_get_images(
+ ws, prompt, client_id, server_address, NODES
+ )
+ finally:
+ if hasattr(self.operator, '_active_ws'):
+ self.operator._active_ws = None
+ type(self.operator)._active_ws = None
+ try:
+ ws.close()
+ except Exception:
+ pass
+
+ if images is None or not isinstance(images, dict) or not images:
+ return {"error": "StableDelight failed — no output received"}
+
+ save_node = NODES['save_image']
+ if save_node not in images or not images[save_node]:
+ return {"error": "StableDelight failed — no images from save node"}
+
+ delight_images = images[save_node]
+ print(f"[StableGen] StableDelight returned {len(delight_images)} image(s)")
+
+ # StableDelight returns a single image
+ return delight_images[0] if delight_images else {"error": "StableDelight returned empty"}
+
def _build_flux_txt2img(self, context):
"""Return (prompt_dict, save_node_id) for a minimal Flux txt2img with LoRA support."""
scene = context.scene