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?

https://forum.godotengine.org/71284/how-to-focus-on-a-node-object-in-the-editor

: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:
			go_to_node("NavPoint1")
		if event.pressed and event.scancode == KEY_4:
			go_to_node("NavPoint2")
		if event.pressed and event.scancode == KEY_5:
			go_to_node("NavPoint3")
		if event.pressed and event.scancode == KEY_6:
			go_to_node("NavPoint4")

func go_to_node(nodeName):
	var es : EditorSelection = editor_interface.get_selection()
	if es == null:
		print("cant initialize EditorSelection, returning")
		return
	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")
		return
		
	es.clear()
	es.add_node(node_to_nav_to)
	print("simulating F Key")
	simulate_key(KEY_F)

func simulate_key(which_key ):
	#https://docs.godotengine.org/en/stable/tutorials/inputs/inputevent.html
	print("simulating key %s" % which_key)
	var ev = InputEventKey.new()
	ev.scancode = which_key
	ev.pressed = true
	Input.parse_input_event(ev)

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):  
    es.clear()
    es.add_node(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