101 lines
3.0 KiB
JavaScript
101 lines
3.0 KiB
JavaScript
// -----------------------------------------------------------------------
|
|
//
|
|
// !\file CNP_random_resolution.py
|
|
// !\brief Generate random image resolutions based on a base unit and various modes.
|
|
// !\author Colin J.D. Stewart
|
|
//
|
|
// Copyright (c) 2026 Colin J.D. Stewart. All rights reserved.
|
|
//
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
import { app } from "../../scripts/app.js";
|
|
|
|
const PREFIX = "arg_";
|
|
const NODE_CLASS = "CNPFormat";
|
|
|
|
function getDynamicInputs(node) {
|
|
return (node.inputs || []).filter((input) => input?.name?.startsWith(PREFIX));
|
|
}
|
|
|
|
function getNextIndex(node) {
|
|
const dynamicInputs = getDynamicInputs(node);
|
|
if (!dynamicInputs.length) {
|
|
return 1;
|
|
}
|
|
|
|
const last = dynamicInputs[dynamicInputs.length - 1];
|
|
const match = /^(?:arg_)(\d+)$/.exec(last.name || "");
|
|
return match ? Number(match[1]) + 1 : (dynamicInputs.length + 1);
|
|
}
|
|
|
|
function hasLink(input) {
|
|
return input && input.link != null;
|
|
}
|
|
|
|
function ensureTrailingEmptyInput(node) {
|
|
const dynamicInputs = getDynamicInputs(node);
|
|
|
|
if (!dynamicInputs.length) {
|
|
node.addInput(`${PREFIX}1`, "*");
|
|
return;
|
|
}
|
|
|
|
const last = dynamicInputs[dynamicInputs.length - 1];
|
|
if (hasLink(last)) {
|
|
node.addInput(`${PREFIX}${getNextIndex(node)}`, "*");
|
|
}
|
|
}
|
|
|
|
function pruneUnusedInputs(node) {
|
|
const dynamicInputs = getDynamicInputs(node);
|
|
const removable = dynamicInputs.slice(0, -1);
|
|
|
|
for (let i = removable.length - 1; i >= 0; i -= 1) {
|
|
const input = removable[i];
|
|
if (!hasLink(input)) {
|
|
const index = node.inputs.indexOf(input);
|
|
if (index >= 0) {
|
|
node.removeInput(index);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function refreshDynamicInputs(node) {
|
|
pruneUnusedInputs(node);
|
|
ensureTrailingEmptyInput(node);
|
|
node.setDirtyCanvas(true, true);
|
|
app.graph.setDirtyCanvas(true, true);
|
|
}
|
|
|
|
app.registerExtension({
|
|
name: "ComfyNodesPlus.CNPFormat",
|
|
|
|
async beforeRegisterNodeDef(nodeType, nodeData) {
|
|
if (nodeData?.name !== NODE_CLASS && nodeType?.comfyClass !== NODE_CLASS && nodeType?.ComfyClass !== NODE_CLASS) {
|
|
return;
|
|
}
|
|
|
|
const onNodeCreated = nodeType.prototype.onNodeCreated;
|
|
nodeType.prototype.onNodeCreated = function () {
|
|
const result = onNodeCreated?.apply(this, arguments);
|
|
refreshDynamicInputs(this);
|
|
return result;
|
|
};
|
|
|
|
const onConnectionsChange = nodeType.prototype.onConnectionsChange;
|
|
nodeType.prototype.onConnectionsChange = function () {
|
|
const result = onConnectionsChange?.apply(this, arguments);
|
|
refreshDynamicInputs(this);
|
|
return result;
|
|
};
|
|
|
|
const onConfigure = nodeType.prototype.onConfigure;
|
|
nodeType.prototype.onConfigure = function () {
|
|
const result = onConfigure?.apply(this, arguments);
|
|
refreshDynamicInputs(this);
|
|
return result;
|
|
};
|
|
},
|
|
}); |