|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | + |
| 5 | +import compas |
| 6 | +import compas_blender |
| 7 | + |
| 8 | +from compas._os import remove |
| 9 | +from compas._os import remove_symlink |
| 10 | +from compas._os import rename |
| 11 | + |
| 12 | + |
| 13 | +BOOTSTRAPPER_TEMPLATE = """ |
| 14 | +import os |
| 15 | +import sys |
| 16 | +
|
| 17 | +import bpy |
| 18 | +
|
| 19 | +ENVIRONMENT_NAME = r"{}" |
| 20 | +PYTHON_DIRECTORY = r"{}" |
| 21 | +
|
| 22 | +def register(): |
| 23 | + pass |
| 24 | +
|
| 25 | +def unregister(): |
| 26 | + pass |
| 27 | +
|
| 28 | +""" |
| 29 | + |
| 30 | + |
| 31 | +def install_windows( |
| 32 | + blender_path, version=None, packages=None, force_reinstall=False, no_deps=False |
| 33 | +): |
| 34 | + """Install COMPAS for Blender on Windows. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + blender_path : str |
| 39 | + The path to the folder with the version number of Blender. |
| 40 | + For example, on Mac: ``'/Applications/Blender.app/Contents/Resources/2.83'``. |
| 41 | + On Windows: ``'%PROGRAMFILES%/Blender Foundation/Blender 2.83/2.83'``. |
| 42 | + version : {'2.83', '2.93', '3.1'}, optional |
| 43 | + The version number of Blender. |
| 44 | + Default is ``'2.93'``. |
| 45 | + packages : list[str], optional |
| 46 | + Additional packages to install. |
| 47 | + Note that the packages should be available on the Python Package Index (PyPI). |
| 48 | + force_reinstall : bool, optional |
| 49 | + Force existing packages to be reinstalled. |
| 50 | + no_deps : bool, optional |
| 51 | + Ignore requirements of the specified packages during installation. |
| 52 | +
|
| 53 | + Examples |
| 54 | + -------- |
| 55 | + .. code-block:: bash |
| 56 | +
|
| 57 | + $ python -m compas_blender.install |
| 58 | +
|
| 59 | + .. code-block:: bash |
| 60 | +
|
| 61 | + $ python -m compas_blender.install -v 2.93 |
| 62 | +
|
| 63 | + .. code-block:: bash |
| 64 | +
|
| 65 | + $ python -m compas_blender.install /Applications/Blender.app/Contents/Resources/2.93 |
| 66 | +
|
| 67 | + """ |
| 68 | + if not compas.WINDOWS: |
| 69 | + print( |
| 70 | + "This install procedure is only suited for installations on Windows. Please use the basic install instead..." |
| 71 | + ) |
| 72 | + sys.exit(-1) |
| 73 | + |
| 74 | + if not version and not blender_path: |
| 75 | + version = "2.93" |
| 76 | + |
| 77 | + if version and blender_path: |
| 78 | + print( |
| 79 | + "Both options cannot be provided simultaneously. Provide the full installation path, or the version with flag -v." |
| 80 | + ) |
| 81 | + sys.exit(-1) |
| 82 | + |
| 83 | + if version: |
| 84 | + if compas.LINUX: |
| 85 | + print( |
| 86 | + "Version-based installs are currently not supported for Linux. Please provide the full installation path with the -p option." |
| 87 | + ) |
| 88 | + sys.exit(-1) |
| 89 | + |
| 90 | + blender_path = compas_blender._get_default_blender_installation_path(version) |
| 91 | + |
| 92 | + if not os.path.exists(blender_path): |
| 93 | + raise FileNotFoundError("Blender version folder not found.") |
| 94 | + |
| 95 | + path, version = os.path.split(blender_path) |
| 96 | + |
| 97 | + print("Installing COMPAS for Blender {}".format(version)) |
| 98 | + |
| 99 | + startup = os.path.join(blender_path, "scripts", "startup") |
| 100 | + |
| 101 | + blenderpython_src = os.path.join(blender_path, "python") |
| 102 | + blenderpython_dst = os.path.join(blender_path, "original_python") |
| 103 | + compas_bootstrapper = os.path.join(startup, "compas_bootstrapper.py") |
| 104 | + |
| 105 | + if os.path.exists(blenderpython_dst): |
| 106 | + print("Found existing installation, restoring bundled python installation...") |
| 107 | + if os.path.exists(blenderpython_src): |
| 108 | + remove_symlink(blenderpython_src) |
| 109 | + |
| 110 | + print( |
| 111 | + " Renaming original_python back to bundled python folder: {} => {}".format( |
| 112 | + blenderpython_dst, blenderpython_src |
| 113 | + ) |
| 114 | + ) |
| 115 | + rename(blenderpython_dst, blenderpython_src) |
| 116 | + |
| 117 | + if os.path.exists(compas_bootstrapper): |
| 118 | + print(" Removing compas bootstrapper...") |
| 119 | + remove(compas_bootstrapper) |
| 120 | + |
| 121 | + # Install COMPAS by running a subprocess triggering pip |
| 122 | + blenderpython = os.path.join(blender_path, "python", "bin", "python.exe") |
| 123 | + |
| 124 | + try: |
| 125 | + subprocess.run( |
| 126 | + [blenderpython, "-m", "pip", "install", "--upgrade", "pip"], check=True |
| 127 | + ) |
| 128 | + except subprocess.CalledProcessError: |
| 129 | + print("Could not upgrade pip") |
| 130 | + sys.exit(-1) |
| 131 | + |
| 132 | + try: |
| 133 | + args = [blenderpython, "-m", "pip", "install", "compas"] |
| 134 | + if packages: |
| 135 | + args += packages |
| 136 | + if force_reinstall: |
| 137 | + args.append("--force-reinstall") |
| 138 | + if no_deps: |
| 139 | + args.append("--no-deps") |
| 140 | + |
| 141 | + subprocess.run(args, check=True) |
| 142 | + except subprocess.CalledProcessError: |
| 143 | + print("Could not install compas or some of the requested additional packages.") |
| 144 | + sys.exit(-1) |
| 145 | + |
| 146 | + # # Take either the CONDA environment directory or the current Python executable's directory |
| 147 | + # python_directory = os.environ.get("CONDA_PREFIX", None) or os.path.dirname( |
| 148 | + # sys.executable |
| 149 | + # ) |
| 150 | + # environment_name = os.environ.get("CONDA_DEFAULT_ENV", "") |
| 151 | + |
| 152 | + # Get current sys.version value, we will override it inside Blender |
| 153 | + # because it seems Blender overrides it as well, but doing so breaks many things after having replaced the Python interpreter |
| 154 | + |
| 155 | + # _handle, bootstrapper_temp_path = tempfile.mkstemp(suffix=".py", text=True) |
| 156 | + |
| 157 | + # with open(bootstrapper_temp_path, "w") as f: |
| 158 | + # f.write(BOOTSTRAPPER_TEMPLATE.format(environment_name, python_directory)) |
| 159 | + |
| 160 | + # print(" Creating bootstrap script: {}".format(compas_bootstrapper)) |
| 161 | + # copy(bootstrapper_temp_path, compas_bootstrapper) |
| 162 | + |
| 163 | + print() |
| 164 | + print("COMPAS for Blender {} has been installed via pip.".format(version)) |
| 165 | + print( |
| 166 | + "Note that functionaliy of conda-only packages has to be run via the command server (RPC)." |
| 167 | + ) |
| 168 | + |
| 169 | + |
| 170 | +# ============================================================================== |
| 171 | +# Main |
| 172 | +# ============================================================================== |
| 173 | + |
| 174 | +if __name__ == "__main__": |
| 175 | + |
| 176 | + import argparse |
| 177 | + |
| 178 | + parser = argparse.ArgumentParser() |
| 179 | + |
| 180 | + parser.add_argument( |
| 181 | + "blenderpath", |
| 182 | + nargs="?", |
| 183 | + help="The path to the folder with the version number of Blender.", |
| 184 | + ) |
| 185 | + parser.add_argument( |
| 186 | + "-v", |
| 187 | + "--version", |
| 188 | + choices=["2.83", "2.93", "3.1"], |
| 189 | + help="The version of Blender to install COMPAS in.", |
| 190 | + ) |
| 191 | + parser.add_argument("-p", "--packages", nargs="+", help="The packages to install.") |
| 192 | + parser.add_argument( |
| 193 | + "--force-reinstall", dest="force_reinstall", default=False, action="store_true" |
| 194 | + ) |
| 195 | + parser.add_argument("--no-deps", dest="no_deps", default=False, action="store_true") |
| 196 | + |
| 197 | + args = parser.parse_args() |
| 198 | + |
| 199 | + install_windows( |
| 200 | + args.blenderpath, |
| 201 | + version=args.version, |
| 202 | + packages=args.packages, |
| 203 | + force_reinstall=args.force_reinstall, |
| 204 | + no_deps=args.no_deps, |
| 205 | + ) |
0 commit comments