Skip to content

Commit 8f95cec

Browse files
committed
Scene import: extract UID paths and store fallback
When extracting meshes, materials and animations, store the uid:// paths as well as a res:// fallback. When validating import settings, load the fallback path if the uid:// path fails to load. Update save_to_file/fallback_path every import to keep the file path in sync with the uid.
1 parent 36d90c7 commit 8f95cec

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

editor/import/3d/resource_importer_scene.cpp

+64-8
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ Node *ResourceImporterScene::_post_fix_animations(Node *p_node, Node *p_root, co
13141314
int end_frame = anim_settings["slice_" + itos(i + 1) + "/end_frame"];
13151315
Animation::LoopMode loop_mode = static_cast<Animation::LoopMode>((int)anim_settings["slice_" + itos(i + 1) + "/loop_mode"]);
13161316
bool save_to_file = anim_settings["slice_" + itos(i + 1) + "/save_to_file/enabled"];
1317-
String save_to_path = anim_settings["slice_" + itos(i + 1) + "/save_to_file/path"];
1317+
String save_to_path = ResourceUID::ensure_path(anim_settings["slice_" + itos(i + 1) + "/save_to_file/path"]);
13181318
bool save_to_file_keep_custom = anim_settings["slice_" + itos(i + 1) + "/save_to_file/keep_custom_tracks"];
13191319

13201320
animation_slices.push_back(slice_name);
@@ -1343,7 +1343,7 @@ Node *ResourceImporterScene::_post_fix_animations(Node *p_node, Node *p_root, co
13431343

13441344
anim->set_loop_mode(static_cast<Animation::LoopMode>((int)anim_settings["settings/loop_mode"]));
13451345
bool save = anim_settings["save_to_file/enabled"];
1346-
String path = anim_settings["save_to_file/path"];
1346+
String path = ResourceUID::ensure_path(anim_settings["save_to_file/path"]);
13471347
bool keep_custom = anim_settings["save_to_file/keep_custom_tracks"];
13481348

13491349
Ref<Animation> saved_anim = _save_animation_to_file(anim, save, path, keep_custom);
@@ -1534,6 +1534,19 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<
15341534
if (matdata.has("use_external/enabled") && bool(matdata["use_external/enabled"]) && matdata.has("use_external/path")) {
15351535
String path = matdata["use_external/path"];
15361536
Ref<Material> external_mat = ResourceLoader::load(path);
1537+
if (!external_mat.is_valid()) {
1538+
if (matdata.has("use_external/fallback_path")) {
1539+
String fallback_save_path = matdata["use_external/fallback_path"];
1540+
if (!fallback_save_path.is_empty()) {
1541+
external_mat = ResourceLoader::load(fallback_save_path);
1542+
if (external_mat.is_valid()) {
1543+
matdata["use_external/path"] = ResourceUID::path_to_uid(fallback_save_path);
1544+
}
1545+
}
1546+
}
1547+
} else {
1548+
matdata["save_to_file/fallback_path"] = ResourceUID::ensure_path(path);
1549+
}
15371550
if (external_mat.is_valid()) {
15381551
m->set_surface_material(i, external_mat);
15391552
}
@@ -2044,6 +2057,7 @@ void ResourceImporterScene::get_internal_import_options(InternalImportCategory p
20442057
case INTERNAL_IMPORT_CATEGORY_MESH: {
20452058
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
20462059
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "save_to_file/path", PROPERTY_HINT_SAVE_FILE, "*.res,*.tres"), ""));
2060+
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "save_to_file/fallback_path", PROPERTY_HINT_SAVE_FILE, "", PROPERTY_USAGE_NO_EDITOR), ""));
20472061
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/shadow_meshes", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0));
20482062
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/lightmap_uv", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0));
20492063
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/lods", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0));
@@ -2052,11 +2066,13 @@ void ResourceImporterScene::get_internal_import_options(InternalImportCategory p
20522066
case INTERNAL_IMPORT_CATEGORY_MATERIAL: {
20532067
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "use_external/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
20542068
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "use_external/path", PROPERTY_HINT_FILE, "*.material,*.res,*.tres"), ""));
2069+
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "use_external/fallback_path", PROPERTY_HINT_FILE, "", PROPERTY_USAGE_NO_EDITOR), ""));
20552070
} break;
20562071
case INTERNAL_IMPORT_CATEGORY_ANIMATION: {
20572072
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "settings/loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Pingpong"), 0));
20582073
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
20592074
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "save_to_file/path", PROPERTY_HINT_SAVE_FILE, "*.res,*.tres"), ""));
2075+
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "save_to_file/fallback_path", PROPERTY_HINT_SAVE_FILE, "", PROPERTY_USAGE_NO_EDITOR), ""));
20602076
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/keep_custom_tracks"), ""));
20612077
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/amount", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0));
20622078

@@ -2067,6 +2083,7 @@ void ResourceImporterScene::get_internal_import_options(InternalImportCategory p
20672083
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slice_" + itos(i + 1) + "/loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Pingpong"), 0));
20682084
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "slice_" + itos(i + 1) + "/save_to_file/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
20692085
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "slice_" + itos(i + 1) + "/save_to_file/path", PROPERTY_HINT_SAVE_FILE, ".res,*.tres"), ""));
2086+
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "slice_" + itos(i + 1) + "/save_to_file/fallback_path", PROPERTY_HINT_SAVE_FILE, "", PROPERTY_USAGE_NO_EDITOR), ""));
20702087
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "slice_" + itos(i + 1) + "/save_to_file/keep_custom_tracks"), false));
20712088
}
20722089
} break;
@@ -2196,17 +2213,17 @@ bool ResourceImporterScene::get_internal_option_visibility(InternalImportCategor
21962213
}
21972214
} break;
21982215
case INTERNAL_IMPORT_CATEGORY_MESH: {
2199-
if (p_option == "save_to_file/path") {
2216+
if (p_option == "save_to_file/path" || p_option == "save_to_file/fallback_path") {
22002217
return p_options["save_to_file/enabled"];
22012218
}
22022219
} break;
22032220
case INTERNAL_IMPORT_CATEGORY_MATERIAL: {
2204-
if (p_option == "use_external/path") {
2221+
if (p_option == "use_external/path" || p_option == "use_external/fallback_path") {
22052222
return p_options["use_external/enabled"];
22062223
}
22072224
} break;
22082225
case INTERNAL_IMPORT_CATEGORY_ANIMATION: {
2209-
if (p_option == "save_to_file/path" || p_option == "save_to_file/keep_custom_tracks") {
2226+
if (p_option == "save_to_file/path" || p_option == "save_to_file/fallback_path" || p_option == "save_to_file/keep_custom_tracks") {
22102227
return p_options["save_to_file/enabled"];
22112228
}
22122229
if (p_option.begins_with("slice_")) {
@@ -2873,13 +2890,52 @@ Node *ResourceImporterScene::pre_import(const String &p_source_file, const HashM
28732890

28742891
Error ResourceImporterScene::_check_resource_save_paths(const Dictionary &p_data) {
28752892
Array keys = p_data.keys();
2876-
for (int i = 0; i < keys.size(); i++) {
2877-
const Dictionary &settings = p_data[keys[i]];
2893+
for (int di = 0; di < keys.size(); di++) {
2894+
Dictionary settings = p_data[keys[di]];
28782895

28792896
if (bool(settings.get("save_to_file/enabled", false)) && settings.has("save_to_file/path")) {
2880-
const String save_path = ResourceUID::ensure_path(settings["save_to_file/path"]);
2897+
const String &raw_save_path = settings["save_to_file/path"];
2898+
String save_path = ResourceUID::ensure_path(raw_save_path);
2899+
if (raw_save_path.begins_with("uid://")) {
2900+
if (save_path.is_empty() || !DirAccess::exists(save_path.get_base_dir())) {
2901+
if (settings.has("save_to_file/fallback_path")) {
2902+
String fallback_save_path = settings["save_to_file/fallback_path"];
2903+
if (!fallback_save_path.is_empty() && DirAccess::exists(fallback_save_path.get_base_dir())) {
2904+
save_path = fallback_save_path;
2905+
ResourceUID::get_singleton()->add_id(ResourceUID::get_singleton()->text_to_id(raw_save_path), save_path);
2906+
}
2907+
}
2908+
} else {
2909+
settings["save_to_file/fallback_path"] = save_path;
2910+
}
2911+
}
28812912
ERR_FAIL_COND_V(!save_path.is_empty() && !DirAccess::exists(save_path.get_base_dir()), ERR_FILE_BAD_PATH);
28822913
}
2914+
2915+
if (settings.has("slices/amount")) {
2916+
int slices_count = settings["slices/amount"];
2917+
for (int si = 0; si < slices_count; si++) {
2918+
if (bool(settings.get("slice_" + itos(si + 1) + "/save_to_file/enabled", false)) &&
2919+
settings.has("slice_" + itos(si + 1) + "/save_to_file/path")) {
2920+
const String &raw_save_path = settings["slice_" + itos(si + 1) + "/save_to_file/path"];
2921+
String save_path = ResourceUID::ensure_path(raw_save_path);
2922+
if (raw_save_path.begins_with("uid://")) {
2923+
if (save_path.is_empty() || !DirAccess::exists(save_path.get_base_dir())) {
2924+
if (settings.has("slice_" + itos(si + 1) + "/save_to_file/fallback_path")) {
2925+
String fallback_save_path = settings["slice_" + itos(si + 1) + "/save_to_file/fallback_path"];
2926+
if (!fallback_save_path.is_empty() && DirAccess::exists(fallback_save_path.get_base_dir())) {
2927+
save_path = fallback_save_path;
2928+
ResourceUID::get_singleton()->add_id(ResourceUID::get_singleton()->text_to_id(raw_save_path), save_path);
2929+
}
2930+
}
2931+
} else {
2932+
settings["save_to_file/fallback_path"] = save_path;
2933+
}
2934+
}
2935+
ERR_FAIL_COND_V(!save_path.is_empty() && !DirAccess::exists(save_path.get_base_dir()), ERR_FILE_BAD_PATH);
2936+
}
2937+
}
2938+
}
28832939
}
28842940

28852941
return OK;

editor/import/3d/scene_import_settings.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -1585,9 +1585,18 @@ void SceneImportSettingsDialog::_save_dir_confirm() {
15851585
continue; //ignore
15861586
}
15871587
String path = item->get_text(1);
1588+
String uid_path;
15881589
if (!path.is_resource_file()) {
15891590
continue;
15901591
}
1592+
if (path.begins_with("uid://")) {
1593+
uid_path = path;
1594+
path = ResourceUID::ensure_path(uid_path);
1595+
} else {
1596+
ResourceUID::ID new_id = ResourceUID::get_singleton()->create_id();
1597+
ResourceUID::get_singleton()->add_id(new_id, path);
1598+
uid_path = ResourceUID::get_singleton()->id_to_text(new_id);
1599+
}
15911600

15921601
String id = item->get_metadata(0);
15931602

@@ -1603,22 +1612,25 @@ void SceneImportSettingsDialog::_save_dir_confirm() {
16031612
}
16041613

16051614
md.settings["use_external/enabled"] = true;
1606-
md.settings["use_external/path"] = path;
1615+
md.settings["use_external/path"] = uid_path;
1616+
md.settings["use_external/fallback_path"] = path;
16071617

16081618
} break;
16091619
case ACTION_CHOOSE_MESH_SAVE_PATHS: {
16101620
ERR_CONTINUE(!mesh_map.has(id));
16111621
MeshData &md = mesh_map[id];
16121622

16131623
md.settings["save_to_file/enabled"] = true;
1614-
md.settings["save_to_file/path"] = path;
1624+
md.settings["save_to_file/path"] = uid_path;
1625+
md.settings["save_to_file/fallback_path"] = path;
16151626
} break;
16161627
case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
16171628
ERR_CONTINUE(!animation_map.has(id));
16181629
AnimationData &ad = animation_map[id];
16191630

16201631
ad.settings["save_to_file/enabled"] = true;
1621-
ad.settings["save_to_file/path"] = path;
1632+
ad.settings["save_to_file/path"] = uid_path;
1633+
ad.settings["save_to_file/fallback_path"] = path;
16221634

16231635
} break;
16241636
}

0 commit comments

Comments
 (0)