ComfyUI-CustomNode/nodes/text_nodes.py

118 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import random
import time
import folder_paths
class LoadText:
@classmethod
def INPUT_TYPES(s):
input_dir = folder_paths.get_input_directory()
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
files = folder_paths.filter_files_content_types(files, ["text"])
return {
"required": {
"file_path": ("STRING", {"default": "/path/to/file"}),
"file": (sorted(files),),
"encoding": ("STRING", {"default": "utf-8"}),
},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "load"
CATEGORY = "不忘科技-自定义节点🚩/文本"
def load(self, file_path, file, encoding):
if not (file_path and len(file_path.strip()) > 0 and file_path != "/path/to/file"):
file_path = folder_paths.get_annotated_filepath(file)
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,)