How can I locate the Viewport Node of the 2D Editor (on the engine)?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By henriquelalves
:warning: Old Version Published before Godot 3 was released.

I’m currently exploring some Addon features (currently only working on GitHub HEAD version of Godot), and what I have in mind is to use a node2D Addon to draw directly on the 2D Edtior Viewport, without actually adding a node to the screen. How can I get the reference to the engine 2D Editor Viewport?

:bust_in_silhouette: Reply From: kocholes

Maybe you can try this:

get_tree().get_root() # access via scenemainloop
get_node("/root") # access via absolute path

I have to use “get_node(”/root")" on the code too, thanks!

henriquelalves | 2016-03-08 20:16

:bust_in_silhouette: Reply From: neikeq

If you are working in C++, it’s simple:


However, CanvasItemEditor is not directly accessible from GDScript, but you can use this trick to find it (may stop working in the future):

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


var editor_node = get_node("/root/EditorNode")
var viewport = find_viewport(editor_node, 0) # Returns null if not found

I took this method by Marcos Bitetti (which searches for SpatialEditorViewport) as base.

That’s a crazy workaround :smiley:
Maybe an issue should be opened about exposing CanvasItemEditor to GDScript (at least for the EditorPlugin API)?

Akien | 2016-03-08 15:56

Yes, this workaround may stop working if the structure of CanvasItemEditor children changes. I agree some some of the built-in editor plugins should be exposed to GDScript.

neikeq | 2016-03-08 19:34

It worked! But I guess I formulated my question kinda wrong, haha.
Now I can draw in the canvas 2D, but what I wanted is to create the lines in the world 2D - the idea would be drawing lines ‘connecting’ two sprites, for example. Is this possible using this workaround?

Thanks anyway, I don’t think I would find a function like this without help!

henriquelalves | 2016-03-08 20:19

I guess im implementing this addon with a wrong architecture in mind, haha. After trying a couple of times, now I understand why Path and Path2d are nodes. I think if I implement the data as a node like those, I can create an interface to work on it.

henriquelalves | 2016-03-08 20:52

In Godot 3.2, this gives the error message:

Invalid call. Nonexistent function ‘get_type’ in base ‘EditorNode’

However, “get_class” works.

Aaron Franke | 2021-01-01 08:36