Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,203 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
import { ComfyApp, app } from "../../scripts/app.js";
export function register_concat_conditionings_with_multiplier_node(nodeType, nodeData, app) {
if (nodeData.name === 'ConcatConditioningsWithMultiplier //Inspire') {
var input_name = "conditioning";
const onConnectionsChange = nodeType.prototype.onConnectionsChange;
let this_handler = async function (type, index, connected, link_info) {
let last_state = this.state_change_handling;
try {
this.state_change_handling = true;
if(!link_info || link_info.type != 'CONDITIONING')
return;
let self = this;
function get_input_count(prefix, linked_only) {
let cnt = 0;
for(let i in self.inputs) {
if(linked_only && !self.inputs[i].link)
continue;
if(self.inputs[i].name.startsWith(prefix))
cnt+=1;
}
return cnt;
}
function get_widget_count(prefix) {
let cnt = 0;
for(let i in self.widgets) {
if(self.widgets[i].name.startsWith(prefix))
cnt+=1;
}
return cnt;
}
function get_unconnected() {
let unconnected = [];
for(let i in self.inputs) {
let input = self.inputs[i];
if(input.name.startsWith('conditioning')) {
if(input.link == undefined)
unconnected.push(i);
}
}
return unconnected;
}
let unconnected = get_unconnected();
function renames() {
let con_i = 1;
let rename_map = {};
for(let i in self.inputs) {
let input = self.inputs[i];
if(input.name.startsWith('conditioning')) {
let orig_i = Number(input.name.substring(12));
if(orig_i != con_i) {
rename_map[orig_i] = con_i;
input.name = 'conditioning'+con_i;
}
con_i++;
}
}
// update multiplier input
for(let i in self.inputs) {
let input = self.inputs[i];
if(input.name.startsWith('multiplier')) {
let orig_i = Number(input.name.substring(10));
if(rename_map[orig_i]) {
input.name = 'multiplier'+rename_map[orig_i];
}
}
}
// update multiplier widget
for(let i in self.widgets) {
let w = self.widgets[i];
if(w.name.startsWith('multiplier')) {
let orig_i = Number(w.name.substring(10));
if(rename_map[orig_i]) {
w.name = 'multiplier'+rename_map[orig_i];
}
}
}
return con_i;
}
function remove_multiplier_link(i, link_id) {
let link = app.graph.links[link_id];
const node = app.graph.getNodeById(link.origin_id);
let x = node.outputs[link.origin_slot].links.findIndex((w) => w == link_id);
node.outputs[link.origin_slot].links.splice(x, 1);
self.disconnectInput(i);
app.graph.links.splice(link_id, 1);
}
async function remove_target_multiplier(target_name) {
// remove strength from slot
for(let i in self.inputs) {
let input = self.inputs[i];
if(input.name.startsWith(target_name)) {
if(input.link) {
remove_multiplier_link(i, input.link);
}
await self.removeInput(i);
break;
}
}
const widget_index = self.widgets.findIndex((w) => w.name == target_name);
self.widgets.splice(widget_index, 1);
}
async function remove_garbage() {
let unconnected = get_unconnected();
// remove unconnected conditionings
while(unconnected.length > 0) {
let last_one = unconnected.reverse()[0];
self.removeInput(last_one);
unconnected = get_unconnected();
}
// remove dangling multipliers
let conds = new Set();
let muls = new Set();
for(let i in self.inputs) {
let input = self.inputs[i];
if(input.link && input.name.startsWith('conditioning')) {
let index = Number(input.name.substring(12));
conds.add(index);
}
else if(input.name.startsWith('multiplier')) {
let index = Number(input.name.substring(10));
muls.add(index);
}
}
for(let i in self.widgets) {
let index = Number(self.widgets[i].name.substring(10));
muls.add(index);
}
let dangling_muls = [...muls].filter(x => !conds.has(x));
while(dangling_muls.length > 0) {
let remove_target = dangling_muls.pop();
let target_name = `multiplier${remove_target}`;
await remove_target_multiplier(target_name);
}
}
async function ensure_multipliers() {
if(self.ensuring_multipliers) {
return;
}
try {
self.ensuring_multipliers = true;
let ncon = get_input_count('conditioning', true);
let nmul = get_input_count('multiplier', false) + get_widget_count('multiplier');
if(ncon == 0 && nmul == 0)
ncon = 1;
for(let i = nmul+1; i<=ncon; i++) {
let config = { min: 0, max: 10, step: 0.1, round: 0.01, precision: 2 };
// NOTE: addWidget trigger calling ensure_multipliers
let widget = await self.addWidget("number", `multiplier${i}`, 1.0, function (v) {
if (config.round) {
self.value = Math.round(v/config.round)*config.round;
} else {
self.value = v;
}
}, config);
}
}
finally{
self.ensuring_multipliers = null;
}
}
async function recover_multipliers() {
if(self.recover_multipliers) {
return;
}
try {
self.recover_multipliers = true;
for(let i = 1; i<self.widgets_values.length; i++) {
let config = { min: 0, max: 10, step: 0.1, round: 0.01, precision: 2 };
// NOTE: addWidget trigger calling recover_multipliers
let widget = await self.addWidget("number", `multiplier${i+1}`, 1.0, function (v) {
if (config.round) {
self.value = Math.round(v/config.round)*config.round;
} else {
self.value = v;
}
}, config);
}
}
finally{
self.recover_multipliers = null;
}
}
async function ensure_inputs() {
if(get_unconnected() == 0) {
let con_i = renames();
self.addInput(`conditioning${con_i}`, self.outputs[0].type);
}
}
const stackTrace = new Error().stack;
if(!stackTrace.includes('loadGraphData') && !stackTrace.includes('pasteFromClipboard')) {
await remove_garbage();
await ensure_inputs();
}
if(!stackTrace.includes('loadGraphData')) {
await ensure_multipliers();
}
else {
await recover_multipliers();
}
await this.setSize( this.computeSize() );
}
finally {
this.state_change_handling = last_state;
}
}
nodeType.prototype.onConnectionsChange = this_handler;
}
}
function ensure_splitter_outputs(node, output_name, value, type) {
if(node.outputs.length != (value + 1)) {
while(node.outputs.length != (value + 1)) {
if(node.outputs.length > value + 1) {
node.removeOutput(node.outputs.length-1);
}
else {
node.addOutput(`output${node.outputs.length+1}`, type);
}
}
for(let i in node.outputs) {
let output = node.outputs[i];
output.name = `${output_name} ${parseInt(i)+1}`;
}
if(node.outputs[0].label == type || node.outputs[0].label == 'remained')
delete node.outputs[0].label;
let last_output = node.outputs[node.outputs.length-1];
last_output.name = 'remained';
}
}
export function register_splitter(node, app) {
if(node.comfyClass === 'ImageBatchSplitter //Inspire' || node.comfyClass === 'LatentBatchSplitter //Inspire') {
let split_count = node.widgets[0];
let output_name = 'output';
let output_type = "*";
if(node.comfyClass === 'ImageBatchSplitter //Inspire') {
output_name = 'image';
output_type = "IMAGE";
}
else if(node.comfyClass === 'LatentBatchSplitter //Inspire') {
output_name = 'latent';
output_type = "LATENT";
}
ensure_splitter_outputs(node, output_name, split_count.value, output_type);
Object.defineProperty(split_count, "value", {
set: async function(value) {
if(value < 0 || value > 50)
return;
ensure_splitter_outputs(node, output_name, value, output_type);
},
get: function() {
return node.outputs.length - 1;
}
});
}
} |