Adding a visual shader node with addon

Godot Version

4.4

Question

How can i refresh the visual shader editor to show my newly added floatconstant node which was added through my button panel?

Right now i am adding the node, but to refresh the ui to show it to me, i need to close and reopen the visual shader editor. I need a programmatic way.

func add_float_constant():
	print("[Plugin] add_float_constant() called")

	var shader := get_target_visual_shader()
	if shader == null:
		print("[Plugin] No VisualShader found")
		return

	print("[Plugin] VisualShader found:", shader)

	var node := VisualShaderNodeFloatConstant.new()
	node.constant = 1.0

	var graph_type := VisualShader.TYPE_FRAGMENT
	var node_id := shader.get_valid_node_id(graph_type)

	shader.add_node(graph_type, node, Vector2(200, 200), node_id)
	
	shader.connect_nodes(VisualShader.TYPE_FRAGMENT, node_id, 0, 0, 0)
	
	shader.emit_changed()

You should open an issue in the issue tracker here:

In the meantime, as a workaround, you can do this:


func add_float_constant():
	# <your code here>

	# Edit the visual shader
	EditorInterface.edit_resource(shader)

	# Search for the internal VisualShaderEditor nodes
	var possible = EditorInterface.get_base_control().find_children("*", "VisualShaderEditor", true, false)
	# And, if it has the method _update_graph() call it
	for vs_editor in possible:
		if vs_editor.has_method("_update_graph"):
			vs_editor._update_graph()

This is an internal API and can change at any point

1 Like

That worked, thanks. Is it possible to get the current active shader that is edited/opened by the VisualShaderEditor? i searched the api docs and i didnt find the VisualShaderEditor class to get any info.