Godot Version
4.4
Question
How to get the active resource shader on the VisualShaderEditor? i tried with no luck :
var editor: EditorInterface = get_editor_interface()
## 1) Inspector edited object
var edited := editor.get_inspector().get_edited_object()
#_visualEditor = editor.get_inspector()
if edited is VisualShader:
print("Found edited object")
return edited
if edited is ShaderMaterial:
var shader = edited.shader
if shader is VisualShader:
print("found shader material shader")
return shader
I got the opened shaders and the selected shader by a hack. By finding all ItemList nodes in the editor and filtering to the ones in the VisualShader editor.
func get_active_visual_shader():
var opened_visual_shader_itemList = get_vse_item_list()
for i in range(opened_visual_shader_itemList.item_count):
var is_selected = opened_visual_shader_itemList.is_selected(i)
if is_selected:
var shader_name = opened_visual_shader_itemList.get_item_text(i)
var shader = load("res://" + shader_name) as VisualShader
return shader
print("[ERROR]: No shader")
return null
func get_vse_item_list() -> ItemList:
var vse = EditorInterface.get_base_control()#.find_children("*", "VisualShaderEditor", true, false)
# We search for ItemLists specifically inside this VSE instance
var item_lists = vse.find_children("*", "ItemList", true, false)
for list in item_lists:
# Validation: The shader list is usually the one in the left panel.
# In Godot 4, it is often a child of a VBoxContainer which is a child of an HSplitContainer.
var parent = list.get_parent()
var grand_parent = parent.get_parent() if parent else null
if grand_parent is HSplitContainer:
print("Bingo! Found the sidebar ItemList: ", list.get_path())
return list
return null