使用Godot编辑器脚本重新分配节点

huangapple go评论65阅读模式
英文:

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()+&quot;_root&quot;
		# 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() # &lt;-- shows the nodes, but they&#39;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 中具有关联的 PackedScenescene_file_path,或在 Godot 3 中具有 filename),则它被视为一个“场景”。

如果 owner 与当前编辑的场景不匹配,那么它必须要么是一个临时节点(不会被保留,也不会在编辑器中显示),要么属于一个子场景(将单独保留)。 并且要明确,当前编辑的场景的子节点被视为“场景”,根据我上面说的方式...子场景的子节点将根据 PackedScene 中的“Children Editable Flag”在编辑器中显示。

您正在将 owner 设置为不被视为“场景”的 Node

node.owner = parent

改为将场景根作为 Nodeowner

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 Nodes instead:

node.owner = get_scene()

huangapple
  • 本文由 发表于 2023年5月28日 01:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定