Editor plugin scene change

Godot Version

4.2.1

Question

on startup, I want to get some objects in the scene, for my editor plugin

@tool
extends EditorPlugin
func _enter_tree():
      var Scene = EditorInterface.get_edited_scene_root()
      print( "scene = " + str(Scene) )

this usually works fine, but if the plugin is already open when Godot is launched, the plugin seems to not yet register the loaded scene. it print “Scene = null” !!
also, if we switch scenes, the plugin needs to register the changed current scene. How do I do this?
how do I get the current scene in editor interface, and update it on scene change or once scene is loaded to make sure we are working with the correct nodes in the correct scene?

I mean, I could do this:

func _process(delta):
      var Scene = EditorInterface.get_edited_scene_root()
      print( "scene =  " + str(Scene) )

but that seems repetitive and unnecessary. surely I could only update the the variable with out scene root on scene change to get the nodes within? only once instead of repetitively??

That’s because you are doing that on _enter_tree() which happens before a scene has been loaded

You’ll need to connect to the signal EditorPlugin.scene_changed and do your logic there.

ive been looking into this but not able to get it working!

func _enter_tree():
	EditorPlugin.scene_changed.connect(scene_changed)
func scene_changed ( ):
	var Scene = EditorInterface.get_edited_scene_root()
	print( "scene changed -> " + str(Scene) )

give me error " Invalid get index ‘scene_changed’ "

func _enter_tree():
	scene_changed.connect(scene_changed)
func scene_changed ( ):
	var Scene = EditorInterface.get_edited_scene_root()
	print( "scene changed -> " + str(Scene) )

"cannot find property ‘connect’ on ‘callable’

what am I missing?

it should be

func _enter_tree():
  scene_changed.connect(_on_scene_change)

func _on_scene_change(scene_root):
  print("scene changed to -> ", scene_root)

your plugin script is EditorPlugin

have you tried this? It kinda seems like you didn’t read what I posted and just rushed to post whatever. Yes I have read those docs and tried to do this but it wont work… am I missing something here?

@tool
extends EditorPlugin

func _enter_tree():
  scene_changed.connect(_on_scene_change)

func _on_scene_change(scene_root):
  print("scene changed to -> ", scene_root)

causes error message as I posted above:
Cannot find property “connect” on base “Callable”.

I clearly renamed the function from scene_changed to _on_scene_change

ok. Please try it on your end to confirm because, as Ive said repeatedly, it does not work for me. are you on Godot version 4.2.1? have you tried this code?

for me, naming scene_changed.connect(_on_scene_change) or scene_changed.connect(scene_changed) or scene_changed.connect(pumpkin_spice_latte) or anything after scene_changed.connect just simply does not work, because the scene_changed method does NOT use connect, hence the error:
Cannot find property “connect” on base “Callable”.

thank you for your effort and I appreciate if you could try it and get back with something that works or an explanation that can shed light on the issue concerning this error.

If you have a scene_changed() method in your script, rename it to anything else.

Full script:

@tool
extends EditorPlugin


func _enter_tree() -> void:
	scene_changed.connect(_on_scene_change)


func _exit_tree() -> void:
	scene_changed.disconnect(_on_scene_change)


func _on_scene_change(scene:Node) -> void:
	print('Changed scene to %s' % (scene.name if scene else "empty"))

Result in the Output dock:

Changed scene to TestArea2DCollision
Changed scene to AudioManager
Changed scene to empty
Changed scene to Test2DGlow
Changed scene to Test2DHDR
Changed scene to TestAnimationPlayerChangeSpeed
Changed scene to empty
1 Like

no idea why but its working now all of a sudden! thanks!