Convert mouse viewport position to canvas position in EditorPlugin

Godot Version

4.5

I’m attempting to write a plugin. The basic functionality I am trying to achieve is to create a node at the mouse position. I have managed to get everything working so far except that the coordinates for the mouse are the editor’s 2D viewport coordinates, but I need the node to be instantiated using canvas/global coordinates.

In my EditorPlugin I have:

func _forward_canvas_gui_input(event):
	if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
		prints("Mouse clicked at", event.position)#, EditorInterface.get_editor_viewport_2d().get_canvas().make_input_local(event.position))
		_create_map_node(event.position)
		return true 
	return false

func _create_map_node(position: Vector2) -> void:
	var node = MapNode.new()
	var scene_root = get_editor_interface().get_edited_scene_root()
	node.position = position
	node.name = "MapNode"
	scene_root.add_child(node)
	#Required to appear in scene/editor
	node.owner = scene_root	
	#Focus the node	
	EditorInterface.edit_node(node)

This creates the node, but at the wrong coordinates (where I clicked relative to the viewport, not the canvas). How do I convert the coordinates?

Maybe you need to look into get_editor_viewport_2d() ?