20tab / UnrealEnginePython

Embed Python in Unreal Engine 4
MIT License
2.7k stars 737 forks source link

Building4.25 #850

Open davidjo opened 3 years ago

davidjo commented 3 years ago

This is my attempt at getting UnrealEnginePython to work on 4.25 running under Ubuntu Linux 20.04. This is based off PR #845 but is highly modified. It builds on both 4.18 and 4.25. It runs all tests (after modification by PR #715 otherwise that test failed on both 4.18 and 4.25). I have tested a few examples for blueprints which I have used previously but modifications were needed to the examples to run under 4.25 - those modified versions added to examples. It needs much more extensive testing as its likely Ive missed some fixups.

k1lly commented 3 years ago

Thanks for your work @davidjo Can you help fixing build issue for Windows? File: UnrealEnginePython\Source\UnrealEnginePython\Private\UEPyModule.cpp Compilation Errors:

UnrealEnginePython/Source/UnrealEnginePython/Private/UEPyModule.cpp(3126): error C2065: 'UK2Node_DynamicCast': undeclared identifier
UnrealEnginePython/Source/UnrealEnginePython/Private/UEPyModule.cpp(3126): error C2065: 'node': undeclared identifier
UnrealEnginePython/Source/UnrealEnginePython/Private/UEPyModule.cpp(3126): error C2065: 'UK2Node_DynamicCast': undeclared identifier
UnrealEnginePython/Source/UnrealEnginePython/Private/UEPyModule.cpp(3126): error C2059: syntax error: ')'

Line 3126, causing compilation error: UK2Node_DynamicCast* node = (UK2Node_DynamicCast*)buffer;

If you simply comment out this line, the plugin compiles, but doesn't work as expected: during the packaging for Windows process the plugin causes errors and build fails.

Edit: Even with the line 3126 commented out in the plugin source, attempting to package project for Windows 64-bit with pixel streaming plugin enabled gives the following errors:

[PROJECT_DIRECTORY]/Plugins/UnrealEnginePython/Source/UnrealEnginePython/Private/UObject/UEPyMaterial.cpp(247): error C2039: 'GetStaticSwitchParameterValue': is not a member of 'UMaterialInstance'
C:\Program Files\Epic Games\UE_4.25\Engine\Intermediate\Build\Win64\UE4\Inc\Engine\MaterialInstanceDynamic.generated.h(11): note: see declaration of 'UMaterialInstance'
[PROJECT_DIRECTORY]/Plugins/UnrealEnginePython/Source/UnrealEnginePython/Private/UObject/UEPyObject.cpp(680): error C2039: 'CanModify': is not a member of 'UObject'
C:\Program Files\Epic Games\UE_4.25\Engine\Intermediate\Build\Win64\UE4\Inc\AIModule\AIBlueprintHelperLibrary.generated.h(19): note: see declaration of 'UObject'
[PROJECT_DIRECTORY]/Plugins/UnrealEnginePython/Source/UnrealEnginePython/Private/UObject/UEPyProperty.cpp(157): error C2039: 'SetMetaData': is not a member of 'FProperty'
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\UObject/PropertyAccessUtil.h(12): note: see declaration of 'FProperty'
[PROJECT_DIRECTORY]/Plugins/UnrealEnginePython/Source/UnrealEnginePython/Private/UObject/UEPyProperty.cpp(183): error C2039: 'GetMetaData': is not a member of 'FProperty'
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\UObject/PropertyAccessUtil.h(12): note: see declaration of 'FProperty'
[PROJECT_DIRECTORY]/Plugins/UnrealEnginePython/Source/UnrealEnginePython/Private/UObject/UEPyProperty.cpp(207): error C2039: 'HasMetaData': is not a member of 'FProperty'
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\UObject/PropertyAccessUtil.h(12): note: see declaration of 'FProperty'
davidjo commented 3 years ago

Sorry - forgot to add the following ifdef which I think should fix this

