Add JSONLoaderDynamic node with JS frontend for auto-discovered outputs
Dynamic node reads JSON keys and exposes them as outputs automatically via 32 AnyType slots managed by a JS extension (show/hide/rename). Includes /json_manager/get_keys API route, bool-safe type handling, and workflow save/reload support. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,22 @@ from typing import Any
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
KEY_BATCH_DATA = "batch_data"
|
||||
MAX_DYNAMIC_OUTPUTS = 32
|
||||
|
||||
|
||||
class AnyType(str):
|
||||
"""Universal connector type that matches any ComfyUI type."""
|
||||
def __ne__(self, __value: object) -> bool:
|
||||
return False
|
||||
|
||||
any_type = AnyType("*")
|
||||
|
||||
|
||||
try:
|
||||
from server import PromptServer
|
||||
from aiohttp import web
|
||||
except ImportError:
|
||||
PromptServer = None
|
||||
|
||||
|
||||
def to_float(val: Any) -> float:
|
||||
@@ -49,6 +65,69 @@ def read_json_data(json_path: str) -> dict[str, Any]:
|
||||
return {}
|
||||
return data
|
||||
|
||||
# --- API Route ---
|
||||
if PromptServer is not None:
|
||||
@PromptServer.instance.routes.get("/json_manager/get_keys")
|
||||
async def get_keys_route(request):
|
||||
json_path = request.query.get("path", "")
|
||||
try:
|
||||
seq = int(request.query.get("sequence_number", "1"))
|
||||
except (ValueError, TypeError):
|
||||
seq = 1
|
||||
data = read_json_data(json_path)
|
||||
target = get_batch_item(data, seq)
|
||||
keys = list(target.keys()) if isinstance(target, dict) else []
|
||||
return web.json_response({"keys": keys})
|
||||
|
||||
|
||||
# ==========================================
|
||||
# 0. DYNAMIC NODE
|
||||
# ==========================================
|
||||
|
||||
class JSONLoaderDynamic:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {
|
||||
"required": {
|
||||
"json_path": ("STRING", {"default": "", "multiline": False}),
|
||||
"sequence_number": ("INT", {"default": 1, "min": 1, "max": 9999}),
|
||||
},
|
||||
"optional": {
|
||||
"output_keys": ("STRING", {"default": ""}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = tuple(any_type for _ in range(MAX_DYNAMIC_OUTPUTS))
|
||||
RETURN_NAMES = tuple(f"output_{i}" for i in range(MAX_DYNAMIC_OUTPUTS))
|
||||
FUNCTION = "load_dynamic"
|
||||
CATEGORY = "utils/json"
|
||||
OUTPUT_NODE = False
|
||||
|
||||
def load_dynamic(self, json_path, sequence_number, output_keys=""):
|
||||
data = read_json_data(json_path)
|
||||
target = get_batch_item(data, sequence_number)
|
||||
|
||||
keys = [k.strip() for k in output_keys.split(",") if k.strip()] if output_keys else []
|
||||
|
||||
results = []
|
||||
for key in keys:
|
||||
val = target.get(key, "")
|
||||
if isinstance(val, bool):
|
||||
results.append(str(val).lower())
|
||||
elif isinstance(val, int):
|
||||
results.append(val)
|
||||
elif isinstance(val, float):
|
||||
results.append(val)
|
||||
else:
|
||||
results.append(str(val))
|
||||
|
||||
# Pad to MAX_DYNAMIC_OUTPUTS
|
||||
while len(results) < MAX_DYNAMIC_OUTPUTS:
|
||||
results.append("")
|
||||
|
||||
return tuple(results)
|
||||
|
||||
|
||||
# ==========================================
|
||||
# 1. STANDARD NODES (Single File)
|
||||
# ==========================================
|
||||
@@ -270,6 +349,7 @@ class JSONLoaderCustom6:
|
||||
|
||||
# --- Mappings ---
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"JSONLoaderDynamic": JSONLoaderDynamic,
|
||||
"JSONLoaderLoRA": JSONLoaderLoRA,
|
||||
"JSONLoaderStandard": JSONLoaderStandard,
|
||||
"JSONLoaderVACE": JSONLoaderVACE,
|
||||
@@ -282,6 +362,7 @@ NODE_CLASS_MAPPINGS = {
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"JSONLoaderDynamic": "JSON Loader (Dynamic)",
|
||||
"JSONLoaderLoRA": "JSON Loader (LoRAs Only)",
|
||||
"JSONLoaderStandard": "JSON Loader (Standard/I2V)",
|
||||
"JSONLoaderVACE": "JSON Loader (VACE Full)",
|
||||
|
||||
Reference in New Issue
Block a user