英文:
Reparenting nodes with Godot Editor Scripts
问题
我正在尝试将每个节点放置在场景中的自己的父节点中。我创建了一个从EditorScript继承的脚本,它创建了父节点,但在调用`reparent()`后,节点从编辑器树视图中消失了。然而,网格本身在编辑器视图中仍然可见。如何在EditorScript工具中重新父节点?
@tool
extends EditorScript
func _run():
var meshes = get_scene().get_children();
for node in meshes:
# 创建父节点
var parent: Node3D = Node3D.new()
parent.name = node.get_name()+"_root"
# 添加到场景中
get_scene().add_child(parent)
parent.owner = get_scene()
# 将网格重新父节点到新节点
node.reparent(parent);
node.owner = parent
# get_scene().print_tree_pretty() # <-- 显示节点,但它们在编辑器树中不可见
print(meshes.size())
#return
为了澄清,我在根级别有多个网格,我想将每个网格放在自己的节点中,并添加一个凸碰撞体,以将场景转换为Mesh Library。
英文:
I'm trying to place each node in a scene in it's own parent node. I made a script that inherits from EditorScript and it creates the parents, but the nodes are disappearing from the editor tree view after calling reparent()
. The meshes themselves are still visible in the editor view however. How do I reparent nodes in an EditorScript tool?
@tool
extends EditorScript
func _run():
var meshes = get_scene().get_children();
for node in meshes:
# make node
var parent: Node3D = Node3D.new()
parent.name = node.get_name()+"_root"
# add to scene
get_scene().add_child(parent)
parent.owner = get_scene()
#reparent mesh to new node
node.reparent(parent);
node.owner=parent
#get_scene().print_tree_pretty() # <-- shows the nodes, but they're not visible in the editor tree
print(meshes.size())
#return
For clarification, I've got multiple meshes on the root level, and I want to place each mesh in it's own node and add a convex collision to it, to convert the scene to a Mesh Library
答案1
得分: 2
owner
是 Godot 知道一个 Node
属于哪个“场景”的方式。
更精确地说,如果一个 Node
要在其自己的文件中保留(因此在 Godot 4 中具有关联的 PackedScene
的 scene_file_path
,或在 Godot 3 中具有 filename
),则它被视为一个“场景”。
如果 owner
与当前编辑的场景不匹配,那么它必须要么是一个临时节点(不会被保留,也不会在编辑器中显示),要么属于一个子场景(将单独保留)。 并且要明确,当前编辑的场景的子节点被视为“场景”,根据我上面说的方式...子场景的子节点将根据 PackedScene
中的“Children Editable Flag”在编辑器中显示。
您正在将 owner
设置为不被视为“场景”的 Node
:
node.owner = parent
改为将场景根作为 Node
的 owner
:
node.owner = get_scene()
英文:
The owner
is how Godot know to which "scene" a Node
belongs.
More precisely a Node
is considered a "scene" if it is to be persisted on its own file (and so it has the path of an associated PackedScene
in its scene_file_path
in Godot 4, or filename
in Godot 3).
If the owner
does not match with the currently edited scene, then it must either be a temporary node (that will not be persisted, and will not be shown in the editor), or belong to a sub-scene (which will be persisted separately). And to be clear, a Node
child of the currently edited scene is considered a "scene"... by the means I said above... The children of the sub-scene are shown in the editor according to the "Children Editable Flag" bundled in the PackedScene
.
You are setting the owner
to a Node
that is not considered a "scene":
node.owner=parent
Make the scene root the owner
of the Node
s instead:
node.owner = get_scene()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论