#ifdef EXTRA_DEBUG_CODE
                                EXTRA_UE_LOG(LogPython, Warning, TEXT("Convert Prop 3a is uclass %s"), *ue_obj->ue_object->GetName());
                                UK2Node_DynamicCast* node = (UK2Node_DynamicCast*)buffer;
                                EXTRA_UE_LOG(LogPython, Warning, TEXT("Setting attr  targetype is %p"), (void *)(node->TargetType));
#endif
davidjo commented 3 years ago

But it wont fix the other errors. What exact version of Unreal Engine is this for??

k1lly commented 3 years ago

But it wont fix the other errors. What exact version of Unreal Engine is this for??

Unreal Engine 4.25.1 (current latest release).

davidjo commented 3 years ago

Great - it looks as tho these are differences between 4.25.0 - which I used as the base - and 4.25.1 (I just checked source for Runtime/Engine/Classes/Materials/MaterialInstance.h and GetStaticSwitchParameterValue is definitely defined).

OK so I dont think its that - just updated my git source and it still has this function in MaterialInstance.h

So it looks as tho this function is in a #if WITH_EDITORONLY_DATA block - so Im guessing if you are packaging WITH_EDITORONLY_DATA is not defined. This is likely the cause of the other errors.

davidjo commented 3 years ago

Further confirmation I think - CanModify is wrapped in #if WITH_EDITOR in Runtime/CoreUObject/Public/UObject/Object.h

k1lly commented 3 years ago

Great, more errors! 🐛 @davidjo I've pulled your latest code and again, building succeeds, but packaging fails. I've tried including references to .h files from the engine, and wrapping functions with ifs, with no success. Can you please look into it?

C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\UObject/UObjectBaseUtility.h(486): error C2440: 'initializing': cannot convert from 'OtherClassType' to 'const UClass *'
        with
        [
            OtherClassType=FFieldClass *
        ]
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\UObject/UObjectBaseUtility.h(486): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\UObject/UObjectBaseUtility.h(503): note: see reference to function template instantiation 'bool UObjectBaseUtility::IsA<FFieldClass*>(OtherClassType) const' being compiled
        with
        [
            OtherClassType=FFieldClass *
        ]
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\Templates/Casts.h(145): note: see reference to function template instantiation 'bool UObjectBaseUtility::IsA<To>(void) const' being compiled
        with
        [
            To=FMapProperty
        ]
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\Templates/Casts.h(144): note: while compiling class template member function 'To *TCastImpl<From,To,ECastType::UObjectToUObject>::DoCast(UObject *)'
        with
        [
            To=FMapProperty,
            From=UObject
        ]
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\Templates/Casts.h(221): note: see reference to function template instantiation 'To *TCastImpl<From,To,ECastType::UObjectToUObject>::DoCast(UObject *)' being compiled
        with
        [
            To=FMapProperty,
            From=UObject
        ]
C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\Templates/Casts.h(221): note: see reference to class template instantiation 'TCastImpl<From,To,ECastType::UObjectToUObject>' being compiled
        with
        [
            From=UObject,
            To=FMapProperty
        ]
[PROJECT_DIRECTORY]\Plugins\UnrealEnginePython\Source\UnrealEnginePython\Private\UEPyModule.h(78): note: see reference to function template instantiation 'To *Cast<T,UObject>(From *)' being compiled
        with
        [
            To=FMapProperty,
            T=FMapProperty,
            From=UObject
        ]
[PROJECT_DIRECTORY]/Plugins/UnrealEnginePython/Source/UnrealEnginePython/Private/UObject/UEPyObject.cpp(1494): note: see reference to function template instantiation 'T *ue_py_check_type<FMapProperty>(ue_PyUObject *)' being compiled
        with
        [
            T=FMapProperty
        ]
davidjo commented 3 years ago

So this looks like something with the new property system - ah - it appears we cannot use ue_py_check_type as that is specifically for UObjects - have to figure out the right fix.

Andygmb commented 3 years ago

@k1lly did you manage to get it packaged and working on windows for 4.25.1?

k1lly commented 3 years ago

@k1lly did you manage to get it packaged and working on windows for 4.25.1?

I did, but it required commenting out all the code execution inside the functions ive mentioned in my 1st log here, and returning constants. The plugin builds, packages and works inside a project, but its a hack, not a solution. Having almost zero knowledge of the plugin internals, I'd wait for a proper fix from PR's author.

