Skip to content

Commit f0ab839

Browse files
committed
first attempt
1 parent 59de3fd commit f0ab839

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import os
2+
import sys
3+
import tempfile
4+
import subprocess
5+
6+
import compas
7+
import compas_blender
8+
9+
from compas._os import copy
10+
from compas._os import remove
11+
from compas._os import remove_symlink
12+
from compas._os import rename
13+
14+
15+
BOOTSTRAPPER_TEMPLATE = """
16+
import os
17+
import sys
18+
19+
import bpy
20+
21+
ENVIRONMENT_NAME = r"{}"
22+
PYTHON_DIRECTORY = r"{}"
23+
24+
def register():
25+
pass
26+
27+
def unregister():
28+
pass
29+
30+
"""
31+
32+
33+
def install_windows(blender_path, version=None):
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+
46+
Examples
47+
--------
48+
.. code-block:: bash
49+
50+
$ python -m compas_blender.install
51+
52+
.. code-block:: bash
53+
54+
$ python -m compas_blender.install -v 2.93
55+
56+
.. code-block:: bash
57+
58+
$ python -m compas_blender.install /Applications/Blender.app/Contents/Resources/2.93
59+
60+
"""
61+
if not compas.WINDOWS:
62+
print(
63+
"This install procedure is only suited for installations on Windows. Please use the basic install instead..."
64+
)
65+
sys.exit(-1)
66+
67+
if not version and not blender_path:
68+
version = "2.93"
69+
70+
if version and blender_path:
71+
print(
72+
"Both options cannot be provided simultaneously. Provide the full installation path, or the version with flag -v."
73+
)
74+
sys.exit(-1)
75+
76+
if version:
77+
if compas.LINUX:
78+
print(
79+
"Version-based installs are currently not supported for Linux. Please provide the full installation path with the -p option."
80+
)
81+
sys.exit(-1)
82+
83+
blender_path = compas_blender._get_default_blender_installation_path(version)
84+
85+
if not os.path.exists(blender_path):
86+
raise FileNotFoundError("Blender version folder not found.")
87+
88+
path, version = os.path.split(blender_path)
89+
90+
print("Installing COMPAS for Blender {}".format(version))
91+
92+
startup = os.path.join(blender_path, "scripts", "startup")
93+
94+
blenderpython_src = os.path.join(blender_path, "python")
95+
blenderpython_dst = os.path.join(blender_path, "original_python")
96+
compas_bootstrapper = os.path.join(startup, "compas_bootstrapper.py")
97+
98+
if os.path.exists(blenderpython_dst):
99+
print("Found existing installation, restoring bundled python installation...")
100+
if os.path.exists(blenderpython_src):
101+
remove_symlink(blenderpython_src)
102+
103+
print(
104+
" Renaming original_python back to bundled python folder: {} => {}".format(
105+
blenderpython_dst, blenderpython_src
106+
)
107+
)
108+
rename(blenderpython_dst, blenderpython_src)
109+
110+
if os.path.exists(compas_bootstrapper):
111+
print(" Removing compas bootstrapper...")
112+
remove(compas_bootstrapper)
113+
114+
# Install COMPAS by running a subprocess triggering pip
115+
blenderpython = os.path.join(blender_path, "python", "bin", "python.exe")
116+
117+
try:
118+
subprocess.run(
119+
[blenderpython, "-m", "pip", "install", "--upgrade", "pip"], check=True
120+
)
121+
except subprocess.CalledProcessError:
122+
print("Could not upgrade pip")
123+
sys.exit(-1)
124+
125+
try:
126+
subprocess.run([blenderpython, "-m", "pip", "install", "compas"], check=True)
127+
except subprocess.CalledProcessError:
128+
print("Could not install compas")
129+
sys.exit(-1)
130+
131+
# Take either the CONDA environment directory or the current Python executable's directory
132+
python_directory = os.environ.get("CONDA_PREFIX", None) or os.path.dirname(
133+
sys.executable
134+
)
135+
environment_name = os.environ.get("CONDA_DEFAULT_ENV", "")
136+
137+
# Get current sys.version value, we will override it inside Blender
138+
# because it seems Blender overrides it as well, but doing so breaks many things after having replaced the Python interpreter
139+
140+
_handle, bootstrapper_temp_path = tempfile.mkstemp(suffix=".py", text=True)
141+
142+
with open(bootstrapper_temp_path, "w") as f:
143+
f.write(BOOTSTRAPPER_TEMPLATE.format(environment_name, python_directory))
144+
145+
print(" Creating bootstrap script: {}".format(compas_bootstrapper))
146+
copy(bootstrapper_temp_path, compas_bootstrapper)
147+
148+
print()
149+
print("COMPAS for Blender {} has been installed.".format(version))
150+
151+
152+
# ==============================================================================
153+
# Main
154+
# ==============================================================================
155+
156+
if __name__ == "__main__":
157+
158+
import argparse
159+
160+
parser = argparse.ArgumentParser()
161+
162+
parser.add_argument(
163+
"blenderpath",
164+
nargs="?",
165+
help="The path to the folder with the version number of Blender.",
166+
)
167+
parser.add_argument(
168+
"-v",
169+
"--version",
170+
choices=["2.83", "2.93", "3.1"],
171+
help="The version of Blender to install COMPAS in.",
172+
)
173+
174+
args = parser.parse_args()
175+
176+
install_windows(args.blenderpath, version=args.version)

0 commit comments

Comments
 (0)