自研插件:自动编组

This commit is contained in:
2026-10-04 19:46:30 +08:00
parent 8ebd331b20
commit b826bfb562
2 changed files with 30 additions and 2 deletions
+22 -2
View File
@@ -65,6 +65,16 @@ const MENU_ADD := "Mesh Collider: 给选中模型加碰撞 / Add collision to se
const MENU_REMOVE := "Mesh Collider: 移除选中模型的碰撞 / Remove collision"
const MENU_SETTINGS := "Mesh Collider: 打开项目设置 / Open project settings"
## The editor's own "group selected nodes" flag - the toggle next to the lock
## icon in the Scene dock, bound to Ctrl+G.
##
## A node carrying it is what gets selected when any of its children is clicked
## in the 2D/3D view, which is exactly what a model with a CollisionBody and a
## pile of CollisionShape3D under it needs. It is a plain node meta, and Godot
## serialises it into the scene as `metadata/_edit_group_ = true`, so setting it
## here is all it takes - no editor-only state, nothing to re-apply on load.
const META_GROUP := &"_edit_group_"
func _enter_tree() -> void:
Settings.install()
@@ -226,11 +236,21 @@ func _inject(model: Node, source: String = "") -> bool:
undo.add_do_method(Collider, "prepare_for_scene", body, scene_root)
undo.add_do_reference(body)
undo.add_undo_method(model, "remove_child", body)
# Group the model, so clicking it in the viewport selects the model rather
# than the body or a shape we just hung under it. Only when it is not already
# grouped, so a choice made by hand is never overwritten - and the undo entry
# is only added in that same case, so undoing an injection cannot un-group
# something the user had grouped themselves.
var grouping := not model.has_meta(META_GROUP)
if grouping:
undo.add_do_method(model, "set_meta", META_GROUP, true)
undo.add_undo_method(model, "remove_meta", META_GROUP)
undo.commit_action()
get_editor_interface().mark_scene_as_unsaved()
print("[MeshCollider] %s (%s): added %s with %d shape(s)"
% [model.name, source.get_file(), body.get_class(), Collider.count_shapes(body)])
print("[MeshCollider] %s (%s): added %s with %d shape(s)%s"
% [model.name, source.get_file(), body.get_class(), Collider.count_shapes(body),
" [已编组]" if grouping else ""])
return true