Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created April 25, 2025 00:50
Show Gist options
  • Save jakehawken/35e893ef2f24425d23208917df8194de to your computer and use it in GitHub Desktop.
Save jakehawken/35e893ef2f24425d23208917df8194de to your computer and use it in GitHub Desktop.
A script that generates a string representation of your the main_scene.tscn in a Godot project and adds it to your clipboard.
def copy_scene_tree
scene_file = "main_scene.tscn"
unless File.exist?(scene_file)
puts "Error: #{scene_file} not found in current directory"
return
end
tree_content = ""
# First pass: build node hierarchy
nodes = []
File.readlines(scene_file).each do |line|
line = line.strip
if line.start_with?("[node")
if line.include?('name="')
node_name = line.match(/name="([^"]+)"/)[1]
parent_path = line.match(/parent="([^"]+)"/)
# Handle both explicit type and instance nodes
node_type = if line.include?('type="')
line.match(/type="([^"]+)"/)[1]
elsif line.include?('instance=')
# Get the base name of the referenced scene
if node_name.include?("BattleMap")
"BattleMap"
elsif node_name.include?("PlayerUnit")
"PlayerUnit"
elsif node_name.include?("EnemyUnit")
"EnemyUnit"
else
"Instance"
end
else
"Node"
end
node = {
name: node_name,
type: node_type,
parent: parent_path ? parent_path[1] : nil,
depth: 0
}
nodes << node
end
end
end
# Second pass: calculate proper depths
nodes.each do |node|
if node[:parent]
parent = node[:parent]
# Base depth on parent path
node[:depth] = if parent == "."
1 # Direct children of root
elsif parent == "Units"
2 # Children of Units node
elsif parent.start_with?("Units/")
3 # Grandchildren of Units node
else
parent.count("/") + 1
end
end
end
# Generate tree content
nodes.each_with_index do |node, index|
if index == 0
# Root node without nesting indicator
tree_content += "#{node[:name]} (#{node[:type]})\n"
else
tree_content += " " * node[:depth]
tree_content += "└── #{node[:name]} (#{node[:type]})\n"
end
end
# Copy to clipboard
IO.popen("pbcopy", "w") { |f| f.write(tree_content) }
puts "Scene tree structure has been copied to clipboard!"
puts "\nPreview of the tree structure:"
puts tree_content
end
copy_string_tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment