Skip to content

Commit fdb6a5f

Browse files
author
Roberto De Ioris
authored
Merge pull request 20tab#404 from HughMacdonald/multiple_python_script_search_paths
Add additional search paths for python modules
2 parents bcdf380 + a959ba8 commit fdb6a5f

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

Source/PythonEditor/Private/PythonProject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ UPythonProject::UPythonProject(const FObjectInitializer& ObjectInitializer)
77
: Super(ObjectInitializer)
88
{
99
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
10-
Path = PythonModule.ScriptsPath;
10+
Path = PythonModule.ScriptsPaths[0];
1111
}

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,18 @@ void FUnrealEnginePythonModule::UESetupPythonInterpreter(bool verbose)
112112
PyObject *py_zip_path = PyUnicode_FromString(zip_path);
113113
PyList_Insert(py_path, 0, py_zip_path);
114114

115-
char *scripts_path = TCHAR_TO_UTF8(*ScriptsPath);
116-
PyObject *py_scripts_path = PyUnicode_FromString(scripts_path);
117-
PyList_Insert(py_path, 0, py_scripts_path);
115+
116+
int i = 0;
117+
for (FString ScriptsPath : ScriptsPaths)
118+
{
119+
char *scripts_path = TCHAR_TO_UTF8(*ScriptsPath);
120+
PyObject *py_scripts_path = PyUnicode_FromString(scripts_path);
121+
PyList_Insert(py_path, i++, py_scripts_path);
122+
if (verbose)
123+
{
124+
UE_LOG(LogPython, Log, TEXT("Python Scripts search path: %s"), UTF8_TO_TCHAR(scripts_path));
125+
}
126+
}
118127

119128
char *additional_modules_path = TCHAR_TO_UTF8(*AdditionalModulesPath);
120129
PyObject *py_additional_modules_path = PyUnicode_FromString(additional_modules_path);
@@ -123,7 +132,6 @@ void FUnrealEnginePythonModule::UESetupPythonInterpreter(bool verbose)
123132
if (verbose)
124133
{
125134
UE_LOG(LogPython, Log, TEXT("Python VM initialized: %s"), UTF8_TO_TCHAR(Py_GetVersion()));
126-
UE_LOG(LogPython, Log, TEXT("Python Scripts search path: %s"), UTF8_TO_TCHAR(scripts_path));
127135
}
128136
}
129137

@@ -258,12 +266,12 @@ void FUnrealEnginePythonModule::StartupModule()
258266

259267
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("ScriptsPath"), IniValue, GEngineIni))
260268
{
261-
ScriptsPath = IniValue;
269+
ScriptsPaths.Add(IniValue);
262270
}
263271

264272
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeScriptsPath"), IniValue, GEngineIni))
265273
{
266-
ScriptsPath = FPaths::Combine(*PROJECT_CONTENT_DIR, *IniValue);
274+
ScriptsPaths.Add(FPaths::Combine(*PROJECT_CONTENT_DIR, *IniValue));
267275
}
268276

269277
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("AdditionalModulesPath"), IniValue, GEngineIni))
@@ -286,20 +294,24 @@ void FUnrealEnginePythonModule::StartupModule()
286294
ZipPath = FPaths::Combine(*PROJECT_CONTENT_DIR, *IniValue);
287295
}
288296

289-
if (ScriptsPath.IsEmpty())
297+
FString ProjectScriptsPath = FPaths::Combine(*PROJECT_CONTENT_DIR, UTF8_TO_TCHAR("Scripts"));
298+
if (FPaths::DirectoryExists(ProjectScriptsPath))
290299
{
291-
ScriptsPath = FPaths::Combine(*PROJECT_CONTENT_DIR, UTF8_TO_TCHAR("Scripts"));
300+
ScriptsPaths.Add(ProjectScriptsPath);
292301
}
293302

294-
if (ZipPath.IsEmpty())
303+
for (TSharedRef<IPlugin>plugin : IPluginManager::Get().GetEnabledPlugins())
295304
{
296-
ZipPath = FPaths::Combine(*PROJECT_CONTENT_DIR, UTF8_TO_TCHAR("ue_python.zip"));
305+
FString PluginScriptsPath = FPaths::Combine(plugin->GetContentDir(), UTF8_TO_TCHAR("Scripts"));
306+
if (FPaths::DirectoryExists(PluginScriptsPath))
307+
{
308+
ScriptsPaths.Add(PluginScriptsPath);
309+
}
297310
}
298311

299-
if (!FPaths::DirectoryExists(ScriptsPath))
312+
if (ZipPath.IsEmpty())
300313
{
301-
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
302-
PlatformFile.CreateDirectory(*ScriptsPath);
314+
ZipPath = FPaths::Combine(*PROJECT_CONTENT_DIR, UTF8_TO_TCHAR("ue_python.zip"));
303315
}
304316

305317
// To ensure there are no path conflicts, if we have a valid python home at this point,
@@ -496,10 +508,26 @@ void FUnrealEnginePythonModule::RunFile(char *filename)
496508
{
497509
FScopePythonGIL gil;
498510
FString full_path = UTF8_TO_TCHAR(filename);
511+
bool foundFile = false;
499512
if (!FPaths::FileExists(filename))
500513
{
501-
full_path = FPaths::Combine(*ScriptsPath, full_path);
514+
for (FString ScriptsPath : ScriptsPaths)
515+
{
516+
full_path = FPaths::Combine(*ScriptsPath, full_path);
517+
if (FPaths::FileExists(full_path))
518+
{
519+
foundFile = true;
520+
break;
521+
}
522+
}
523+
}
524+
525+
if (!foundFile)
526+
{
527+
UE_LOG(LogPython, Error, TEXT("Unable to find file %s"), filename);
528+
return;
502529
}
530+
503531
#if PY_MAJOR_VERSION >= 3
504532
FILE *fd = nullptr;
505533

@@ -544,9 +572,24 @@ void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void(*callback)
544572
{
545573
FScopePythonGIL gil;
546574
FString full_path = filename;
575+
bool foundFile = false;
547576
if (!FPaths::FileExists(filename))
548577
{
549-
full_path = FPaths::Combine(*ScriptsPath, full_path);
578+
for (FString ScriptsPath : ScriptsPaths)
579+
{
580+
full_path = FPaths::Combine(*ScriptsPath, full_path);
581+
if (FPaths::FileExists(full_path))
582+
{
583+
foundFile = true;
584+
break;
585+
}
586+
}
587+
}
588+
589+
if (!foundFile)
590+
{
591+
UE_LOG(LogPython, Error, TEXT("Unable to find file %s"), filename);
592+
return;
550593
}
551594

552595
PyThreadState *_main = PyThreadState_Get();

Source/UnrealEnginePython/Public/UnrealEnginePython.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
112112

113113
void UESetupPythonInterpreter(bool);
114114

115-
FString ScriptsPath;
115+
TArray<FString> ScriptsPaths;
116116
FString ZipPath;
117117
FString AdditionalModulesPath;
118118

0 commit comments

Comments
 (0)