slotDefaults.js 2.23 KB
Newer Older
1
import { app } from "/scripts/app.js";
2
import { ComfyWidgets } from "/scripts/widgets.js";
3
4
5
6
// Adds defaults for quickly adding nodes with middle click on the input/output

app.registerExtension({
	name: "Comfy.SlotDefaults",
7
	suggestionsNumber: null,
8
9
	init() {
		LiteGraph.middle_click_slot_add_default_node = true;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
		this.suggestionsNumber = app.ui.settings.addSetting({
			id: "Comfy.NodeSuggestions.number",
			name: "number of nodes suggestions",
			type: "slider",
			attrs: {
				min: 1,
				max: 100,
				step: 1,
			},
			defaultValue: 5,
			onChange: (newVal, oldVal) => {
				this.setDefaults(newVal);
			}
		});
24
	},
25
26
	slot_types_default_out: {},
	slot_types_default_in: {},
27
	async beforeRegisterNodeDef(nodeType, nodeData, app) {
28
				var nodeId = nodeData.name;
29
		var inputs = [];
30
		inputs = nodeData["input"]["required"]; //only show required inputs to reduce the mess also not logical to create node with optional inputs
31
32
33
34
35
36
37
38
39
40
		for (const inputKey in inputs) {
			var input = (inputs[inputKey]);
			if (typeof input[0] !== "string") continue;

			var type = input[0]
			if (type in ComfyWidgets) {
				var customProperties = input[1]
				if (!(customProperties?.forceInput)) continue; //ignore widgets that don't force input
			}

41
42
			if (!(type in this.slot_types_default_out)) {
				this.slot_types_default_out[type] = ["Reroute"];
43
			}
44
45
46
			if (this.slot_types_default_out[type].includes(nodeId)) continue;
			this.slot_types_default_out[type].push(nodeId);
		} 
47
48
49
50

		var outputs = nodeData["output"];
		for (const key in outputs) {
			var type = outputs[key];
51
52
			if (!(type in this.slot_types_default_in)) {
				this.slot_types_default_in[type] = ["Reroute"];// ["Reroute", "Primitive"];  primitive doesn't always work :'()
53
			}
54

55
56
			this.slot_types_default_in[type].push(nodeId);
		}
comfyanonymous's avatar
comfyanonymous committed
57
		var maxNum = this.suggestionsNumber.value;
58
		this.setDefaults(maxNum);
59
	},
60
61
62
63
64
65
66
67
68
69
70
71
	setDefaults(maxNum) {

		LiteGraph.slot_types_default_out = {};
		LiteGraph.slot_types_default_in = {};

		for (const type in this.slot_types_default_out) {
			LiteGraph.slot_types_default_out[type] = this.slot_types_default_out[type].slice(0, maxNum);
		}
		for (const type in this.slot_types_default_in) {
			LiteGraph.slot_types_default_in[type] = this.slot_types_default_in[type].slice(0, maxNum);
		}
	}
72
});