If you are working in C++, it's simple:
CanvasItemEditor::get_singleton()->get_viewport_control()
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)
else:
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
Usage:
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.