Encrypt the python file into a pyd file available to UE4


foreword

Reference articles:
https://yafeng.blog.csdn.net/article/details/81166062
https://www.cnblogs.com/lvdongjie/p/14181116.html
https://www.cnblogs.com/lazyboy/p /4017567.html

UE4 version: UE4.25.4


1. Check the python version that comes with UE4

Note: pyd files packaged in different versions cannot be imported

1. Check UE4python version by vs or cdm

It was mentioned in the previous article:
https://blog.csdn.net/weixin_43912248/article/details/122423597

insert image description here

2. Install the same version of python

In order not to destroy the python environment that comes with UE4, it is recommended to install a new python, which can be used after packaging

If you can use the trypackage library, you can try it (the test library that I see occasionally, I haven't tried it yet)

python2.7.14 download link
https://www.python.org/downloads/release/python-2714/

Note: After the installation is complete, if the computer itself has other versions of python, you must pay attention to distinguishing it when using cmd.

You can rename python.exe in the root directory of python2.7.14 to python27.exe.
The following commands will continue to use pthon27. Modify the command according to your own name.
insert image description here
If it is not displayed, please check and add the configuration environment:
https://blog.csdn.net/weixin_43912248 /article/details/122423597

3. Install the libraries required for packaging pyd

1. install cython

(name according to your own python.exe, modify the python command)

python27 -m pip install cython

insert image description here

2. Install easycython

python27 -m pip install easycython

insert image description here

Fourth, install vcpython27

Installation tutorial
https://www.cnblogs.com/lazyboy/p/4017567.html

Because the download link of Microsoft is invalid here, I just found it on the Internet and downloaded it. It is available for personal testing, so please post the link. Link
: https://pan.baidu.com/s/1CDjOxJOeQ_dsCBjV9lo0pA
Extraction code: vkgq

The following content is excerpted from the original text of the python extension compiled with Microsoft Visual C++ Compiler for Python 2.7 on the windows platform to prevent loss

1. Download and install. Taking this machine as an example, the path after installation is:

C:\Users\Administrator\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0

2. Modify the Lib\distutils\msvc9compiler.py file in the python installation directory (if necessary, the msvccompiler.py file may also need to be changed accordingly, depending on the system), find the get_build_version method and directly return 9.0

def get_build_version():
    """Return the version of MSVC that was used to build Python.
 
    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """
    return 9.0

3. Then find the find_vcvarsall method and directly return the path of vcvarsall.bat (subject to the path after installation on your own machine)

def find_vcvarsall(version):
    """Find the vcvarsall.bat file
 
    At first it tries to find the productdir of VS 2008 in the registry. If
    that fails it falls back to the VS90COMNTOOLS env var.
    """
    return r'C:\Users\Administrator\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat'

Five, transfer to pyd operation

1. Change the file name

Rename the .py file to .pyx
insert image description here

2. cmd command

Enter cmd in the current file path and press Enter to open the cmd in the current path
insert image description here

Then enter the command in cmd

easycython *.pyx

insert image description here
If there is this error and the pyd file cannot be generated, follow the prompts to install the vcpython27
installation tutorial
https://www.cnblogs.com/lazyboy/p/4017567.html

Try typing the command again

6. Test

1. The pyd file in the folder is imported and executed through init_unreal.py

There is only one .py file in the folder, so that UE4 can execute
insert image description here

2. Open UE4 and execute init_unreal.py
insert image description here

3. The execution is successful, no import error is reported
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43912248/article/details/122424873