Skip to content

Commit 60638c9

Browse files
committed
Merge pull request godotengine#115422 from Rindbee/fix-EditorPropertyNodePath-base-node-is-not-local-scene
Fix `NodePath` `EditorProperty` using the wrong scene root
2 parents 34204bd + c3a61d5 commit 60638c9

2 files changed

Lines changed: 69 additions & 8 deletions

File tree

editor/inspector/editor_properties.cpp

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,15 +3242,29 @@ Node *EditorPropertyNodePath::get_base_node() {
32423242
}
32433243
}
32443244
}
3245-
if (use_path_from_scene_root) {
3246-
if (get_edited_object()->has_method("get_root_path")) {
3247-
base_node = Object::cast_to<Node>(get_edited_object()->call("get_root_path"));
3248-
} else {
3249-
base_node = get_tree()->get_edited_scene_root();
3250-
}
3245+
3246+
if (!use_path_from_scene_root) {
3247+
return base_node;
32513248
}
32523249

3253-
return base_node;
3250+
if (get_edited_object()->has_method("get_root_path")) {
3251+
return Object::cast_to<Node>(get_edited_object()->call("get_root_path"));
3252+
}
3253+
3254+
if (!base_node) {
3255+
return nullptr; // Editing external resources.
3256+
}
3257+
3258+
if (base_node->is_instance()) {
3259+
return base_node; // Known scene root.
3260+
}
3261+
3262+
base_node = base_node->get_owner();
3263+
if (base_node) {
3264+
return base_node; // Node in known scene.
3265+
}
3266+
3267+
return get_tree()->get_edited_scene_root(); // Treat as a node in the main scene.
32543268
}
32553269

32563270
EditorPropertyNodePath::EditorPropertyNodePath() {
@@ -3354,6 +3368,17 @@ void EditorPropertyResource::_resource_changed(const Ref<Resource> &p_resource)
33543368
}
33553369
}
33563370

3371+
if (p_resource.is_valid() && p_resource->is_local_to_scene()) {
3372+
// Attempting to configure the local scene.
3373+
Node *local_scene = _get_base_node();
3374+
if (local_scene) {
3375+
HashMap<Ref<Resource>, Ref<Resource>> remap;
3376+
p_resource->configure_for_local_scene(local_scene, remap);
3377+
} else {
3378+
WARN_PRINT("You are attempting to assign a local-to-scene resource outside the scene.");
3379+
}
3380+
}
3381+
33573382
// The bool is_script applies only to an object's main script.
33583383
// Changing the value of Script-type exported variables of the main script should not trigger saving/reloading properties.
33593384
bool is_script = false;
@@ -3471,6 +3496,39 @@ bool EditorPropertyResource::_should_stop_editing() const {
34713496
return !resource_picker->is_toggle_pressed();
34723497
}
34733498

3499+
Node *EditorPropertyResource::_get_base_node() {
3500+
Node *base_node = Object::cast_to<Node>(get_edited_object());
3501+
3502+
if (!base_node) {
3503+
base_node = Object::cast_to<Node>(InspectorDock::get_inspector_singleton()->get_edited_object());
3504+
}
3505+
3506+
if (!base_node) {
3507+
// Try a base node within history.
3508+
if (EditorNode::get_singleton()->get_editor_selection_history()->get_path_size() > 0) {
3509+
Object *base = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_selection_history()->get_path_object(0));
3510+
if (base) {
3511+
base_node = Object::cast_to<Node>(base);
3512+
}
3513+
}
3514+
}
3515+
3516+
if (!base_node) {
3517+
return nullptr; // Editing external resources.
3518+
}
3519+
3520+
if (!base_node->get_scene_file_path().is_empty()) {
3521+
return base_node; // Known scene root.
3522+
}
3523+
3524+
base_node = base_node->get_owner();
3525+
if (base_node) {
3526+
return base_node; // Node in known scene.
3527+
}
3528+
3529+
return get_tree()->get_edited_scene_root(); // Treat as a node in the main scene.
3530+
}
3531+
34743532
void EditorPropertyResource::_viewport_selected(const NodePath &p_path) {
34753533
Node *to_node = get_node(p_path);
34763534
if (!Object::cast_to<Viewport>(to_node)) {
@@ -3481,7 +3539,9 @@ void EditorPropertyResource::_viewport_selected(const NodePath &p_path) {
34813539
Ref<ViewportTexture> vt = get_edited_property_value();
34823540
ERR_FAIL_COND(vt.is_null());
34833541

3484-
vt->set_viewport_path_in_scene(get_tree()->get_edited_scene_root()->get_path_to(to_node));
3542+
Node *local_scene = _get_base_node();
3543+
ERR_FAIL_NULL(local_scene);
3544+
vt->set_viewport_path_in_scene(local_scene->get_path_to(to_node));
34853545

34863546
emit_changed(get_edited_property(), vt);
34873547
update_property();

editor/inspector/editor_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ class EditorPropertyResource : public EditorProperty {
746746
void _resource_selected(const Ref<Resource> &p_resource, bool p_inspect);
747747
void _resource_changed(const Ref<Resource> &p_resource);
748748

749+
Node *_get_base_node();
749750
void _viewport_selected(const NodePath &p_path);
750751

751752
void _sub_inspector_property_keyed(const String &p_property, const Variant &p_value, bool p_advance);

0 commit comments

Comments
 (0)