From 3d9459b37a7c2a223f70462757b4a29fa2c04ffd Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Sat, 3 Aug 2024 13:13:57 +0100 Subject: [PATCH] Refactor tooltips, add node level support --- nodes.py | 162 ++++++++++++++------------------ server.py | 4 +- web/extensions/core/tooltips.js | 81 ++++++++-------- web/scripts/app.js | 3 +- web/scripts/widgets.js | 2 + 5 files changed, 121 insertions(+), 131 deletions(-) diff --git a/nodes.py b/nodes.py index 0e0c9a0c5..36bba9e77 100644 --- a/nodes.py +++ b/nodes.py @@ -47,18 +47,18 @@ MAX_RESOLUTION=16384 class CLIPTextEncode: @classmethod def INPUT_TYPES(s): - return {"required": {"text": ("STRING", {"multiline": True, "dynamicPrompts": True}), "clip": ("CLIP", )}} + return { + "required": { + "text": ("STRING", {"multiline": True, "dynamicPrompts": True, "tooltip": "The text to be encoded."}), + "clip": ("CLIP", {"tooltip": "The CLIP model used for encoding the text."}) + } + } RETURN_TYPES = ("CONDITIONING",) + OUTPUT_TOOLTIPS = ("A conditioning containing the embedded text used to guide the diffusion model.",) FUNCTION = "encode" CATEGORY = "conditioning" - TOOLTIPS = { - "input": { - "text": "The text to be encoded.", - "clip": "The CLIP model used for encoding the text.", - }, - "output": ("A conditioning containing the embedded text used to guide the diffusion model.",) - } + DESCRIPTION = "Encodes a text prompt using a CLIP model into an embedding that can be used to guide the diffusion model towards generating specific images." def encode(self, clip, text): tokens = clip.tokenize(text) @@ -267,18 +267,18 @@ class ConditioningSetTimestepRange: class VAEDecode: @classmethod def INPUT_TYPES(s): - return {"required": { "samples": ("LATENT", ), "vae": ("VAE", )}} + return { + "required": { + "samples": ("LATENT", {"tooltip": "The latent to be decoded."}), + "vae": ("VAE", {"tooltip": "The VAE model used for decoding the latent."}) + } + } RETURN_TYPES = ("IMAGE",) + OUTPUT_TOOLTIPS = ("The decoded image.",) FUNCTION = "decode" CATEGORY = "latent" - TOOLTIPS = { - "input": { - "samples": "The latent to be decoded.", - "vae": "The VAE model used for decoding the latent.", - }, - "output": ("The decoded image.",) - } + DESCRIPTION = "Decodes latent images back into pixel space images." def decode(self, vae, samples): return (vae.decode(samples["samples"]), ) @@ -520,18 +520,19 @@ class CheckpointLoader: class CheckpointLoaderSimple: @classmethod def INPUT_TYPES(s): - return {"required": { "ckpt_name": (folder_paths.get_filename_list("checkpoints"), ), - }} + return { + "required": { + "ckpt_name": (folder_paths.get_filename_list("checkpoints"), {"tooltip": "The name of the checkpoint (model) to load."}), + } + } RETURN_TYPES = ("MODEL", "CLIP", "VAE") + OUTPUT_TOOLTIPS = ("The model used for denoising latents.", + "The CLIP model used for encoding text prompts.", + "The VAE model used for encoding and decoding images to and from latent space.") FUNCTION = "load_checkpoint" CATEGORY = "loaders" - TOOLTIPS = { - "input": { - "ckpt_name": "The name of the checkpoint (model) to load.", - }, - "output": ("The model used for denoising latents.", "The CLIP model used for encoding text prompts.", "The VAE model used for encoding and decoding images to and from latent space.") - } + DESCRIPTION = "Loads a diffusion model checkpoint, diffusion models are used to denoise latents." def load_checkpoint(self, ckpt_name): ckpt_path = folder_paths.get_full_path("checkpoints", ckpt_name) @@ -602,26 +603,22 @@ class LoraLoader: @classmethod def INPUT_TYPES(s): - return {"required": { "model": ("MODEL",), - "clip": ("CLIP", ), - "lora_name": (folder_paths.get_filename_list("loras"), ), - "strength_model": ("FLOAT", {"default": 1.0, "min": -100.0, "max": 100.0, "step": 0.01}), - "strength_clip": ("FLOAT", {"default": 1.0, "min": -100.0, "max": 100.0, "step": 0.01}), - }} + return { + "required": { + "model": ("MODEL", {"tooltip": "The diffusion model the LoRA will be applied to."}), + "clip": ("CLIP", {"tooltip": "The CLIP model the LoRA will be applied to."}), + "lora_name": (folder_paths.get_filename_list("loras"), {"tooltip": "The name of the LoRA."}), + "strength_model": ("FLOAT", {"default": 1.0, "min": -100.0, "max": 100.0, "step": 0.01, "tooltip": "How strongly to modify the diffusion model. This value can be negative."}), + "strength_clip": ("FLOAT", {"default": 1.0, "min": -100.0, "max": 100.0, "step": 0.01, "tooltip": "How strongly to modify the CLIP model. This value can be negative."}), + } + } + RETURN_TYPES = ("MODEL", "CLIP") + OUTPUT_TOOLTIPS = ("The modified diffusion model.", "The modified CLIP model.") FUNCTION = "load_lora" CATEGORY = "loaders" - TOOLTIPS = { - "input": { - "model": "The diffusion model the LoRA will be applied to.", - "clip": "The CLIP model the LoRA will be applied to.", - "lora_name": "The name of the LoRA.", - "strength_model": "How strongly to modify the diffusion model. This value can be negative.", - "strength_clip": "How strongly to modify the CLIP model. This value can be negative.", - }, - "output": ("The modified diffusion model.", "The modified CLIP model.") - } + DESCRIPTION = "LoRAs are used to modify diffusion and CLIP models, altering the way in which latents are denoised such as applying styles. Multiple LoRA nodes can be linked together." def load_lora(self, model, clip, lora_name, strength_model, strength_clip): if strength_model == 0 and strength_clip == 0: @@ -1063,21 +1060,19 @@ class EmptyLatentImage: @classmethod def INPUT_TYPES(s): - return {"required": { "width": ("INT", {"default": 512, "min": 16, "max": MAX_RESOLUTION, "step": 8}), - "height": ("INT", {"default": 512, "min": 16, "max": MAX_RESOLUTION, "step": 8}), - "batch_size": ("INT", {"default": 1, "min": 1, "max": 4096})}} + return { + "required": { + "width": ("INT", {"default": 512, "min": 16, "max": MAX_RESOLUTION, "step": 8, "tooltip": "The width of the latent images in pixels."}), + "height": ("INT", {"default": 512, "min": 16, "max": MAX_RESOLUTION, "step": 8, "tooltip": "The height of the latent images in pixels."}), + "batch_size": ("INT", {"default": 1, "min": 1, "max": 4096, "tooltip": "The number of latent images in the batch."}) + } + } RETURN_TYPES = ("LATENT",) + OUTPUT_TOOLTIPS = ("The empty latent image batch.",) FUNCTION = "generate" CATEGORY = "latent" - TOOLTIPS = { - "input": { - "width": "The width of the latent images in pixels.", - "height": "The height of the latent images in pixels.", - "batch_size": "The number of latent images in the batch.", - }, - "output": ("The empty latent image batch.",) - } + DESCRIPTION = "Create a new batch of empty latent images to be denoised via sampling." def generate(self, width, height, batch_size=1): latent = torch.zeros([batch_size, 4, height // 8, width // 8], device=self.device) @@ -1397,39 +1392,27 @@ def common_ksampler(model, seed, steps, cfg, sampler_name, scheduler, positive, class KSampler: @classmethod def INPUT_TYPES(s): - return {"required": - {"model": ("MODEL",), - "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), - "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), - "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "step":0.1, "round": 0.01}), - "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), - "scheduler": (comfy.samplers.KSampler.SCHEDULERS, ), - "positive": ("CONDITIONING", ), - "negative": ("CONDITIONING", ), - "latent_image": ("LATENT", ), - "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), - } - } + return { + "required": { + "model": ("MODEL", {"tooltip": "The model used for denoising the input latent."}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "The random seed used for creating the noise."}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "The number of steps used in the denoising process."}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "step":0.1, "round": 0.01, "tooltip": "The Classifier-Free Guidance scale balances creativity and adherence to the prompt. Higher values result in images more closely matching the prompt however too high values will negatively impact quality."}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, {"tooltip": "The algorithm used when sampling, this can affect the quality, speed, and style of the generated output."}), + "scheduler": (comfy.samplers.KSampler.SCHEDULERS, {"tooltip": "The scheduler controls how noise is gradually removed to form the image."}), + "positive": ("CONDITIONING", {"tooltip": "The conditioning describing the attributes you want to include in the image."}), + "negative": ("CONDITIONING", {"tooltip": "The conditioning describing the attributes you want to exclude from the image."}), + "latent_image": ("LATENT", {"tooltip": "The latent image to denoise."}), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "The amount of denoising applied, lower values will maintain the structure of the initial image allowing for image to image sampling."}), + } + } RETURN_TYPES = ("LATENT",) + OUTPUT_TOOLTIPS = ("The denoised latent.",) FUNCTION = "sample" CATEGORY = "sampling" - TOOLTIPS = { - "input": { - "model": "The model used for denoising the input latent.", - "seed": "The random seed used for creating the noise.", - "steps": "The number of steps used in the denoising process.", - "cfg": "The Classifier-Free Guidance scale balances creativity and adherence to the prompt. Higher values result in images more closely matching the prompt however too high values will negatively impact quality.", - "sampler_name": "The algorithm used when sampling, this can affect the quality, speed, and style of the generated output.", - "scheduler": "The scheduler controls how noise is gradually removed to form the image.", - "positive": "The conditioning describing the attributes you want to include in the image.", - "negative": "The conditioning describing the attributes you want to exclude from the image.", - "latent_image": "The latent image to denoise.", - "denoise": "The amount of denoising applied, lower values will maintain the structure of the initial image allowing for image to image sampling.", - }, - "output": ("The denoised latent.",) - } + DESCRIPTION = "Uses the provided model, positive and negative conditioning to denoise the latent image." def sample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise=1.0): return common_ksampler(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise=denoise) @@ -1477,11 +1460,15 @@ class SaveImage: @classmethod def INPUT_TYPES(s): - return {"required": - {"images": ("IMAGE", ), - "filename_prefix": ("STRING", {"default": "ComfyUI"})}, - "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}, - } + return { + "required": { + "images": ("IMAGE", {"tooltip": "The images to save."}), + "filename_prefix": ("STRING", {"default": "ComfyUI", "tooltip": "The prefix for the file to save. This may include formatting information such as %date:yyyy-MM-dd% or %Empty Latent Image.width% to include values from nodes."}) + }, + "hidden": { + "prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO" + }, + } RETURN_TYPES = () FUNCTION = "save_images" @@ -1489,12 +1476,7 @@ class SaveImage: OUTPUT_NODE = True CATEGORY = "image" - TOOLTIPS = { - "input": { - "images": "The images to save.", - "filename_prefix": "The prefix for the file to save. This may include formatting information such as %date:yyyy-MM-dd% or %Empty Latent Image.width% to include values from nodes.", - } - } + DESCRIPTION = "Saves the input images to your ComfyUI output directory." def save_images(self, images, filename_prefix="ComfyUI", prompt=None, extra_pnginfo=None): filename_prefix += self.prefix_append diff --git a/server.py b/server.py index 4c45b9a4b..eeb706693 100644 --- a/server.py +++ b/server.py @@ -434,8 +434,8 @@ class PromptServer(): if hasattr(obj_class, 'CATEGORY'): info['category'] = obj_class.CATEGORY - if hasattr(obj_class, 'TOOLTIPS'): - info['tooltips'] = obj_class.TOOLTIPS + if hasattr(obj_class, 'OUTPUT_TOOLTIPS'): + info['output_tooltips'] = obj_class.OUTPUT_TOOLTIPS return info @routes.get("/object_info") diff --git a/web/extensions/core/tooltips.js b/web/extensions/core/tooltips.js index cc8194005..6747e732f 100644 --- a/web/extensions/core/tooltips.js +++ b/web/extensions/core/tooltips.js @@ -39,60 +39,65 @@ app.registerExtension({ const tooltipEl = $el("div.comfy-graph-tooltip", { parent: document.body, }); + let idleTimeout; - let tooltipTimeout; const hideTooltip = () => { - if (tooltipTimeout) { - clearTimeout(tooltipTimeout); - } tooltipEl.style.display = "none"; }; const showTooltip = (tooltip) => { - if (tooltipTimeout) { - clearTimeout(tooltipTimeout); - } - if (tooltip) { - tooltipTimeout = setTimeout(() => { - tooltipEl.textContent = tooltip; - tooltipEl.style.display = "block"; - tooltipEl.style.left = app.canvas.mouse[0] + "px"; - tooltipEl.style.top = app.canvas.mouse[1] + "px"; - const rect = tooltipEl.getBoundingClientRect(); - if(rect.right > window.innerWidth) { - tooltipEl.style.left = (app.canvas.mouse[0] - rect.width) + "px"; - } + if (!tooltip) return; - if(rect.top < 0) { - tooltipEl.style.top = (app.canvas.mouse[1] + rect.height) + "px"; - } - }, 500); + tooltipEl.textContent = tooltip; + tooltipEl.style.display = "block"; + tooltipEl.style.left = app.canvas.mouse[0] + "px"; + tooltipEl.style.top = app.canvas.mouse[1] + "px"; + const rect = tooltipEl.getBoundingClientRect(); + if (rect.right > window.innerWidth) { + tooltipEl.style.left = app.canvas.mouse[0] - rect.width + "px"; + } + + if (rect.top < 0) { + tooltipEl.style.top = app.canvas.mouse[1] + rect.height + "px"; } }; + const getInputTooltip = (nodeData, name) => { + const inputDef = nodeData.input?.required?.[name] ?? nodeData.input?.optional?.[name]; + return inputDef?.[1]?.tooltip; + }; + const onIdle = () => { + const { canvas } = app; + const node = canvas.node_over; + const nodeData = node.constructor.nodeData ?? {}; - const onCanvasPointerMove = function () { - hideTooltip(); - const node = this.node_over; - if (!node) return; - - const tooltips = node.constructor.nodeData?.tooltips; - if (!tooltips) return; - - const inputSlot = this.isOverNodeInput(node, this.graph_mouse[0], this.graph_mouse[1], [0, 0]); - if (inputSlot !== -1) { - return showTooltip(tooltips.input?.[node.inputs[inputSlot].name]); + if (node.constructor.title_mode !== LiteGraph.NO_TITLE && canvas.graph_mouse[1] < node.pos[1]) { + return showTooltip(nodeData.description); } - const outputSlot = this.isOverNodeOutput(node, this.graph_mouse[0], this.graph_mouse[1], [0, 0]); + if (node.flags?.collapsed) return; + + const inputSlot = canvas.isOverNodeInput(node, canvas.graph_mouse[0], canvas.graph_mouse[1], [0, 0]); + if (inputSlot !== -1) { + const inputName = node.inputs[inputSlot].name; + return showTooltip(getInputTooltip(nodeData, inputName)); + } + + const outputSlot = canvas.isOverNodeOutput(node, canvas.graph_mouse[0], canvas.graph_mouse[1], [0, 0]); if (outputSlot !== -1) { - return showTooltip(tooltips.output?.[outputSlot]); + return showTooltip(nodeData.output_tooltips?.[outputSlot]); } const widget = getHoveredWidget(); // Dont show for DOM widgets, these use native browser tooltips as we dont get proper mouse events on these if (widget && !widget.element) { - return showTooltip(tooltips.input?.[widget.name]); + return showTooltip(widget.tooltip ?? getInputTooltip(nodeData, widget.name)); } - }.bind(app.canvas); + }; + + const onMouseMove = () => { + hideTooltip(); + clearTimeout(idleTimeout); + idleTimeout = setTimeout(onIdle, 500); + }; app.ui.settings.addSetting({ id: "Comfy.EnableTooltips", @@ -101,10 +106,10 @@ app.registerExtension({ defaultValue: true, onChange(value) { if (value) { - LiteGraph.pointerListenerAdd(app.canvasEl, "move", onCanvasPointerMove); + window.addEventListener("mousemove", onMouseMove); window.addEventListener("click", hideTooltip); } else { - LiteGraph.pointerListenerRemove(app.canvasEl, "move", onCanvasPointerMove); + window.removeEventListener("mousemove", onMouseMove); window.removeEventListener("click", hideTooltip); } }, diff --git a/web/scripts/app.js b/web/scripts/app.js index 8b4478a32..df9245097 100644 --- a/web/scripts/app.js +++ b/web/scripts/app.js @@ -1713,9 +1713,10 @@ export class ComfyApp { for (const o in nodeData["output"]) { let output = nodeData["output"][o]; if(output instanceof Array) output = "COMBO"; + const outputTooltip = nodeData["output_tooltips"]?.[o]; const outputName = nodeData["output_name"][o] || output; const outputShape = nodeData["output_is_list"][o] ? LiteGraph.GRID_SHAPE : LiteGraph.CIRCLE_SHAPE ; - this.addOutput(outputName, output, { shape: outputShape }); + this.addOutput(outputName, output, { shape: outputShape, tooltip: outputTooltip }); } const s = this.computeSize(); diff --git a/web/scripts/widgets.js b/web/scripts/widgets.js index 6a6899705..59b17fd13 100644 --- a/web/scripts/widgets.js +++ b/web/scripts/widgets.js @@ -75,6 +75,7 @@ export function addValueControlWidgets(node, targetWidget, defaultValue = "rando serialize: false, // Don't include this in prompt. } ); + valueControl.tooltip = "Allows the linked widget to be changed automatically, for example randomizing the noise seed."; valueControl[IS_CONTROL_WIDGET] = true; updateControlWidgetLabel(valueControl); widgets.push(valueControl); @@ -95,6 +96,7 @@ export function addValueControlWidgets(node, targetWidget, defaultValue = "rando } ); updateControlWidgetLabel(comboFilter); + comboFilter.tooltip = "Allows for filtering the list of values when changing the value via the control generate mode. Allows for RegEx matches in the format /abc/ to only filter to values containing 'abc'." widgets.push(comboFilter); }