Add editable reference image preprocessor node

This commit is contained in:
2026-11-02 09:02:09 +00:00
parent 83773470bf
commit 6646ecdfd9
5 changed files with 248 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
import { app } from "../../scripts/app.js";
const CLASS = "DumasReferenceImagePreprocessor";
const get = (node, name) => node.widgets?.find((widget) => widget.name === name);
const promptFor = (node) => {
const type = String(get(node, "reference_type")?.value || "Character");
const id = String(get(node, "reference_id")?.value || "1");
const description = String(get(node, "description")?.value || "").trim();
const token = `@R${id}`;
const opening = {
Character: `Describe the character in the image in exquisite detail. Include clothing, accessories, body type, proportions, and size. The character will be referred to as ${token} and should be treated as a photographic representation of a real person, so start the description with "${token}". Do not refer to individual shots; treat this as a visual description of the character.`,
Object: `Describe the object in the image in exquisite detail. Include its shape, size, materials, colours, construction, surface detail, condition, and distinctive features. The object will be referred to as ${token}. Do not refer to individual shots; treat this as a visual description of the object.`,
Scene: `Describe the scene in the image in exquisite detail. Include the environment, architecture, spatial layout, lighting, atmosphere, colours, perspective, and important background elements. The scene will be referred to as ${token}. Do not refer to individual shots; treat this as a visual description of the scene.`,
Creature: `Describe the creature in the image in exquisite detail. Include its anatomy, body structure, size, proportions, skin, fur, scales, colouring, distinctive features, expression, and physical condition. The creature will be referred to as ${token}. Do not refer to individual shots; treat this as a visual description of the creature.`,
Vehicle: `Describe the vehicle in the image in exquisite detail. Include its type, dimensions, shape, materials, colours, design, mechanical features, condition, markings, and distinctive details. The vehicle will be referred to as ${token}. Do not refer to individual shots; treat this as a visual description of the vehicle.`,
}[type] || "";
return `${opening}${description ? `\n\nAdditional information about the ${type.toLowerCase()}: "${description}"` : ""}`;
};
function setPrompt(node) {
const widget = get(node, "generated_prompt");
if (!widget) return;
widget.value = promptFor(node);
widget.callback?.(widget.value);
node.setDirtyCanvas?.(true, true);
}
function configure(node) {
if (node?.comfyClass !== CLASS || node.__dumasReferencePreprocessor) return;
node.__dumasReferencePreprocessor = true;
const type = get(node, "reference_type");
const id = get(node, "reference_id");
const prompt = get(node, "generated_prompt");
const wrap = (widget) => {
if (!widget) return;
const callback = widget.callback;
widget.callback = function (value) {
const result = callback?.call(this, value);
setPrompt(node);
return result;
};
};
wrap(type);
wrap(id);
if (prompt) {
prompt.options ||= {};
prompt.options.multiline = true;
prompt.computeSize = (width) => [Math.max(320, Number(width) || 420), 150];
}
const button = node.addWidget?.("button", "regenerate_prompt", "Regenerate prompt", () => setPrompt(node));
if (button) button.serialize = false;
setPrompt(node);
}
app.registerExtension({
name: "Dumas.ReferenceImagePreprocessor",
nodeCreated(node) { configure(node); },
});