Skip to content

Getting editor camera in a tool script #15

Description

@me2beats

Problem:
There isn't a simple way to get current CanvasEditor/SpatialEditor viewports and SpatialEditor's camera in a tool script (not a plugin).

Solution:
This is a workaround:

func _ready():
	var editor_viewport_2d = find_viewport_2d(get_node("/root/EditorNode"), 0)
	var editor_viewport_3d = find_viewport_3d(get_node("/root/EditorNode"), 0)
	var editor_camera_3d = editor_viewport_3d.get_child(0)


func find_viewport_2d(node: Node, recursive_level):
	if node.get_class() == "CanvasItemEditor":
		return node.get_child(1).get_child(0).get_child(0).get_child(0).get_child(0)
	else:
		recursive_level += 1
		if recursive_level > 15:
			return null
		for child in node.get_children():
			var result = find_viewport_2d(child, recursive_level)
			if result != null:
				return result


func find_viewport_3d(node: Node, recursive_level):
	if node.get_class() == "SpatialEditor":
		return node.get_child(1).get_child(0).get_child(0).get_child(0).get_child(0).get_child(0)
	else:
		recursive_level += 1
		if recursive_level > 15:
			return null
		for child in node.get_children():
			var result = find_viewport_3d(child, recursive_level)
			if result != null:
				return result

Additional context:
the code copied from:
godotengine/godot-proposals#1302 (comment)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions