import os import random import time import folder_paths def get_allowed_dirs(): return { "input": "$input/**/*.txt", "output": "$output/**/*.txt", "temp": "$temp/**/*.txt" } def get_valid_dirs(): return get_allowed_dirs().keys() def get_dir_from_name(name): dirs = get_allowed_dirs() if name not in dirs: raise KeyError(name + " dir not found") path = dirs[name] path = path.replace("$input", folder_paths.get_input_directory()) path = path.replace("$output", folder_paths.get_output_directory()) path = path.replace("$temp", folder_paths.get_temp_directory()) return path def is_child_dir(parent_path, child_path): parent_path = os.path.abspath(parent_path) child_path = os.path.abspath(child_path) return os.path.commonpath([parent_path]) == os.path.commonpath([parent_path, child_path]) def get_real_path(dir): dir = dir.replace("/**/", "/") dir = os.path.abspath(dir) dir = os.path.split(dir)[0] return dir def get_file(root_dir, file): if file == "[none]" or not file or not file.strip(): raise ValueError("No file") root_dir = get_dir_from_name(root_dir) root_dir = get_real_path(root_dir) if not os.path.exists(root_dir): os.mkdir(root_dir) full_path = os.path.join(root_dir, file) if not is_child_dir(root_dir, full_path): raise ReferenceError() return full_path class LoadTextLocal: @classmethod def INPUT_TYPES(s): return { "required": { "root_dir": (list(get_valid_dirs()), {}), "file": (["[none]"], { "pysssss.binding": [{ "source": "root_dir", "callback": [{ "type": "set", "target": "$this.disabled", "value": True }, { "type": "fetch", "url": "/pysssss/text-file/{$source.value}", "then": [{ "type": "set", "target": "$this.options.values", "value": "$result" }, { "type": "validate-combo" }, { "type": "set", "target": "$this.disabled", "value": False }] }], }] }), "encoding": ("STRING", {"default": "utf-8"}), }, } RETURN_TYPES = ("STRING",) FUNCTION = "load" CATEGORY = "不忘科技-自定义节点🚩/文本" @classmethod def VALIDATE_INPUTS(self, root_dir, file, **kwargs): if file == "[none]" or not file or not file.strip(): return True get_file(root_dir, file) return True def load(self, root_dir, file, encoding): with open(get_file(root_dir, file), "r", encoding=encoding) as f: return (f.read(),) class LoadTextOnline: @classmethod def INPUT_TYPES(s): return { "required": { "file_path": ("STRING", {"default": "input/"}), "encoding": ("STRING", {"default": "utf-8"}), } } RETURN_TYPES = ("STRING",) FUNCTION = "load" CATEGORY = "不忘科技-自定义节点🚩/文本" def load(self, file_path, encoding): with open(file_path, "r", encoding=encoding) as f: return (f.read(),) class StringEmptyJudgement: @classmethod def INPUT_TYPES(s): return { "required": { "input": ("STRING", {"forceInput": True}), }, } RETURN_TYPES = ("BOOLEAN",) RETURN_NAMES = ("是否为空",) FUNCTION = "compute" CATEGORY = "不忘科技-自定义节点🚩/文本" def compute(self, input): if len(input) == 0: return (True,) else: return (False,) class RandomLineSelector: """ ComfyUI自定义节点:随机行选择器 从输入的多行文本中随机选择一行输出 """ @classmethod def INPUT_TYPES(cls): return { "required": { "text": ("STRING", { "multiline": True, "default": """ 保持人物样貌和衣服不变,改变人物的光影效果。更换背景: 图像呈现出一个温馨的维多利亚复古风格房间,墙面被多样化的艺术画框与照片装饰,展现出浓郁的怀旧氛围。中心的金框全身镜是视觉焦点,上面缠绕着精致花卉装饰,增强了空间的艺术感。左侧靠墙摆放花瓶与烛台,白色地板映衬着跳动的暖黄色光线,营造出柔和而梦幻的氛围。右侧床铺整洁柔软,与一旁的小抽屉柜和台灯形成和谐的组合。整体画面色调以暖黄色为主,搭配田园风格装饰元素,构图均衡,注重细节,体现了舒适浪漫的生活场景。人物重新打光融合到场景中。 保持人物形象和衣服不变,改变人物的光影效果。更换背景: 衣帽间的开放式悬挂区搭配浅色木地板,衣杆上挂满中性色长裙,旁边搁板整齐叠放针织毛衣与围巾。 保持人物形象和衣服不变,改变人物的光影效果。更换背景: 白色衣柜门打开后,分层挂满长裙和连体裤,底部抽屉收纳着色彩鲜艳的运动服与家居裤。 保持人物形象和衣服不变,改变人物的光影效果。更换背景: 玻璃门的衣柜设计体现精致生活,内部悬挂精致衬衫和镂空裙摆,饰品抽屉随时为配件搭配提供轻便选择。 """ }), "seed": ("INT", { "default": 0, "min": -1, "max": 0xffffffffffffffff }), }, } RETURN_TYPES = ("STRING",) RETURN_NAMES = ("selected_line",) FUNCTION = "select_random_line" CATEGORY = "不忘科技-自定义节点🚩/文本" def select_random_line(self, text, seed): """ 从输入文本中随机选择一行 Args: text (str): 输入的多行文本 seed (int): 随机种子 Returns: tuple: 包含选中行的元组 """ # 设置随机种子(-1表示使用当前时间戳) if seed == -1: random.seed(int(time.time() * 1000000)) # 使用微秒级时间戳 else: random.seed(seed) # 过滤掉空行 non_empty_lines = [line.strip() for line in text.split('\n') if line.strip()] # 如果没有非空行,返回空字符串 if not non_empty_lines: return ("",) # 随机选择一行 selected_line = random.choice(non_empty_lines) return (selected_line,)