Andygmb commented 3 years ago

@k1lly did you manage to get it packaged and working on windows for 4.25.1?

I did, but it required commenting out all the code execution inside the functions ive mentioned in my 1st log here, and returning constants. The plugin builds, packages and works inside a project, but its a hack, not a solution. Having almost zero knowledge of the plugin internals, I'd wait for a proper fix from PR's author.

Fair enough. Hopefully we can get a basic-functioning version packaged up soon from OP.

davidjo commented 3 years ago

So Ive just added what looks like a reasonable fixup - it builds but what Im doing doesnt seem to test this feature and Im only really working with the Editor ie this is totally untested.

k1lly commented 3 years ago

So Ive just added what looks like a reasonable fixup - it builds but what Im doing doesnt seem to test this feature and Im only really working with the Editor ie this is totally untested.

I've tested: the plugin both builds and packages without throwing errors, thank you!

wartheking commented 3 years ago

4.25.1 crash when running sequencer_scripting.py!!

davidjo commented 3 years ago

Yes - I am also getting a crash - at least after fixing the python syntax error plus adding the Mannequin to the project. Have you run sequencer_scripting.py successfully with prior versions of the Engine? If so what versions?

wartheking commented 3 years ago

Yes - I am also getting a crash - at least after fixing the python syntax error plus adding the Mannequin to the project. Have you run sequencer_scripting.py successfully with prior versions of the Engine? If so what versions?

it also crash in engine version 4.22 with plugin's official version 4.22.so it have been a problem for a long time.

wartheking commented 3 years ago

Yes - I am also getting a crash - at least after fixing the python syntax error plus adding the Mannequin to the project. Have you run sequencer_scripting.py successfully with prior versions of the Engine? If so what versions?

Yes - I am also getting a crash - at least after fixing the python syntax error plus adding the Mannequin to the project. Have you run sequencer_scripting.py successfully with prior versions of the Engine? If so what versions?

it also crash in engine version 4.22 with plugin's official version 4.22.so it have been a problem for a long time.

can you help us to slove this problem? I am a Technical Artist,yours plugins is wonderful and this task is very important to me

davidjo commented 3 years ago

It didnt exactly inspire confidence when first error is a trivial python syntax error. I am running 4.18 and 4.21 (in addition to 4.25) so will try these and see what happens. However, its not an area of the Engine Ive used at all and the error seems to be occurring deep inside the Engine core so if cant get some form of working script in some version not sure can fix it. Have you used any form of sequencer script in any version that worked? According to the source code somebody sponsered them to add the sequencer API functionality so Im assuming at some point it worked.

davidjo commented 3 years ago

Well Ive confirmed that it seems to work in 4.21 - doesnt crash and get a sequencer window. However, the 4.22 release notes suggest Epic made a lot of changes to the Sequencer so this is likely the cause. This is likely to take some time.

wartheking commented 3 years ago

Well Ive confirmed that it seems to work in 4.21 - doesnt crash and get a sequencer window. However, the 4.22 release notes suggest Epic made a lot of changes to the Sequencer so this is likely the cause. This is likely to take some time.

Good news!I have a try in 4.21 however i isn't support the ray-tracing.So i 'd like to work your plugin in 4.25.hope that i can help you but i know a little about engine source.best wish to you and have a good luck! :)

wartheking commented 3 years ago

Well Ive confirmed that it seems to work in 4.21 - doesnt crash and get a sequencer window. However, the 4.22 release notes suggest Epic made a lot of changes to the Sequencer so this is likely the cause. This is likely to take some time.

Good news!I have a try in 4.21 however i isn't support the ray-tracing.So i 'd like to work your plugin in 4.25.hope that i can help you but i know a little about engine source.best wish to you and have a good luck! :)

i commented the code"sequencer_add_actor(camera)" found that the program run successfully without crash in 4.25. it seem to be the source of this problem.

davidjo commented 3 years ago

