Skip to content

changing_widgets

chrisgoringe edited this page Sep 29, 2023 · 1 revision

Changing the value of a widget

Maybe you want a node to update its widgets. It's not very hard, but there's a gotcha: this won't change the values for this run (even if the node hasn't executed yet, the whole prompt has already been sent). That's why Comfy has control_after_generate not control_before_generate!

// somewhere inside a callback, perhaps in onExecuted, you want to set the value of a widget called `name_of_widget` to `new_value`
	var w = this.widgets?.find((w) => w.name === 'name_of_widget')
	if (w) {
		w.value = new_value;
		this.onResize?.(this.size);  // onResize redraws the node
	}   

What if it's another node you want to change? If you know its id, it's pretty much the same:

	var node_id = parseInt(other_node_id);                             // if you have it as a string it needs to be an int
	var node = this.graph._nodes_by_id[node_id];                       // for you to look it up like this
	var w = node?.widgets.find((w) => w.name==='name_of_widget'); // and then it's just the same
	if (w) { 
		w.value = new_value; 
		node.onResize?.(node.size);
	} 
Clone this wiki locally