|
| 1 | +import os, sys, shutil, time |
| 2 | +import platform, glob |
| 3 | +import FbxUtils |
| 4 | + |
| 5 | +WINDOWS_PLATFORM = (platform.system() == 'Windows' or platform.system() == "Microsoft") |
| 6 | + |
| 7 | +rootPath = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 8 | +buildPath = os.path.join(rootPath, 'build') |
| 9 | +distribPath = os.path.join(buildPath, "Distrib", "fbxpythonsdk") |
| 10 | +cmdLineDescr = "PythonBindings.py" |
| 11 | +pythonExecutable = sys.executable |
| 12 | + |
| 13 | +def PrintError(txt): |
| 14 | + print ("\n================================================================================================\n") |
| 15 | + FbxUtils.Log(2, "ERROR: " + txt) |
| 16 | + print ("\n================================================================================================\n") |
| 17 | + exit(1) |
| 18 | + |
| 19 | + |
| 20 | +def log(txt): |
| 21 | + msg = "=> " + txt + "\n" |
| 22 | + FbxUtils.Log(1, msg) |
| 23 | + |
| 24 | +def install_SIP(): |
| 25 | + log("") |
| 26 | + log("-=[ Installing SIP ]=-") |
| 27 | + log("") |
| 28 | + |
| 29 | + command = pythonExecutable + ' -m pip install --force-reinstall -v "sip==6.6.2"' |
| 30 | + |
| 31 | + log("%s" % command) |
| 32 | + os.system(command) |
| 33 | + |
| 34 | +def generate_wheel(python_dir): |
| 35 | + log("") |
| 36 | + log("-=[ Generating wheel ]=-") |
| 37 | + log("") |
| 38 | + |
| 39 | + os.chdir(python_dir) |
| 40 | + log("%s" % python_dir) |
| 41 | + command = pythonExecutable + " -m sipbuild.tools.wheel" |
| 42 | + |
| 43 | + log("%s" % command) |
| 44 | + os.system(command) |
| 45 | + wheelFileName = glob.glob(os.path.join(python_dir,'*.whl')) |
| 46 | + return wheelFileName[0] |
| 47 | + |
| 48 | +def install_wheel(python_dir): |
| 49 | + log("") |
| 50 | + log("-=[ Install wheel ]=-") |
| 51 | + log("") |
| 52 | + |
| 53 | + os.chdir(python_dir) |
| 54 | + log("%s" % python_dir) |
| 55 | + wheelFileName = glob.glob(os.path.join(python_dir,'*.whl')) |
| 56 | + |
| 57 | + command = pythonExecutable + " -m pip install " + wheelFileName[0] |
| 58 | + |
| 59 | + log("%s" % command) |
| 60 | + os.system(command) |
| 61 | + |
| 62 | +def test_python_fbx_examples(fbx_wrapper_dir, build_dir, sdk_lib_dir): |
| 63 | + log("") |
| 64 | + log("-=[ Testing the samples ]=-") |
| 65 | + log("") |
| 66 | + |
| 67 | + src_samples_dir = os.path.join(fbx_wrapper_dir, 'samples') |
| 68 | + dst_samples_dir = os.path.join(build_dir, 'samples') |
| 69 | + try: |
| 70 | + FbxUtils.DirCopy(src_samples_dir, dst_samples_dir) |
| 71 | + except: |
| 72 | + PrintError("Failed to copy samples directories") |
| 73 | + exit(1) |
| 74 | + |
| 75 | + example_dirs = ( |
| 76 | + os.path.join(dst_samples_dir, 'Audio'), |
| 77 | + os.path.join(dst_samples_dir, 'ExportScene01'), |
| 78 | + os.path.join(dst_samples_dir, 'ExportScene02'), |
| 79 | + os.path.join(dst_samples_dir, 'ExportScene03'), |
| 80 | + os.path.join(dst_samples_dir, 'ExportScene04'), |
| 81 | + os.path.join(dst_samples_dir, 'Layers'), |
| 82 | + os.path.join(dst_samples_dir, 'SplitMeshPerMaterial'), |
| 83 | + ) |
| 84 | + example_scripts = ( |
| 85 | + 'Audio.py', |
| 86 | + 'ExportScene01.py', |
| 87 | + 'ExportScene02.py', |
| 88 | + 'ExportScene03.py', |
| 89 | + 'ExportScene04.py', |
| 90 | + 'Layers.py', |
| 91 | + 'SplitMeshPerMaterial.py multiplematerials.FBX', |
| 92 | + ) |
| 93 | + |
| 94 | + os.environ['PYTHONPATH'] = build_dir |
| 95 | + for index in range(len(example_dirs)): |
| 96 | + os.chdir(example_dirs[index]) |
| 97 | + command = " ".join(['"' + pythonExecutable + '"', example_scripts[index]]) |
| 98 | + log(" RUN COMMAND : %s" % command) |
| 99 | + result = os.system(command) |
| 100 | + |
| 101 | + os.chdir(os.path.join(dst_samples_dir, 'ImportScene')) |
| 102 | + files = ( |
| 103 | + '../Audio/Audio.fbx', |
| 104 | + '../ExportScene01/ExportScene01.fbx', |
| 105 | + '../ExportScene02/ExportScene02.fbx', |
| 106 | + '../ExportScene03/ExportScene03.fbx', |
| 107 | + '../ExportScene04/ExportScene04.fbx', |
| 108 | + '../Layers/Layers.fbx' |
| 109 | + ) |
| 110 | + |
| 111 | + dump_output = '' if WINDOWS_PLATFORM else '> /dev/null' |
| 112 | + for index in range(len(files)): |
| 113 | + command = " ".join(['"' + pythonExecutable + '"', 'ImportScene.py', files[index], dump_output]) |
| 114 | + log(" RUN COMMAND : %s" % command) |
| 115 | + result = os.system(command) |
| 116 | + |
| 117 | + |
| 118 | +def generate_python_fbx_documentation(python_dir, api_doc_dir): |
| 119 | + if WINDOWS_PLATFORM: |
| 120 | + # documentation can only be generated on Windows |
| 121 | + log("") |
| 122 | + log("-=[ Generating documentation ]=-") |
| 123 | + log("") |
| 124 | + |
| 125 | + pydocInstallPath = os.path.dirname(pythonExecutable) + "/Lib" |
| 126 | + |
| 127 | + os.chdir(python_dir) |
| 128 | + log("%s" % pydocInstallPath) |
| 129 | + command = pythonExecutable + " " + pydocInstallPath + "/pydoc.py -w fbx" |
| 130 | + log("%s" % command) |
| 131 | + os.system(command) |
| 132 | + |
| 133 | + if not os.path.exists(api_doc_dir): |
| 134 | + os.mkdir(api_doc_dir) |
| 135 | + |
| 136 | + # The generation with pydoc can take a while so wait until file exists before copying |
| 137 | + timeout = 0 |
| 138 | + while ((not os.path.exists(os.path.join(python_dir, "fbx.html"))) and (timeout < 60)): |
| 139 | + time.sleep(1) |
| 140 | + timeout += 1 |
| 141 | + |
| 142 | + shutil.copyfile(os.path.join(python_dir, "fbx.html"), os.path.join(api_doc_dir, "fbx.html")) |
| 143 | + |
| 144 | + |
| 145 | +def main(args): |
| 146 | + |
| 147 | + # Display help if wrong number arguments |
| 148 | + nbArgs = len(args) |
| 149 | + if nbArgs > 2: |
| 150 | + print ("Syntax: " + cmdLineDescr + " [test] [doc]\n") |
| 151 | + exit(1) |
| 152 | + |
| 153 | + test = False |
| 154 | + doc = False |
| 155 | + |
| 156 | + for i in range(2,nbArgs): |
| 157 | + if args[i].lower() == 'test': test = True; |
| 158 | + if args[i].lower() == 'doc': doc = True; |
| 159 | + |
| 160 | + # -------------------------------------- |
| 161 | + # compile fbx python binding |
| 162 | + # -------------------------------------- |
| 163 | + |
| 164 | + install_SIP() |
| 165 | + |
| 166 | + wheel_filename = generate_wheel(buildPath) |
| 167 | + |
| 168 | + sdk_lib_dir = distribPath |
| 169 | + log("Copy the results to the folder:" + sdk_lib_dir) |
| 170 | + if not os.path.exists(sdk_lib_dir): |
| 171 | + os.makedirs(sdk_lib_dir) |
| 172 | + |
| 173 | + shutil.copyfile(wheel_filename, os.path.join(sdk_lib_dir, os.path.basename(wheel_filename))) |
| 174 | + |
| 175 | + if test or doc: |
| 176 | + install_wheel(buildPath) |
| 177 | + |
| 178 | + if test: |
| 179 | + test_python_fbx_examples(rootPath, buildPath, sdk_lib_dir) |
| 180 | + |
| 181 | + if doc: |
| 182 | + python_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 183 | + api_doc_dir = os.path.join(distribPath,"doc") |
| 184 | + if not os.path.exists(api_doc_dir): |
| 185 | + os.makedirs(api_doc_dir) |
| 186 | + generate_python_fbx_documentation(python_dir, api_doc_dir) |
| 187 | + |
| 188 | + sys.exit(0) |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == '__main__': |
| 192 | + main(sys.argv) |
0 commit comments