Select node in Scene dock based on mesh click in 3D viewport

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

Does anyone know how to select a node in the Scene dock based on a mesh click in the 3D viewport? This is for an Editor Plugin.

The mesh is a virtual construct, essentially, a plane (with StaticBody) attached between two Spatial nodes. When clicked, it selects the nearest Spatial node.

I’ve mocked up something that sort of works using EditorPlugin.forward_spatial_gui_input to evaluate mouse clicks (see below). But, it only works if the mouse click is flagged as “handled” (return = true).

If the mouse click is flagged as “unhandled” (return = false), then the selection doesn’t work.

Unfortunately, if the mouse click is flagged as “handled”, then the Spatial node’s transform widget exhibits strange behavior. It is the default transform widget typically seen in the 3D viewport.

If one selects the Spatial node, manually, in the scene dock, then the transform widget works fine. But, if one clicks the mesh, which selects the Spatial node, then the Spatial’s transform widget acts funny and transforms don’t work.

Here is a rough representation of the EditorPlugin code:

func forward_spatial_gui_input(camera: Camera, event: InputEvent)->bool:
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and not event.pressed:
            var spatial = check_for_mesh_collision(camera, event.position)
            if spatial:
                _edi.get_selection().clear()
                _edi.get_selection().add_node(spatial)
                _edi.edit_node(spatial)
                return true
    return false