I am creating an editor plugin which makes use of EditorSpatialGizmoPlugin and the undo redo system. If an undo action has been created by the gizmo plugin, remove_spatial_gizmo_plugin
seems to fail, and the gizmo handles remain active in the editor.
I have created a minimal reproduction as below:
I have an EditorPlugin as:
tool
extends EditorPlugin
var gizmo_plugin := preload("gizmo_plugin.gd").new(self) as EditorSpatialGizmoPlugin
func _enter_tree():
add_spatial_gizmo_plugin(gizmo_plugin)
func _exit_tree():
# get_undo_redo().clear_history() # Uncomment this and the plugin disables properly
remove_spatial_gizmo_plugin(gizmo_plugin)
And inside gizmo_plugin.gd I have:
extends EditorSpatialGizmoPlugin
var editor_plugin: EditorPlugin
func _init(plugin: EditorPlugin):
create_handle_material("handles")
editor_plugin = plugin
func has_gizmo(spatial):
return true
func get_handle_name(gizmo: EditorSpatialGizmo, index: int):
return "name"
func get_handle_value(gizmo: EditorSpatialGizmo, index: int):
return 0
func redraw(gizmo: EditorSpatialGizmo):
gizmo.clear()
var handles = PoolVector3Array()
handles.append(Vector3.ZERO)
gizmo.add_handles(handles, get_material("handles", gizmo))
func undo_method(value) -> void:
pass
func commit_handle(gizmo: EditorSpatialGizmo, index: int, restore, cancel=false) -> void:
# The cause, as far as I can tell:
var undo_redo: UndoRedo = editor_plugin.get_undo_redo()
undo_redo.create_action("Action")
undo_redo.add_undo_method(self, "undo_method")
undo_redo.commit_action()
To recreate the issue, try to drag the handle added by the gizmo as this will call commit_handle, and then disable the plugin, and the gizmo will not disappear.