Plugin - Getting if the 3D Viewport is the active tab

Godot Version

4.2

Question

Hey guys, I am developing a plugin and there is something I’m absolutely stumped on. I am developing a simple “point and click” addon for multimeshes that allows you to distribute multimeshes across a 3D terrain. The general workflow is:

  • create a Dock plugin
  • pass the get_editor_interface() to the script attached to the TCSN via an init() function
  • the dock Control contains a Node3D , which has access to the get_world_3d() function
  • ultimately this allows me to raycast from the editor camera to the 3D world and intersect any collision objects and get the location

My problem is that I have no way of knowing for certain that the 3D viewport is open, so the plugin ends up assigning multimeshes even when you have the script editor open, for example… any ideas?

I have seen this post for getting the editor’s viewport and camera:

But it seems to be much easier now in 4.2, you can simply do (assuming that get_editor_interface() is passed to the dock):

var viewport = _editor_interface.get_editor_viewport_3d(0)
var mouse_pos = viewport.get_mouse_position()
var camera = viewport.get_camera_3d()

There’s a lot of stuff you need to know.

First, read : Godot Spatial Queries in the Editor · Wiki · Donn Ingle —Dbat / lessons-in-dev · GitLab

Next, have a look at code like:

Also have a look at the number one scatter code base:

hth

Thanks for the references. I was really hoping not to have to go to the physics server level, as I really just want a very simple point-and-click multimesh instancer.

Would this actually solve my issue of only wanting the tool to be active when the 3D Viewport is the active tab?

I got the answer on discord. The main_screen_changed signal of EditorPlugin is what ultimately allows me to detect that the 3D Viewport is open.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.