20tab / UnrealEnginePython

Embed Python in Unreal Engine 4
MIT License
2.77k stars 754 forks source link

embed python and packages for installation from source(windows) #827

Closed arafat0007 closed 4 years ago

arafat0007 commented 4 years ago

Great works on this plugin!

I have been using python 3.5.2 and some other packages (pip, numpy,cv2,sklearn etc). For now i use hard coded python address (like C:/Python35) at UnrealEnginePython.Build.cs and of course after packaging they do not work.

I want to run python and those package on run-time and embed. Any follow through?

arafat0007 commented 4 years ago

I followed the below steps and it worked:

  1. Build the project from source where i put hard coded python path (such as C:/Python35) in the UnrealEnginePython.Build.cs file.
  2. Here, for test purpose i created python files (ue_site.py file has to be created even if its empty)
  3. I packaged the game (use pak option of package option was true)
  4. I put the scripted files in the content folder of the new packaged game folder (like GameName/Content/Scripts) and opened the game, it was getting python (though it is still the hard coded path)
  5. Since Pak option was true, i could not find Config folder anymore in the new packaged game folder. I copy Config folder from the source game files and paste it to the new packaged game folder (so GameName folder has Binaries, Config, Content and Saved folders)
  6. In the Config folder, i modified the DefaultEngine.ini file. I add, [Python] RelativeHome = Python35 Now relative path will be compared to our Content folder. Now i placed my python folder in Content. So If user changed place, it will always get the path of this python. Besides, i also changed that "C:/Python35" name, so that i can be sure, game is using this relative path python. If all is ok, i get result. If not then i get error.

After running the game, i got the result of my python files. I moved whole game folder to various places so that it ensures that relative path was working. Also now that hard coded python path does not matter.

Though, I got into another problem. import unreal_engine as ue testwidget = ue.get_asset("asset address")

I was trying to get asset but it was showing that "unreal_ue has no attribute named get_asset". But during gameplay, it was ok!!! After packaging, it is not working but in the gameplay, it works. Still i don't know why.

I am new to this and sorry if i made some errors while writing this. Please enlighten me if anyone has any correction or other way.

darrickyee commented 4 years ago

I haven't tested extensively but I used the instructions here and it seemed to work: https://github.com/20tab/UnrealEnginePython/issues/163#issuecomment-299550279

Basically:

  1. Download embeddable Python distribution (for example, "Windows x86-64 embeddable zip file" from https://www.python.org/downloads/windows/)
  2. Package a shipping build of the project
  3. Unzip the embeddable Python distribution into YourMainGameFolder/Binaries/Win64/ folder of the packaged game.

I tested it by including a simple Python script that moves an actor around in the game and renaming the folder of my system-wide Python distribution so that it could not be found. The script works if I follow the instructions above, but it doesn't if I remove the embeddable Python distribution from that game executable folder, so I assume it's using the embeddable distribution. However, the real test would be to see if it works after coping to a machine that doesn't have any Python distribution installed, which I haven't tried yet.