Spatial gizmo plugins

Introduction

Spatial gizmo plugins are used by the editor and custom plugins to define the gizmos attached to any kind of Spatial node.

This tutorial will show you the two main approaches to defining your own custom gizmos. The first option works well for simple gizmos and creates less clutter in your plugin structure, while the second one will let you store some per-gizmo data.

Note

This tutorial assumes you already know how to make generic plugins. If in doubt, refer to the Making plugins page.

The EditorSpatialGizmoPlugin

Regardless of the approach we choose, we will need to create a new EditorSpatialGizmoPlugin. This will allow us to set a name for the new gizmo type and define other behaviors such as whether the gizmo can be hidden or not.

This would be a basic setup:

  1. # MyCustomGizmoPlugin.gd
  2. extends EditorSpatialGizmoPlugin
  3. func get_name():
  4. return "CustomNode"
  1. # MyCustomEditorPlugin.gd
  2. tool
  3. extends EditorPlugin
  4. const MyCustomGizmoPlugin = preload("res://addons/my-addon/MyCustomGizmoPlugin.gd")
  5. var gizmo_plugin = MyCustomGizmoPlugin.new()
  6. func _enter_tree():
  7. add_spatial_gizmo_plugin(gizmo_plugin)
  8. func _exit_tree():
  9. remove_spatial_gizmo_plugin(gizmo_plugin)

For simple gizmos, just inheriting EditorSpatialGizmoPlugin is enough. If you want to store some per-gizmo data or you are porting a Godot 3.0 gizmo to 3.1+, you should go with the second approach.

Simple approach

The first step is to, in our custom gizmo plugin, override the has_gizmo() method so that it returns true when the spatial parameter is of our target type.

  1. # ...
  2. func has_gizmo(spatial):
  3. return spatial is MyCustomSpatial
  4. # ...

Then we can override methods like redraw() or all the handle related ones.

  1. # ...
  2. func _init():
  3. create_material("main", Color(1, 0, 0))
  4. create_handles_material("handles")
  5. func redraw(gizmo):
  6. gizmo.clear()
  7. var spatial = gizmo.get_spatial_node()
  8. var lines = PoolVector3Array()
  9. lines.push_back(Vector3(0, 1, 0))
  10. lines.push_back(Vector3(0, spatial.my_custom_value, 0))
  11. var handles = PoolVector3Array()
  12. handles.push_back(Vector3(0, 1, 0))
  13. handles.push_back(Vector3(0, spatial.my_custom_value, 0))
  14. gizmo.add_lines(lines, get_material("main", gizmo), false)
  15. gizmo.add_handles(handles, get_material("handles", gizmo))
  16. # ...

Note that we created a material in the _init method, and retrieved it in the redraw method using get_material(). This method retrieves one of the material’s variants depending on the state of the gizmo (selected and/or editable).

So the final plugin would look somewhat like this:

  1. extends EditorSpatialGizmoPlugin
  2. const MyCustomSpatial = preload("res://addons/my-addon/MyCustomSpatial.gd")
  3. func _init():
  4. create_material("main", Color(1,0,0))
  5. create_handles_material("handles")
  6. func has_gizmo(spatial):
  7. return spatial is MyCustomSpatial
  8. func redraw(gizmo):
  9. gizmo.clear()
  10. var spatial = gizmo.get_spatial_node()
  11. var lines = PoolVector3Array()
  12. lines.push_back(Vector3(0, 1, 0))
  13. lines.push_back(Vector3(0, spatial.my_custom_value, 0))
  14. var handles = PoolVector3Array()
  15. handles.push_back(Vector3(0, 1, 0))
  16. handles.push_back(Vector3(0, spatial.my_custom_value, 0))
  17. gizmo.add_lines(lines, get_material("main", gizmo), false)
  18. gizmo.add_handles(handles, get_material("handles", gizmo))
  19. # you should implement the rest of handle-related callbacks
  20. # (get_handle_name(), get_handle_value(), commit_handle()...)

Note that we just added some handles in the redraw method, but we still need to implement the rest of handle-related callbacks in EditorSpatialGizmoPlugin to get properly working handles.

Alternative approach

In some cases we want to provide our own implementation of EditorSpatialGizmo, maybe because we want to have some state stored in each gizmo or because we are porting an old gizmo plugin and we don’t want to go through the rewriting process.

In these cases all we need to do is, in our new gizmo plugin, override create_gizmo(), so it returns our custom gizmo implementation for the Spatial nodes we want to target.

  1. # MyCustomGizmoPlugin.gd
  2. extends EditorSpatialGizmoPlugin
  3. const MyCustomSpatial = preload("res://addons/my-addon/MyCustomSpatial.gd")
  4. const MyCustomGizmo = preload("res://addons/my-addon/MyCustomGizmo.gd")
  5. func _init():
  6. create_material("main", Color(1, 0, 0))
  7. create_handles_material("handles")
  8. func create_gizmo(spatial):
  9. if spatial is MyCustomSpatial:
  10. return MyCustomGizmo.new()
  11. else:
  12. return null

This way all the gizmo logic and drawing methods can be implemented in a new clas extending EditorSpatialGizmo, like so:

  1. # MyCustomGizmo.gd
  2. extends EditorSpatialGizmo
  3. # You can store data in the gizmo itself (more useful when working with handles)
  4. var gizmo_size = 3.0
  5. func redraw():
  6. clear()
  7. var spatial = get_spatial_node()
  8. var lines = PoolVector3Array()
  9. lines.push_back(Vector3(0, 1, 0))
  10. lines.push_back(Vector3(gizmo_size, spatial.my_custom_value, 0))
  11. var handles = PoolVector3Array()
  12. handles.push_back(Vector3(0, 1, 0))
  13. handles.push_back(Vector3(gizmo_size, spatial.my_custom_value, 0))
  14. var material = get_plugin().get_material("main", self)
  15. add_lines(lines, material, false)
  16. var handles_material = get_plugin().get_material("handles", self)
  17. add_handles(handles, handles_material)
  18. # you should implement the rest of handle-related callbacks
  19. # (get_handle_name(), get_handle_value(), commit_handle()...)

Note that we just added some handles in the redraw method, but we still need to implement the rest of handle-related callbacks in EditorSpatialGizmo to get properly working handles.