Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .nodes._comfyui_bootstrap import ensure_comfyui_base
from comfy_env import register_nodes

ensure_comfyui_base()
NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS = register_nodes()


Expand Down
4 changes: 4 additions & 0 deletions nodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Automatic cleanup (no manual SAM3CloseVideoSession needed)
"""

from ._comfyui_bootstrap import ensure_comfyui_base

ensure_comfyui_base()

from .load_model import NODE_CLASS_MAPPINGS as LOAD_MODEL_MAPPINGS
from .load_model import NODE_DISPLAY_NAME_MAPPINGS as LOAD_MODEL_DISPLAY_MAPPINGS
from .segmentation import NODE_CLASS_MAPPINGS as SEGMENTATION_MAPPINGS
Expand Down
76 changes: 76 additions & 0 deletions nodes/_comfyui_bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Helpers for finding ComfyUI core modules from comfy-env isolation workers.
"""

from __future__ import annotations

import os
import sys
from pathlib import Path


def _is_comfyui_base(path: Path) -> bool:
return (
path.is_dir()
and (path / "folder_paths.py").exists()
and (path / "comfy_api").is_dir()
)


def _candidate_paths() -> list[Path]:
candidates: list[Path] = []

for key in ("COMFYUI_BASE", "COMFYUI_PATH", "COMFYUI_DIR"):
value = os.environ.get(key)
if value:
candidates.append(Path(value))

host_python_dir = os.environ.get("COMFYUI_HOST_PYTHON_DIR")
if host_python_dir:
host_dir = Path(host_python_dir)
candidates.extend(
[
host_dir,
host_dir.parent,
host_dir.parent.parent,
host_dir.parent.parent.parent,
host_dir.parent.parent / "resources" / "ComfyUI",
]
)

local_appdata = Path(os.environ.get("LOCALAPPDATA", Path.home() / "AppData" / "Local"))
candidates.append(local_appdata / "Programs" / "ComfyUI" / "resources" / "ComfyUI")

cwd = Path.cwd()
candidates.extend([cwd, *list(cwd.parents)[:4]])

here = Path(__file__).resolve()
candidates.extend(list(here.parents)[:6])

unique_candidates: list[Path] = []
seen: set[Path] = set()
for candidate in candidates:
try:
resolved = candidate.expanduser().resolve()
except Exception:
continue
if resolved in seen:
continue
seen.add(resolved)
unique_candidates.append(resolved)
return unique_candidates


def ensure_comfyui_base() -> Path | None:
"""Add the ComfyUI core directory to sys.path if it can be located."""
for candidate in _candidate_paths():
if not _is_comfyui_base(candidate):
continue

candidate_str = str(candidate)
if candidate_str not in sys.path:
sys.path.insert(0, candidate_str)
os.environ.setdefault("COMFYUI_BASE", candidate_str)
return candidate

return None