How to programmatically change editor cameras position with GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cs341

I have a editor tool script that changes the in-game camera position on the map…

I would also like to add functionality to change the Editor’s viewport position… where its looking in the map.

To quickly jump around to different pre-set locations.

Only other workaround i can think of is to place a bunch of empty Position3Ds around the map then somehow call the Godot hotkey to center focus on an object?

:bust_in_silhouette: Reply From: cs341

FHeres a hacky workaround I did using a Plugin… there may be a more elegant solution but for now this works.

## place this code in a background plugin in the editor
# Then place 4 Spatials around your map with names "NavPoint1" , "NavPoint2" etc..

func _unhandled_input(event):

	if event is InputEventKey:
		if event.pressed and event.scancode == KEY_3:
		if event.pressed and event.scancode == KEY_4:
		if event.pressed and event.scancode == KEY_5:
		if event.pressed and event.scancode == KEY_6:

func go_to_node(nodeName):
	var es : EditorSelection = editor_interface.get_selection()
	if es == null:
		print("cant initialize EditorSelection, returning")
	var node_to_nav_to = get_tree().get_edited_scene_root().find_node(nodeName)
	if node_to_nav_to == null:
		print("cant nav to node")
	print("simulating F Key")

func simulate_key(which_key ):
	print("simulating key %s" % which_key)
	var ev =
	ev.scancode = which_key
	ev.pressed = true

This is hacky as hell, but it works. Thank you!

In my experience, to restore the selection afterwards you’ll have to use call_deferred()

func go_to_node(node_name):
    # aforementioned code to select a node
    call_deferred(restore_selection, initial_node)

func restore_selection(initial_node):  

This way you can focus wherever you need, and then resume your work where you left off.

dreadpon | 2022-04-07 00:23