Skip to content

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

initial trellis.2 implementation

sakalonddadcedb2f434
diff --git a/README.md b/README.md
index fbab5dd..8360733 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,8 @@
StableGen is an open-source Blender plugin designed to seamlessly integrate advanced diffusion models (SDXL, FLUX.1-dev, Qwen Image Edit 2509) into your creative process. Generate complex, coherent, and controllable textures for your 3D models and entire scenes using a flexible ComfyUI backend.
+New: **TRELLIS.2 Image-to-3D** — generate textured 3D meshes from a single reference image, powered by [ComfyUI-TRELLIS2](https://github.com/PozzettiAndrea/ComfyUI-TRELLIS2).
+
---
<details>
@@ -58,6 +60,11 @@ StableGen empowers 3D artists by bringing cutting-edge AI texturing capabilities
* Leverage multiple ControlNet units (Depth, Canny, Normal) simultaneously to ensure generated textures respect your model's geometry.
* Fine-tune strength, start/end steps for each ControlNet unit.
* Supports custom ControlNet model mapping.
+* 🧊 **TRELLIS.2 Image-to-3D (Experimental):**
+ * Generate a fully textured 3D mesh from a single reference image using TRELLIS.2.
+ * Configurable resolution, VRAM mode (CPU/disk offloading for 16GB cards), guidance strengths, and mesh export settings.
+ * GLB output is automatically imported into the Blender scene.
+ * Requires `ComfyUI-TRELLIS2` custom node (installable via `installer.py`).
* 🖌️ **Powerful Style Guidance with IPAdapter:**
* Use external reference images to guide the style, mood, and content of your textures with IPAdapter.
* Employ IPAdapter without an reference image for enhanced consistency in multi-view generation modes.
@@ -390,6 +397,7 @@ StableGen builds upon the fantastic work of many individuals and communities. Ou
* Full thesis available at: [https://dspace.cvut.cz/handle/10467/123567](https://dspace.cvut.cz/handle/10467/123567)
* **Core Technologies & Communities:**
* **ComfyUI** by ComfyAnonymous ([GitHub](https://github.com/comfyanonymous/ComfyUI)) for the powerful and flexible backend.
+ * **ComfyUI-TRELLIS2** by PozzettiAndrea ([GitHub](https://github.com/PozzettiAndrea/ComfyUI-TRELLIS2)) for the TRELLIS.2 ComfyUI integration.
* The **Blender Foundation** and its community for the amazing open-source 3D creation suite.
* **Inspired by following Blender Addons:**
* **Dream Textures** by Carson Katri et al. ([GitHub](https://github.com/carson-katri/dream-textures))
@@ -424,7 +432,7 @@ Here are some features we plan to implement in the future (in no particular orde
* **Custom VAE, CLIP model selection:** Ability to select custom VAE and CLIP models in addition to custom ControlNet and LoRA models.
* **Refine mode improvements:** Features like brush based inpainting.
* **Z-Image support** (and eventual Z-Image editing model support)
-* **Mesh generation:** Integration of mesh generation capabilities.
+* **Mesh generation:** Integration of mesh generation capabilities (TRELLIS.2 support now available as experimental feature).
* **Brush-based inpainting:** Paint masks directly on the viewport for targeted local edits.
If you have any suggestions, please feel free to open an issue!
diff --git a/installer.py b/installer.py
index 0e2b403..db5d7e1 100644
--- a/installer.py
+++ b/installer.py
@@ -14,6 +14,317 @@ try:
except ImportError:
TQDM_AVAILABLE = False
+# --- Compatibility Patches ---
+# Polyfill for Path.is_junction() which requires Python 3.12+.
+# comfy_env uses it but many ComfyUI installs still run Python 3.10/3.11.
+_PATCH_PY310_IS_JUNCTION = (
+ "# -- StableGen patch: Path.is_junction polyfill for Python < 3.12 --\n"
+ "import pathlib as _pathlib\n"
+ "if not hasattr(_pathlib.Path, 'is_junction'):\n"
+ " import os as _os, stat as _stat\n"
+ " def _is_junction(self):\n"
+ " try:\n"
+ " return bool(\n"
+ " _os.lstat(str(self)).st_file_attributes\n"
+ " & _stat.FILE_ATTRIBUTE_REPARSE_POINT\n"
+ " )\n"
+ " except (OSError, AttributeError):\n"
+ " return False\n"
+ " _pathlib.Path.is_junction = _is_junction\n"
+ "# -- End StableGen patch --\n\n"
+)
+
+# Patch for lazy_manager.py: Add attn_backend to config comparison.
+# Without this, switching attention backend (e.g. xformers -> flash_attn)
+# doesn't recreate the model manager, leaving stale config.
+_PATCH_TRELLIS_ATTN_CMP_ANCHOR = (
+ " elif (_LAZY_MANAGER.model_name != model_name or\n"
+ " _LAZY_MANAGER.resolution != resolution or\n"
+ " _LAZY_MANAGER.vram_mode != vram_mode):\n"
+ " # Config changed, recreate manager\n"
+ " _LAZY_MANAGER.cleanup()\n"
+)
+
+_PATCH_TRELLIS_ATTN_CMP_REPLACE = (
+ " elif (_LAZY_MANAGER.model_name != model_name or\n"
+ " _LAZY_MANAGER.resolution != resolution or\n"
+ " _LAZY_MANAGER.vram_mode != vram_mode or\n"
+ " _LAZY_MANAGER.attn_backend != attn_backend):\n"
+ " # Config changed, recreate manager\n"
+ " print(f\"[TRELLIS2] Config changed, recreating model manager...\", file=sys.stderr)\n"
+ " _LAZY_MANAGER.cleanup()\n"
+)
+
+# Patch for stages.py: Wrap DinoV3 feature extraction in torch.no_grad().
+# Without this, autograd retains ViT-L intermediate activations on GPU,
+# leaking ~5-9 GB between runs in the persistent comfy-env worker.
+_PATCH_TRELLIS_NOGRAD_ANCHOR = (
+ " # Load DinoV3 and extract features\n"
+ " model = manager.get_dinov3(device)\n"
+ "\n"
+ " # Get 512px conditioning\n"
+ " model.image_size = 512\n"
+ " cond_512 = model([pil_image])\n"
+ "\n"
+ " # Get 1024px conditioning if requested\n"
+ " cond_1024 = None\n"
+ " if include_1024:\n"
+ " model.image_size = 1024\n"
+ " cond_1024 = model([pil_image])\n"
+ "\n"
+ " # Unload DinoV3 immediately\n"
+ " manager.unload_dinov3()\n"
+ "\n"
+ " # Create negative conditioning\n"
+ " neg_cond = torch.zeros_like(cond_512)\n"
+ "\n"
+ " conditioning = {\n"
+ " 'cond_512': cond_512.cpu(),\n"
+ " 'neg_cond': neg_cond.cpu(),\n"
+ " }\n"
+ " if cond_1024 is not None:\n"
+ " conditioning['cond_1024'] = cond_1024.cpu()\n"
+)
+
+_PATCH_TRELLIS_NOGRAD_REPLACE = (
+ " # Load DinoV3 and extract features\n"
+ " model = manager.get_dinov3(device)\n"
+ "\n"
+ " # Use no_grad to prevent autograd from retaining intermediate activations\n"
+ " # on GPU. Without this, the computation graph keeps ViT-L activations alive\n"
+ " # in the comfy-env worker, leaking ~5-9 GB between runs.\n"
+ " with torch.no_grad():\n"
+ " # Get 512px conditioning\n"
+ " model.image_size = 512\n"
+ " cond_512 = model([pil_image])\n"
+ "\n"
+ " # Get 1024px conditioning if requested\n"
+ " cond_1024 = None\n"
+ " if include_1024:\n"
+ " model.image_size = 1024\n"
+ " cond_1024 = model([pil_image])\n"
+ "\n"
+ " # Unload DinoV3 immediately\n"
+ " manager.unload_dinov3()\n"
+ "\n"
+ " # Create negative conditioning\n"
+ " neg_cond = torch.zeros_like(cond_512)\n"
+ "\n"
+ " # .detach() breaks any remaining graph references before moving to CPU\n"
+ " conditioning = {\n"
+ " 'cond_512': cond_512.detach().cpu(),\n"
+ " 'neg_cond': neg_cond.detach().cpu(),\n"
+ " }\n"
+ " if cond_1024 is not None:\n"
+ " conditioning['cond_1024'] = cond_1024.detach().cpu()\n"
+ "\n"
+ " # Free the GPU originals immediately\n"
+ " del cond_512, cond_1024, neg_cond\n"
+ " gc.collect()\n"
+ " torch.cuda.empty_cache()\n"
+)
+
+# ---------------------------------------------------------------------------
+# comfy-env VRAM leak fix patches
+# ---------------------------------------------------------------------------
+# comfy-env hooks torch.nn.Module.to() and .cuda() to auto-register every
+# model that lands on CUDA in a permanent _model_registry (with no unregister
+# API). Between ComfyUI node calls the model manager issues
+# "Requested to load SubprocessModel" which calls model_to_device("cuda"),
+# moving ALL registered models back to GPU. This leaks ~7 GB of model
+# weights between runs. The fix: unregister from the registry before
+# moving models to CPU / deleting them.
+# ---------------------------------------------------------------------------
+
+# -- base.py _unload_model: unregister from comfy-env + model.cpu() before del
+_PATCH_TRELLIS_UNLOAD_CPU_ANCHOR = (
+ " # Delete the model entirely\n"
+ " self.models[model_key] = None\n"
+ " del model\n"
+)
+
+_PATCH_TRELLIS_UNLOAD_CPU_REPLACE = (
+ " # Unregister from comfy-env's model registry BEFORE moving to CPU.\n"
+ " # comfy-env auto-registers every nn.Module that lands on CUDA via\n"
+ " # hooked Module.to(). Without unregistering, the registry keeps a\n"
+ " # permanent reference and ComfyUI's model manager re-loads the model\n"
+ " # to GPU between calls, leaking the full model weights per run.\n"
+ " # Replace with zero-param dummy so host's model_to_device succeeds\n"
+ " # but consumes 0 VRAM. Registry accessed via closure on\n"
+ " # sys.modules['comfy_worker'].register_model.\n"
+ " try:\n"
+ " _cw = sys.modules.get('comfy_worker')\n"
+ " if _cw is not None:\n"
+ " _reg_fn = getattr(_cw, 'register_model', None)\n"
+ " if _reg_fn is not None and hasattr(_reg_fn, '__closure__'):\n"
+ " _fv = _reg_fn.__code__.co_freevars\n"
+ " _cl = _reg_fn.__closure__\n"
+ " _by_obj = _cl[_fv.index('_model_id_by_obj')].cell_contents\n"
+ " _registry = _cl[_fv.index('_model_registry')].cell_contents\n"
+ " _meta = _cl[_fv.index('_model_registry_meta')].cell_contents\n"
+ " obj_id = id(model)\n"
+ " if obj_id in _by_obj:\n"
+ " ce_model_id = _by_obj.pop(obj_id)\n"
+ " dummy = torch.nn.Module()\n"
+ " _registry[ce_model_id] = dummy\n"
+ " _meta[ce_model_id] = {'size': 0, 'kind': 'other'}\n"
+ " _by_obj[id(dummy)] = ce_model_id\n"
+ ' print(f"[TRELLIS2] Unregistered {model_key} from comfy-env registry (id={ce_model_id})", file=sys.stderr, flush=True)\n'
+ " except Exception:\n"
+ " pass\n"
+ " # Force all parameters and buffers off GPU.\n"
+ " try:\n"
+ " model.cpu()\n"
+ " except Exception:\n"
+ " pass\n"
+ " # Delete the model entirely\n"
+ " self.models[model_key] = None\n"
+ " del model\n"
+)
+
+# -- lazy_manager.py: insert _unregister_from_comfy_env helper function
+_PATCH_TRELLIS_COMFY_HELPER_ANCHOR = (
+ "# Global model manager instance\n"
+ '_LAZY_MANAGER: Optional["LazyModelManager"] = None\n'
+)
+
+_PATCH_TRELLIS_COMFY_HELPER_REPLACE = (
+ "\n"
+ "def _unregister_from_comfy_env(model, label: str = \"\"):\n"
+ ' """Remove an nn.Module from comfy-env\'s worker-side model registry.\n'
+ "\n"
+ " comfy-env hooks Module.to() to auto-register every model that lands on\n"
+ " CUDA. The registry keeps a permanent reference, and ComfyUI's model\n"
+ " manager will re-load the model to GPU between calls via\n"
+ " 'Requested to load SubprocessModel'. We replace the model with a\n"
+ " zero-param dummy so the host's model_to_device succeeds (no-op) but\n"
+ " no VRAM is consumed. Registry accessed via closure on\n"
+ " sys.modules['comfy_worker'].register_model.\n"
+ ' """\n'
+ " try:\n"
+ " _cw = sys.modules.get('comfy_worker')\n"
+ " if _cw is None:\n"
+ " return\n"
+ " _reg_fn = getattr(_cw, 'register_model', None)\n"
+ " if _reg_fn is None or not hasattr(_reg_fn, '__closure__'):\n"
+ " return\n"
+ " _fv = _reg_fn.__code__.co_freevars\n"
+ " _cl = _reg_fn.__closure__\n"
+ " _by_obj = _cl[_fv.index('_model_id_by_obj')].cell_contents\n"
+ " _registry = _cl[_fv.index('_model_registry')].cell_contents\n"
+ " _meta = _cl[_fv.index('_model_registry_meta')].cell_contents\n"
+ " obj_id = id(model)\n"
+ " if obj_id in _by_obj:\n"
+ " model_id = _by_obj.pop(obj_id)\n"
+ " dummy = torch.nn.Module()\n"
+ " _registry[model_id] = dummy\n"
+ " _meta[model_id] = {'size': 0, 'kind': 'other'}\n"
+ " _by_obj[id(dummy)] = model_id\n"
+ ' print(f"[TRELLIS2] Unregistered {label} from comfy-env registry (id={model_id})", file=sys.stderr)\n'
+ " except Exception:\n"
+ " pass\n"
+ "\n"
+ "\n"
+ "# Global model manager instance\n"
+ '_LAZY_MANAGER: Optional["LazyModelManager"] = None\n'
+)
+
+# -- lazy_manager.py unload_dinov3: add comfy-env unregistration
+_PATCH_TRELLIS_UNLOAD_DINOV3_ANCHOR = (
+ " def unload_dinov3(self):\n"
+ ' """Unload DinoV3 to free VRAM."""\n'
+ " if self.dinov3_model is not None:\n"
+ " self.dinov3_model.cpu()\n"
+ " self.dinov3_model = None\n"
+ " gc.collect()\n"
+ " torch.cuda.empty_cache()\n"
+ ' print(f"[TRELLIS2] DinoV3 offloaded", file=sys.stderr)\n'
+)
+
+_PATCH_TRELLIS_UNLOAD_DINOV3_REPLACE = (
+ " def unload_dinov3(self):\n"
+ ' """Unload DinoV3 to free VRAM."""\n'
+ " if self.dinov3_model is not None:\n"
+ " # Unregister the inner nn.Module (DINOv3ViTModel) from comfy-env.\n"
+ " # DinoV3FeatureExtractor is a plain class; comfy-env hooks on the\n"
+ " # inner .model which is the actual nn.Module that got .to(cuda).\n"
+ " inner = getattr(self.dinov3_model, 'model', self.dinov3_model)\n"
+ ' _unregister_from_comfy_env(inner, "dinov3")\n'
+ " self.dinov3_model.cpu()\n"
+ " self.dinov3_model = None\n"
+ " gc.collect()\n"
+ " torch.cuda.empty_cache()\n"
+ ' print(f"[TRELLIS2] DinoV3 offloaded", file=sys.stderr)\n'
+)
+
+# -- lazy_manager.py unload_shape_pipeline: iterate models + comfy-env unreg
+_PATCH_TRELLIS_UNLOAD_SHAPE_ANCHOR = (
+ " def unload_shape_pipeline(self):\n"
+ ' """Unload shape pipeline to free VRAM."""\n'
+ " if self.shape_pipeline is not None:\n"
+ " self.shape_pipeline = None\n"
+ " gc.collect()\n"
+ " torch.cuda.empty_cache()\n"
+ ' print(f"[TRELLIS2] Shape pipeline offloaded", file=sys.stderr)\n'
+)
+
+_PATCH_TRELLIS_UNLOAD_SHAPE_REPLACE = (
+ " def unload_shape_pipeline(self):\n"
+ ' """Unload shape pipeline to free VRAM."""\n'
+ " if self.shape_pipeline is not None:\n"
+ " # Force any remaining models off GPU before dropping pipeline\n"
+ " if hasattr(self.shape_pipeline, 'models'):\n"
+ " for key in list(self.shape_pipeline.models.keys()):\n"
+ " model = self.shape_pipeline.models[key]\n"
+ " if model is not None:\n"
+ ' _unregister_from_comfy_env(model, f"shape/{key}")\n'
+ " try:\n"
+ " model.cpu()\n"
+ " except Exception:\n"
+ " pass\n"
+ " self.shape_pipeline.models[key] = None\n"
+ " del model\n"
+ " self.shape_pipeline.models.clear()\n"
+ " self.shape_pipeline = None\n"
+ " gc.collect()\n"
+ " torch.cuda.empty_cache()\n"
+ ' print(f"[TRELLIS2] Shape pipeline offloaded", file=sys.stderr)\n'
+)
+
+# -- lazy_manager.py unload_texture_pipeline: iterate models + comfy-env unreg
+_PATCH_TRELLIS_UNLOAD_TEX_ANCHOR = (
+ " def unload_texture_pipeline(self):\n"
+ ' """Unload texture pipeline to free VRAM."""\n'
+ " if self.texture_pipeline is not None:\n"
+ " self.texture_pipeline = None\n"
+ " gc.collect()\n"
+ " torch.cuda.empty_cache()\n"
+ ' print(f"[TRELLIS2] Texture pipeline offloaded", file=sys.stderr)\n'
+)
+
+_PATCH_TRELLIS_UNLOAD_TEX_REPLACE = (
+ " def unload_texture_pipeline(self):\n"
+ ' """Unload texture pipeline to free VRAM."""\n'
+ " if self.texture_pipeline is not None:\n"
+ " # Force any remaining models off GPU before dropping pipeline\n"
+ " if hasattr(self.texture_pipeline, 'models'):\n"
+ " for key in list(self.texture_pipeline.models.keys()):\n"
+ " model = self.texture_pipeline.models[key]\n"
+ " if model is not None:\n"
+ ' _unregister_from_comfy_env(model, f"texture/{key}")\n'
+ " try:\n"
+ " model.cpu()\n"
+ " except Exception:\n"
+ " pass\n"
+ " self.texture_pipeline.models[key] = None\n"
+ " del model\n"
+ " self.texture_pipeline.models.clear()\n"
+ " self.texture_pipeline = None\n"
+ " gc.collect()\n"
+ " torch.cuda.empty_cache()\n"
+ ' print(f"[TRELLIS2] Texture pipeline offloaded", file=sys.stderr)\n'
+)
+
# --- Configuration: Dependencies Data ---
# Sizes are in MB.
DEPENDENCIES: Dict[str, Dict[str, Any]] = {
@@ -174,6 +485,80 @@ 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"]
},
+ # --- TRELLIS.2 ---
+ "cn_trellis2": {
+ "id": "cn_trellis2", "type": "node", "name": "ComfyUI TRELLIS.2",
+ "git_url": "https://github.com/PozzettiAndrea/ComfyUI-TRELLIS2.git",
+ "commit": "6b0b2148f45bbafa0b86bfd25c63602b63a7aae0",
+ "target_dir_relative": "custom_nodes",
+ "repo_name": "ComfyUI-TRELLIS2",
+ "license": "MIT", "packages": ["trellis2"],
+ "pip_packages": ["comfy-env==0.2.0"],
+ "run_install_script": True,
+ "post_clone_patches": [
+ {
+ "file": "__init__.py",
+ "marker": "StableGen patch: Path.is_junction",
+ "anchor": "from comfy_env import",
+ "patch": _PATCH_PY310_IS_JUNCTION,
+ },
+ {
+ "file": "prestartup_script.py",
+ "marker": "StableGen patch: Path.is_junction",
+ "anchor": "from comfy_env import",
+ "patch": _PATCH_PY310_IS_JUNCTION,
+ },
+ {
+ "file": "nodes/trellis_utils/lazy_manager.py",
+ "marker": "_LAZY_MANAGER.attn_backend != attn_backend",
+ "anchor": _PATCH_TRELLIS_ATTN_CMP_ANCHOR,
+ "patch": _PATCH_TRELLIS_ATTN_CMP_REPLACE,
+ "mode": "replace",
+ },
+ {
+ "file": "nodes/trellis_utils/stages.py",
+ "marker": "# Use no_grad to prevent autograd",
+ "anchor": _PATCH_TRELLIS_NOGRAD_ANCHOR,
+ "patch": _PATCH_TRELLIS_NOGRAD_REPLACE,
+ "mode": "replace",
+ },
+ {
+ "file": "nodes/trellis2/pipelines/base.py",
+ "marker": "_model_id_by_obj",
+ "anchor": _PATCH_TRELLIS_UNLOAD_CPU_ANCHOR,
+ "patch": _PATCH_TRELLIS_UNLOAD_CPU_REPLACE,
+ "mode": "replace",
+ },
+ {
+ "file": "nodes/trellis_utils/lazy_manager.py",
+ "marker": "_unregister_from_comfy_env",
+ "anchor": _PATCH_TRELLIS_COMFY_HELPER_ANCHOR,
+ "patch": _PATCH_TRELLIS_COMFY_HELPER_REPLACE,
+ "mode": "replace",
+ },
+ {
+ "file": "nodes/trellis_utils/lazy_manager.py",
+ "marker": '_unregister_from_comfy_env(self.dinov3_model',
+ "anchor": _PATCH_TRELLIS_UNLOAD_DINOV3_ANCHOR,
+ "patch": _PATCH_TRELLIS_UNLOAD_DINOV3_REPLACE,
+ "mode": "replace",
+ },
+ {
+ "file": "nodes/trellis_utils/lazy_manager.py",
+ "marker": '_unregister_from_comfy_env(model, f"shape/',
+ "anchor": _PATCH_TRELLIS_UNLOAD_SHAPE_ANCHOR,
+ "patch": _PATCH_TRELLIS_UNLOAD_SHAPE_REPLACE,
+ "mode": "replace",
+ },
+ {
+ "file": "nodes/trellis_utils/lazy_manager.py",
+ "marker": '_unregister_from_comfy_env(model, f"texture/',
+ "anchor": _PATCH_TRELLIS_UNLOAD_TEX_ANCHOR,
+ "patch": _PATCH_TRELLIS_UNLOAD_TEX_REPLACE,
+ "mode": "replace",
+ },
+ ]
+ },
}
# Define what items each menu option entails by listing package tags
@@ -206,7 +591,11 @@ MENU_PACKAGES: Dict[str, Dict[str, Any]] = {
'7': {"name": "[QWEN NUNCHAKU] Nunchaku Nodes + Model",
"tags": ["qwen_core", "qwen_nunchaku"],
"size_gb": 33.0,
- "description_suffix": "*Installs Qwen Core components plus Nunchaku nodes and the Int4 quantized model (12.7GB).*"}
+ "description_suffix": "*Installs Qwen Core components plus Nunchaku nodes and the Int4 quantized model (12.7GB).*"},
+ '8': {"name": "[TRELLIS.2] Image-to-3D Node",
+ "tags": ["trellis2"],
+ "size_gb": 0.1,
+ "description_suffix": "*Installs ComfyUI-TRELLIS2 custom node. Models are downloaded automatically on first use by the node.*"},
}
# --- Helper Functions ---
@@ -236,6 +625,136 @@ def get_comfyui_path_from_args() -> Path:
sys.exit(1)
return comfyui_path
+def find_comfyui_python(comfyui_path: Path) -> str:
+ """Detect the Python executable used by ComfyUI.
+
+ Checks (in order):
+ 1. Windows portable: python_embedded/python.exe
+ 2. Virtual-env: venv/Scripts/python.exe or venv/bin/python
+ 3. Fallback: the Python running this script (sys.executable)
+ """
+ # Windows portable build
+ embedded = comfyui_path / "python_embedded" / "python.exe"
+ if embedded.is_file():
+ return str(embedded)
+ # venv (Windows)
+ venv_win = comfyui_path / "venv" / "Scripts" / "python.exe"
+ if venv_win.is_file():
+ return str(venv_win)
+ # venv (Linux / macOS)
+ venv_unix = comfyui_path / "venv" / "bin" / "python"
+ if venv_unix.is_file():
+ return str(venv_unix)
+ # Fallback
+ return sys.executable
+
+
+def install_pip_packages(pip_packages: List[str], comfyui_path: Path):
+ """Install pip packages into ComfyUI's Python environment."""
+ python_exe = find_comfyui_python(comfyui_path)
+ print(f" Installing pip packages into ComfyUI Python: {python_exe}")
+ for pkg in pip_packages:
+ print(f" pip install {pkg} ...")
+ try:
+ subprocess.run(
+ [python_exe, "-m", "pip", "install", pkg],
+ check=True,
+ )
+ print(f" Successfully installed '{pkg}'.")
+ except subprocess.CalledProcessError as e:
+ print(f" ERROR: Failed to install '{pkg}' (exit code {e.returncode}).")
+ print(f" You may need to install it manually: pip install {pkg}")
+ except FileNotFoundError:
+ print(f" ERROR: Python executable not found at '{python_exe}'.")
+ print(f" Please install '{pkg}' manually into your ComfyUI Python environment.")
+ break
+
+
+def run_node_install_script(item_details: Dict[str, Any], comfyui_path: Path):
+ """Run a custom node's install.py to resolve all its dependencies.
+
+ Some nodes (e.g. ComfyUI-TRELLIS2) use comfy_env which manages CUDA
+ wheel installation from custom indices. Running their install.py
+ after cloning lets comfy_env handle everything automatically.
+ """
+ repo_path = comfyui_path / item_details["target_dir_relative"] / item_details["repo_name"]
+ install_script = repo_path / "install.py"
+ if not install_script.is_file():
+ print(f" WARNING: No install.py found in '{repo_path}'. Skipping dependency install.")
+ return
+
+ python_exe = find_comfyui_python(comfyui_path)
+ print(f" Running install.py for {item_details['name']}...")
+ print(f" {python_exe} {install_script}")
+ try:
+ subprocess.run(
+ [python_exe, str(install_script)],
+ check=True,
+ cwd=str(repo_path),
+ )
+ print(f" Successfully ran install.py for '{item_details['name']}'.")
+ except subprocess.CalledProcessError as e:
+ print(f" ERROR: install.py failed (exit code {e.returncode}).")
+ print(f" You may need to run it manually:")
+ print(f" cd {repo_path}")
+ print(f" {python_exe} install.py")
+ except FileNotFoundError:
+ print(f" ERROR: Python executable not found at '{python_exe}'.")
+
+
+def apply_post_clone_patches(item_details: Dict[str, Any], comfyui_path: Path):
+ """Apply compatibility patches to a cloned custom node repository.
+
+ Patches are defined in the dependency entry under 'post_clone_patches'.
+ Each patch dict has:
+ file – relative path inside the repo
+ marker – string checked to avoid re-applying
+ mode – 'insert' (default) or 'replace'
+
+ Insert mode (default):
+ anchor – text before which the patch is inserted
+ patch – code string to insert
+
+ Replace mode:
+ anchor – exact text to replace
+ patch – replacement text
+ """
+ patches = item_details.get("post_clone_patches")
+ if not patches:
+ return
+
+ repo_path = (comfyui_path / item_details["target_dir_relative"]
+ / item_details["repo_name"])
+
+ for p in patches:
+ target_file = repo_path / p["file"]
+ if not target_file.is_file():
+ print(f" WARNING: Patch target '{p['file']}' not found. Skipping.")
+ continue
+
+ content = target_file.read_text(encoding="utf-8")
+
+ # Already patched?
+ marker = p.get("marker", "")
+ if marker and marker in content:
+ print(f" Compat patch already applied to '{p['file']}'.")
+ continue
+
+ anchor = p.get("anchor", "")
+ patch_code = p.get("patch", "")
+ mode = p.get("mode", "insert")
+
+ if anchor and anchor in content:
+ if mode == "replace":
+ content = content.replace(anchor, patch_code, 1)
+ else:
+ content = content.replace(anchor, patch_code + anchor, 1)
+ target_file.write_text(content, encoding="utf-8")
+ print(f" Applied compat patch ({mode}) to '{p['file']}'.")
+ else:
+ print(f" WARNING: Anchor not found in '{p['file']}'. Patch skipped.")
+
+
def create_dir_if_not_exists(path: Path):
path.mkdir(parents=True, exist_ok=True)
@@ -316,20 +835,48 @@ def clone_git_repo(item_details: Dict[str, Any], comfyui_path: Path):
target_parent_dir = comfyui_path / item_details["target_dir_relative"]
repo_name = item_details["repo_name"]
final_repo_path = target_parent_dir / repo_name
+ pinned_commit = item_details.get("commit")
if final_repo_path.is_dir():
- print(f"INFO: Custom node directory '{repo_name}' already exists at '{final_repo_path}'. Skipping clone.")
- print(f" Please ensure it's the correct repository and up-to-date if you encounter issues (License: {item_details['license']}).")
+ # If a commit is pinned, verify the checkout matches
+ if pinned_commit:
+ try:
+ result = subprocess.run(
+ ['git', 'rev-parse', 'HEAD'], capture_output=True, text=True,
+ check=True, cwd=str(final_repo_path)
+ )
+ current = result.stdout.strip()
+ if current != pinned_commit:
+ print(f" Pinning '{repo_name}' to verified commit {pinned_commit[:12]}...")
+ subprocess.run(['git', 'fetch', 'origin'], check=True, cwd=str(final_repo_path))
+ subprocess.run(['git', 'checkout', pinned_commit], check=True, cwd=str(final_repo_path))
+ print(f" Successfully pinned to {pinned_commit[:12]}.")
+ else:
+ print(f"INFO: '{repo_name}' already at pinned commit {pinned_commit[:12]}. Skipping.")
+ except Exception as e:
+ print(f" WARNING: Could not verify/pin commit for '{repo_name}': {e}")
+ else:
+ print(f"INFO: Custom node directory '{repo_name}' already exists at '{final_repo_path}'. Skipping clone.")
+ print(f" Please ensure it's the correct repository and up-to-date if you encounter issues (License: {item_details['license']}).")
return
create_dir_if_not_exists(target_parent_dir)
print(f" Cloning: {item_details['name']} - License: {item_details['license']}")
print(f" From: {git_url}")
print(f" To: {final_repo_path}")
+ if pinned_commit:
+ print(f" Pinned commit: {pinned_commit[:12]}")
try:
subprocess.run(['git', 'clone', git_url, str(final_repo_path)], check=True, cwd=str(target_parent_dir))
- print(f" Successfully cloned '{repo_name}'.")
+
+ # Checkout pinned commit if specified
+ if pinned_commit:
+ subprocess.run(['git', 'checkout', pinned_commit], check=True, cwd=str(final_repo_path))
+ print(f" Successfully cloned and pinned '{repo_name}' to {pinned_commit[:12]}.")
+ else:
+ print(f" Successfully cloned '{repo_name}'.")
+
print(f" IMPORTANT: Restart ComfyUI if it was running to load this new custom node.")
except subprocess.CalledProcessError as e:
print(f" ERROR cloning {item_details['name']}: Git command failed with exit code {e.returncode}")
@@ -389,7 +936,7 @@ def main():
while True:
display_menu(comfyui_base_path)
- choice = input("Enter your choice (1-6, or q to quit): ").strip().lower()
+ choice = input("Enter your choice (1-8, or q to quit): ").strip().lower()
if choice == 'q':
print("Exiting installer.")
@@ -437,6 +984,15 @@ def main():
if item_details["type"] == "node":
target_full_path = comfyui_base_path / item_details["target_dir_relative"] / item_details["repo_name"]
clone_git_repo(item_details, comfyui_base_path)
+ # Install pip dependencies required by this custom node
+ if item_details.get("pip_packages"):
+ install_pip_packages(item_details["pip_packages"], comfyui_base_path)
+ # Apply compatibility patches (e.g. Python 3.10 polyfills)
+ if item_details.get("post_clone_patches"):
+ apply_post_clone_patches(item_details, comfyui_base_path)
+ # Run node's install.py for full dependency resolution (e.g. CUDA wheels)
+ if item_details.get("run_install_script"):
+ run_node_install_script(item_details, comfyui_base_path)
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)
diff --git a/stablegen/__init__.py b/stablegen/__init__.py
index ee28bfa..cb3aac4 100644
--- a/stablegen/__init__.py
+++ b/stablegen/__init__.py
@@ -4,7 +4,7 @@ from .stablegen import StableGenPanel, ApplyPreset, SavePreset, DeletePreset, ge
from .render_tools import BakeTextures, AddCameras, CloneCamera, MirrorCamera, ToggleCameraLabels, SwitchMaterial, ExportOrbitGIF, CollectCameraPrompts, CameraPromptItem, CameraOrderItem, SG_UL_CameraOrderList, SyncCameraOrder, MoveCameraOrder, ApplyCameraOrderPreset
from .debug_tools import debug_classes as _debug_classes
from .utils import AddHDRI, ApplyModifiers, CurvesToMesh
-from .generator import ComfyUIGenerate, Reproject, Regenerate, MirrorReproject
+from .generator import ComfyUIGenerate, Reproject, Regenerate, MirrorReproject, Trellis2Generate
import os
import requests
import json
@@ -46,6 +46,7 @@ classes = [
Reproject,
Regenerate,
MirrorReproject,
+ Trellis2Generate,
]
# Global caches for model lists fetched via API
@@ -54,6 +55,80 @@ _cached_lora_list = [("NONE_AVAILABLE", "None available", "Fetch models from ser
_cached_checkpoint_architecture = None
_pending_checkpoint_refresh_architecture = None
+
+def update_architecture_mode(self, context):
+ """Called when the user changes the architecture_mode dropdown.
+
+ For standard diffusion modes (sdxl / flux1 / qwen_image_edit) the hidden
+ ``model_architecture`` property is synced which in turn triggers the
+ existing ``update_combined`` callback (server check, checkpoint refresh,
+ etc.).
+
+ For trellis2 mode the diffusion backbone is only synced when the
+ active ``trellis2_texture_mode`` is itself a diffusion architecture.
+ """
+ scene = context.scene
+ mode = scene.architecture_mode
+
+ if mode != 'trellis2':
+ # Standard diffusion — sync backbone and let its callback refresh
+ if scene.model_architecture != mode:
+ scene.model_architecture = mode # triggers update_combined
+ else:
+ # Entering TRELLIS.2 mode — sync backbone only if texture_mode is diffusion
+ tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
+ if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit'):
+ if scene.model_architecture != tex_mode:
+ scene.model_architecture = tex_mode # triggers update_combined
+
+
+def update_trellis2_texture_mode(self, context):
+ """Called when the user changes the texture generation mode inside TRELLIS.2.
+
+ * Syncs ``trellis2_skip_texture`` (True when *not* native).
+ * If the chosen mode is a diffusion architecture and differs from the
+ current backbone, syncs ``model_architecture`` (which triggers a
+ checkpoint refresh).
+ * If ``generate_from`` is *prompt* and the user picks ``none`` or
+ ``native`` (no diffusion), auto-switch to ``sdxl`` so there is always
+ a diffusion model available for the image-generation step.
+ """
+ scene = context.scene
+ if getattr(scene, 'architecture_mode', '') != 'trellis2':
+ return
+
+ tex_mode = scene.trellis2_texture_mode
+
+ # When prompt mode, a diffusion backbone is mandatory
+ if getattr(scene, 'trellis2_generate_from', 'image') == 'prompt' and tex_mode in ('none', 'native'):
+ scene.trellis2_texture_mode = 'sdxl'
+ return # re-entrant; callback fires again
+
+ # Sync skip_texture flag used by the workflow code
+ scene.trellis2_skip_texture = (tex_mode != 'native')
+
+ if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit'):
+ if scene.model_architecture != tex_mode:
+ scene.model_architecture = tex_mode # triggers update_combined
+
+
+def update_trellis2_generate_from(self, context):
+ """Called when the user switches between Image and Prompt input mode.
+
+ If switching to *prompt* while the texture mode is ``none`` or
+ ``native`` (no diffusion model selected), force ``trellis2_texture_mode``
+ to ``sdxl`` so a checkpoint is available for image generation.
+ """
+ scene = context.scene
+ if getattr(scene, 'architecture_mode', '') != 'trellis2':
+ return
+
+ if scene.trellis2_generate_from == 'prompt':
+ tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
+ if tex_mode in ('none', 'native'):
+ scene.trellis2_texture_mode = 'sdxl' # triggers update_trellis2_texture_mode
+
+
def update_combined(self, context):
# This now primarily updates the preset status and might trigger Enum updates implicitly
prefs = context.preferences.addons[__package__].preferences
@@ -83,6 +158,12 @@ def update_combined(self, context):
else:
context.preferences.addons[__package__].preferences.server_online = True
+ # Auto-detect TRELLIS.2 availability
+ trellis2_found = check_trellis2_available(
+ context.preferences.addons[__package__].preferences.server_address, timeout=1.0
+ )
+ context.scene.trellis2_available = trellis2_found
+
update_parameters(self, context)
load_handler(None)
@@ -312,6 +393,9 @@ class CheckServerStatus(bpy.types.Operator):
bpy.ops.stablegen.refresh_lora_list('INVOKE_DEFAULT')
bpy.ops.stablegen.refresh_controlnet_mappings('INVOKE_DEFAULT')
load_handler(None)
+ # Auto-detect TRELLIS.2 availability
+ trellis2_found = check_trellis2_available(server_addr)
+ context.scene.trellis2_available = trellis2_found
else:
self.report({'ERROR'}, f"ComfyUI server unreachable or timed out at {server_addr}.")
@@ -457,6 +541,34 @@ def check_server_availability(server_address, timeout=0.5):
except requests.exceptions.ConnectionError:
print(" Initial server check failed: Connection Error.")
return False
+
+
+def check_trellis2_available(server_address, timeout=1.0):
+ """
+ Checks if TRELLIS.2 custom nodes are installed in ComfyUI by querying /object_info.
+
+ Args:
+ server_address (str): The address:port of the ComfyUI server.
+ timeout (float): Timeout in seconds.
+
+ Returns:
+ bool: True if TRELLIS.2 nodes are detected, False otherwise.
+ """
+ if not server_address:
+ return False
+ try:
+ url = f"http://{server_address}/object_info/Trellis2ImageToShape"
+ response = requests.get(url, timeout=timeout)
+ if response.status_code == 200:
+ data = response.json()
+ if 'Trellis2ImageToShape' in data:
+ print("[TRELLIS2] Auto-detect: TRELLIS.2 nodes found in ComfyUI.")
+ return True
+ print("[TRELLIS2] Auto-detect: TRELLIS.2 nodes NOT found in ComfyUI.")
+ return False
+ except Exception as e:
+ print(f"[TRELLIS2] Auto-detect failed: {e}")
+ return False
except requests.exceptions.RequestException as e:
# Other errors (like 404 on root) might still mean the server is *running*
# but depends on the chosen endpoint. /queue should be reliable.
@@ -1394,6 +1506,10 @@ def register():
)
if is_local_edit:
items.append(('original_render', 'Use original render', 'Uses the existing texture render from each camera viewpoint as IPAdapter reference', 2))
+ # TRELLIS.2 input image option — only when architecture is trellis2
+ if getattr(context.scene, 'architecture_mode', '') == 'trellis2':
+ items.append(('trellis2_input', 'Use TRELLIS.2 input image',
+ 'Uses the input image from TRELLIS.2 mesh generation as IPAdapter reference', 3))
return items
bpy.types.Scene.sequential_ipadapter_mode = bpy.props.EnumProperty(
name="IPAdapter Mode",
@@ -1913,7 +2029,53 @@ def register():
default='sdxl',
update=update_combined
)
-
+
+ bpy.types.Scene.architecture_mode = bpy.props.EnumProperty(
+ name="Architecture",
+ description="Select the overall generation architecture",
+ items=[
+ ('sdxl', 'SDXL', 'Stable Diffusion XL'),
+ ('flux1', 'Flux 1', 'Flux 1 architecture'),
+ ('qwen_image_edit', 'Qwen Image Edit', 'Qwen Image Edit architecture'),
+ ('trellis2', 'TRELLIS.2', 'Image to 3D mesh generation with TRELLIS.2'),
+ ],
+ default='sdxl',
+ update=update_architecture_mode
+ )
+
+ bpy.types.Scene.trellis2_generate_from = bpy.props.EnumProperty(
+ name="Generate From",
+ description="Source for the TRELLIS.2 input",
+ items=[
+ ('image', 'Image', 'Use an existing image as input'),
+ ('prompt', 'Prompt', 'Generate an image from the prompt first, then feed to TRELLIS.2'),
+ ],
+ default='image',
+ update=update_trellis2_generate_from
+ )
+
+ bpy.types.Scene.trellis2_texture_mode = bpy.props.EnumProperty(
+ name="Texture Mode",
+ description="How to generate textures for the 3D mesh",
+ items=[
+ ('none', 'None', 'Shape only, no texture generation'),
+ ('native', 'Native (TRELLIS.2)', 'Use TRELLIS.2 built-in texture generation'),
+ ('sdxl', 'SDXL', 'Use SDXL for camera-based texture projection'),
+ ('flux1', 'Flux 1', 'Use Flux 1 for camera-based texture projection'),
+ ('qwen_image_edit', 'Qwen Image Edit', 'Use Qwen for camera-based texture projection'),
+ ],
+ default='native',
+ update=update_trellis2_texture_mode
+ )
+
+ bpy.types.Scene.trellis2_camera_count = bpy.props.IntProperty(
+ name="Camera Count",
+ description="Number of cameras to place around the generated mesh for texture projection",
+ default=8,
+ min=2,
+ max=32
+ )
+
bpy.types.Scene.qwen_guidance_map_type = bpy.props.EnumProperty(
name="Guidance Map",
description="The type of guidance map to use for Qwen Image Edit",
@@ -2223,6 +2385,207 @@ def register():
bpy.types.Scene.lora_units_index = bpy.props.IntProperty(default=0)
bpy.app.handlers.load_post.append(load_handler)
+ # --- TRELLIS.2 Properties ---
+ bpy.types.Scene.trellis2_available = bpy.props.BoolProperty(
+ name="TRELLIS.2 Available",
+ description="Whether TRELLIS.2 nodes are available on the ComfyUI server (auto-detected)",
+ default=False
+ )
+ bpy.types.Scene.show_trellis2_params = bpy.props.BoolProperty(
+ name="Show TRELLIS.2 Section",
+ description="Toggle TRELLIS.2 Image-to-3D section",
+ default=False
+ )
+ bpy.types.Scene.show_trellis2_advanced = bpy.props.BoolProperty(
+ name="Show TRELLIS.2 Settings",
+ description="Toggle TRELLIS.2 advanced settings",
+ default=False
+ )
+ bpy.types.Scene.show_trellis2_mesh_settings = bpy.props.BoolProperty(
+ name="Show Mesh Generation Settings",
+ description="Toggle TRELLIS.2 mesh generation advanced settings",
+ default=False
+ )
+ bpy.types.Scene.show_trellis2_texture_settings = bpy.props.BoolProperty(
+ name="Show Texture Settings",
+ description="Toggle TRELLIS.2 native texture settings",
+ default=False
+ )
+ bpy.types.Scene.trellis2_last_input_image = bpy.props.StringProperty(
+ name="TRELLIS.2 Last Input Image",
+ description="Path to the image most recently used as TRELLIS.2 input (set automatically after generation)",
+ subtype='FILE_PATH',
+ default=""
+ )
+ bpy.types.Scene.qwen_use_trellis2_style = bpy.props.BoolProperty(
+ name="Use TRELLIS.2 Input as Style",
+ description="Use the TRELLIS.2 input image as the Qwen style reference",
+ default=False
+ )
+ # 3-tier progress pipeline flags (written by Trellis2Generate, read by UI)
+ bpy.types.Scene.trellis2_pipeline_active = bpy.props.BoolProperty(
+ name="TRELLIS.2 Pipeline Active",
+ description="True while the TRELLIS.2 pipeline (incl. texturing) is running",
+ default=False
+ )
+ bpy.types.Scene.trellis2_pipeline_phase_start_pct = bpy.props.FloatProperty(
+ name="Phase Start %",
+ description="Overall-% at which the current texturing phase begins",
+ default=0.0, min=0.0, max=100.0
+ )
+ bpy.types.Scene.trellis2_pipeline_total_phases = bpy.props.IntProperty(
+ name="Total Phases",
+ description="Total number of phases in the TRELLIS.2 pipeline",
+ default=3, min=1, max=3
+ )
+ bpy.types.Scene.trellis2_input_image = bpy.props.StringProperty(
+ name="Input Image",
+ description="Path to the reference image for 3D mesh generation",
+ subtype='FILE_PATH',
+ default=""
+ )
+ bpy.types.Scene.trellis2_resolution = bpy.props.EnumProperty(
+ name="Resolution",
+ description="Model resolution for generation. Higher values use more VRAM",
+ items=[
+ ('512', '512', 'Low resolution, fast, less VRAM'),
+ ('1024_cascade', '1024 Cascade', 'Medium resolution with cascade (recommended)'),
+ ('1536_cascade', '1536 Cascade', 'High resolution with cascade, most VRAM'),
+ ],
+ default='1024_cascade'
+ )
+ bpy.types.Scene.trellis2_vram_mode = bpy.props.EnumProperty(
+ name="VRAM Mode",
+ description="Controls model offloading strategy to manage VRAM usage",
+ items=[
+ ('keep_loaded', 'Keep Loaded', 'Keep all models in VRAM (fastest, ~12GB VRAM)'),
+ ('disk_offload', 'Disk Offload', 'Load models on demand from disk (recommended for <=16GB VRAM)'),
+ ],
+ default='disk_offload'
+ )
+ bpy.types.Scene.trellis2_attn_backend = bpy.props.EnumProperty(
+ name="Attention Backend",
+ description="Attention implementation to use. flash_attn is fastest but requires CUDA",
+ items=[
+ ('flash_attn', 'Flash Attention', 'Fastest (requires flash-attn package)'),
+ ('xformers', 'xFormers', 'Fast (requires xformers package)'),
+ ('sdpa', 'SDPA', 'PyTorch native, always available'),
+ ('sageattn', 'SageAttention', 'SageAttention backend (requires sageattn package)'),
+ ],
+ default='flash_attn'
+ )
+ bpy.types.Scene.trellis2_seed = bpy.props.IntProperty(
+ name="Seed",
+ description="Random seed for generation (0 = random)",
+ default=0,
+ min=0,
+ max=2147483647
+ )
+ bpy.types.Scene.trellis2_ss_guidance = bpy.props.FloatProperty(
+ name="SS Guidance",
+ description="Sparse structure CFG scale. Higher = stronger adherence to input image",
+ default=7.5,
+ min=1.0,
+ max=20.0,
+ step=10
+ )
+ bpy.types.Scene.trellis2_ss_steps = bpy.props.IntProperty(
+ name="SS Steps",
+ description="Sparse structure sampling steps. More steps = better quality but slower",
+ default=12,
+ min=1,
+ max=50
+ )
+ bpy.types.Scene.trellis2_shape_guidance = bpy.props.FloatProperty(
+ name="Shape Guidance",
+ description="Shape CFG scale. Higher = stronger adherence to input image",
+ default=7.5,
+ min=1.0,
+ max=20.0,
+ step=10
+ )
+ bpy.types.Scene.trellis2_shape_steps = bpy.props.IntProperty(
+ name="Shape Steps",
+ description="Shape sampling steps. More steps = better quality but slower",
+ default=12,
+ min=1,
+ max=50
+ )
+ bpy.types.Scene.trellis2_tex_guidance = bpy.props.FloatProperty(
+ name="Texture Guidance",
+ description="Texture CFG scale. Higher = stronger adherence to input image",
+ default=7.5,
+ min=1.0,
+ max=20.0,
+ step=10
+ )
+ bpy.types.Scene.trellis2_tex_steps = bpy.props.IntProperty(
+ name="Texture Steps",
+ description="Texture sampling steps. More steps = better quality but slower",
+ default=12,
+ min=1,
+ max=50
+ )
+ bpy.types.Scene.trellis2_max_tokens = bpy.props.IntProperty(
+ name="Max Tokens",
+ description="Maximum tokens for shape generation. Lower = less VRAM. 24576 (~6GB) recommended for 16GB GPUs",
+ default=24576,
+ min=16384,
+ max=65536,
+ step=4096
+ )
+ bpy.types.Scene.trellis2_texture_size = bpy.props.IntProperty(
+ name="Texture Size",
+ description="Output texture resolution in pixels",
+ default=2048,
+ min=512,
+ max=8192,
+ step=512
+ )
+ bpy.types.Scene.trellis2_decimation = bpy.props.IntProperty(
+ name="Decimation Target",
+ description="Target polygon count for mesh simplification. Lower = simpler mesh",
+ default=100000,
+ min=1000,
+ max=5000000,
+ step=10000
+ )
+ bpy.types.Scene.trellis2_remesh = bpy.props.BoolProperty(
+ name="Remesh",
+ description="Apply remeshing for cleaner topology",
+ default=True
+ )
+ bpy.types.Scene.trellis2_skip_texture = bpy.props.BoolProperty(
+ name="Skip Texture",
+ description="Export shape-only mesh (no PBR textures). Much faster and uses less VRAM",
+ default=False
+ )
+ bpy.types.Scene.trellis2_low_vram = bpy.props.BoolProperty(
+ name="Low VRAM BG Removal",
+ description="Use low VRAM mode for background removal (BiRefNet)",
+ default=True
+ )
+ bpy.types.Scene.trellis2_background_color = bpy.props.EnumProperty(
+ name="Background Color",
+ description="Background color for image conditioning",
+ items=[
+ ('black', 'Black', 'Black background (default)'),
+ ('gray', 'Gray', 'Gray background'),
+ ('white', 'White', 'White background'),
+ ],
+ default='black'
+ )
+ bpy.types.Scene.trellis2_include_1024 = bpy.props.BoolProperty(
+ name="Include 1024 Conditioning",
+ description="Include 1024-resolution conditioning. Disable for lower VRAM usage with 512 resolution",
+ default=True
+ )
+ bpy.types.Scene.trellis2_fill_holes = bpy.props.BoolProperty(
+ name="Fill Holes",
+ description="Fill holes in the mesh during simplification (shape-only mode)",
+ default=True
+ )
+
def unregister():
"""
Unregisters the addon.
@@ -2308,6 +2671,7 @@ def unregister():
del bpy.types.Scene.clip_skip
del bpy.types.Scene.stablegen_preset
del bpy.types.Scene.model_architecture
+ del bpy.types.Scene.architecture_mode
del bpy.types.Scene.output_timestamp
del bpy.types.Scene.camera_prompts
del bpy.types.Scene.use_camera_prompts
@@ -2345,6 +2709,26 @@ def unregister():
del bpy.types.Scene.qwen_context_fallback_dilation
del bpy.types.Scene.qwen_timestep_zero_ref
+ # --- TRELLIS.2 Properties ---
+ trellis2_props = [
+ 'trellis2_available', 'show_trellis2_params', 'show_trellis2_advanced',
+ 'show_trellis2_mesh_settings', 'show_trellis2_texture_settings',
+ 'trellis2_last_input_image', 'qwen_use_trellis2_style',
+ 'trellis2_pipeline_active', 'trellis2_pipeline_phase_start_pct',
+ 'trellis2_pipeline_total_phases',
+ 'trellis2_generate_from', 'trellis2_texture_mode', 'trellis2_camera_count',
+ 'trellis2_input_image', 'trellis2_resolution', 'trellis2_vram_mode',
+ 'trellis2_attn_backend', 'trellis2_seed', 'trellis2_ss_guidance',
+ 'trellis2_ss_steps', 'trellis2_shape_guidance', 'trellis2_shape_steps',
+ 'trellis2_tex_guidance', 'trellis2_tex_steps', 'trellis2_max_tokens',
+ 'trellis2_texture_size', 'trellis2_decimation', 'trellis2_remesh',
+ 'trellis2_skip_texture', 'trellis2_low_vram', 'trellis2_background_color',
+ 'trellis2_include_1024', 'trellis2_fill_holes',
+ ]
+ for prop in trellis2_props:
+ if hasattr(bpy.types.Scene, prop):
+ delattr(bpy.types.Scene, prop)
+
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
diff --git a/stablegen/generator.py b/stablegen/generator.py
index e154cf6..fed9902 100644
--- a/stablegen/generator.py
+++ b/stablegen/generator.py
@@ -1,5 +1,6 @@
import os
import bpy # pylint: disable=import-error
+import mathutils # pylint: disable=import-error
import numpy as np
import cv2
@@ -1022,6 +1023,11 @@ class ComfyUIGenerate(bpy.types.Operator):
# Get info for IPAdapter reference image
if context.scene.use_ipadapter:
ipadapter_ref_info = self._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(context.scene.ipadapter_image))
+ elif context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_mode == 'trellis2_input':
+ # Use the TRELLIS.2 input image as IPAdapter reference
+ t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
+ if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
+ ipadapter_ref_info = self._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
elif context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_mode == 'original_render' and context.scene.generation_method == 'local_edit':
# Use the existing texture render from this camera's viewpoint as IPAdapter reference
ipadapter_ref_info = self._get_uploaded_image_info(context, "inpaint", subtype="render", camera_id=camera_id)
@@ -1035,7 +1041,8 @@ class ComfyUIGenerate(bpy.types.Operator):
# Generate image without ControlNet if needed
if context.scene.generation_mode == 'standard' and camera_id == 0 and (context.scene.generation_method == 'sequential' or context.scene.generation_method in ('refine', 'local_edit'))\
- and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and context.scene.sequential_ipadapter_mode == 'first':
+ and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and context.scene.sequential_ipadapter_mode == 'first'\
+ and context.scene.sequential_ipadapter_mode != 'trellis2_input':
self._stage = "Generating Reference Image"
# Don't use ControlNet for the first image if sequential_ipadapter_regenerate_wo_controlnet is enabled
if context.scene.sequential_ipadapter_regenerate_wo_controlnet:
@@ -1786,3 +1793,402 @@ class ComfyUIGenerate(bpy.types.Operator):
# If a *required* image fails to upload, the workflow submission
# will likely fail later when ComfyUI can't find the input.
return None
+
+
+class Trellis2Generate(bpy.types.Operator):
+ """Generate a 3D mesh from a reference image using TRELLIS.2 via ComfyUI.
+
+ Requires the PozzettiAndrea/ComfyUI-TRELLIS2 custom node pack installed on the ComfyUI server.
+ Uploads the input image, runs the full TRELLIS.2 pipeline (background removal, conditioning,
+ shape generation, texture generation, GLB export), downloads the resulting GLB file, and
+ imports it into the Blender scene."""
+ bl_idname = "object.trellis2_generate"
+ bl_label = "Generate 3D Mesh (TRELLIS.2)"
+ bl_options = {'REGISTER', 'UNDO'}
+
+ _timer = None
+ _thread = None
+ _error = None
+ _glb_data = None
+ _is_running = False
+ _progress = 0.0
+ _stage = "Initializing"
+ workflow_manager: object = None
+
+ # ── 3-tier progress ──────────────────────────────────────────────
+ _overall_progress: float = 0.0
+ _overall_stage: str = "Initializing"
+ _phase_progress: float = 0.0
+ _phase_stage: str = ""
+ _detail_progress: float = 0.0
+ _detail_stage: str = ""
+ _current_phase: int = 0
+ _total_phases: int = 3 # 2 when gen_from == 'image'
+
+ def _update_overall(self):
+ """Recompute *_overall_progress* from current phase + phase progress."""
+ if self._total_phases == 3:
+ starts = {1: 0, 2: 15, 3: 65}
+ weights = {1: 15, 2: 50, 3: 35}
+ elif self._total_phases == 2:
+ starts = {1: 0, 2: 65}
+ weights = {1: 65, 2: 35}
+ else: # Single phase — scale to full 0-100
+ starts = {1: 0}
+ weights = {1: 100}
+ s = starts.get(self._current_phase, 0)
+ w = weights.get(self._current_phase, 0)
+ self._overall_progress = s + (self._phase_progress / 100.0) * w
+ self._overall_progress = max(0.0, min(self._overall_progress, 100.0))
+ # Keep legacy _progress in sync for any code that reads it
+ self._progress = self._overall_progress
+
+ @classmethod
+ def poll(cls, context):
+ if cls._is_running:
+ return True # Allow cancellation
+ addon_prefs = context.preferences.addons[__package__].preferences
+ if not addon_prefs.server_address or not addon_prefs.server_online:
+ return False
+ if not os.path.exists(addon_prefs.output_dir):
+ return False
+ if bpy.app.online_access == False:
+ return False
+ # Check that an input image is specified (only required when generate_from = image)
+ gen_from = getattr(context.scene, 'trellis2_generate_from', 'image')
+ if gen_from == 'image' and not context.scene.trellis2_input_image:
+ return False
+ # Check no other generation is running
+ for window in context.window_manager.windows:
+ for op in window.modal_operators:
+ if op.bl_idname in ('OBJECT_OT_test_stable', 'OBJECT_OT_trellis2_generate',
+ 'OBJECT_OT_bake_textures', 'OBJECT_OT_add_cameras'):
+ return False
+ return True
+
+ def execute(self, context):
+ if Trellis2Generate._is_running:
+ # Cancel
+ Trellis2Generate._is_running = False
+ self.report({'WARNING'}, "TRELLIS.2 generation cancelled")
+ return {'FINISHED'}
+
+ scene = context.scene
+ gen_from = getattr(scene, 'trellis2_generate_from', 'image')
+ tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
+
+ # Validate input image (only required in image mode)
+ image_path = None
+ if gen_from == 'image':
+ image_path = bpy.path.abspath(scene.trellis2_input_image)
+ if not os.path.exists(image_path):
+ self.report({'ERROR'}, f"Input image not found: {image_path}")
+ return {'CANCELLED'}
+
+ Trellis2Generate._is_running = True
+ self._error = None
+ self._glb_data = None
+ self._progress = 0.0
+ self._stage = "Initializing"
+ self._texture_mode = tex_mode
+ self.workflow_manager = WorkflowManager(self)
+
+ # 3-tier progress init
+ has_txt2img = (gen_from == 'prompt')
+ has_texturing = (tex_mode in ('sdxl', 'flux1', 'qwen_image_edit'))
+ if has_txt2img and has_texturing:
+ self._total_phases = 3
+ elif has_txt2img or has_texturing:
+ self._total_phases = 2
+ else:
+ self._total_phases = 1
+ self._current_phase = 0
+ self._overall_progress = 0.0
+ self._overall_stage = "Initializing"
+ self._phase_progress = 0.0
+ self._phase_stage = ""
+ self._detail_progress = 0.0
+ self._detail_stage = ""
+
+ # Compute revision directory on the main thread (may write output_timestamp)
+ from .utils import get_generation_dirs
+ gen_dirs = get_generation_dirs(context)
+ revision_dir = gen_dirs.get("revision", "")
+
+ # Start generation in background thread
+ self._thread = threading.Thread(
+ target=self._run_trellis2,
+ args=(context, image_path, gen_from, revision_dir),
+ daemon=True
+ )
+ self._thread.start()
+
+ # Register modal timer
+ context.window_manager.modal_handler_add(self)
+ self._timer = context.window_manager.event_timer_add(0.5, window=context.window)
+
+ return {'RUNNING_MODAL'}
+
+ def modal(self, context, event):
+ if event.type != 'TIMER':
+ return {'PASS_THROUGH'}
+
+ # Redraw UI for progress updates
+ for area in context.screen.areas:
+ area.tag_redraw()
+
+ # Check if thread is still running
+ if self._thread and self._thread.is_alive():
+ return {'RUNNING_MODAL'}
+
+ # Thread finished - clean up timer
+ context.window_manager.event_timer_remove(self._timer)
+ self._timer = None
+ Trellis2Generate._is_running = False
+
+ if self._error:
+ self.report({'ERROR'}, f"TRELLIS.2 error: {self._error}")
+ return {'CANCELLED'}
+
+ if self._glb_data is None or (isinstance(self._glb_data, dict) and "error" in self._glb_data):
+ error_msg = self._glb_data.get("error", "Unknown error") if isinstance(self._glb_data, dict) else "No data received"
+ self.report({'ERROR'}, f"TRELLIS.2 failed: {error_msg}")
+ return {'CANCELLED'}
+
+ # Save GLB to revision directory and import into Blender
+ try:
+ from .utils import get_generation_dirs
+ gen_dirs = get_generation_dirs(context)
+ save_dir = gen_dirs.get("revision", "")
+ if not save_dir:
+ save_dir = context.preferences.addons[__package__].preferences.output_dir
+ os.makedirs(save_dir, exist_ok=True)
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
+ glb_filename = f"trellis2_{timestamp}.glb"
+ glb_path = os.path.join(save_dir, glb_filename)
+
+ with open(glb_path, 'wb') as f:
+ f.write(self._glb_data)
+
+ # Store the TRELLIS.2 input image path for downstream use (IPAdapter/Qwen style)
+ input_img = getattr(self, '_input_image_path', None)
+ if input_img:
+ context.scene.trellis2_last_input_image = input_img
+
+ print(f"[TRELLIS2] Saved GLB to: {glb_path} ({len(self._glb_data)} bytes)")
+
+ # Import GLB into Blender
+ bpy.ops.import_scene.gltf(filepath=glb_path)
+
+ # --- Phase 3: If diffusion texturing, auto-place cameras + start generation ---
+ tex_mode = getattr(self, '_texture_mode', 'native')
+ if tex_mode in ('sdxl', 'flux1', 'qwen_image_edit'):
+ # Place cameras NOW (while operator context is still valid)
+ camera_count = getattr(context.scene, 'trellis2_camera_count', 8)
+ imported_objects = [obj for obj in context.selected_objects]
+
+ if imported_objects:
+ bpy.context.view_layer.objects.active = imported_objects[0]
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in imported_objects:
+ obj.select_set(True)
+
+ # Force viewport to standard front view so AddCameras uses a
+ # consistent reference direction for sorting and auto-prompts.
+ # TRELLIS.2 always imports meshes in standard orientation so the
+ # viewport should match.
+ for area in context.screen.areas:
+ if area.type == 'VIEW_3D':
+ for space in area.spaces:
+ if space.type == 'VIEW_3D':
+ rv3d = space.region_3d
+ if rv3d:
+ # Blender front view (Numpad 1): -Y looking at +Y
+ rv3d.view_rotation = mathutils.Quaternion(
+ (0.7071068, 0.7071068, 0.0, 0.0)
+ )
+ rv3d.view_perspective = 'PERSP'
+ break
+
+ try:
+ bpy.ops.object.add_cameras(
+ placement_mode='normal_weighted',
+ num_cameras=camera_count,
+ auto_prompts=True,
+ review_placement=False,
+ purge_others=True,
+ exclude_bottom=True,
+ )
+ print(f"[TRELLIS2] Placed {camera_count} cameras (normal-weighted)")
+ except Exception as cam_err:
+ print(f"[TRELLIS2] Warning: Camera placement failed: {cam_err}")
+ traceback.print_exc()
+
+ # Defer texture generation so Blender digests the new cameras
+ self._schedule_texture_generation(context)
+ self.report({'INFO'}, f"TRELLIS.2: Mesh imported. Camera placement done, texture generation starting...")
+ else:
+ self.report({'INFO'}, f"TRELLIS.2: Imported 3D mesh from {glb_filename}")
+
+ return {'FINISHED'}
+
+ except Exception as e:
+ self.report({'ERROR'}, f"Failed to import GLB: {e}")
+ traceback.print_exc()
+ return {'CANCELLED'}
+
+ def _schedule_texture_generation(self, context):
+ """Defer texture generation via a timer so Blender can digest the new cameras.
+
+ Camera placement has already happened in ``modal()``. This only
+ selects all cameras and starts ``object.test_stable``.
+ Sets scene-level pipeline flags so the UI can show the overall
+ progress bar on top of the ComfyUIGenerate bars.
+ """
+ # Compute the overall-% at which texturing begins
+ if self._total_phases == 3:
+ phase_start = 65.0
+ elif self._total_phases == 2:
+ phase_start = 65.0
+ else:
+ phase_start = 0.0
+
+ scene = context.scene
+ scene.trellis2_pipeline_active = True
+ scene.trellis2_pipeline_phase_start_pct = phase_start
+ scene.trellis2_pipeline_total_phases = self._total_phases
+
+ def _deferred_generate():
+ try:
+ # Defensive VRAM flush before loading the diffusion checkpoint.
+ # The TRELLIS post-generation flush should have freed VRAM,
+ # but if it silently failed the models are still resident (Gap C).
+ # Also clear history to release cached node outputs.
+ try:
+ srv = bpy.context.preferences.addons[__package__].preferences.server_address
+ # 1. Set unload flags
+ flush_data = json.dumps({"unload_models": True, "free_memory": True}).encode('utf-8')
+ flush_req = urllib.request.Request(
+ f"http://{srv}/free", data=flush_data,
+ headers={"Content-Type": "application/json"}
+ )
+ urllib.request.urlopen(flush_req, timeout=10)
+ # 2. Clear history/cache
+ hist_data = json.dumps({"clear": True}).encode('utf-8')
+ hist_req = urllib.request.Request(
+ f"http://{srv}/history", data=hist_data,
+ headers={"Content-Type": "application/json"}
+ )
+ urllib.request.urlopen(hist_req, timeout=10)
+ # 3. Wait for VRAM release
+ import time
+ time.sleep(3)
+ print("[TRELLIS2] Pre-texturing VRAM flush sent (unload+history clear)")
+ except Exception as flush_err:
+ print(f"[TRELLIS2] Pre-texturing flush warning: {flush_err}")
+
+ bpy.ops.object.select_all(action='DESELECT')
+ for obj in bpy.context.scene.objects:
+ if obj.type == 'CAMERA':
+ obj.select_set(True)
+
+ bpy.ops.object.test_stable('INVOKE_DEFAULT')
+ print("[TRELLIS2] Texture generation started")
+ except Exception as e:
+ print(f"[TRELLIS2] Warning: Texture generation failed to start: {e}")
+ traceback.print_exc()
+ return None # Run once
+
+ def _pipeline_watcher():
+ """Clear the pipeline flag when texturing finishes."""
+ if bpy.context.scene.generation_status == 'idle':
+ bpy.context.scene.trellis2_pipeline_active = False
+ print("[TRELLIS2] Pipeline complete — overall bar removed")
+ return None # Stop timer
+ return 1.0 # Check again in 1s
+
+ bpy.app.timers.register(_deferred_generate, first_interval=0.5)
+ bpy.app.timers.register(_pipeline_watcher, first_interval=2.0)
+
+ def _run_trellis2(self, context, image_path, gen_from, revision_dir):
+ """Background thread: runs the TRELLIS.2 pipeline.
+
+ If *gen_from* is ``'prompt'`` the method first generates an input
+ image via a lightweight txt2img ComfyUI workflow, saves it to the
+ revision directory and passes that to the TRELLIS.2 mesh workflow.
+ """
+ try:
+ # --- Phase 1: Image acquisition ---
+ if gen_from == 'prompt':
+ self._current_phase = 1
+ self._phase_stage = "Generating Input Image"
+ self._phase_progress = 0
+ self._detail_progress = 0
+ self._detail_stage = "Flushing stale models"
+ self._overall_stage = f"Phase 1/{self._total_phases}: Input Image"
+ self._update_overall()
+
+ # Flush any stale models from prior runs before loading a
+ # diffusion checkpoint for txt2img (Gap A).
+ try:
+ server_addr = context.preferences.addons[__package__].preferences.server_address
+ self.workflow_manager._flush_comfyui_vram(server_addr, label="Pre-txt2img")
+ except Exception:
+ pass
+
+ self._detail_stage = "Starting txt2img"
+
+ img_result = self.workflow_manager.generate_txt2img(context)
+ if isinstance(img_result, dict) and "error" in img_result:
+ self._error = f"txt2img failed: {img_result['error']}"
+ return
+
+ # Phase 1 complete
+ self._phase_progress = 100
+ self._update_overall()
+
+ # Save the generated image bytes to the revision directory
+ save_dir = revision_dir if revision_dir else (
+ context.preferences.addons[__package__].preferences.output_dir
+ )
+ os.makedirs(save_dir, exist_ok=True)
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
+ image_path = os.path.join(save_dir, f"trellis2_input_{timestamp}.png")
+ with open(image_path, 'wb') as f:
+ f.write(img_result)
+ print(f"[TRELLIS2] Saved txt2img result to: {image_path}")
+
+ # Flush VRAM so the txt2img model (SDXL/Flux) is evicted
+ # before TRELLIS loads its own models via raw PyTorch.
+ # Without this, both models coexist and OOM on <=16 GB GPUs.
+ # (Gap B – between txt2img and TRELLIS Phase 1)
+ self._detail_stage = "Flushing txt2img models"
+ try:
+ server_addr = context.preferences.addons[__package__].preferences.server_address
+ self.workflow_manager._flush_comfyui_vram(server_addr, label="Post-txt2img")
+ except Exception:
+ pass
+
+ # --- Phase 2 (or 1 if no txt2img): TRELLIS.2 mesh generation ---
+ trellis_phase = 2 if gen_from == 'prompt' else 1
+ self._current_phase = trellis_phase
+ self._phase_stage = "TRELLIS.2 Mesh Generation"
+ self._phase_progress = 0
+ self._detail_progress = 0
+ self._detail_stage = "Uploading image"
+ self._overall_stage = f"Phase {trellis_phase}/{self._total_phases}: 3D Mesh"
+ self._update_overall()
+
+ # Store the final input image path for later use (IPAdapter/Qwen style)
+ self._input_image_path = image_path
+
+ result = self.workflow_manager.generate_trellis2(context, image_path)
+
+ if isinstance(result, dict) and "error" in result:
+ self._error = result["error"]
+ else:
+ self._glb_data = result
+
+ except Exception as e:
+ self._error = str(e)
+ traceback.print_exc()
diff --git a/stablegen/stablegen.py b/stablegen/stablegen.py
index 86adfb6..d80e31a 100644
--- a/stablegen/stablegen.py
+++ b/stablegen/stablegen.py
@@ -270,66 +270,178 @@ class StableGenPanel(bpy.types.Panel):
elif not addon_prefs.server_online:
config_error_message = "Cannot reach server"
+ # Determine if we are in TRELLIS.2 mode (used by generate button & later sections)
+ _arch_mode = getattr(scene, 'architecture_mode', 'sdxl')
+ _is_trellis2_mode = (_arch_mode == 'trellis2')
+
action_row = layout.row()
action_row.scale_y = 2.0 # Scale the row vertically
- if config_error_message:
- # Split the row to have the error message/disabled button and the refresh button
- if config_error_message == "Cannot reach server":
- split = action_row.split(factor=0.85, align=True) # Adjust factor as needed
- split.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="ERROR") # Use ERROR icon
- # Use the operator from __init__.py
- split.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
- else:
- action_row.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="ERROR")
- action_row.enabled = False
- else:
- action_row.enabled = True
- if not bpy.app.online_access:
- action_row.operator("object.test_stable", text="Enable online access in preferences", icon="ERROR")
+ if _is_trellis2_mode:
+ # --- TRELLIS.2 Generate Button ---
+ trellis2_op = next(
+ (op for win in context.window_manager.windows
+ for op in win.modal_operators
+ if op.bl_idname == 'OBJECT_OT_trellis2_generate'),
+ None
+ )
+ # Also look for ComfyUIGenerate running as the texturing phase
+ comfy_tex_op = next(
+ (op for win in context.window_manager.windows
+ for op in win.modal_operators
+ if op.bl_idname == 'OBJECT_OT_test_stable'),
+ None
+ ) if not trellis2_op else None
+
+ if config_error_message:
+ if config_error_message == "Cannot reach server":
+ split = action_row.split(factor=0.85, align=True)
+ split.operator("object.trellis2_generate", text="Cannot generate: " + config_error_message, icon="ERROR")
+ split.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
+ else:
+ action_row.operator("object.trellis2_generate", text="Cannot generate: " + config_error_message, icon="ERROR")
+ action_row.enabled = False
+
+ elif trellis2_op:
+ # ── Phases 1 & 2: Trellis2Generate is alive ──
+ action_row.operator("object.trellis2_generate", text="Cancel TRELLIS.2", icon="CANCEL")
+ progress_col = layout.column()
+
+ # Bar 1 — Overall
+ overall_pct = getattr(trellis2_op, '_overall_progress', 0)
+ overall_label = getattr(trellis2_op, '_overall_stage', 'Initializing')
+ progress_col.progress(
+ text=f"{overall_label} ({overall_pct:.0f}%)",
+ factor=max(0.0, min(overall_pct / 100.0, 1.0))
+ )
+
+ # Bar 2 — Phase
+ phase_pct = getattr(trellis2_op, '_phase_progress', 0)
+ phase_label = getattr(trellis2_op, '_phase_stage', '')
+ if phase_label:
+ progress_col.progress(
+ text=f"{phase_label} ({phase_pct:.0f}%)",
+ factor=max(0.0, min(phase_pct / 100.0, 1.0))
+ )
+
+ # Bar 3 — Detail (only when there's actual sampler progress)
+ detail_pct = getattr(trellis2_op, '_detail_progress', 0)
+ detail_label = getattr(trellis2_op, '_detail_stage', '')
+ if detail_label and detail_pct > 0:
+ progress_col.progress(
+ text=f"{detail_label} ({detail_pct:.0f}%)",
+ factor=max(0.0, min(detail_pct / 100.0, 1.0))
+ )
+
+ elif comfy_tex_op and getattr(scene, 'trellis2_pipeline_active', False):
+ # ── Phase 3: Texturing via ComfyUIGenerate ──
+ action_row.operator("object.test_stable", text="Cancel Texturing", icon="CANCEL")
+ progress_col = layout.column()
+
+ # Compute overall from scene pipeline props + ComfyUI progress
+ phase_start = getattr(scene, 'trellis2_pipeline_phase_start_pct', 65.0)
+ phase_weight = 100.0 - phase_start
+ comfy_progress = getattr(comfy_tex_op, '_progress', 0) / 100.0
+ total_imgs = getattr(comfy_tex_op, '_total_images', 0)
+ cur_img_idx = getattr(comfy_tex_op, '_current_image', 0)
+ if total_imgs > 1:
+ comfy_overall = (cur_img_idx + comfy_progress) / total_imgs
+ else:
+ comfy_overall = comfy_progress
+ overall_pct = phase_start + comfy_overall * phase_weight
+ overall_pct = max(0.0, min(overall_pct, 100.0))
+
+ total_phases = getattr(scene, 'trellis2_pipeline_total_phases', 3)
+ # Bar 1 — Overall
+ progress_col.progress(
+ text=f"Phase {total_phases}/{total_phases}: Texturing ({overall_pct:.0f}%)",
+ factor=max(0.0, min(overall_pct / 100.0, 1.0))
+ )
+
+ # Bar 2 — Per-image (same as normal ComfyUI bar)
+ stage = getattr(comfy_tex_op, '_stage', 'Generating')
+ img_pct = getattr(comfy_tex_op, '_progress', 0)
+ progress_col.progress(
+ text=f"{stage} ({img_pct:.0f}%)",
+ factor=max(0.0, min(img_pct / 100.0, 1.0))
+ )
+
+ # Bar 3 — Image N/M (same as normal ComfyUI overall bar)
+ if total_imgs > 1:
+ cur_img = min(cur_img_idx + 1, total_imgs)
+ img_overall = max(0.0, min(comfy_overall, 1.0))
+ progress_col.progress(
+ text=f"Overall: Image {cur_img}/{total_imgs}",
+ factor=img_overall
+ )
+
+ elif scene.trellis2_generate_from == 'image' and not scene.trellis2_input_image:
+ action_row.operator("object.trellis2_generate", text="Select an image first", icon="ERROR")
action_row.enabled = False
- elif not scene.model_name or scene.model_name == "NONE_FOUND":
- action_row.operator("object.test_stable", text="Cannot generate: Model Directory Empty", icon="ERROR")
+ elif not getattr(scene, 'trellis2_available', False):
+ action_row.operator("object.trellis2_generate", text="TRELLIS.2 nodes not found on server", icon="ERROR")
action_row.enabled = False
- elif scene.generation_status == 'idle':
- # Check if any cameras are selected and if there is existing output
- selected_cameras = [obj for obj in context.selected_objects if obj.type == 'CAMERA']
- if not selected_cameras or scene.get("output_timestamp") == "":
- action_row.operator("object.test_stable", text="Generate", icon="PLAY")
+ else:
+ action_row.operator("object.trellis2_generate", text="Generate 3D Mesh", icon="MESH_ICOSPHERE")
+ else:
+ # --- Standard Diffusion Generate Button ---
+ if config_error_message:
+ # Split the row to have the error message/disabled button and the refresh button
+ if config_error_message == "Cannot reach server":
+ split = action_row.split(factor=0.85, align=True) # Adjust factor as needed
+ split.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="ERROR") # Use ERROR icon
+ # Use the operator from __init__.py
+ split.operator("stablegen.check_server_status", text="", icon='FILE_REFRESH')
else:
- # Use the regenerate operator
- action_row.operator("object.stablegen_regenerate", text="Regenerate Selected Views", icon="PLAY")
- elif scene.generation_status == 'running':
- action_row.operator("object.test_stable", text="Cancel Generation", icon="CANCEL")
-
- operator_instance = next((op for win in context.window_manager.windows for op in win.modal_operators if op.bl_idname == 'OBJECT_OT_test_stable'), None)
- if operator_instance:
- progress_col = layout.column()
- progress_text = f"{getattr(operator_instance, '_stage', 'Generating')} ({getattr(operator_instance, '_progress', 0):.0f}%)"
- progress_factor = getattr(operator_instance, '_progress', 0) / 100.0
- progress_col.progress(text=progress_text, factor=max(0.0, min(progress_factor, 1.0))) # Ensure factor is <= 1.0 (logic maintained)
-
- total_images = getattr(operator_instance, '_total_images', 0)
- if total_images > 1:
- current_image_idx = getattr(operator_instance, '_current_image', 0)
- current_image_decimal_progress = max(0.0, min(progress_factor, 1.0))
-
- # Ensure total_images is not zero to prevent division by zero
- overall_progress_factor = (current_image_idx + current_image_decimal_progress) / total_images if total_images > 0 else 0
- overall_progress_factor_clamped = max(0.0, min(overall_progress_factor, 1.0))
+ action_row.operator("object.test_stable", text="Cannot generate: " + config_error_message, icon="ERROR")
+ action_row.enabled = False
+ else:
+ action_row.enabled = True
+ if not bpy.app.online_access:
+ action_row.operator("object.test_stable", text="Enable online access in preferences", icon="ERROR")
+ action_row.enabled = False
+ elif not scene.model_name or scene.model_name == "NONE_FOUND":
+ action_row.operator("object.test_stable", text="Cannot generate: Model Directory Empty", icon="ERROR")
+ action_row.enabled = False
+ elif scene.generation_status == 'idle':
+ # Check if any cameras are selected and if there is existing output
+ selected_cameras = [obj for obj in context.selected_objects if obj.type == 'CAMERA']
+ if not selected_cameras or scene.get("output_timestamp") == "":
+ action_row.operator("object.test_stable", text="Generate", icon="PLAY")
+ else:
+ # Use the regenerate operator
+ action_row.operator("object.stablegen_regenerate", text="Regenerate Selected Views", icon="PLAY")
+ elif scene.generation_status == 'running':
+ action_row.operator("object.test_stable", text="Cancel Generation", icon="CANCEL")
+
+ operator_instance = next((op for win in context.window_manager.windows for op in win.modal_operators if op.bl_idname == 'OBJECT_OT_test_stable'), None)
+ if operator_instance:
+ progress_col = layout.column()
+ progress_text = f"{getattr(operator_instance, '_stage', 'Generating')} ({getattr(operator_instance, '_progress', 0):.0f}%)"
+ progress_factor = getattr(operator_instance, '_progress', 0) / 100.0
+ progress_col.progress(text=progress_text, factor=max(0.0, min(progress_factor, 1.0))) # Ensure factor is <= 1.0 (logic maintained)
+
+ total_images = getattr(operator_instance, '_total_images', 0)
+ if total_images > 1:
+ current_image_idx = getattr(operator_instance, '_current_image', 0)
+ current_image_decimal_progress = max(0.0, min(progress_factor, 1.0))
+
+ # Ensure total_images is not zero to prevent division by zero
+ overall_progress_factor = (current_image_idx + current_image_decimal_progress) / total_images if total_images > 0 else 0
+ overall_progress_factor_clamped = max(0.0, min(overall_progress_factor, 1.0))
- current_img = min(current_image_idx + 1, total_images) # Clamp to total_images
+ current_img = min(current_image_idx + 1, total_images) # Clamp to total_images
- progress_col.progress(
- text=f"Overall: Image {current_img}/{total_images}",
- factor=overall_progress_factor_clamped # Ensure factor is <= 1.0 (logic maintained)
- )
-
- elif context.scene.generation_status == 'waiting':
- action_row.operator("object.test_stable", text="Waiting for Cancellation", icon="TIME")
- else:
- action_row.operator("object.test_stable", text="Fix Issues to Generate", icon="ERROR")
- action_row.enabled = False
+ progress_col.progress(
+ text=f"Overall: Image {current_img}/{total_images}",
+ factor=overall_progress_factor_clamped # Ensure factor is <= 1.0 (logic maintained)
+ )
+
+ elif context.scene.generation_status == 'waiting':
+ action_row.operator("object.test_stable", text="Waiting for Cancellation", icon="TIME")
+ else:
+ action_row.operator("object.test_stable", text="Fix Issues to Generate", icon="ERROR")
+ action_row.enabled = False
bake_row = layout.row()
if config_error_message:
@@ -378,6 +490,10 @@ class StableGenPanel(bpy.types.Panel):
# --- Main Parameters section ---
if not hasattr(scene, 'show_generation_params'):
scene.show_generation_params = True
+
+ is_trellis2 = getattr(scene, 'architecture_mode', 'sdxl') == 'trellis2'
+ trellis2_tex_mode = getattr(scene, 'trellis2_texture_mode', 'native')
+ trellis2_diffusion_texturing = is_trellis2 and trellis2_tex_mode in ('sdxl', 'flux1', 'qwen_image_edit')
main_params_box = layout.box()
main_params_col = main_params_box.column()
@@ -388,32 +504,74 @@ class StableGenPanel(bpy.types.Panel):
split = params_container.split(factor=0.25)
split.label(text="Prompt:")
split.prop(scene, "comfyui_prompt", text="")
-
- # Split for model name
- split = params_container.split(factor=0.25)
- split.label(text="Checkpoint:")
- row = split.row(align=True)
- row.prop(scene, "model_name", text="")
- row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
-
- # Split for model architecture
+ # Architecture selector (architecture_mode — includes TRELLIS.2)
split = params_container.split(factor=0.5)
split.label(text="Architecture:")
- split.prop(scene, "model_architecture", text="")
-
- # Split for generation method
- split = params_container.split(factor=0.5)
- split.label(text="Generation Mode:")
- if scene.model_architecture.startswith("qwen"):
- split.prop(scene, "qwen_generation_method", text="")
+ split.prop(scene, "architecture_mode", text="")
+
+ if is_trellis2:
+ # --- TRELLIS.2 specific layout ---
+ # Warning if TRELLIS.2 nodes not detected
+ if not getattr(scene, 'trellis2_available', False):
+ warn_row = params_container.row()
+ warn_row.alert = True
+ warn_row.label(text="TRELLIS.2 nodes not detected on server", icon="ERROR")
+
+ # Generate From toggle (Image / Prompt)
+ split = params_container.split(factor=0.5)
+ split.label(text="Generate From:")
+ split.prop(scene, "trellis2_generate_from", text="")
+
+ # Input image picker (only when generate_from = image)
+ if scene.trellis2_generate_from == 'image':
+ split = params_container.split(factor=0.25)
+ split.label(text="Input Image:")
+ split.prop(scene, "trellis2_input_image", text="")
+
+ # Texture Generation Mode
+ split = params_container.split(factor=0.5)
+ split.label(text="Texture Mode:")
+ split.prop(scene, "trellis2_texture_mode", text="")
+
+ # When diffusion texturing: show checkpoint, generation mode, camera count
+ if trellis2_diffusion_texturing:
+ split = params_container.split(factor=0.25)
+ split.label(text="Checkpoint:")
+ row = split.row(align=True)
+ row.prop(scene, "model_name", text="")
+ row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
+
+ split = params_container.split(factor=0.5)
+ split.label(text="Generation Mode:")
+ if scene.model_architecture.startswith("qwen"):
+ split.prop(scene, "qwen_generation_method", text="")
+ else:
+ split.prop(scene, "generation_method", text="")
+
+ row = params_container.row()
+ row.prop(scene, "trellis2_camera_count", text="Camera Count")
else:
- split.prop(scene, "generation_method", text="")
+ # --- Standard diffusion layout ---
+ # Split for model name
+ split = params_container.split(factor=0.25)
+ split.label(text="Checkpoint:")
+ row = split.row(align=True)
+ row.prop(scene, "model_name", text="")
+ row.operator("stablegen.refresh_checkpoint_list", text="", icon='FILE_REFRESH')
+
+ # Split for generation method
+ split = params_container.split(factor=0.5)
+ split.label(text="Generation Mode:")
+ if scene.model_architecture.startswith("qwen"):
+ split.prop(scene, "qwen_generation_method", text="")
+ else:
+ split.prop(scene, "generation_method", text="")
- # Split for object selection
- split = params_container.split(factor=0.5)
- split.label(text="Target Objects:")
- split.prop(scene, "texture_objects", text="")
+ # Split for object selection
+ split = params_container.split(factor=0.5)
+ split.label(text="Target Objects:")
+ split.prop(scene, "texture_objects", text="")
# Helper to create collapsible sections
def draw_collapsible_section(parent_layout, toggle_prop_name, title, icon="NONE"):
@@ -443,10 +601,92 @@ class StableGenPanel(bpy.types.Panel):
advanced_params_box = advanced_params_box.column()
advanced_params_box.prop(scene, "show_advanced_params", text="Advanced Parameters", icon="TRIA_DOWN" if scene.show_advanced_params else "TRIA_RIGHT", emboss=False)
if context.scene.show_advanced_params:
-
+
+ # --- TRELLIS.2: Mesh Generation Settings ---
+ if is_trellis2:
+ content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_mesh_settings", "Mesh Generation Settings", icon="MESH_DATA")
+ if content_box:
+ # Core mesh params
+ row = content_box.row()
+ row.prop(scene, "trellis2_seed", text="Seed")
+ row = content_box.row()
+ row.prop(scene, "trellis2_decimation", text="Decimation Target")
+ row = content_box.row()
+ row.prop(scene, "trellis2_remesh", text="Remesh")
+
+ content_box.separator()
+
+ # Model settings
+ content_box.label(text="Model:", icon="SETTINGS")
+ split = content_box.split(factor=0.4)
+ split.label(text="Resolution:")
+ split.prop(scene, "trellis2_resolution", text="")
+
+ split = content_box.split(factor=0.4)
+ split.label(text="VRAM Mode:")
+ split.prop(scene, "trellis2_vram_mode", text="")
+
+ split = content_box.split(factor=0.4)
+ split.label(text="Attention:")
+ split.prop(scene, "trellis2_attn_backend", text="")
+
+ content_box.separator()
+
+ # Shape generation
+ content_box.label(text="Shape Generation:", icon="MESH_ICOSPHERE")
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_ss_guidance", text="SS Guidance")
+ row.prop(scene, "trellis2_ss_steps", text="SS Steps")
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_shape_guidance", text="Shape Guidance")
+ row.prop(scene, "trellis2_shape_steps", text="Shape Steps")
+ row = content_box.row()
+ row.prop(scene, "trellis2_max_tokens", text="Max Tokens (VRAM)")
+
+ content_box.separator()
+
+ # Conditioning
+ content_box.label(text="Conditioning:", icon="IMAGE_DATA")
+ split = content_box.split(factor=0.4)
+ split.label(text="Background:")
+ split.prop(scene, "trellis2_background_color", text="")
+ row = content_box.row()
+ row.prop(scene, "trellis2_include_1024", text="Include 1024 Conditioning")
+
+ content_box.separator()
+
+ # Shape-only extras
+ content_box.label(text="Post-processing:", icon="OUTLINER_OB_MESH")
+ row = content_box.row()
+ row.prop(scene, "trellis2_fill_holes", text="Fill Holes")
+
+ content_box.separator()
+
+ # Misc
+ content_box.label(text="Misc:", icon="PREFERENCES")
+ row = content_box.row()
+ row.prop(scene, "trellis2_low_vram", text="Low VRAM Background Removal")
+
+ # --- TRELLIS.2: Native Texture Settings ---
+ if is_trellis2 and trellis2_tex_mode == 'native':
+ content_box = draw_collapsible_section(advanced_params_box, "show_trellis2_texture_settings", "Texture Settings (TRELLIS.2 Native)", icon="TEXTURE")
+ if content_box:
+ row = content_box.row(align=True)
+ row.prop(scene, "trellis2_tex_guidance", text="Tex Guidance")
+ row.prop(scene, "trellis2_tex_steps", text="Tex Steps")
+ row = content_box.row()
+ row.prop(scene, "trellis2_texture_size", text="Texture Size")
+
+ # --- Diffusion-based advanced sections ---
+ # Each is individually guarded: shown for standard arches or TRELLIS.2 with diffusion texturing
+ _show_diffusion_sections = not is_trellis2 or trellis2_diffusion_texturing
+
# --- Core Generation Settings ---
- content_box = draw_collapsible_section(advanced_params_box, "show_core_settings", "Core Generation Settings", icon="SETTINGS")
+ if _show_diffusion_sections:
+ content_box = draw_collapsible_section(advanced_params_box, "show_core_settings", "Core Generation Settings", icon="SETTINGS")
+ else:
+ content_box = None
if content_box:
row = content_box.row()
row.prop(scene, "seed", text="Seed")
@@ -477,7 +717,10 @@ class StableGenPanel(bpy.types.Panel):
row.prop(scene, "clip_skip", text="Clip Skip")
# --- LoRA Settings ---
- content_box = draw_collapsible_section(advanced_params_box, "show_lora_settings", "LoRA Management", icon="MODIFIER")
+ if _show_diffusion_sections:
+ content_box = draw_collapsible_section(advanced_params_box, "show_lora_settings", "LoRA Management", icon="MODIFIER")
+ else:
+ content_box = None
if content_box:
row = content_box.row()
row.alignment = 'CENTER'
@@ -518,7 +761,10 @@ class StableGenPanel(bpy.types.Panel):
btn_row_lora.operator("stablegen.remove_lora_unit", text="Remove Selected", icon="REMOVE")
# --- Camera Options ---
- content_box = draw_collapsible_section(advanced_params_box, "show_camera_options", "Camera Settings", icon="CAMERA_DATA")
+ if _show_diffusion_sections:
+ content_box = draw_collapsible_section(advanced_params_box, "show_camera_options", "Camera Settings", icon="CAMERA_DATA")
+ else:
+ content_box = None
if content_box:
row = content_box.row(align=True)
row.prop(scene, "use_camera_prompts", text="Use Camera Prompts", toggle=True, icon="OUTLINER_OB_CAMERA")
@@ -553,7 +799,10 @@ class StableGenPanel(bpy.types.Panel):
op.direction = 'DOWN'
# --- Viewpoint Blending Settings ---
- content_box = draw_collapsible_section(advanced_params_box, "show_scene_understanding_settings", "Viewpoint Blending Settings", icon="ZOOM_IN")
+ if _show_diffusion_sections:
+ content_box = draw_collapsible_section(advanced_params_box, "show_scene_understanding_settings", "Viewpoint Blending Settings", icon="ZOOM_IN")
+ else:
+ content_box = None
if content_box:
# Row 1: Discard-Over Angle | Weight Exponent
split = content_box.split(factor=0.5, align=True)
@@ -585,7 +834,10 @@ class StableGenPanel(bpy.types.Panel):
# --- Output & Material Settings ---
- content_box = draw_collapsible_section(advanced_params_box, "show_output_material_settings", "Output & Material Settings", icon="MATERIAL")
+ if _show_diffusion_sections:
+ content_box = draw_collapsible_section(advanced_params_box, "show_output_material_settings", "Output & Material Settings", icon="MATERIAL")
+ else:
+ content_box = None
if content_box:
split = content_box.split(factor=0.5)
split.label(text="Fallback Color:")
@@ -608,10 +860,13 @@ class StableGenPanel(bpy.types.Panel):
row.prop(scene, "overwrite_material", text="Overwrite Material", toggle=True, icon="FILE_REFRESH")
# --- Image Guidance (IPAdapter & ControlNet) ---
- if scene.model_architecture in ['sdxl', 'flux1']:
- content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Image Guidance (IPAdapter & ControlNet)", icon="MODIFIER")
- else: # Qwen Image Edit
- content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Qwen-Image-Edit Guidance", icon="MODIFIER")
+ if _show_diffusion_sections:
+ if scene.model_architecture in ['sdxl', 'flux1']:
+ content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Image Guidance (IPAdapter & ControlNet)", icon="MODIFIER")
+ else: # Qwen Image Edit
+ content_box = draw_collapsible_section(advanced_params_box, "show_image_guidance_settings", "Qwen-Image-Edit Guidance", icon="MODIFIER")
+ else:
+ content_box = None
if content_box:
if scene.model_architecture == 'qwen_image_edit':
if scene.qwen_generation_method == 'generate':
@@ -622,6 +877,11 @@ class StableGenPanel(bpy.types.Panel):
row = content_box.row()
row.prop(scene, "qwen_use_external_style_image", text="Use External Image as Style", toggle=True, icon="FILE_IMAGE")
+ # TRELLIS.2 input as style (only in trellis2 architecture mode)
+ if getattr(scene, 'architecture_mode', '') == 'trellis2':
+ row = content_box.row()
+ row.prop(scene, "qwen_use_trellis2_style", text="Use TRELLIS.2 Input as Style", toggle=True, icon="MESH_MONKEY")
+
if scene.qwen_use_external_style_image:
style_box = content_box.box()
row = style_box.row()
@@ -668,6 +928,11 @@ class StableGenPanel(bpy.types.Panel):
row = content_box.row()
row.prop(scene, "qwen_use_external_style_image", text="Use External Image as Style", toggle=True, icon="FILE_IMAGE")
+ # TRELLIS.2 input as style (only in trellis2 architecture mode)
+ if getattr(scene, 'architecture_mode', '') == 'trellis2':
+ row = content_box.row()
+ row.prop(scene, "qwen_use_trellis2_style", text="Use TRELLIS.2 Input as Style", toggle=True, icon="MESH_MONKEY")
+
if scene.qwen_use_external_style_image:
style_box = content_box.box()
row = style_box.row()
@@ -809,7 +1074,7 @@ class StableGenPanel(bpy.types.Panel):
cn_box.operator("stablegen.add_controlnet_unit", text="Add ControlNet Unit", icon="ADD")
cn_box.operator("stablegen.remove_controlnet_unit", text="Remove Last ControlNet Unit", icon="REMOVE")
- if not scene.model_architecture == 'qwen_image_edit':
+ if _show_diffusion_sections and not scene.model_architecture == 'qwen_image_edit':
# --- Inpainting Options (Conditional) ---
if scene.generation_method == 'uv_inpaint' or scene.generation_method == 'sequential':
content_box = draw_collapsible_section(advanced_params_box, "show_masking_inpainting_settings", "Inpainting Options", icon="MOD_MASK")
@@ -842,7 +1107,10 @@ class StableGenPanel(bpy.types.Panel):
# --- Generation Mode Specifics ---
- mode_specific_outer_box = draw_collapsible_section(advanced_params_box, "show_mode_specific_settings", "Generation Mode Specifics", icon="OPTIONS")
+ if _show_diffusion_sections:
+ mode_specific_outer_box = draw_collapsible_section(advanced_params_box, "show_mode_specific_settings", "Generation Mode Specifics", icon="OPTIONS")
+ else:
+ mode_specific_outer_box = None
if mode_specific_outer_box: # This is the box where all mode-specific UIs should go
# Qwen Local Edit Mode Parameters
diff --git a/stablegen/util/helpers.py b/stablegen/util/helpers.py
index 32d19f3..96135e0 100755
--- a/stablegen/util/helpers.py
+++ b/stablegen/util/helpers.py
@@ -1493,3 +1493,186 @@ prompt_text_qwen_image_edit = """
}
"""
+# Workflow for TRELLIS.2 Image-to-3D generation via PozzettiAndrea/ComfyUI-TRELLIS2 nodes
+# Pipeline: LoadImage → LoadModels → RemoveBackground → GetConditioning → ImageToShape → ShapeToTexturedMesh → ExportGLB
+prompt_text_trellis2 = """
+{
+ "1": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Input Image"
+ }
+ },
+ "2": {
+ "inputs": {
+ "resolution": "1024_cascade",
+ "attn_backend": "flash_attn",
+ "vram_mode": "disk_offload"
+ },
+ "class_type": "LoadTrellis2Models",
+ "_meta": {
+ "title": "Load TRELLIS.2 Models"
+ }
+ },
+ "3": {
+ "inputs": {
+ "image": ["1", 0],
+ "low_vram": true
+ },
+ "class_type": "Trellis2RemoveBackground",
+ "_meta": {
+ "title": "Remove Background"
+ }
+ },
+ "4": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "image": ["3", 0],
+ "mask": ["3", 1],
+ "include_1024": true,
+ "background_color": "black"
+ },
+ "class_type": "Trellis2GetConditioning",
+ "_meta": {
+ "title": "Get Conditioning"
+ }
+ },
+ "5": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "conditioning": ["4", 0],
+ "seed": 0,
+ "ss_guidance_strength": 7.5,
+ "ss_sampling_steps": 12,
+ "shape_guidance_strength": 7.5,
+ "shape_sampling_steps": 12,
+ "max_tokens": 24576
+ },
+ "class_type": "Trellis2ImageToShape",
+ "_meta": {
+ "title": "Image to Shape"
+ }
+ },
+ "6": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "conditioning": ["4", 0],
+ "shape_result": ["5", 0],
+ "seed": 0,
+ "tex_guidance_strength": 7.5,
+ "tex_sampling_steps": 12
+ },
+ "class_type": "Trellis2ShapeToTexturedMesh",
+ "_meta": {
+ "title": "Shape to Textured Mesh"
+ }
+ },
+ "7": {
+ "inputs": {
+ "voxelgrid_path": ["6", 1],
+ "decimation_target": 100000,
+ "texture_size": 2048,
+ "remesh": true,
+ "filename_prefix": "trellis2"
+ },
+ "class_type": "Trellis2ExportGLB",
+ "_meta": {
+ "title": "Export GLB"
+ }
+ }
+}
+"""
+
+prompt_text_trellis2_shape_only = """
+{
+ "1": {
+ "inputs": {
+ "image": "",
+ "upload": "image"
+ },
+ "class_type": "LoadImage",
+ "_meta": {
+ "title": "Load Input Image"
+ }
+ },
+ "2": {
+ "inputs": {
+ "resolution": "1024_cascade",
+ "attn_backend": "flash_attn",
+ "vram_mode": "disk_offload"
+ },
+ "class_type": "LoadTrellis2Models",
+ "_meta": {
+ "title": "Load TRELLIS.2 Models"
+ }
+ },
+ "3": {
+ "inputs": {
+ "image": ["1", 0],
+ "low_vram": true
+ },
+ "class_type": "Trellis2RemoveBackground",
+ "_meta": {
+ "title": "Remove Background"
+ }
+ },
+ "4": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "image": ["3", 0],
+ "mask": ["3", 1],
+ "include_1024": true,
+ "background_color": "black"
+ },
+ "class_type": "Trellis2GetConditioning",
+ "_meta": {
+ "title": "Get Conditioning"
+ }
+ },
+ "5": {
+ "inputs": {
+ "model_config": ["2", 0],
+ "conditioning": ["4", 0],
+ "seed": 0,
+ "ss_guidance_strength": 7.5,
+ "ss_sampling_steps": 12,
+ "shape_guidance_strength": 7.5,
+ "shape_sampling_steps": 12,
+ "max_tokens": 24576
+ },
+ "class_type": "Trellis2ImageToShape",
+ "_meta": {
+ "title": "Image to Shape"
+ }
+ },
+ "6": {
+ "inputs": {
+ "trimesh": ["5", 1],
+ "target_face_count": 100000,
+ "fill_holes": true,
+ "remesh": false,
+ "remesh_band": 1.0
+ },
+ "class_type": "Trellis2Simplify",
+ "_meta": {
+ "title": "Simplify Mesh"
+ }
+ },
+ "7": {
+ "inputs": {
+ "trimesh": ["6", 0],
+ "filename_prefix": "trellis2",
+ "file_format": "glb"
+ },
+ "class_type": "Trellis2ExportTrimesh",
+ "_meta": {
+ "title": "Export Trimesh"
+ }
+ }
+}
+"""
+
diff --git a/stablegen/workflows.py b/stablegen/workflows.py
index 1002261..3266979 100644
--- a/stablegen/workflows.py
+++ b/stablegen/workflows.py
@@ -2,11 +2,13 @@ import bpy
import os
import json
import uuid
+import random
import websocket
import socket
import urllib.request
+from datetime import datetime, timedelta
-from .util.helpers import prompt_text_qwen_image_edit
+from .util.helpers import prompt_text_qwen_image_edit, prompt_text_trellis2, prompt_text_trellis2_shape_only
from .utils import get_generation_dirs
from io import BytesIO
@@ -339,6 +341,14 @@ class WorkflowManager:
if style_image_info:
prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
+ # Fallback to TRELLIS.2 input image as style
+ if not style_image_info and getattr(context.scene, 'qwen_use_trellis2_style', False):
+ t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
+ if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
+ style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
+ if style_image_info:
+ prompt[NODES['style_map_loader']]['inputs']['image'] = style_image_info['name']
+
# If still no style image, remove Image 2 inputs
if not style_image_info:
del prompt[NODES['style_map_loader']]
@@ -514,6 +524,18 @@ class WorkflowManager:
pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+ # Case 1b: TRELLIS.2 input image as style (when no external style configured)
+ elif getattr(context.scene, 'qwen_use_trellis2_style', False) and is_initial_image:
+ t2_path = getattr(context.scene, 'trellis2_last_input_image', '')
+ if t2_path and os.path.exists(bpy.path.abspath(t2_path)):
+ style_image_info = self.operator._get_uploaded_image_info(context, "custom", filename=bpy.path.abspath(t2_path))
+ if style_image_info:
+ if context.scene.qwen_use_custom_prompts:
+ pos_prompt_text = (context.scene.qwen_custom_prompt_initial).format(main_prompt=user_prompt)
+ else:
+ pos_prompt_text = self._get_qwen_default_prompts(context, is_initial_image).format(main_prompt=user_prompt)
+ prompt[NODES['pos_prompt']]['inputs']['prompt'] = pos_prompt_text
+
# Case 2: Sequential generation (after first image)
elif not is_initial_image:
if context_mode == 'REPLACE_STYLE':
@@ -688,6 +710,818 @@ class WorkflowManager:
# Return the generated image from the save_image node
return images[NODES['save_image']][0]
+ def _check_server_alive(self, server_address, timeout=5):
+ """Return True if the ComfyUI server responds to a lightweight request."""
+ try:
+ req = urllib.request.Request(
+ f"http://{server_address}/system_stats",
+ method='GET'
+ )
+ urllib.request.urlopen(req, timeout=timeout)
+ return True
+ except Exception:
+ return False
+
+ def _get_vram_stats(self, server_address):
+ """Return (vram_free, vram_total) in MB from ComfyUI /system_stats."""
+ try:
+ req = urllib.request.Request(
+ f"http://{server_address}/system_stats", method='GET'
+ )
+ resp = json.loads(urllib.request.urlopen(req, timeout=5).read())
+ dev = resp.get("devices", [{}])[0]
+ vram_free = dev.get("vram_free", 0) / (1024 * 1024)
+ vram_total = dev.get("vram_total", 0) / (1024 * 1024)
+ return vram_free, vram_total
+ except Exception:
+ return None, None
+
+ def _flush_comfyui_vram(self, server_address, retries=3, label="Post-generation"):
+ """Unload all models and free VRAM on the ComfyUI server.
+
+ The ``/free`` endpoint only **sets flags** on the prompt queue; the
+ actual unloading happens asynchronously in ComfyUI's execution loop.
+ This method therefore:
+ 1. Sends ``/free`` (unload models + free memory).
+ 2. Clears the execution cache history and queue.
+ 3. Sends ``/free`` *again* — the first round triggers model unloads
+ whose CUDA tensors may still reside in PyTorch's cache; the
+ second round triggers ``soft_empty_cache`` which calls
+ ``torch.cuda.empty_cache()`` on the now-freed allocations.
+ 4. Polls ``/system_stats`` waiting for VRAM to increase.
+ Retries the whole sequence up to *retries* times.
+ """
+ import time
+
+ vram_before, vram_total = self._get_vram_stats(server_address)
+ if vram_before is not None:
+ print(f"[TRELLIS2] {label} VRAM before flush: {vram_before:.0f} MB free / {vram_total:.0f} MB total")
+
+ def _send_free():
+ d = json.dumps({"unload_models": True, "free_memory": True}).encode('utf-8')
+ r = urllib.request.Request(
+ f"http://{server_address}/free", data=d,
+ headers={'Content-Type': 'application/json'}, method='POST'
+ )
+ urllib.request.urlopen(r, timeout=10)
+
+ def _clear_history():
+ d = json.dumps({"clear": True}).encode('utf-8')
+ r = urllib.request.Request(
+ f"http://{server_address}/history", data=d,
+ headers={'Content-Type': 'application/json'}, method='POST'
+ )
+ urllib.request.urlopen(r, timeout=10)
+
+ def _clear_queue():
+ d = json.dumps({"clear": True}).encode('utf-8')
+ r = urllib.request.Request(
+ f"http://{server_address}/queue", data=d,
+ headers={'Content-Type': 'application/json'}, method='POST'
+ )
+ urllib.request.urlopen(r, timeout=10)
+
+ for attempt in range(1, retries + 1):
+ try:
+ # Round 1: Unload models
+ _send_free()
+ time.sleep(1)
+
+ # Clear caches
+ try:
+ _clear_history()
+ except Exception:
+ pass
+ try:
+ _clear_queue()
+ except Exception:
+ pass
+
+ # Round 2: Free the now-released CUDA allocations
+ _send_free()
+
+ # Poll VRAM (up to 8s)
+ for check in range(8):
+ time.sleep(1)
+ vram_after, _ = self._get_vram_stats(server_address)
+ if vram_after is not None:
+ print(f"[TRELLIS2] {label} VRAM after flush (check {check+1}): {vram_after:.0f} MB free")
+ if vram_before is not None and vram_after > vram_before + 500:
+ print(f"[TRELLIS2] {label} flush verified: freed {vram_after - vram_before:.0f} MB")
+ return True
+ if vram_total is not None and vram_after > vram_total * 0.7:
+ print(f"[TRELLIS2] {label} flush OK: >70% VRAM free")
+ return True
+
+ print(f"[TRELLIS2] {label} flush attempt {attempt}/{retries}: VRAM didn't free enough — retrying")
+ except Exception as e:
+ print(f"[TRELLIS2] {label} flush attempt {attempt}/{retries} failed: {e}")
+
+ if attempt < retries:
+ time.sleep(2)
+
+ print(f"[TRELLIS2] Warning: {label} VRAM flush unverified after {retries} attempts")
+ return False
+
+ # ------------------------------------------------------------------
+ # Txt2Img — lightweight single-image generation for prompt-to-image
+ # ------------------------------------------------------------------
+
+ def generate_txt2img(self, context):
+ """Generate a single image from the scene prompt and checkpoint.
+
+ Builds a minimal txt2img ComfyUI workflow (with LoRA support) and
+ returns the generated image as raw PNG bytes delivered via the
+ ``SaveImageWebsocket`` node.
+
+ Returns:
+ bytes: Raw image data on success.
+ dict: ``{"error": "..."}`` on failure.
+ """
+ server_address = context.preferences.addons[__package__].preferences.server_address
+ client_id = str(uuid.uuid4())
+ scene = context.scene
+ architecture = scene.model_architecture # synced from texture_mode
+
+ if architecture == 'qwen_image_edit':
+ prompt, save_node = self._build_qwen_txt2img(context)
+ elif architecture == 'flux1':
+ prompt, save_node = self._build_flux_txt2img(context)
+ else:
+ prompt, save_node = self._build_sdxl_txt2img(context)
+
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "WebSocket connection failed"}
+
+ try:
+ images = self._execute_prompt_and_get_images(
+ ws, prompt, client_id, server_address, {"save_image": save_node}
+ )
+ finally:
+ try:
+ ws.close()
+ except Exception:
+ pass
+
+ if images is None or not isinstance(images, dict) or not images:
+ return {"error": "txt2img generation failed"}
+
+ if save_node not in images or not images[save_node]:
+ return {"error": "No image received from txt2img"}
+
+ return images[save_node][0]
+
+ def _build_sdxl_txt2img(self, context):
+ """Return (prompt_dict, save_node_id) for a minimal SDXL txt2img with LoRA support."""
+ scene = context.scene
+ prompt = {
+ "1": {
+ "class_type": "CheckpointLoaderSimple",
+ "inputs": {"ckpt_name": scene.model_name}
+ },
+ "2": {
+ "class_type": "CLIPTextEncode",
+ "inputs": {"text": scene.comfyui_prompt, "clip": ["1", 1]}
+ },
+ "3": {
+ "class_type": "CLIPTextEncode",
+ "inputs": {"text": scene.comfyui_negative_prompt, "clip": ["1", 1]}
+ },
+ "4": {
+ "class_type": "EmptyLatentImage",
+ "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
+ },
+ "5": {
+ "class_type": "KSampler",
+ "inputs": {
+ "model": ["1", 0],
+ "positive": ["2", 0],
+ "negative": ["3", 0],
+ "latent_image": ["4", 0],
+ "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
+ "steps": scene.steps,
+ "cfg": scene.cfg,
+ "sampler_name": scene.sampler,
+ "scheduler": scene.scheduler,
+ "denoise": 1.0
+ }
+ },
+ "6": {
+ "class_type": "VAEDecode",
+ "inputs": {"samples": ["5", 0], "vae": ["1", 2]}
+ },
+ "7": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["6", 0]}
+ }
+ }
+
+ # LoRA chain (LoraLoader): model + clip
+ prompt, final_model, final_clip = self._build_lora_chain(
+ prompt, context,
+ initial_model_input=["1", 0],
+ initial_clip_input=["1", 1],
+ start_node_id=300,
+ lora_class_type="LoraLoader"
+ )
+ prompt["5"]["inputs"]["model"] = final_model
+ prompt["2"]["inputs"]["clip"] = final_clip
+ prompt["3"]["inputs"]["clip"] = final_clip
+
+ return prompt, "7"
+
+ def _build_flux_txt2img(self, context):
+ """Return (prompt_dict, save_node_id) for a minimal Flux txt2img with LoRA support."""
+ scene = context.scene
+ unet_name = scene.model_name
+ is_gguf = ".gguf" in unet_name.lower()
+ unet_loader_class = "UnetLoaderGGUF" if is_gguf else "UNETLoader"
+
+ prompt = {
+ "10": {
+ "class_type": "VAELoader",
+ "inputs": {"vae_name": "ae.sft"}
+ },
+ "11": {
+ "class_type": "DualCLIPLoader",
+ "inputs": {
+ "clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
+ "clip_name2": "clip_l.safetensors",
+ "type": "flux",
+ "device": "default"
+ }
+ },
+ "12": {
+ "class_type": unet_loader_class,
+ "inputs": {"unet_name": unet_name}
+ },
+ "6": {
+ "class_type": "CLIPTextEncode",
+ "inputs": {"text": scene.comfyui_prompt, "clip": ["11", 0]}
+ },
+ "26": {
+ "class_type": "FluxGuidance",
+ "inputs": {"guidance": scene.cfg, "conditioning": ["6", 0]}
+ },
+ "25": {
+ "class_type": "RandomNoise",
+ "inputs": {"noise_seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31)}
+ },
+ "16": {
+ "class_type": "KSamplerSelect",
+ "inputs": {"sampler_name": scene.sampler}
+ },
+ "17": {
+ "class_type": "BasicScheduler",
+ "inputs": {
+ "scheduler": scene.scheduler,
+ "steps": scene.steps,
+ "denoise": 1.0,
+ "model": ["12", 0]
+ }
+ },
+ "22": {
+ "class_type": "BasicGuider",
+ "inputs": {"model": ["12", 0], "conditioning": ["26", 0]}
+ },
+ "30": {
+ "class_type": "EmptyLatentImage",
+ "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
+ },
+ "13": {
+ "class_type": "SamplerCustomAdvanced",
+ "inputs": {
+ "noise": ["25", 0],
+ "guider": ["22", 0],
+ "sampler": ["16", 0],
+ "sigmas": ["17", 0],
+ "latent_image": ["30", 0]
+ }
+ },
+ "8": {
+ "class_type": "VAEDecode",
+ "inputs": {"samples": ["13", 0], "vae": ["10", 0]}
+ },
+ "7": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["8", 0]}
+ }
+ }
+
+ # LoRA chain (LoraLoaderModelOnly): model only, no clip
+ prompt, final_model, _ = self._build_lora_chain(
+ prompt, context,
+ initial_model_input=["12", 0],
+ initial_clip_input=["12", 0], # dummy, not used by LoraLoaderModelOnly
+ start_node_id=300,
+ lora_class_type="LoraLoaderModelOnly"
+ )
+ prompt["17"]["inputs"]["model"] = final_model
+ prompt["22"]["inputs"]["model"] = final_model
+
+ return prompt, "7"
+
+ def _build_qwen_txt2img(self, context):
+ """Return (prompt_dict, save_node_id) for a minimal Qwen txt2img.
+
+ Qwen-Image-Edit can generate images from text alone when no input
+ images are provided to the ``TextEncodeQwenImageEditPlus`` nodes.
+ """
+ scene = context.scene
+ unet_name = scene.model_name
+ is_gguf = ".gguf" in unet_name.lower()
+ unet_loader_class = "UnetLoaderGGUF" if is_gguf else "UNETLoader"
+
+ prompt = {
+ "3": {
+ "class_type": "CLIPLoader",
+ "inputs": {
+ "clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
+ "type": "qwen_image",
+ "device": "default"
+ }
+ },
+ "4": {
+ "class_type": "VAELoader",
+ "inputs": {"vae_name": "qwen_image_vae.safetensors"}
+ },
+ "13": {
+ "class_type": unet_loader_class,
+ "inputs": {"unet_name": unet_name}
+ },
+ "6": {
+ "class_type": "ModelSamplingAuraFlow",
+ "inputs": {"shift": 3, "model": ["13", 0]}
+ },
+ "9": {
+ "class_type": "CFGNorm",
+ "inputs": {"strength": 1, "model": ["6", 0]}
+ },
+ "12": {
+ "class_type": "TextEncodeQwenImageEditPlus",
+ "inputs": {
+ "prompt": scene.comfyui_prompt,
+ "clip": ["3", 0]
+ }
+ },
+ "11": {
+ "class_type": "TextEncodeQwenImageEditPlus",
+ "inputs": {
+ "prompt": "",
+ "clip": ["3", 0]
+ }
+ },
+ "30": {
+ "class_type": "EmptyLatentImage",
+ "inputs": {"width": 1024, "height": 1024, "batch_size": 1}
+ },
+ "1": {
+ "class_type": "KSampler",
+ "inputs": {
+ "model": ["9", 0],
+ "positive": ["12", 0],
+ "negative": ["11", 0],
+ "latent_image": ["30", 0],
+ "seed": scene.seed if scene.seed != 0 else random.randint(0, 2**31),
+ "steps": scene.steps,
+ "cfg": scene.cfg,
+ "sampler_name": scene.sampler,
+ "scheduler": scene.scheduler,
+ "denoise": 1.0
+ }
+ },
+ "2": {
+ "class_type": "VAEDecode",
+ "inputs": {"samples": ["1", 0], "vae": ["4", 0]}
+ },
+ "7": {
+ "class_type": "SaveImageWebsocket",
+ "inputs": {"images": ["2", 0]}
+ }
+ }
+
+ # LoRA chain: Qwen uses NunchakuQwenImageLoraLoader for nunchaku
+ # models, LoraLoaderModelOnly otherwise
+ is_nunchaku = unet_name.lower().endswith('.safetensors')
+ lora_class = "NunchakuQwenImageLoraLoader" if is_nunchaku else "LoraLoaderModelOnly"
+
+ prompt, final_model, _ = self._build_lora_chain(
+ prompt, context,
+ initial_model_input=["13", 0],
+ initial_clip_input=["13", 0], # dummy
+ start_node_id=500,
+ lora_class_type=lora_class
+ )
+ # ModelSamplingAuraFlow takes the final LoRA output
+ prompt["6"]["inputs"]["model"] = final_model
+
+ return prompt, "7"
+
+ def generate_trellis2(self, context, input_image_path):
+ """
+ Generates a 3D mesh using TRELLIS.2 via ComfyUI.
+
+ Uploads an input image, runs the TRELLIS.2 pipeline (background removal,
+ conditioning, shape generation, optionally texture generation, GLB export),
+ and downloads the resulting GLB file from the ComfyUI server.
+ VRAM is always flushed before AND after generation.
+
+ Args:
+ context: Blender context.
+ input_image_path: Local path to the input image file.
+
+ Returns:
+ bytes: GLB file binary data on success.
+ dict: {"error": "message"} on failure.
+ """
+ import urllib.parse
+
+ server_address = context.preferences.addons[__package__].preferences.server_address
+ client_id = str(uuid.uuid4())
+
+ # Pre-generation flush: free any loaded diffusion/other models so
+ # TRELLIS.2 has maximum VRAM available.
+ print("[TRELLIS2] Pre-generation VRAM flush — freeing loaded models...")
+ self._flush_comfyui_vram(server_address, label="Pre-generation")
+
+ import time
+ time.sleep(1) # Brief pause for CUDA memory to be released
+
+ # Verify the server is alive before proceeding
+ if not self._check_server_alive(server_address):
+ return {"error": "ComfyUI server is not responding. Please restart it and try again."}
+
+ # Wrap the entire generation so VRAM is always freed at the end
+ try:
+ return self._generate_trellis2_inner(context, input_image_path, server_address, client_id)
+ finally:
+ self._flush_comfyui_vram(server_address, label="Post-generation")
+
+ def _generate_trellis2_inner(self, context, input_image_path, server_address, client_id):
+ """Inner implementation of generate_trellis2, called within a try/finally VRAM flush."""
+ import urllib.parse
+
+ # Upload the input image to ComfyUI
+ from .generator import upload_image_to_comfyui
+ image_info = upload_image_to_comfyui(server_address, input_image_path)
+ if image_info is None:
+ self.operator._error = f"Failed to upload input image: {input_image_path}"
+ return {"error": self.operator._error}
+
+ scene = context.scene
+ skip_texture = scene.trellis2_skip_texture
+
+ # Load the appropriate workflow template
+ if skip_texture:
+ prompt = json.loads(prompt_text_trellis2_shape_only)
+ NODES = {
+ 'input_image': '1',
+ 'load_models': '2',
+ 'remove_bg': '3',
+ 'get_conditioning': '4',
+ 'image_to_shape': '5',
+ 'simplify': '6',
+ 'export_trimesh': '7',
+ }
+ export_node_key = 'export_trimesh'
+ else:
+ prompt = json.loads(prompt_text_trellis2)
+ NODES = {
+ 'input_image': '1',
+ 'load_models': '2',
+ 'remove_bg': '3',
+ 'get_conditioning': '4',
+ 'image_to_shape': '5',
+ 'shape_to_textured_mesh': '6',
+ 'export_glb': '7',
+ }
+ export_node_key = 'export_glb'
+
+ # Set input image
+ prompt[NODES['input_image']]["inputs"]["image"] = image_info['name']
+
+ # Configure model settings from scene properties
+ prompt[NODES['load_models']]["inputs"]["resolution"] = scene.trellis2_resolution
+ prompt[NODES['load_models']]["inputs"]["vram_mode"] = scene.trellis2_vram_mode
+ prompt[NODES['load_models']]["inputs"]["attn_backend"] = scene.trellis2_attn_backend
+
+ # Configure remove background
+ prompt[NODES['remove_bg']]["inputs"]["low_vram"] = scene.trellis2_low_vram
+
+ # Configure conditioning
+ prompt[NODES['get_conditioning']]["inputs"]["background_color"] = scene.trellis2_background_color
+ prompt[NODES['get_conditioning']]["inputs"]["include_1024"] = scene.trellis2_include_1024
+
+ # Configure shape generation
+ seed = scene.trellis2_seed
+ prompt[NODES['image_to_shape']]["inputs"]["seed"] = seed
+ prompt[NODES['image_to_shape']]["inputs"]["ss_guidance_strength"] = scene.trellis2_ss_guidance
+ prompt[NODES['image_to_shape']]["inputs"]["ss_sampling_steps"] = scene.trellis2_ss_steps
+ prompt[NODES['image_to_shape']]["inputs"]["shape_guidance_strength"] = scene.trellis2_shape_guidance
+ prompt[NODES['image_to_shape']]["inputs"]["shape_sampling_steps"] = scene.trellis2_shape_steps
+ prompt[NODES['image_to_shape']]["inputs"]["max_tokens"] = scene.trellis2_max_tokens
+
+ # Configure export with unique prefix for identification
+ unique_prefix = f"trellis2_{uuid.uuid4().hex[:8]}"
+
+ if skip_texture:
+ # Configure simplify node
+ prompt[NODES['simplify']]["inputs"]["target_face_count"] = scene.trellis2_decimation
+ prompt[NODES['simplify']]["inputs"]["fill_holes"] = scene.trellis2_fill_holes
+ prompt[NODES['simplify']]["inputs"]["remesh"] = scene.trellis2_remesh
+ prompt[NODES['export_trimesh']]["inputs"]["filename_prefix"] = unique_prefix
+ else:
+ # Configure texture generation
+ prompt[NODES['shape_to_textured_mesh']]["inputs"]["seed"] = seed
+ prompt[NODES['shape_to_textured_mesh']]["inputs"]["tex_guidance_strength"] = scene.trellis2_tex_guidance
+ prompt[NODES['shape_to_textured_mesh']]["inputs"]["tex_sampling_steps"] = scene.trellis2_tex_steps
+
+ # Configure GLB export
+ prompt[NODES['export_glb']]["inputs"]["decimation_target"] = scene.trellis2_decimation
+ prompt[NODES['export_glb']]["inputs"]["texture_size"] = scene.trellis2_texture_size
+ prompt[NODES['export_glb']]["inputs"]["remesh"] = scene.trellis2_remesh
+ prompt[NODES['export_glb']]["inputs"]["filename_prefix"] = unique_prefix
+
+ # Save prompt for debugging
+ revision_dir = get_generation_dirs(context).get("revision", "")
+ if revision_dir:
+ self._save_prompt_to_file(prompt, revision_dir)
+
+ # --- Two-phase VRAM management for textured path ---
+ # NOTE: Two-phase execution (shape first, flush, then texture) was
+ # removed because ComfyUI requires at least one OUTPUT_NODE per prompt
+ # and the shape-only subset has none. The TRELLIS pipeline stages
+ # handle VRAM management internally (unload_shape_pipeline before
+ # loading texture models), so a single full-prompt submission works.
+
+ # Connect WebSocket
+ ws = self._connect_to_websocket(server_address, client_id)
+ if ws is None:
+ return {"error": "conn_failed"}
+
+ prompt_id = None
+ try:
+ # Queue prompt
+ prompt_id = self._queue_prompt(prompt, client_id, server_address)
+
+ # Node-level progress (isolated subprocess doesn't emit within-node progress)
+ # Weights approximate actual time spent per node
+ if skip_texture:
+ NODE_PROGRESS = {
+ NODES['input_image']: 2,
+ NODES['load_models']: 3,
+ NODES['remove_bg']: 12,
+ NODES['get_conditioning']: 25,
+ NODES['image_to_shape']: 80,
+ NODES['simplify']: 90,
+ NODES['export_trimesh']: 98,
+ }
+ else:
+ # Full textured pipeline (single submission)
+ NODE_PROGRESS = {
+ NODES['input_image']: 2,
+ NODES['load_models']: 3,
+ NODES['remove_bg']: 10,
+ NODES['get_conditioning']: 18,
+ NODES['image_to_shape']: 55,
+ NODES['shape_to_textured_mesh']: 85,
+ NODES['export_glb']: 98,
+ }
+
+ # Wait for execution to complete via WebSocket
+ # Also capture 'executed' events which may contain the output path
+ export_node_id = NODES[export_node_key]
+ glb_output_path = None
+
+ while True:
+ try:
+ out = ws.recv()
+ except (ConnectionError, OSError, Exception) as ws_err:
+ err_name = type(ws_err).__name__
+ print(f"[TRELLIS2] WebSocket died ({err_name}): {ws_err}")
+ return {"error": (
+ "ComfyUI server crashed during TRELLIS.2 generation "
+ "(likely VRAM exhaustion). Please restart ComfyUI, "
+ "reduce max_tokens or resolution, and try again."
+ )}
+ if isinstance(out, str):
+ message = json.loads(out)
+
+ if message['type'] == 'executing':
+ data = message['data']
+ if data['prompt_id'] == prompt_id:
+ if data['node'] is None:
+ self.operator._phase_progress = 100
+ self.operator._detail_progress = 100
+ if hasattr(self.operator, '_update_overall'):
+ self.operator._update_overall()
+ break # Execution complete
+ else:
+ node_id = data['node']
+ # Map node IDs to readable names for progress
+ node_names = {v: k for k, v in NODES.items()}
+ node_label = node_names.get(node_id, node_id)
+ print(f"[TRELLIS2] Executing: {node_label}")
+ self.operator._phase_stage = f"TRELLIS.2: {node_label}"
+ # Update progress based on node weight
+ if node_id in NODE_PROGRESS:
+ self.operator._phase_progress = NODE_PROGRESS[node_id]
+ if hasattr(self.operator, '_update_overall'):
+ self.operator._update_overall()
+ self.operator._detail_stage = node_label
+ self.operator._detail_progress = 0
+
+ elif message['type'] == 'executed':
+ # Capture output from export node (newer ComfyUI versions)
+ data = message.get('data', {})
+ if data.get('node') == export_node_id:
+ ws_output = data.get('output', {})
+ if ws_output:
+ for key in ['glb_path', 'file_path', 'text', 'string']:
+ val = ws_output.get(key)
+ if val:
+ if isinstance(val, list) and len(val) > 0:
+ val = val[0]
+ if isinstance(val, str) and val:
+ glb_output_path = val
+ print(f"[TRELLIS2] Got path from WS executed event: {glb_output_path}")
+ break
+ # Also check any key with a file-like value
+ if not glb_output_path:
+ for key, val in ws_output.items():
+ if isinstance(val, list) and len(val) > 0:
+ val = val[0]
+ if isinstance(val, str) and val.endswith(('.glb', '.obj', '.ply')):
+ glb_output_path = val
+ print(f"[TRELLIS2] Got path from WS (key={key}): {glb_output_path}")
+ break
+
+ elif message['type'] == 'progress':
+ # Within-node progress (sampler steps)
+ progress = (message['data']['value'] / message['data']['max']) * 100
+ if progress != 0:
+ self.operator._detail_progress = progress
+ self.operator._detail_stage = f"Step {message['data']['value']}/{message['data']['max']}"
+ print(f"[TRELLIS2] Progress: {progress:.1f}%")
+
+ elif message['type'] == 'execution_error':
+ error_data = message.get('data', {})
+ error_msg = error_data.get('exception_message', 'Unknown error')
+ self.operator._error = f"TRELLIS.2 execution error: {error_msg}"
+ print(f"[TRELLIS2] Error: {self.operator._error}")
+ return {"error": self.operator._error}
+ finally:
+ if ws:
+ try:
+ ws.close()
+ except Exception:
+ pass
+
+ # ── Retrieve the GLB file ──────────────────────────────────────
+ # Strategy 1: path from WS 'executed' event (already captured above)
+ # Strategy 2: path from history API
+ # Strategy 3: direct file read from disk (local ComfyUI)
+ # Strategy 4: HTTP download via /view endpoint
+ # Strategy 5: scan ComfyUI output dir for our prefix
+
+ glb_source_path = glb_output_path # May already be set from WS event
+
+ # Strategy 2: Query history for the output path
+ if not glb_source_path:
+ try:
+ history_url = f"http://{server_address}/history/{prompt_id}"
+ history_response = json.loads(urllib.request.urlopen(history_url).read())
+
+ if prompt_id in history_response:
+ outputs = history_response[prompt_id].get("outputs", {})
+ export_output = outputs.get(export_node_id, {})
+ print(f"[TRELLIS2] History output for node {export_node_id}: {export_output}")
+
+ for key in ['glb_path', 'file_path', 'text', 'string']:
+ val = export_output.get(key)
+ if val:
+ if isinstance(val, list) and len(val) > 0:
+ val = val[0]
+ if isinstance(val, str) and val:
+ glb_source_path = val
+ print(f"[TRELLIS2] Got path from history (key={key}): {glb_source_path}")
+ break
+
+ # Check any key with a path-like value
+ if not glb_source_path:
+ for key, val in export_output.items():
+ if isinstance(val, list) and len(val) > 0:
+ val = val[0]
+ if isinstance(val, str) and val.endswith(('.glb', '.obj', '.ply')):
+ glb_source_path = val
+ print(f"[TRELLIS2] Got path from history (key={key}): {glb_source_path}")
+ break
+ except Exception as e:
+ print(f"[TRELLIS2] History query failed: {e}")
+
+ # Strategy 3: Read directly from disk (works when ComfyUI is local)
+ if glb_source_path and os.path.isfile(glb_source_path):
+ try:
+ print(f"[TRELLIS2] Reading GLB directly from disk: {glb_source_path}")
+ with open(glb_source_path, 'rb') as f:
+ glb_data = f.read()
+ print(f"[TRELLIS2] Read {len(glb_data)} bytes from disk")
+ if glb_data and len(glb_data) > 0:
+ return glb_data
+ except Exception as e:
+ print(f"[TRELLIS2] Direct file read failed: {e}")
+
+ # Strategy 4: HTTP download via /view endpoint
+ if glb_source_path:
+ glb_filename = os.path.basename(glb_source_path)
+ try:
+ view_url = f"http://{server_address}/view?filename={urllib.parse.quote(glb_filename)}&type=output"
+ print(f"[TRELLIS2] Downloading GLB via HTTP: {view_url}")
+ glb_response = urllib.request.urlopen(view_url)
+ glb_data = glb_response.read()
+ print(f"[TRELLIS2] Downloaded GLB: {len(glb_data)} bytes")
+ if glb_data and len(glb_data) > 0:
+ return glb_data
+ except Exception as e:
+ print(f"[TRELLIS2] HTTP download failed: {e}")
+
+ # Strategy 5: Scan ComfyUI output directory for files matching our prefix
+ # This handles the case where the export node path wasn't captured via API
+ print(f"[TRELLIS2] Scanning for files with prefix '{unique_prefix}'...")
+
+ # 5a: Try to discover ComfyUI's output directory from the server
+ comfyui_output_dir = None
+ try:
+ system_url = f"http://{server_address}/system_stats"
+ system_response = json.loads(urllib.request.urlopen(system_url).read())
+ # Some ComfyUI versions include directory info
+ comfyui_output_dir = system_response.get("output_dir")
+ except Exception:
+ pass
+
+ # 5b: Try common local paths relative to server
+ if not comfyui_output_dir:
+ # Check if server is localhost - if so, try to find output dir
+ host = server_address.split(':')[0]
+ if host in ('127.0.0.1', 'localhost', '0.0.0.0', '::1'):
+ # Try to discover via the /view endpoint with a known file
+ # Or just check common ComfyUI locations
+ common_paths = [
+ os.path.join(os.environ.get('COMFYUI_PATH', ''), 'output'),
+ 'E:/tools/ComfyUI/output',
+ 'C:/ComfyUI/output',
+ os.path.expanduser('~/ComfyUI/output'),
+ ]
+ for candidate in common_paths:
+ if candidate and os.path.isdir(candidate):
+ comfyui_output_dir = candidate
+ break
+
+ if comfyui_output_dir and os.path.isdir(comfyui_output_dir):
+ print(f"[TRELLIS2] Scanning output dir: {comfyui_output_dir}")
+ try:
+ matching_files = sorted(
+ [f for f in os.listdir(comfyui_output_dir)
+ if f.startswith(unique_prefix) and f.endswith(('.glb', '.obj', '.ply'))],
+ key=lambda f: os.path.getmtime(os.path.join(comfyui_output_dir, f)),
+ reverse=True
+ )
+ if matching_files:
+ found_path = os.path.join(comfyui_output_dir, matching_files[0])
+ print(f"[TRELLIS2] Found matching file: {found_path}")
+ with open(found_path, 'rb') as f:
+ glb_data = f.read()
+ print(f"[TRELLIS2] Read {len(glb_data)} bytes from disk")
+ if glb_data and len(glb_data) > 0:
+ return glb_data
+ except Exception as e:
+ print(f"[TRELLIS2] Output dir scan failed: {e}")
+
+ # 5c: Try HTTP download with timestamp-based filename guesses
+ # The export nodes use format: {prefix}_{YYYYMMDD_HHMMSS}.glb
+ now = datetime.now()
+ for delta_seconds in range(0, 120): # Try last 2 minutes of timestamps
+ for delta in [timedelta(seconds=-delta_seconds), timedelta(seconds=delta_seconds)]:
+ candidate_time = now + delta
+ candidate_name = f"{unique_prefix}_{candidate_time.strftime('%Y%m%d_%H%M%S')}.glb"
+ try:
+ view_url = f"http://{server_address}/view?filename={urllib.parse.quote(candidate_name)}&type=output"
+ glb_response = urllib.request.urlopen(view_url)
+ glb_data = glb_response.read()
+ if glb_data and len(glb_data) > 0:
+ print(f"[TRELLIS2] Found GLB via timestamp scan: {candidate_name} ({len(glb_data)} bytes)")
+ return glb_data
+ except Exception:
+ continue
+
+ self.operator._error = (
+ f"Failed to retrieve GLB from ComfyUI. "
+ f"The workflow completed but the output file could not be located. "
+ f"Prefix: {unique_prefix}"
+ )
+ return {"error": self.operator._error}
+
def _create_base_prompt(self, context):
"""Creates and configures the base prompt with user settings."""
from .util.helpers import prompt_text
@@ -765,7 +1599,15 @@ class WorkflowManager:
prompt[NODES['clip_skip']]["inputs"]["clip"] = final_lora_clip_out
# If using IPAdapter, set the model input
- if context.scene.use_ipadapter or (context.scene.generation_method == 'separate' and context.scene.sequential_ipadapter and self.operator._current_image > 0):
+ _is_trellis2_input_ipadapter = (
+ context.scene.sequential_ipadapter
+ and context.scene.sequential_ipadapter_mode == 'trellis2_input'
+ )
+ if (context.scene.use_ipadapter
+ or _is_trellis2_input_ipadapter
+ or (context.scene.generation_method == 'separate'
+ and context.scene.sequential_ipadapter
+ and self.operator._current_image > 0)):
# Set the model input for IPAdapter
prompt[NODES['ipadapter_loader']]["inputs"]["model"] = current_model_out
current_model_out = [NODES['ipadapter'], 0]
@@ -1048,6 +1890,13 @@ class WorkflowManager:
progress = (message['data']['value'] / message['data']['max']) * 100
if progress != 0:
self.operator._progress = progress # Update progress for UI
+ # Also update 3-tier detail when called from Trellis2Generate
+ if hasattr(self.operator, '_detail_progress'):
+ self.operator._detail_progress = progress
+ self.operator._detail_stage = f"Step {message['data']['value']}/{message['data']['max']}"
+ self.operator._phase_progress = progress
+ if hasattr(self.operator, '_update_overall'):
+ self.operator._update_overall()
print(f"Progress: {progress:.1f}%")
else:
# Binary data (image)
@@ -1267,7 +2116,11 @@ class WorkflowManager:
and context.scene.sequential_ipadapter_mode == 'original_render'
and context.scene.generation_method == 'local_edit'
)
- if (context.scene.use_ipadapter or _is_original_render_ipadapter or (context.scene.sequential_ipadapter and self.operator._current_image > 0)) and context.scene.generation_method != 'uv_inpaint':
+ _is_trellis2_input_ipadapter = (
+ context.scene.sequential_ipadapter
+ and context.scene.sequential_ipadapter_mode == 'trellis2_input'
+ )
+ if (context.scene.use_ipadapter or _is_original_render_ipadapter or _is_trellis2_input_ipadapter or (context.scene.sequential_ipadapter and self.operator._current_image > 0)) and context.scene.generation_method != 'uv_inpaint':
# Set the model input for IPAdapter
prompt[NODES['ipadapter_loader']]["inputs"]["model"] = current_model_out
current_model_out = [NODES['ipadapter'], 0]