File size: 9,007 Bytes
1e3b872 |
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 |
import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
import { ComfyDialog, $el } from "../../scripts/ui.js";
export function rebootAPI() {
if (confirm("Are you sure you'd like to reboot the server?")) {
try {
api.fetchApi("/ttN/reboot");
}
catch(exception) {
console.log("Failed to reboot: " + exception);
}
return true;
}
return false;
}
export function wait(ms = 16, value) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(value);
}, ms);
});
}
const CONVERTED_TYPE = "converted-widget";
const GET_CONFIG = Symbol();
export function getConfig(widgetName, node) {
const { nodeData } = node.constructor;
return nodeData?.input?.required[widgetName] ?? nodeData?.input?.optional?.[widgetName];
}
export function hideWidget(node, widget, suffix = "") {
widget.origType = widget.type;
widget.origComputeSize = widget.computeSize;
widget.origSerializeValue = widget.serializeValue;
widget.computeSize = () => [0, -4]; // -4 is due to the gap litegraph adds between widgets automatically
widget.type = CONVERTED_TYPE + suffix;
widget.serializeValue = () => {
// Prevent serializing the widget if we have no input linked
if (!node.inputs) {
return undefined;
}
let node_input = node.inputs.find((i) => i.widget?.name === widget.name);
if (!node_input || !node_input.link) {
return undefined;
}
return widget.origSerializeValue ? widget.origSerializeValue() : widget.value;
};
// Hide any linked widgets, e.g. seed+seedControl
if (widget.linkedWidgets) {
for (const w of widget.linkedWidgets) {
hideWidget(node, w, ":" + widget.name);
}
}
}
export function getWidgetType(config) {
// Special handling for COMBO so we restrict links based on the entries
let type = config[0];
if (type instanceof Array) {
type = "COMBO";
}
return { type };
}
export function convertToInput(node, widget, config) {
hideWidget(node, widget);
const { type } = getWidgetType(config);
// Add input and store widget config for creating on primitive node
const sz = node.size;
node.addInput(widget.name, type, {
widget: { name: widget.name, [GET_CONFIG]: () => config },
});
for (const widget of node.widgets) {
widget.last_y += LiteGraph.NODE_SLOT_HEIGHT;
}
// Restore original size but grow if needed
node.setSize([Math.max(sz[0], node.size[0]), Math.max(sz[1], node.size[1])]);
}
export function tinyterraReloadNode(node) {
// Retrieves original values or uses current ones as fallback. Options for creating a new node.
const { title: nodeTitle, color: nodeColor, bgcolor: bgColor } = node.properties.origVals || node;
const options = {
size: [...node.size],
color: nodeColor,
bgcolor: bgColor,
pos: [...node.pos]
};
// Store a reference to the old node before it gets replaced.
const oldNode = node
// Track connections to re-establish later.
const inputConnections = [], outputConnections = [];
if (node.inputs) {
for (const input of node.inputs ?? []) {
if (input.link) {
const input_name = input.name
const input_slot = node.findInputSlot(input_name)
const input_node = node.getInputNode(input_slot)
const input_link = node.getInputLink(input_slot)
inputConnections.push([input_link.origin_slot, input_node, input_name])
}
}
}
if (node.outputs) {
for (const output of node.outputs) {
if (output.links) {
const output_name = output.name
for (const linkID of output.links) {
const output_link = graph.links[linkID]
const output_node = graph._nodes_by_id[output_link.target_id]
outputConnections.push([output_name, output_node, output_link.target_slot])
}
}
}
}
// Remove old node and create a new one.
app.graph.remove(node)
const newNode = app.graph.add(LiteGraph.createNode(node.constructor.type, nodeTitle, options));
if (newNode?.constructor?.hasOwnProperty('ttNnodeVersion')) {
newNode.properties.ttNnodeVersion = newNode.constructor.ttNnodeVersion;
}
// A function to handle reconnection of links to the new node.
function handleLinks() {
for (let ow of oldNode.widgets) {
if (ow.type === CONVERTED_TYPE) {
const config = getConfig(ow.name, oldNode)
const WidgetToConvert = newNode.widgets.find((nw) => nw.name === ow.name);
if (WidgetToConvert && !newNode?.inputs?.find((i) => i.name === ow.name)) {
convertToInput(newNode, WidgetToConvert, config);
}
}
}
// replace input and output links
for (let input of inputConnections) {
const [output_slot, output_node, input_name] = input;
output_node.connect(output_slot, newNode.id, input_name)
}
for (let output of outputConnections) {
const [output_name, input_node, input_slot] = output;
newNode.connect(output_name, input_node, input_slot)
}
}
// fix widget values
let values = oldNode.widgets_values;
if (!values) {
console.log('NO VALUES')
newNode.widgets.forEach((newWidget, index) => {
let pass = false
while ((index < oldNode.widgets.length) && !pass) {
const oldWidget = oldNode.widgets[index];
if (newWidget.type === oldWidget.type) {
newWidget.value = oldWidget.value;
pass = true
}
index++;
}
});
}
else {
let isValid = false
const isIterateForwards = values.length <= newNode.widgets.length;
let valueIndex = isIterateForwards ? 0 : values.length - 1;
const parseWidgetValue = (value, widget) => {
if (['', null].includes(value) && (widget.type === "button" || widget.type === "converted-widget")) {
return { value, isValid: true };
}
if (typeof value === "boolean" && widget.options?.on && widget.options?.off) {
return { value, isValid: true };
}
if (widget.options?.values?.includes(value)) {
return { value, isValid: true };
}
if (widget.inputEl) {
if (typeof value === "string" || value === widget.value) {
return { value, isValid: true };
}
}
if (!isNaN(value)) {
value = parseFloat(value);
if (widget.options?.min <= value && value <= widget.options?.max) {
return { value, isValid: true };
}
}
return { value: widget.value, isValid: false };
};
function updateValue(widgetIndex) {
const oldWidget = oldNode.widgets[widgetIndex];
let newWidget = newNode.widgets[widgetIndex];
let newValueIndex = valueIndex
if (newWidget.name === oldWidget.name && (newWidget.type === oldWidget.type || oldWidget.type === 'ttNhidden' || newWidget.type === 'ttNhidden')) {
while ((isIterateForwards ? newValueIndex < values.length : newValueIndex >= 0) && !isValid) {
let { value, isValid } = parseWidgetValue(values[newValueIndex], newWidget);
if (isValid && value !== NaN) {
newWidget.value = value;
break;
}
newValueIndex += isIterateForwards ? 1 : -1;
}
if (isIterateForwards) {
if (newValueIndex === valueIndex) {
valueIndex++;
}
if (newValueIndex === valueIndex + 1) {
valueIndex++;
valueIndex++;
}
} else {
if (newValueIndex === valueIndex) {
valueIndex--;
}
if (newValueIndex === valueIndex - 1) {
valueIndex--;
valueIndex--;
}
}
//console.log('\n')
}
};
if (isIterateForwards) {
for (let widgetIndex = 0; widgetIndex < newNode.widgets.length; widgetIndex++) {
updateValue(widgetIndex);
}
} else {
for (let widgetIndex = newNode.widgets.length - 1; widgetIndex >= 0; widgetIndex--) {
updateValue(widgetIndex);
}
}
}
handleLinks();
newNode.setSize(options.size)
newNode.onResize([0,0]);
}; |