Skip to content

Commit

Permalink
add text concatenate dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraScott committed Sep 23, 2024
1 parent 77370c5 commit dc4c529
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 10 deletions.
2 changes: 1 addition & 1 deletion MaraScott_Nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
# active : \ud83d\udc30
# deprecated : \u274C
NODE_DISPLAY_NAME_MAPPINGS = {
"MaraScottDisplayInfo_v2": "\ud83d\udc30 Display Info - Text v2 /i",
"MaraScottDisplayInfo_v2": "\ud83d\udc30 Display Any - Text v2 /i",
"MaraScottMcBoatyUpscalerRefiner_v5": "\ud83d\udc30 Large Refiner - McBoaty [1/3] v5 /u",
"MaraScottMcBoatyUpscaler_v5": "\ud83d\udc30 Upscaler - McBoaty [1/3] v5 /u",
"MaraScottMcBoatyTilePrompter_v5": "\ud83d\udc30 Tile Prompter - McBoaty [2/3] v5 /u",
Expand Down
10 changes: 6 additions & 4 deletions py/nodes/Prompt/TextConcatenate_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class TextConcatenate_v1:
@classmethod
def INPUT_TYPES(s):
return {
"hidden": {"id":"UNIQUE_ID"},
"hidden": {
"id":"UNIQUE_ID",
**ProfileNodeTextConcatenate.ENTRIES,
},
"required": {
"delimiter": ("STRING", { "label": "Delimiter" }),
"delimiter": ("STRING", { "default": ", ", "label": "Delimiter" }),
},
"optional": {
**ProfileNodeTextConcatenate.ENTRIES,
}
}

Expand All @@ -33,7 +35,7 @@ def INPUT_TYPES(s):

def fn(self, **kwargs):

delimiter = kwargs.get('delimiter', "")
delimiter = kwargs.get('delimiter', ", ")

prefix="string"
filtered_kwargs = {k: v for k, v in kwargs.items() if k.startswith(prefix)}
Expand Down
5 changes: 0 additions & 5 deletions web/assets/js/DisplayInfo_v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { ComfyWidgets } from "../../scripts/widgets.js";
app.registerExtension({
name: "ComfyUI.MaraScott.DisplayInfo_v2",
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (!nodeData?.category?.startsWith("MaraScott")) {
return;
}

if (nodeData.name === "MaraScottDisplayInfo_v2") {
const onExecuted = nodeType.prototype.onExecuted;

Expand All @@ -18,7 +14,6 @@ app.registerExtension({
for (let i = 1; i < this.widgets.length; i++) {
this.widgets[i].onRemove?.();
}
this.widgets.length = 1;
}

// Check if the "text" widget already exists.
Expand Down
162 changes: 162 additions & 0 deletions web/assets/js/TextConcatenate_v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* TextConcatenate_v1
*/

import { app } from "../../scripts/app.js";

if (!window.marascott) {
window.marascott = {}
}
if (!window.marascott.TextConcatenate_v1) {
window.marascott.TextConcatenate_v1 = {
init: false,
}
}

class MaraScottTextConcatenate_v1NodeWidget {

static INPUTS = {
name: "Nb Inputs",
default: 2,
min: 2,
max: 24,
};

static setValueInputs(node, name, value) {
let qty = 0;
let _value = value + MaraScottTextConcatenate_v1.FIRST_INDEX;
if (node.inputs.length > _value) {
qty = node.inputs.length - _value;
for (let i = qty; i > 0; i--) {
node.removeInput(node.inputs.length - 1);
}
}
if (node.inputs.length < _value) {
qty = _value - node.inputs.length;
for (let i = 0; i < qty; i++) {
const name = "string " + (node.inputs.length + 1).toString().padStart(2, '0');
const type = "STRING";
node.addInput(name, type);
}
}
}

static setValue(node, name, value) {
const nodeWidget = this.getByName(node, name);
nodeWidget.value = value;
node.setProperty(name, nodeWidget.value ?? node.properties[name]);
this.setValueInputs(node, name, value);
node.setDirtyCanvas(true);
}

static setInputsSelect(node) {

const nodeWidget = this.getByName(node, this.INPUTS.name);

if (nodeWidget == undefined) {
let values = [];
for (let i = this.INPUTS.min; i <= this.INPUTS.max; i++) {
values.push(i);
}

node.addWidget(
"combo",
this.INPUTS.name,
node.properties[this.INPUTS.name] ?? this.INPUTS.default,
(value, LGraphCanvas, Node, Coordinate, PointerEvent) => {
this.setValue(node, this.INPUTS.name, value);
},
{ "values": values }
);
node.setProperty(this.INPUTS.name, this.INPUTS.default);
this.setValue(node, this.INPUTS.name, this.INPUTS.default);

for (let i = node.widgets.length - 1; i >= 0; i--) {
if (node.widgets[i].name.startsWith("string") ) {
node.widgets.splice(i, 1); // Remove the widget from node.widgets
if(node.widgets_values) {
node.widgets_values.splice(i, 1); // Remove the corresponding value from node.widgets_values
}
}
}

}
}

static getByName(node, name) {
return node.widgets?.find((w) => w.name === name);
}
}

class MaraScottTextConcatenate_v1 {

static TYPE = "MaraScottTextConcatenate_v1"

static FIRST_INDEX = 0;

static configure(node) {
node.shape = LiteGraph.CARD_SHAPE;
node.color = LGraphCanvas.node_colors.green.color;
node.bgcolor = LGraphCanvas.node_colors.green.bgcolor;
node.size[0] = 150;
// node.title = "Text Concatenate"

}

static setWidgets(node) {
MaraScottTextConcatenate_v1NodeWidget.setInputsSelect(node);
}
}

class MaraScottTextConcatenate_v1LiteGraph {
static onNodeCreated(nodeType) {
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
const r = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined;

MaraScottTextConcatenate_v1.configure(this);
MaraScottTextConcatenate_v1.setWidgets(this);

return r;
}
}
}

const MaraScottTextConcatenate_v1NodeExtension = {
// Unique name for the extension
name: "Comfy.MaraScott.TextConcatenate_v1",

// init(app) {},
// setup(app) {},
// addCustomNodeDefs(defs, app) {
// const withNodesNames = false
// if (withNodesNames) {
// } else {
// }
// },
// getCustomWidgets(app) {
// },
// registerCustomNodes(app) {
// },
loadedGraphNode(node, app) {
if (node.type == MaraScottTextConcatenate_v1.TYPE) {
node.setProperty('uuid', node.id)
}
},
// nodeCreated(node, app) {
// },
beforeRegisterNodeDef(nodeType, nodeData, app) {
if (nodeData.name == MaraScottTextConcatenate_v1.TYPE) {
MaraScottTextConcatenate_v1LiteGraph.onNodeCreated(nodeType)
}
},
beforeConfigureGraph(app) {
window.marascott.TextConcatenate_v1.init = false
},
afterConfigureGraph(app) {
window.marascott.TextConcatenate_v1.init = true
},

};

app.registerExtension(MaraScottTextConcatenate_v1NodeExtension);

0 comments on commit dc4c529

Please sign in to comment.