Ive pinned down the issue - it seems to be an infinite recursion loop in 4.22 in specific code added in 4.22. Im suspecting its something to do with the camera initialization - just creating a plain camera object in python may not setup enough in 4.22 - theres is a CreateCamera function so not clear why this wasnt used. Ill try and see how to replicate the camera addition manually in the 4.22 editor to get the end result of 4.21.

davidjo commented 3 years ago

I have updated my repo master with fixups for 4.25 (among others) sequencer - sequencer_scripting425.py runs without crashing and looks the same as the 4.21 version.

avivazran commented 3 years ago

Build passed successfully on 4.25.3 but when trying to run the engine the following exception occurs.

Unhandled exception at 0x00007FFA2201DB9E (ucrtbase.dll) in UE4Editor.exe: Fatal program exit requested. image

Any fix for this?

davidjo commented 3 years ago

Probably cant help with this - I dont do Windows - Im guessing whatver IDE this is the lower left is some form of stack trace and the top source is where fault occurring - however I dont see a proper stack trace as I would in Linux - but it currently looks like its in the # if PLATFORM_WINDOWS block which is fairly obviously some Windows specific initialization code - but this has nothing to do with Property changes. So question is when did it last work for you - that section was last updated Dec 2018 (fileno to _fileno) so its been there a while - could of course be something has changed in the Engine - but _setmode/_fileno/stdin etc are very basic c++ io functions. What python version are you using? From the comments above the _setmode calls its trying to fix something the python initialization code changed - but maybe newer pythons dont do this - or maybe Unreal has modified stdin etc. which causes this to fail. Could try commenting those 3 lines and seeing what happens.

mr-maul commented 3 years ago

I'm trying to pack test project (Windows 10, UE 4.25) and have a python VM at runtime. I built plugin from source as described in README. Everything is ok in Editor, but i get the following error during build procedure:

UATHelper: Packaging (Windows (64-bit)): ERROR: Unable to instantiate module 'UnrealEd': Unable to instantiate UnrealEd module for non-editor targets. UATHelper: Packaging (Windows (64-bit)): (referenced via Target -> UnrealEnginePython.Build.cs -> Sequencer.Build.cs) PackagingResults: Error: Unable to instantiate module 'UnrealEd': Unable to instantiate UnrealEd module for non-editor targets.

How can I fix this?

davidjo commented 3 years ago

Try this version of the build tool file - I was having problems getting the sequencer to compile and link properly and I now realize I added some sequencer stuff (apparently editor only) to the package and editor building - moved into the editor building only section and it seems to build OK in the editor. UnrealEnginePython.Build.cs.txt

Well apparently you cant upload .cs files - added .txt so you need to rename it back to UnrealEnginePython.Build.cs.

avivazran commented 3 years ago

Well I'm happy to say IT WORKED!

Now all that's left is to hope it'll run python scripts in the packaged project (on runtime). I've packaged successfully but without any python script. just the plugin enabled.

Will it run the scripts after packaging?

avivazran commented 3 years ago

@davidjo so I've to package a project with the plugin and it worked!!

Now, I've been trying all day to embbed a python runtime distribution in the packaged version but the instructions in the original repo are really bad.

Have you managed to do so? I've copied an embbedable python distribution to the plugin binaries and managed to build and run inside the editor but when but after packaging the exe just closes.

davidjo commented 3 years ago

Sorry - packaging is not something Ive needed to do yet. Ill add the new build tool version to main repo now it looks as tho it works so far.

mr-maul commented 3 years ago

Thanks for this fix, now project can be packaged at least. I've tried new build with system-wide python (specified in "pythonHome") , not embedded. Game is running but scripts don't work. I hope that we can make it work after time anyway.

avivazran commented 3 years ago

@mr-maul Have you added your Content/Scripts folder to additional dependencies in your project packaging settings? I've tested your scenario and it worked for me

avivazran commented 3 years ago

@avivazran @davidjo Also, If any of you is able to successfully package a project witn an embedded python distribution please write how you did it.

I've managed to package successfully and run the exe on my pc but when moving to another pc with no python installed on it, the exe crashes without any message.

my project build.cs:

using System;
using System.IO;
using UnrealBuildTool;

public class SoundSinulatorSteam : ModuleRules
{

