Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting editor camera in a tool script #15

Open
me2beats opened this issue Dec 21, 2021 · 0 comments
Open

Getting editor camera in a tool script #15

me2beats opened this issue Dec 21, 2021 · 0 comments

Comments

@me2beats
Copy link
Owner

me2beats commented Dec 21, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant