Work in progress

The content of this page was not yet updated for Godot 4.2 and may be outdated. If you know how to improve this page or you can confirm that it’s up to date, feel free to open a pull request.

3D gizmo plugins

Introduction

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

This tutorial shows 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, and 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 EditorNode3DGizmoPlugin

Regardless of the approach we choose, we will need to create a new EditorNode3DGizmoPlugin. 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. # my_custom_gizmo_plugin.gd
  2. extends EditorNode3DGizmoPlugin
  3. func get_name():
  4. return "CustomNode"
  1. # MyCustomEditorPlugin.gd
  2. @tool
  3. extends EditorPlugin
  4. const MyCustomGizmoPlugin = preload("res://addons/my-addon/my_custom_gizmo_plugin.gd")
  5. var gizmo_plugin = MyCustomGizmoPlugin.new()
  6. func _enter_tree():
  7. add_node_3d_gizmo_plugin(gizmo_plugin)
  8. func _exit_tree():
  9. remove_node_3d_gizmo_plugin(gizmo_plugin)

For simple gizmos, inheriting EditorNode3DGizmoPlugin 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 node parameter is of our target type.

  1. # ...
  2. func _has_gizmo(node):
  3. return node is MyCustomNode3D
  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_handle_material("handles")
  5. func _redraw(gizmo):
  6. gizmo.clear()
  7. var node3d = gizmo.get_node_3d()
  8. var lines = PackedVector3Array()
  9. lines.push_back(Vector3(0, 1, 0))
  10. lines.push_back(Vector3(0, node3d.my_custom_value, 0))
  11. var handles = PackedVector3Array()
  12. handles.push_back(Vector3(0, 1, 0))
  13. handles.push_back(Vector3(0, node3d.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 EditorNode3DGizmoPlugin
  2. const MyCustomNode3D = preload("res://addons/my-addon/my_custom_node_3d.gd")
  3. func _init():
  4. create_material("main", Color(1,0,0))
  5. create_handle_material("handles")
  6. func _has_gizmo(node):
  7. return node is MyCustomNode3D
  8. func _redraw(gizmo):
  9. gizmo.clear()
  10. var node3d = gizmo.get_node_3d()
  11. var lines = PackedVector3Array()
  12. lines.push_back(Vector3(0, 1, 0))
  13. lines.push_back(Vector3(0, node3d.my_custom_value, 0))
  14. var handles = PackedVector3Array()
  15. handles.push_back(Vector3(0, 1, 0))
  16. handles.push_back(Vector3(0, node3d.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 EditorNode3DGizmoPlugin to get properly working handles.

Alternative approach

In some cases we want to provide our own implementation of EditorNode3DGizmo, 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 Node3D nodes we want to target.

  1. # my_custom_gizmo_plugin.gd
  2. extends EditorNode3DGizmoPlugin
  3. const MyCustomNode3D = preload("res://addons/my-addon/my_custom_node_3d.gd")
  4. const MyCustomGizmo = preload("res://addons/my-addon/my_custom_gizmo.gd")
  5. func _init():
  6. create_material("main", Color(1, 0, 0))
  7. create_handle_material("handles")
  8. func _create_gizmo(node):
  9. if node is MyCustomNode3D:
  10. return MyCustomGizmo.new()
  11. else:
  12. return null

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

  1. # my_custom_gizmo.gd
  2. extends EditorNode3DGizmo
  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 node3d = get_node_3d()
  8. var lines = PackedVector3Array()
  9. lines.push_back(Vector3(0, 1, 0))
  10. lines.push_back(Vector3(gizmo_size, node3d.my_custom_value, 0))
  11. var handles = PackedVector3Array()
  12. handles.push_back(Vector3(0, 1, 0))
  13. handles.push_back(Vector3(gizmo_size, node3d.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 EditorNode3DGizmo to get properly working handles.

Previous Next


© Copyright 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision 53e837c6.

Built with Sphinx using a theme provided by Read the Docs.