    public SoundSinulatorSteam(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", });
        PrivateDependencyModuleNames.Add("SteamAudio");
        PrivateDependencyModuleNames.Add("SteamAudio");
        //PublicDelayLoadDLLs.Add("python36.dll");
        //PublicDelayLoadDLLs.Add("embree3.dll");
        //PublicDelayLoadDLLs.Add("tbb.dll");

        PrivateDependencyModuleNames.AddRange(new string[] {});
        PublicIncludePaths.AddRange(new string[] { "C:/Projects/SoundSinulatorSteam/Plugins/UnrealEnginePython/Binaries/Win64/include" });
        PublicAdditionalLibraries.Add("C:/Projects/SoundSinulatorSteam/Plugins/UnrealEnginePython/Binaries/Win64/libs/python36.lib");
        RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)", "python36.dll"), "C:/Projects/SoundSinulatorSteam/Plugins/UnrealEnginePython/Binaries/Win64/python36.dll");

        PublicIncludePaths.AddRange(new string[] { "C:/Projects/SoundSinulatorSteam/ThirdParty/embree-3.11.0.x64.vc14.windows/include" });
        PublicAdditionalLibraries.Add("C:/Projects/SoundSinulatorSteam/ThirdParty/embree-3.11.0.x64.vc14.windows/lib/embree3.lib");
        PublicAdditionalLibraries.Add("C:/Projects/SoundSinulatorSteam/ThirdParty/embree-3.11.0.x64.vc14.windows/lib/tbb.lib");
        RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)", "embree3.dll"), "C:/Projects/SoundSinulatorSteam/ThirdParty/embree-3.11.0.x64.vc14.windows/bin/embree3.dll");
        RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)", "tbb.dll"), "C:/Projects/SoundSinulatorSteam/ThirdParty/embree-3.11.0.x64.vc14.windows/bin/tbb.dll");
        RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)", "msvcp140.dll"), "C:/Projects/SoundSinulatorSteam/ThirdParty/embree-3.11.0.x64.vc14.windows/bin/msvcp140.dll");
        RuntimeDependencies.Add(Path.Combine("$(BinaryOutputDir)", "glfw3.dll"), "C:/Projects/SoundSinulatorSteam/ThirdParty/embree-3.11.0.x64.vc14.windows/bin/glfw3.dll");

        // Uncomment if you are using Slate UI
        // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

        // Uncomment if you are using online features
        // PrivateDependencyModuleNames.Add("OnlineSubsystem");

        // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
    }
}
mr-maul commented 3 years ago

@mr-maul Have you added your Content/Scripts folder to additional dependencies in your project packaging settings? I've tested your scenario and it worked for me

Oh, I didn't add this folder. Can you clarify please how certainly should it be included? Should it be in "PublicAdditionalLibraries" of MyProject.Build.cs? I suppose no, but besides that there are only "PublicAdditionalFrameworks", "AdditionalBundleResources" , "AdditionalPropertiesForReceipt".

Also there is mentioned: "_If you want to package your project (it is required only if you need to have a python VM at runtime, read: your game logic is programmed in python) ensure the Content/Scripts/uesite.py file is in your project (it can be empty).". Is it still actual?

avivazran commented 3 years ago

@mr-maul image

mr-maul commented 3 years ago

@avivazran Thanks, got it. What about embedded vm, did you try to set "RelativeHome" and place python somewhere in Content directory? (My attempt to perform this trick was failed but maybe it can be a way)

shdwdln commented 3 years ago

@davidjo Hey thanks for your support with keeping this going (and everyone on this thread for that matter!). @k1lly Can you tell me how you are setting this up from start to finish? I assume you are creating a new project, then adding a new blank plugin, and then launching the solution for that in VS? Then are you adding the source that is downloaded here into those respective folders.

I am building the code and I am currently getting this error: 1>C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\UObject/Script.h(372): error C2079: 'FScriptInstrumentationSignal' uses undefined struct 'COREUOBJECT_API'

I am not sure why this is happening because all the include libraries are present for the solution. Any thoughts where I am going wrong here?

Do you have any builds for 4.25.3 you could share also? Or even share your solution etc?