Skip to content

Commit 9107078

Browse files
committed
Add setup script setup the blender conda env. For Ubuntu
1 parent a4184b8 commit 9107078

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

setup_conda_blender.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Usage: sudo ./blender-conda.sh /usr/share/blender/4.1
4+
# ------------------------------------------------------------------------------
5+
6+
set -euo pipefail
7+
8+
if [[ $# -lt 1 ]]; then
9+
echo "Usage: $0 <BLENDER_ROOT>"
10+
exit 1
11+
fi
12+
13+
BLENDER_ROOT="$(realpath "$1")"
14+
PYTHON_DIR="$BLENDER_ROOT/python"
15+
16+
# ──────────────────────────────────────────────────────────────────────────────
17+
# 1. Locate Blender’s bundled Python binary, whatever it’s called
18+
# ──────────────────────────────────────────────────────────────────────────────
19+
BLENDER_PY_BIN="$(find "$PYTHON_DIR/bin" -maxdepth 1 -type f -executable -name 'python3*' | head -n1 || true)"
20+
21+
if [[ -z "$BLENDER_PY_BIN" ]]; then
22+
echo "❌ Cannot find Blender's bundled Python inside: $PYTHON_DIR/bin/" >&2
23+
exit 1
24+
fi
25+
26+
# Pull out the major.minor version (e.g. 3.11)
27+
BLENDER_PY_VERSION="$("$BLENDER_PY_BIN" - <<'PY'
28+
import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")
29+
PY
30+
)"
31+
echo "→ Blender uses Python $BLENDER_PY_VERSION"
32+
33+
# ──────────────────────────────────────────────────────────────────────────────
34+
# 2. Create / reuse matching Conda environment
35+
# ──────────────────────────────────────────────────────────────────────────────
36+
ENV_NAME="blender_env${BLENDER_PY_VERSION//./}" # e.g. blender_env311
37+
CONDA_HOME="$(conda info --base)"
38+
ENV_PATH="$CONDA_HOME/envs/$ENV_NAME"
39+
40+
if conda env list | grep -qE "^$ENV_NAME\s"; then
41+
echo "✓ Conda env '$ENV_NAME' already exists"
42+
else
43+
echo "→ Creating Conda env '$ENV_NAME' with python=$BLENDER_PY_VERSION"
44+
conda create -y -n "$ENV_NAME" python="$BLENDER_PY_VERSION"
45+
fi
46+
47+
# ──────────────────────────────────────────────────────────────────────────────
48+
# 3. Swap Blender’s python/ with a symlink to the env
49+
# ──────────────────────────────────────────────────────────────────────────────
50+
cd "$BLENDER_ROOT"
51+
52+
if [[ ! -L python ]]; then
53+
TS=$(date +%s)
54+
sudo mv python "python.bak.$TS"
55+
echo "→ Backed up original python/ to python.bak.$TS"
56+
fi
57+
58+
sudo ln -sfn "$ENV_PATH" python # -f/-n: replace if link already exists
59+
echo "✓ Linked $ENV_PATH$BLENDER_ROOT/python"
60+
61+
echo
62+
echo "All set! Launch Blender and run in the console:"
63+
echo " import sys, numpy, pandas, bpy"
64+
echo " print(sys.executable)"
65+
echo
66+
echo "to verify that it's using your new Conda environment."

0 commit comments

Comments
 (0)