forked from space-nuko/ComfyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.ts
125 lines (106 loc) · 4.26 KB
/
widgets.ts
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
import { LGraphNode, LiteGraph } from "@litegraph-ts/core";
import type IComfyInputSlot from "./IComfyInputSlot";
import type { ComfyInputConfig } from "./IComfyInputSlot";
import { ComfyComboNode, ComfyNumberNode, ComfyTextNode } from "./nodes/widgets";
import type { ComfyNodeDefInput } from "./ComfyNodeDef";
type WidgetFactoryCallback = (node: LGraphNode, inputName: string, inputData: ComfyNodeDefInput) => IComfyInputSlot;
type WidgetFactory = {
/* Creates the input */
callback: WidgetFactoryCallback,
/* Input type as used by litegraph */
inputType: string,
/* Node type to instantiate */
nodeType: string,
/* Number of widgets this factory instantiates. */
addedWidgetCount: number
}
function getNumberDefaults(inputData: ComfyNodeDefInput, defaultStep: number): ComfyInputConfig {
let defaultValue = inputData[1].default;
let { min, max, step } = inputData[1];
if (defaultValue == undefined) defaultValue = 0;
if (min == undefined) min = 0;
if (max == undefined) max = 2048;
if (step == undefined) step = defaultStep;
return { min, max, step: step, precision: 0, defaultValue };
}
function addComfyInput(node: LGraphNode, inputName: string, extraInfo: Partial<IComfyInputSlot> = {}): IComfyInputSlot {
const input = node.addInput(inputName) as IComfyInputSlot
for (const [k, v] of Object.entries(extraInfo))
input[k] = v
if (input.defaultWidgetNode) {
const ty = Object.values(LiteGraph.registered_node_types)
.find(v => v.class === input.defaultWidgetNode)
if (ty)
input.widgetNodeType = ty.type
}
input.serialize = true;
return input;
}
const FLOAT: WidgetFactory = {
callback: (node: LGraphNode, inputName: string, inputData: ComfyNodeDefInput): IComfyInputSlot => {
const config = getNumberDefaults(inputData, 0.5);
return addComfyInput(node, inputName, { type: "number", config, defaultWidgetNode: ComfyNumberNode })
},
inputType: "number",
nodeType: "ui/number",
addedWidgetCount: 1
}
const INT: WidgetFactory = {
callback: (node: LGraphNode, inputName: string, inputData: ComfyNodeDefInput): IComfyInputSlot => {
const config = getNumberDefaults(inputData, 1);
return addComfyInput(node, inputName, { type: "number", config, defaultWidgetNode: ComfyNumberNode })
},
nodeType: "ui/number",
inputType: "number",
addedWidgetCount: 1
}
const STRING: WidgetFactory = {
callback: (node: LGraphNode, inputName: string, inputData: ComfyNodeDefInput): IComfyInputSlot => {
const defaultValue = inputData[1].default || "";
const multiline = !!inputData[1].multiline;
return addComfyInput(node, inputName, { type: "string", config: { defaultValue, multiline }, defaultWidgetNode: ComfyTextNode })
},
inputType: "number",
nodeType: "ui/text",
addedWidgetCount: 1
}
const COMBO: WidgetFactory = {
callback: (node: LGraphNode, inputName: string, inputData: ComfyNodeDefInput): IComfyInputSlot => {
const type = inputData[0] as string[];
let defaultValue = type[0];
if (inputData[1] && inputData[1].default) {
defaultValue = inputData[1].default;
}
return addComfyInput(node, inputName, { type: "string", config: { values: type, defaultValue }, defaultWidgetNode: ComfyComboNode })
},
inputType: "number",
nodeType: "ui/combo",
addedWidgetCount: 1
}
const IMAGEUPLOAD: WidgetFactory = {
callback: (node: LGraphNode, inputName: string, inputData: ComfyNodeDefInput): IComfyInputSlot => {
return addComfyInput(node, inputName, { type: "number", config: {} })
},
inputType: "COMFY_IMAGES",
nodeType: "ui/image_upload",
addedWidgetCount: 1
}
const INT_seed: WidgetFactory = {
...INT,
// Adds a "randomize" combo box
// When converting from vanilla it should be skipped in the widgets_values
// array, so indicate this here
// litegraph really ought to key these by name instead of array indices...
addedWidgetCount: 2
}
export type WidgetRepository = Record<string, WidgetFactory>
const ComfyWidgets: WidgetRepository = {
"INT:seed": INT_seed,
"INT:noise_seed": INT_seed,
FLOAT,
INT,
STRING,
COMBO,
IMAGEUPLOAD,
}
export default ComfyWidgets