From a052ebaa99cd17f49133e399b49d266917e0ae8d Mon Sep 17 00:00:00 2001 From: bad Date: Sun, 19 Jun 2022 02:01:19 +0200 Subject: [PATCH] Move "Create Debug Tangents" into an editor plugin Instead of exporting the "create_debug_tangents" function via METHOD_FLAG_EDITOR it gets executed by an editor plugin. This moves it from the "Manage object properties" menu into a mesh menu. It also adds undo/redo functionality to the create debug tangents option. --- .../mesh_instance_3d_editor_plugin.cpp | 19 ++++++++++++ .../plugins/mesh_instance_3d_editor_plugin.h | 1 + scene/3d/mesh_instance_3d.cpp | 30 +++++++++++-------- scene/3d/mesh_instance_3d.h | 1 + 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/editor/plugins/mesh_instance_3d_editor_plugin.cpp b/editor/plugins/mesh_instance_3d_editor_plugin.cpp index c502d47669d..d5cdb70ccfe 100644 --- a/editor/plugins/mesh_instance_3d_editor_plugin.cpp +++ b/editor/plugins/mesh_instance_3d_editor_plugin.cpp @@ -270,6 +270,24 @@ void MeshInstance3DEditor::_menu_option(int p_option) { case MENU_OPTION_CREATE_OUTLINE_MESH: { outline_dialog->popup_centered(Vector2(200, 90)); } break; + case MENU_OPTION_CREATE_DEBUG_TANGENTS: { + Ref ur = EditorNode::get_singleton()->get_undo_redo(); + ur->create_action(TTR("Create Debug Tangents")); + + MeshInstance3D *tangents = node->create_debug_tangents_node(); + + if (tangents) { + Node *owner = get_tree()->get_edited_scene_root(); + + ur->add_do_reference(tangents); + ur->add_do_method(node, "add_child", tangents, true); + ur->add_do_method(tangents, "set_owner", owner); + + ur->add_undo_method(node, "remove_child", tangents); + } + + ur->commit_action(); + } break; case MENU_OPTION_CREATE_UV2: { Ref mesh2 = node->get_mesh(); if (!mesh2.is_valid()) { @@ -511,6 +529,7 @@ MeshInstance3DEditor::MeshInstance3DEditor() { options->get_popup()->add_separator(); options->get_popup()->add_item(TTR("Create Outline Mesh..."), MENU_OPTION_CREATE_OUTLINE_MESH); options->get_popup()->set_item_tooltip(options->get_popup()->get_item_count() - 1, TTR("Creates a static outline mesh. The outline mesh will have its normals flipped automatically.\nThis can be used instead of the StandardMaterial Grow property when using that property isn't possible.")); + options->get_popup()->add_item(TTR("Create Debug Tangents"), MENU_OPTION_CREATE_DEBUG_TANGENTS); options->get_popup()->add_separator(); options->get_popup()->add_item(TTR("View UV1"), MENU_OPTION_DEBUG_UV1); options->get_popup()->add_item(TTR("View UV2"), MENU_OPTION_DEBUG_UV2); diff --git a/editor/plugins/mesh_instance_3d_editor_plugin.h b/editor/plugins/mesh_instance_3d_editor_plugin.h index 7968176744e..88800227d1f 100644 --- a/editor/plugins/mesh_instance_3d_editor_plugin.h +++ b/editor/plugins/mesh_instance_3d_editor_plugin.h @@ -46,6 +46,7 @@ class MeshInstance3DEditor : public Control { MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES, MENU_OPTION_CREATE_NAVMESH, MENU_OPTION_CREATE_OUTLINE_MESH, + MENU_OPTION_CREATE_DEBUG_TANGENTS, MENU_OPTION_CREATE_UV2, MENU_OPTION_DEBUG_UV1, MENU_OPTION_DEBUG_UV2, diff --git a/scene/3d/mesh_instance_3d.cpp b/scene/3d/mesh_instance_3d.cpp index a76f4dd0d59..b0503c9c02b 100644 --- a/scene/3d/mesh_instance_3d.cpp +++ b/scene/3d/mesh_instance_3d.cpp @@ -390,13 +390,13 @@ void MeshInstance3D::_mesh_changed() { update_gizmos(); } -void MeshInstance3D::create_debug_tangents() { +MeshInstance3D *MeshInstance3D::create_debug_tangents_node() { Vector lines; Vector colors; Ref mesh = get_mesh(); if (!mesh.is_valid()) { - return; + return nullptr; } for (int i = 0; i < mesh->get_surface_count(); i++) { @@ -457,15 +457,23 @@ void MeshInstance3D::create_debug_tangents() { MeshInstance3D *mi = memnew(MeshInstance3D); mi->set_mesh(am); mi->set_name("DebugTangents"); - add_child(mi, true); -#ifdef TOOLS_ENABLED + return mi; + } - if (is_inside_tree() && this == get_tree()->get_edited_scene_root()) { - mi->set_owner(this); - } else { - mi->set_owner(get_owner()); - } -#endif + return nullptr; +} + +void MeshInstance3D::create_debug_tangents() { + MeshInstance3D *mi = create_debug_tangents_node(); + if (!mi) { + return; + } + + add_child(mi, true); + if (is_inside_tree() && this == get_tree()->get_edited_scene_root()) { + mi->set_owner(this); + } else { + mi->set_owner(get_owner()); } } @@ -495,8 +503,6 @@ void MeshInstance3D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_blend_shape_value", "blend_shape_idx", "value"), &MeshInstance3D::set_blend_shape_value); ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance3D::create_debug_tangents); - ClassDB::set_method_flags("MeshInstance3D", "create_debug_tangents", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh"); ADD_GROUP("Skeleton", ""); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin"); diff --git a/scene/3d/mesh_instance_3d.h b/scene/3d/mesh_instance_3d.h index 48d76b9a889..caf7d256168 100644 --- a/scene/3d/mesh_instance_3d.h +++ b/scene/3d/mesh_instance_3d.h @@ -90,6 +90,7 @@ public: Node *create_multiple_convex_collisions_node(); void create_multiple_convex_collisions(); + MeshInstance3D *create_debug_tangents_node(); void create_debug_tangents(); virtual AABB get_aabb() const override;