This commit adds the DynamicImageCombiner class, enabling dynamic addition of multiple image inputs and outputting a list of connected images. The init.py file now includes this new node in NODE_CLASS_MAPPINGS and NODE_DISPLAY_NAME_MAPPINGS. A new JavaScript file has also been created to manage the node's behavior, enhancing image processing functionality. 引入DynamicImageCombiner类并更新__init__.py以实现节点集成。 此提交添加了DynamicImageCombiner类,可以动态地添加多个图像输入,并输出所有连接图像的列表。init.py文件现在在NODE_CLASS_MAPPINGS和NODE_DISPLAY_NAME_MAPPINGS中包含了这个新节点。此外,还创建了一个新的JavaScript文件来管理该节点的行为,增强了图像处理功能。
64 lines
3.0 KiB
JavaScript
64 lines
3.0 KiB
JavaScript
import { app } from "/scripts/app.js";
|
|
|
|
app.registerExtension({
|
|
name: "Comfy.PaintingCoder.DynamicImageCombiner",
|
|
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
|
if (nodeData.name === "DynamicImageCombiner") {
|
|
// 重写 onNodeCreated
|
|
const onNodeCreated = nodeType.prototype.onNodeCreated;
|
|
nodeType.prototype.onNodeCreated = function() {
|
|
const result = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined;
|
|
|
|
// 初始化状态
|
|
this.lastConnectedCount = 0;
|
|
|
|
// 添加第一个输入点
|
|
console.log("初始化:添加第一个图像输入点");
|
|
this.addInput("image_1", "IMAGE");
|
|
|
|
return result;
|
|
};
|
|
|
|
// 添加连接监听
|
|
nodeType.prototype.onConnectionsChange = function(connectionType, slot, isConnected) {
|
|
if (connectionType === 1) { // 输入连接
|
|
console.log(`连接变化 - 槽位: ${slot}, 已连接: ${isConnected}`);
|
|
|
|
// 计算已连接的输入点数量
|
|
const connectedCount = this.inputs.reduce((count, input) =>
|
|
count + (input.link ? 1 : 0), 0);
|
|
|
|
console.log(`当前连接数量: ${connectedCount}, 上次连接数量: ${this.lastConnectedCount}`);
|
|
|
|
// 只在连接数量发生变化时更新
|
|
if (connectedCount !== this.lastConnectedCount) {
|
|
this.lastConnectedCount = connectedCount;
|
|
const targetCount = connectedCount + 1;
|
|
console.log(`目标输入点数量: ${targetCount}`);
|
|
|
|
// 添加新的输入点
|
|
while (this.inputs.length < targetCount) {
|
|
const newIndex = this.inputs.length + 1;
|
|
console.log(`添加新输入点: image_${newIndex}`);
|
|
this.addInput(`image_${newIndex}`, "IMAGE");
|
|
}
|
|
|
|
// 移除多余的输入点,但至少保留一个
|
|
while (this.inputs.length > Math.max(targetCount, 1)) {
|
|
const lastInput = this.inputs[this.inputs.length - 1];
|
|
if (!lastInput.link) {
|
|
console.log(`移除输入点: ${this.inputs.length}`);
|
|
this.removeInput(this.inputs.length - 1);
|
|
}
|
|
}
|
|
|
|
// 更新节点大小
|
|
console.log(`更新节点大小,当前输入点数量: ${this.inputs.length}`);
|
|
this.setSize(this.computeSize());
|
|
app.graph.setDirtyCanvas(true);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
}
|
|
});
|