+4 votes

Is there a way to get the editor's camera from a tool node or editor plugin? I need it to perform a raycast to my custom node.

in Engine by (29,046 points)

I have the same problem, did you found something in the end Zylann?

At the moment I only found access to the camera by overriding forward_spatial_input_event(), which is where you get input and the said camera: https://github.com/Zylann/godot_terrain_plugin/blob/master/addons/zylann.terrain/terrain_plugin.gd#L122

It's not convenient to have it in process though, and it looks hacky to cache it from the input method :/

Thanks for info! Maybe I will be able to hack something after all :)

I found a VERY hacky workaround for 2D, which can be adopted to 3D:
First we need to apply this diff to expose the transform matrix from CanvasItemEditor. (You can do the same with the camera for SpatialEditor). https://gist.github.com/timoschwarzer/62b6c9869fdd7eab33ee805e4040fbf9
Then we can get the editor canvas transform in our EditorPlugin using get_editor_viewport().get_child(0).get_canvas_transform_matrix(). get_child(1) is the SpatialEditor. Again: this should be exposed in another way since this relies on the Editor's order in the EditorNode, which may change in the future.

I know it's an old post, but didn't found a solution elsewhere. Pretty sure it worked differently in 3.0 as compared to 3.1 (alpha), but..... Anyway, another a bit less hackier approach to get the editor's viewport is: get_tree().get_edited_scene_root().get_parent()

Nevermind. This hack sometimes needs another .get_parent() (and at other times that's the ViewportContainer).

1 Answer

0 votes

I found I can manually change editor camera by this way:

func geteditorcamera(camera_index):

  var e = get_editor_interface().get_editor_viewport()
  var cam = e.get_child(1).get_child(1).get_child(0).get_child(0).get_child(cam_index).get_child(0).get_child(0).get_camera()
  return cam

If the function not work,you can use code below to check(new version godot change the camera hierarchy)

#   var root = e.get_parent()
#   var all_nodes = []
#   var c = root.get_children()
#   while !c.empty():
#       var child_list = []                        
#       for i in c:
#           child_list += i.get_children()
#       all_nodes += child_list
#       c = child_list
#   for node in all_nodes:
#       if node.get_class() == "Camera":
#           print(node.transform)
#           print(node.get_path())
#           for i in range(deep):
#               node = node.get_parent()
#               print(node.get_class() + str(node.get_index()))
#   print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
by (22 points)

I know the camera (or anything else) can be obtained by forcing some code into the scene tree of the editor, but this is not what I'm after. A solution like this is a hack that can break any moment when new versions of Godot release. I wish there was an official API for it.

Agree. Recently I make editor plugin for my own game, I found the plugin API still lacks a lot of thing, for example it seems cannot draw something only on certain editor viewport. I hope this will get improve later.

Do we have API for access to the editor camera for now as it is 4.0 beta?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.