Created
March 4, 2023 21:30
-
-
Save Qubus0/f3e1d77bbfd593288a6fc12b1edef206 to your computer and use it in GitHub Desktop.
Godot Plugins: Get the full property path from the Inspector panel including nested resource inspectors (4.x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@tool | |
extends EditorPlugin | |
var last_path := [] | |
func _process(delta: float) -> void: | |
# Optimally just do this on button press or connected to a signal | |
var new_path := get_inspector_full_selected_path() | |
# Don't flood the console | |
if not new_path == last_path: | |
last_path = new_path | |
print("/".join(new_path)) | |
func get_inspector_full_selected_path() -> Array[String]: | |
var full_path: Array[String] = [""] | |
var insp := get_editor_interface().get_inspector() | |
full_path[0] = insp.get_selected_path() | |
full_path = get_inspector_nested_selected_path(insp, 0, full_path) | |
return full_path | |
func get_inspector_nested_selected_path(node: Node, index: int, full_path: Array[String]) -> Array[String]: | |
for child in node.get_children(): | |
# Every property. Exclude categories and so on | |
# EditorPropertyResource is not exposed to scripting, can't check for that | |
if child is EditorProperty: | |
# But it is the only prop with an EditorResourcePicker, check for that | |
if child.get_child(0) is EditorResourcePicker: | |
# Check if the parent property is the same as the one at the | |
# previous step in the path. | |
var parent_editor_property: String = child.label.to_lower().replace(" ", "_") | |
if not parent_editor_property == full_path[index]: | |
continue | |
# If the picker is closed, the inspector is removed as child | |
if child.get_child_count() > 1: | |
# The EditorInspector is inside a container as well | |
var nested_inspector: EditorInspector = child.get_child(1).get_child(0) | |
var path := nested_inspector.get_selected_path() | |
if not path == "": | |
# Shift the position in the array and set it to the current path | |
index += 1 | |
full_path.resize(index +1) | |
full_path[index] = path | |
# Recurse for every child | |
get_inspector_nested_selected_path(child, index, full_path) | |
return full_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment