From d4b54e35f962373c23a9034f4f96f8fb02cb45db Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= <rverschelde@gmail.com>
Date: Thu, 7 Apr 2022 23:33:28 +0200
Subject: Fix path handling in FBX and Blend importers

Fixes #59996.
---
 modules/gltf/editor/editor_scene_importer_blend.cpp |  5 +++--
 modules/gltf/editor/editor_scene_importer_fbx.cpp   | 11 +++++++----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/modules/gltf/editor/editor_scene_importer_blend.cpp b/modules/gltf/editor/editor_scene_importer_blend.cpp
index cdb22b7d19..ae05c1b68d 100644
--- a/modules/gltf/editor/editor_scene_importer_blend.cpp
+++ b/modules/gltf/editor/editor_scene_importer_blend.cpp
@@ -62,10 +62,11 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_
 		List<String> *r_missing_deps, Error *r_err) {
 	// Get global paths for source and sink.
 
-	const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path);
+	// Escape paths to be valid Python strings to embed in the script.
+	const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path).c_escape();
 	const String sink = ProjectSettings::get_singleton()->get_imported_files_path().plus_file(
 			vformat("%s-%s.gltf", p_path.get_file().get_basename(), p_path.md5_text()));
-	const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink);
+	const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink).c_escape();
 
 	// Handle configuration options.
 
diff --git a/modules/gltf/editor/editor_scene_importer_fbx.cpp b/modules/gltf/editor/editor_scene_importer_fbx.cpp
index 7cfd85c73a..24564f55be 100644
--- a/modules/gltf/editor/editor_scene_importer_fbx.cpp
+++ b/modules/gltf/editor/editor_scene_importer_fbx.cpp
@@ -53,10 +53,13 @@ Node *EditorSceneFormatImporterFBX::import_scene(const String &p_path, uint32_t
 		List<String> *r_missing_deps, Error *r_err) {
 	// Get global paths for source and sink.
 
-	const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path);
+	// Don't use `c_escape()` as it can generate broken paths. These paths will be
+	// enclosed in double quotes by OS::execute(), so we only need to escape those.
+	// `c_escape_multiline()` seems to do this (escapes `\` and `"` only).
+	const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path).c_escape_multiline();
 	const String sink = ProjectSettings::get_singleton()->get_imported_files_path().plus_file(
 			vformat("%s-%s.glb", p_path.get_file().get_basename(), p_path.md5_text()));
-	const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink);
+	const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink).c_escape_multiline();
 
 	// Run fbx2gltf.
 
@@ -65,9 +68,9 @@ Node *EditorSceneFormatImporterFBX::import_scene(const String &p_path, uint32_t
 	List<String> args;
 	args.push_back("--pbr-metallic-roughness");
 	args.push_back("--input");
-	args.push_back(vformat("\"%s\"", source_global));
+	args.push_back(source_global);
 	args.push_back("--output");
-	args.push_back(vformat("\"%s\"", sink_global));
+	args.push_back(sink_global);
 	args.push_back("--binary");
 
 	String standard_out;
-- 
cgit v1.2.3