Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,390 Bytes
3d5837a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
import { ComfyApp, app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
let get_wildcards_list;
try {
const ImpactPack = await import("../ComfyUI-Impact-Pack/impact-pack.js");
get_wildcards_list = ImpactPack.get_wildcards_list;
}
catch (error) {}
// fallback
if(!get_wildcards_list) {
get_wildcards_list = () => { return ["Impact Pack isn't installed or is outdated."]; }
}
let pb_cache = {};
async function get_prompt_builder_items(category) {
if(pb_cache[category])
return pb_cache[category];
else {
let res = await api.fetchApi(`/inspire/prompt_builder?category=${category}`);
let data = await res.json();
pb_cache[category] = data.presets;
return data.presets;
}
}
app.registerExtension({
name: "Comfy.Inspire.Prompts",
nodeCreated(node, app) {
if(node.comfyClass == "WildcardEncode //Inspire") {
const wildcard_text_widget_index = node.widgets.findIndex((w) => w.name == 'wildcard_text');
const populated_text_widget_index = node.widgets.findIndex((w) => w.name == 'populated_text');
const mode_widget_index = node.widgets.findIndex((w) => w.name == 'mode');
const wildcard_text_widget = node.widgets[wildcard_text_widget_index];
const populated_text_widget = node.widgets[populated_text_widget_index];
// lora selector, wildcard selector
let combo_id = 5;
Object.defineProperty(node.widgets[combo_id], "value", {
set: (value) => {
const stackTrace = new Error().stack;
if(stackTrace.includes('inner_value_change')) {
if(value != "Select the LoRA to add to the text") {
let lora_name = value;
if (lora_name.endsWith('.safetensors')) {
lora_name = lora_name.slice(0, -12);
}
wildcard_text_widget.value += `<lora:${lora_name}>`;
}
}
},
get: () => { return "Select the LoRA to add to the text"; }
});
Object.defineProperty(node.widgets[combo_id+1], "value", {
set: (value) => {
const stackTrace = new Error().stack;
if(stackTrace.includes('inner_value_change')) {
if(value != "Select the Wildcard to add to the text") {
if(wildcard_text_widget.value != '')
wildcard_text_widget.value += ', '
wildcard_text_widget.value += value;
}
}
},
get: () => { return "Select the Wildcard to add to the text"; }
});
Object.defineProperty(node.widgets[combo_id+1].options, "values", {
set: (x) => {},
get: () => {
return get_wildcards_list();
}
});
// Preventing validation errors from occurring in any situation.
node.widgets[combo_id].serializeValue = () => { return "Select the LoRA to add to the text"; }
node.widgets[combo_id+1].serializeValue = () => { return "Select the Wildcard to add to the text"; }
// wildcard populating
populated_text_widget.inputEl.disabled = true;
const mode_widget = node.widgets[mode_widget_index];
// mode combo
Object.defineProperty(mode_widget, "value", {
set: (value) => {
node._mode_value = value == true || value == "Populate";
populated_text_widget.inputEl.disabled = value == true || value == "Populate";
},
get: () => {
if(node._mode_value != undefined)
return node._mode_value;
else
return true;
}
});
}
else if(node.comfyClass == "MakeBasicPipe //Inspire") {
const pos_wildcard_text_widget = node.widgets.find((w) => w.name == 'positive_wildcard_text');
const pos_populated_text_widget = node.widgets.find((w) => w.name == 'positive_populated_text');
const neg_wildcard_text_widget = node.widgets.find((w) => w.name == 'negative_wildcard_text');
const neg_populated_text_widget = node.widgets.find((w) => w.name == 'negative_populated_text');
const mode_widget = node.widgets.find((w) => w.name == 'wildcard_mode');
const direction_widget = node.widgets.find((w) => w.name == 'Add selection to');
// lora selector, wildcard selector
let combo_id = 5;
Object.defineProperty(node.widgets[combo_id], "value", {
set: (value) => {
const stackTrace = new Error().stack;
if(stackTrace.includes('inner_value_change')) {
if(value != "Select the LoRA to add to the text") {
let lora_name = value;
if (lora_name.endsWith('.safetensors')) {
lora_name = lora_name.slice(0, -12);
}
if(direction_widget.value) {
pos_wildcard_text_widget.value += `<lora:${lora_name}>`;
}
else {
neg_wildcard_text_widget.value += `<lora:${lora_name}>`;
}
}
}
},
get: () => { return "Select the LoRA to add to the text"; }
});
Object.defineProperty(node.widgets[combo_id+1], "value", {
set: (value) => {
const stackTrace = new Error().stack;
if(stackTrace.includes('inner_value_change')) {
if(value != "Select the Wildcard to add to the text") {
let w = null;
if(direction_widget.value) {
w = pos_wildcard_text_widget;
}
else {
w = neg_wildcard_text_widget;
}
if(w.value != '')
w.value += ', '
w.value += value;
}
}
},
get: () => { return "Select the Wildcard to add to the text"; }
});
Object.defineProperty(node.widgets[combo_id+1].options, "values", {
set: (x) => {},
get: () => {
return get_wildcards_list();
}
});
// Preventing validation errors from occurring in any situation.
node.widgets[combo_id].serializeValue = () => { return "Select the LoRA to add to the text"; }
node.widgets[combo_id+1].serializeValue = () => { return "Select the Wildcard to add to the text"; }
// wildcard populating
pos_populated_text_widget.inputEl.disabled = true;
neg_populated_text_widget.inputEl.disabled = true;
// mode combo
Object.defineProperty(mode_widget, "value", {
set: (value) => {
pos_populated_text_widget.inputEl.disabled = node._mode_value;
neg_populated_text_widget.inputEl.disabled = node._mode_value;
node._mode_value = value;
},
get: () => {
if(node._mode_value != undefined)
return node._mode_value;
else
return true;
}
});
}
else if(node.comfyClass == "PromptBuilder //Inspire") {
const preset_widget = node.widgets[node.widgets.findIndex(obj => obj.name === 'preset')];
const category_widget = node.widgets[node.widgets.findIndex(obj => obj.name === 'category')];
Object.defineProperty(preset_widget.options, "values", {
set: (x) => {},
get: () => {
get_prompt_builder_items(category_widget.value);
if(pb_cache[category_widget.value] == undefined) {
return ["#PRESET"];
}
return pb_cache[category_widget.value];
}
});
Object.defineProperty(preset_widget, "value", {
set: (x) => {
const stackTrace = new Error().stack;
if(stackTrace.includes('inner_value_change')) {
if(node.widgets[2].value) {
node.widgets[2].value += ', ';
}
const y = x.split(':');
if(y.length == 2)
node.widgets[2].value += y[1].trim();
else
node.widgets[2].value += x.trim();
if(node.widgets_values) {
node.widgets_values[2] = node.widgets[2].values;
}
};
},
get: () => { return '#PRESET'; }
});
preset_widget.serializeValue = (workflowNode, widgetIndex) => { return "#PRESET"; };
}
else if(node.comfyClass == "SeedExplorer //Inspire"
|| node.comfyClass == "RegionalSeedExplorerMask //Inspire"
|| node.comfyClass == "RegionalSeedExplorerColorMask //Inspire") {
const prompt_widget = node.widgets[node.widgets.findIndex(obj => obj.name === 'seed_prompt')];
const seed_widget = node.widgets[node.widgets.findIndex(obj => obj.name === 'additional_seed')];
const strength_widget = node.widgets[node.widgets.findIndex(obj => obj.name === 'additional_strength')];
let allow_init_seed = node.comfyClass == "SeedExplorer //Inspire";
node.addWidget("button", "Add to prompt", null, () => {
if(!prompt_widget.value?.trim() && allow_init_seed) {
prompt_widget.value = ''+seed_widget.value;
}
else {
if(prompt_widget.value?.trim())
prompt_widget.value += ', ';
prompt_widget.value += `${seed_widget.value}:${strength_widget.value.toFixed(2)}`;
seed_widget.value += 1;
}
});
}
}
});
const original_queuePrompt = api.queuePrompt;
async function queuePrompt_with_widget_idxs(number, { output, workflow }) {
workflow.widget_idx_map = {};
for(let i in app.graph._nodes_by_id) {
let widgets = app.graph._nodes_by_id[i].widgets;
if(widgets) {
for(let j in widgets) {
if(['seed', 'noise_seed', 'sampler_name', 'scheduler'].includes(widgets[j].name)
&& widgets[j].type != 'converted-widget') {
if(workflow.widget_idx_map[i] == undefined) {
workflow.widget_idx_map[i] = {};
}
workflow.widget_idx_map[i][widgets[j].name] = parseInt(j);
}
}
}
}
return await original_queuePrompt.call(api, number, { output, workflow });
}
api.queuePrompt = queuePrompt_with_widget_idxs; |