前言

  关于 FBX SDK Python binding 在我最初的 Unreal Python 文章里面有所记录 链接
  当时是直接下载官方编译好的版本

alt

  但是官方只提供了 python2.7 和 python3.3 的版本
  过去 Unreal 4.26 以前 内置的 Python 是 2.7 还是可以兼容官方提供的 fbx 模块。
  但是到了 Unreal 4.26 官方总算是升级到了 3.7 ,以至于 Autodesk 官方提供的 fbx 模块无法使用了。
  那么如何才能让 Unreal 4.26 使用 fbx 模块呢?

  官方提供了 源码 编译的相关文件,可以自己动手生成不同版本来兼容需求。

Python Binding 下载

alt

  下载安装 Binding ,安装了可以打开 readme 文档
  里面有很详细的操作说明。

What Is The Purpose Of Package? Binding 编译的目的

The FBX Python Bindings allow developers to write the FBX Python SDK using
their desired Python interpreter and Riverbank's Sip. Autodesk provides a
pre-compiled version of the FBX Python SDK but only for a specific version
of the Python interpreter (see the FBX Python SDK package)

FBX Python 手动编译可以兼容不同的 Python 版本

Pre-Requisites 前置工作

To be able to compile the sources in this package, you must also install the
FBX SDK and you need to make sure that your system already has Sip (Linux system
usually provide it by default).

If your system does not provide Sip, you can download the Riverbanks Sip sources
(https://www.riverbankcomputing.com/software/sip/download/) and build it as a 
pre-step of the PythonBinding.py script (see the HOW-TO section).

A C++ compiler for your operating system will be required. For instance,
on Microsoft Windows, the PythonBinding.py script will assume that you are
working with Microsoft VisualStudio 2015. This can be changed by modifying the
"vcCompiler" and "vsCompiler" variables defined in the script.

Finally, the Python interpreter must be available on the build machine.

总结起来就是需要 FBX SDK C++ 源码,以及 C++ 编译器,默认脚本配置使用 VisualStudio 2015 的 C++ 编译器。

How-to 操作步骤

3.1  Download the latest version of the FBX SDK and install it.

  下载并安装最新的 FBX SDK

alt

3.2  Define the FBXSDK_ROOT environment variable to the location of the
    installed FBX SDK from step 3.1
    (example: C:\Program Files\Autodesk\FBX\FBX SDK\2019.5)

  设置 FBXSDK_ROOT 的环境变量指向上面安装的路径

3.3  Run the PythonBinding.py script with the desired options (you can just
    call the script without arguments to see the available options).

    note: you may have to edit the script file to add your version of the
    python interpreter. By the default, the script defines the 32 and 64 bits
    ucs2 and ucs4 flavors of Python 2.x and 3.x (depending on the build machine).

    The PythonBinding.py script will try to locate the sipconfig.py file
    in the Python's sites using the getsitepackages() and getusersitepackages()
    returned paths. If found, it assumes that Sip has been installed on your
    machine and that it can be located using the system paths.

    If you want to use a specific RiverBanks's Sip sources version. You have to:
        a) download and extract the package sources.
        b) define SIP_ROOT environment variable to the location of the extracted
          Sip sources.
        c) call the PythonBinding.py with the buildsip option

    3.4  The resulting files will be located in "build/Distrib/site-packages/fbx".
    The fbx folder can then be, either moved inside Python's site-packages or,
    referenced using the PYTHONPATH environment variable (alternatively, by adding
    it into your python script using: sys.path.append()).

Remarks:

    - If you generated the FBX Python SDK using a pre-installed Sip, you need to
      tell Python where it can find the libfbxsdk library. You achieve this by
      adding this library path to LD_LIBRARY_PATH environment variable.

    - Autodesk official build of the FBX Python SDK is using Sip sources version
      4.19.3 because it is known that it correctly supports the '--sip-module'
      option and generates a private copy of sip named fbxsip. This fully qualified
      name does guarantee that there is not going to be conflicts with other sip
      modules in the system. Other versions of Sip sources, may fail to properly
      name the generated sip module. Therefore, if moving the content of
      "build/Distrib/site-packages/fbx", be careful that you do not overwrite other
      sip module files already present in your system.

  运行 PythonBinding.py 会有相应的配置提示

alt

  使用之前可以先在 Python 下安装 sip ,这样就不需要源码编译 sip
  但是这里比较坑的点是 sip 最新版本不兼容, fbx sdk 里面的代码,运行会报错。
  建议使用安装使用老版本,我这里用的是比较久的 Anaconda ,默认是 python3.7 的,内置 sip 4.18.1 版本
  我顺带查了一下 sip 4.18.1 版本最高也只支持到 3.7
  riverbank 官网有 sip 源码可以下载 链接

alt

  但是使用新版本源码编译 sip 同样会出错,貌似是 fbx sdk 里面的 sip 文件有格式错误,估计是版本太新不兼容老的。
  于是我现在自己的 4.18.1 的版本运行编译,成功编译出了 fbx.pyd ,只是使用必须要加上 sip.pyd 才可以。

  Remark 里面提到官方编译使用 4.19.3 ,然后利用 4.19.3 的特性可以 --sip-module 可以将 sip 转为 fbxsip 进行导入 避免 sip 模块的冲突
  于是我进一步研究了 buildsip 命令,从 sip 的源码开始把编译流程又走了一遍。

sip 模块编译

  作为已经踩了一遍坑的前情提要, sip 的模块必须是 4.19.3 - 4.19.8 之间,因为最新的版本反而无法使用 fbxsip

alt

  fbxsip 已经配置在 PythonBindings.py 里面,如果不用上述版本之间的 sip 则使用默认生成的 sip.pyd

alt

sip 4.19.8 下载和编译准备

https://riverbankcomputing.com/software/sip/download

sip 使用 Mercurial 版本管理工具进行仓库管理

alt

  点击上面的 repository 可以打开 sip 的仓库,后续的操作可以参考 github ,只是图形界面少了点。

alt

  选择 Tag 可以找到 4.19.8 的版本。

alt

  然后点击上面的三种不同的压缩包,都可以将当前版本下载下来。

  下载下来之后,需要安装使用 bison 和 flex 来准备编译编译用的代码,具体可以查看 readme 说明,需要运行 build.py 来准备好编译文件。
  bison 和 flex 都是 Linux 下内置的 C 语言解析工具, windows 下需要特殊编译的版本,找到了个 github 仓库有编译好的版本 winflexbison
  将编译好的版本添加环境变量,运行 build.py prepare 进行编译准备。

alt

  后续的编译调用可以借助 PythonBindings.py 的 buildsip 命令。

PythonBindings 编译

  需要配置 SIP_ROOT 的环境变量(路径为上面配置好的 sip 源码目录),我这里为了省事,直接将路径写到了 py 脚本里面了。

1
python PythonBindings.py Python3_x64 buildsip

  这个过程会自动进行 sip 的编译,生成 fbxsip.pyd
  脚本默认配置使用 VS2015 进行编译,需要配合 VS2015 的 SDK ,我本地都配置好了,编译很顺利,只是过程有些代码警告。

alt

  然后就可以得到我们需要的 pyd 文件了。
  最后生成的 pyd 完美在 Python3.7 下运行,兼容 UE4.26 引擎。

pyd下载

总结

  找时间一定要研究下如何使用命令行调用 VS2015 的编译器进行编译的, VisualStudio 全部封装成按钮,反而让我很不爽,看不到背后的调用过程了。