diff --git a/.github/workflows/blender_extension.yml b/.github/workflows/blender_extension.yml new file mode 100644 index 000000000000..54ed7a7c0456 --- /dev/null +++ b/.github/workflows/blender_extension.yml @@ -0,0 +1,101 @@ +name: blender_extension + +# Builds the COMPAS Blender extension packages (one per Blender version range and platform) +# and uploads each package as its own, unzipped workflow artifact. +# Called by the release workflow, which attaches the packages to the GitHub release, +# and run on pull requests that touch the Blender integration. + +on: + workflow_call: + workflow_dispatch: + pull_request: + paths: + - "extensions/blender/**" + - "src/compas_blender/**" + - "requirements.txt" + - ".github/workflows/blender_extension.yml" + +env: + # Any Blender >= 4.2 can build packages for all supported Blender versions. + BLENDER_VERSION: "5.2.4" + +jobs: + build: + name: build (Blender ${{ matrix.blender }}, ${{ matrix.platform }}) + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - { blender: "4.2-5.0", python: "3.11", platform: windows-x64 } + - { blender: "4.2-5.0", python: "3.11", platform: macos-arm64 } + - { blender: "4.2-5.0", python: "3.11", platform: macos-x64 } + - { blender: "4.2-5.0", python: "3.11", platform: linux-x64 } + - { blender: "5.1+", python: "3.13", platform: windows-x64 } + - { blender: "5.1+", python: "3.13", platform: windows-arm64 } + - { blender: "5.1+", python: "3.13", platform: macos-arm64 } + - { blender: "5.1+", python: "3.13", platform: linux-x64 } + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install Blender runtime libraries + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + libx11-6 libxi6 libxxf86vm1 libxfixes3 libxrender1 libxkbcommon0 \ + libsm6 libice6 libgl1 libegl1 + + - name: Cache Blender + id: cache-blender + uses: actions/cache@v4 + with: + path: ~/blender + key: blender-${{ env.BLENDER_VERSION }}-linux-x64 + + - name: Download Blender + if: steps.cache-blender.outputs.cache-hit != 'true' + run: | + series="${BLENDER_VERSION%.*}" + curl -sSfL "https://download.blender.org/release/Blender${series}/blender-${BLENDER_VERSION}-linux-x64.tar.xz" | tar -xJ -C ~ + mv ~/blender-${BLENDER_VERSION}-linux-x64 ~/blender + + - name: Check Blender + run: ~/blender/blender --version + + - name: Build extension package + run: python extensions/blender/build.py --blender ~/blender/blender --python ${{ matrix.python }} --platform ${{ matrix.platform }} + + # Uploaded unzipped, so the artifact is the extension package itself (named after the file). + - uses: actions/upload-artifact@v7 + with: + path: dist/blender/*.zip + archive: false + if-no-files-found: error + retention-days: 14 + + # Downloads the packages the same way the release workflow does, to catch problems before a release. + check: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@v8 + with: + pattern: compas-*.zip + path: dist/blender + merge-multiple: true + skip-decompress: true + + - name: Check extension packages + run: | + ls -l dist/blender + count=0 + for package in dist/blender/*.zip; do + unzip -l "$package" | grep -q " blender_manifest.toml$" || { echo "::error::$package is not an extension package"; exit 1; } + count=$((count + 1)) + done + [ "$count" -eq 8 ] || { echo "::error::Expected 8 extension packages, found $count"; exit 1; } diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2e2893bb386e..3e1bbde3c547 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] + os: [ubuntu-24.04, macos-latest, windows-latest] # ubuntu-24.04 is the last image with Python 3.9 python: ["3.9", "3.10", "3.11", "3.12"] steps: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 57436e45b691..df0a4abff125 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,7 +10,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] + os: [ubuntu-24.04, macos-latest, windows-latest] # ubuntu-24.04 is the last image with Python 3.9 python: ["3.9", "3.10", "3.11", "3.12"] steps: @@ -33,3 +33,25 @@ jobs: gh_source: src/compas_ghpython/components gh_target: src/compas_ghpython/components/ghuser release_name_prefix: COMPAS + + blender_extension: + needs: build + uses: ./.github/workflows/blender_extension.yml + + attach_blender_extension: + needs: [publish, blender_extension] + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/download-artifact@v8 + with: + pattern: compas-*.zip + path: dist/blender + merge-multiple: true + skip-decompress: true + + - name: Attach Blender extension packages to the release + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: gh release upload "${{ github.ref_name }}" dist/blender/*.zip --repo "${{ github.repository }}" --clobber diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c24f03db714..c599b9265f86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added `TOL.update()` method for explicit global state modification. * Added `TOL.temporary()` context manager for scoped changes. * Added missing implementation of `Brep.to_polygons()` in `compas_rhino.geometry.RhinoBrep`. +* Added COMPAS as a Blender extension (Blender 4.2+), with all dependencies bundled as wheels. Build it with `invoke build-blender-extension`. +* Added `blender_extension` workflow that builds the Blender extension packages and attaches them to GitHub releases. ### Changed @@ -26,9 +28,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Changed `angle_vectors` to raise `ValueError` when one of the input vectors is a zero-length vector instead of returning 0. * Changed `transformation_to_rhino` and `transformation_matrix_to_rhino` to assign the matrix components directly to the fields of `Rhino.Geometry.Transform` instead of looping over its indexer. About 11x faster per conversion, noticable when used inside an animation loop. * Fixed the parameter names in the docstrings of `Shear.from_entries`, `Polyline.split_by_length` and `trimesh_gradient_numpy`. +* Fixed `compas_blender.redraw` raising `RuntimeError` when Blender runs in background mode. ### Removed +* Removed `compas_blender.install` and `compas_blender.print_python_path`. Install COMPAS in Blender as an extension instead. +* Removed `compas_blender.INSTALLABLE_PACKAGES`, `compas_blender.SUPPORTED_VERSIONS`, `compas_blender.DEFAULT_VERSION` and `compas_blender.INSTALLATION_ARGUMENTS`. +* Removed the `installable_blender_packages` and `after_blender_install` pluggables. + ## [2.15.0] 2025-11-12 diff --git a/docs/userguide/cad.blender.rst b/docs/userguide/cad.blender.rst index 1e0470f35454..b4f66e029d9e 100644 --- a/docs/userguide/cad.blender.rst +++ b/docs/userguide/cad.blender.rst @@ -10,109 +10,74 @@ and :mod:`compas_blender` provides functionality for converting COMPAS objects t .. note:: - These instructions are for the current LTS versions of Blender (3.3, 3.6 and 4.2). - Other versions are currently not supported. - -.. warning:: - - The installation procedure is a work in progress. - It has currently only been tested on macOS, and may change in the future. + COMPAS is distributed as a Blender extension for Blender 4.2 and newer. + Older versions of Blender are not supported. Installation ============ -The installation of COMPAS in Blender can be done in two ways: - -* by installing COMPAS in Blender's Python environment using ``pip``, or -* by using the ``compas_blender.install`` command. - -Using ``pip`` (recommended) ---------------------------- +COMPAS is packaged as a `Blender extension `_. +The extension is self-contained: it bundles COMPAS and its dependencies (except ``numpy``, which ships with Blender), +so there is no need to install anything into Blender's Python environment manually. -Blender comes with its own Python installation. -This procedure simply uses that Python installation and associated ``pip`` to install COMPAS. -The location of the executable is different on different platforms. -The default locations are: - -* Windows: ``C:\Program Files\Blender Foundation\Blender 4.2\4.2\python\bin\python.exe`` -* macOS: ``/Applications/Blender.app/Contents/Resources/4.2/python/bin/python3.10`` -* Linux: ``/usr/share/blender/4.2/python/bin/python3.10`` (i think :) - -.. note:: +Blender ships different versions of Python, and some dependencies contain compiled code, +so there is one extension package per Blender version range and operating system: - If you already have an installation of COMPAS on your system, you can try finding the Blender Python executable by running the following in a terminal or command prompt: +========================= ======================================================= +Blender Extension package +========================= ======================================================= +4.2 to 5.0 ``compas--blender4.2-.zip`` +5.1 and newer ``compas--blender5.1-.zip`` +========================= ======================================================= - .. code-block:: python +where ```` is one of ``windows_x64``, ``windows_arm64`` (Blender 5.1+ only), +``macos_arm64``, ``macos_x64`` (Blender 4.2 to 5.0 only) or ``linux_x64``. - python -m compas_blender.print_python_path +Install from disk +----------------- -Update `pip` -~~~~~~~~~~~~ +1. Download the extension package for your Blender version and operating system from the + `COMPAS releases `_. +2. In Blender, open *Edit > Preferences > Get Extensions*. +3. In the drop-down menu in the top right corner, select *Install from Disk...* and pick the downloaded ``.zip`` file. -Before installing `compas` with `pip`, it is highly recommended that you update `pip` itself. +Alternatively, drag and drop the ``.zip`` file onto the Blender window, +or install it from the command line: .. code-block:: bash - $ /Applications/Blender.app/Contents/Resources/4.2/python/bin/python3.10 -m pip install --upgrade pip + $ blender --command extension install-file -r user_default -e compas--blender5.1-macos_arm64.zip +Once the extension is enabled, ``compas`` and ``compas_blender`` can be imported in the Python console, +in scripts in the Text Editor, and from other add-ons. +To update COMPAS, install the package of the newer version in the same way. +To uninstall it, remove the extension from the *Get Extensions* panel. -Install from PyPI -~~~~~~~~~~~~~~~~~ +Build from source +----------------- -For example on Mac: - -.. code-block:: bash - - $ /Applications/Blender.app/Contents/Resources/4.2/python/bin/python3.10 -m pip install compas - - -Install from Source -~~~~~~~~~~~~~~~~~~~ +To build the extension packages from a checkout of the COMPAS repository, +you need a Python environment with ``pip`` and a Blender executable (4.2 or newer): .. code-block:: bash $ cd path/to/compas - $ /Applications/Blender.app/Contents/Resources/4.2/python/bin/python3.10 -m pip install -e . - - -Using ``compas_blender.install`` --------------------------------- - -.. warning:: - - This procedure will not install any of the dependencies required by COMPAS. - They have to already be present in the Blender Python environment. - -The ``compas_blender.install`` command installs COMPAS in Blender's Python ``site-packages`` folder -by creating symlinks to COMPAS packages installed in an existing (``conda``) environment. -Therefore, before running the command below, you should activate the environment in which you have installed COMPAS. - -.. code-block:: bash - - $ python -m compas_blender.install - -Optionally, you can specify the version of Blender using the ``-v`` flag. - -.. code-block:: bash - - $ python -m compas_blender.install -v 4.2 - -To remove all previously installed symlinks before installing new ones, use the ``--clean`` flag. - -.. code-block:: bash + $ invoke build-blender-extension --blender /Applications/Blender.app/Contents/MacOS/Blender - $ python -m compas_blender.install --clean +This builds a ``compas`` wheel from the source, downloads the wheels of all dependencies for every supported +platform and Python version, and writes the extension packages to ``dist/blender``. +See ``extensions/blender/build.py`` for more options. Verification ============ -To test if the installation was successful, you can run the following on the Blender command line. +To test if the installation was successful, you can run the following on the Blender Python console. >>> import compas >>> print(compas.__version__) -'2.4.0' +'2.15.0' Visualisation diff --git a/extensions/blender/README.md b/extensions/blender/README.md new file mode 100644 index 000000000000..1ca999625f14 --- /dev/null +++ b/extensions/blender/README.md @@ -0,0 +1,14 @@ +# COMPAS Blender extension + +Source of the [Blender extension](https://docs.blender.org/manual/en/latest/advanced/extensions/getting_started.html) that distributes COMPAS for Blender 4.2 and newer. + +* `addon/` contains the extension source: the add-on `__init__.py` and the source `blender_manifest.toml`. +* `build.py` builds the extension packages into `dist/blender`, one per Blender version range and platform, + bundling the `compas` wheel and all its dependencies (except `numpy`, which ships with Blender). + +```bash +invoke build-blender-extension --blender /path/to/blender +``` + +The manifest `version` is kept in sync by `bump-my-version`; `blender_version_max`, `platforms` and `wheels` +are generated by `build.py` for each build. diff --git a/extensions/blender/addon/__init__.py b/extensions/blender/addon/__init__.py new file mode 100644 index 000000000000..5b747be2f75e --- /dev/null +++ b/extensions/blender/addon/__init__.py @@ -0,0 +1,41 @@ +# SPDX-License-Identifier: MIT +"""COMPAS for Blender. + +This extension only bundles COMPAS and its dependencies as Python wheels. +Blender installs them into the extensions site-packages folder, so once the +extension is enabled, ``compas`` and ``compas_blender`` can be imported from +any script, the Python console, or other add-ons. +""" + +import os + +import bpy + + +class COMPAS_AddonPreferences(bpy.types.AddonPreferences): + bl_idname = __package__ + + def draw(self, context): + layout = self.layout + try: + import compas + except ImportError as error: + layout.label(text="COMPAS could not be imported: {}".format(error), icon="ERROR") + return + + column = layout.column(align=True) + column.label(text="COMPAS {}".format(compas.__version__), icon="CHECKMARK") + column.label(text=os.path.dirname(os.path.dirname(compas.__file__))) + + +classes = (COMPAS_AddonPreferences,) + + +def register(): + for cls in classes: + bpy.utils.register_class(cls) + + +def unregister(): + for cls in reversed(classes): + bpy.utils.unregister_class(cls) diff --git a/extensions/blender/addon/blender_manifest.toml b/extensions/blender/addon/blender_manifest.toml new file mode 100644 index 000000000000..51904f7acee3 --- /dev/null +++ b/extensions/blender/addon/blender_manifest.toml @@ -0,0 +1,33 @@ +schema_version = "1.0.0" + +# COMPAS as a Blender extension. +# +# This is the source manifest. `extensions/blender/build.py` copies it into a staging folder +# and fills in the build-specific values (`blender_version_min`, `blender_version_max`, +# `platforms` and `wheels`) for each supported Blender Python version before running +# `blender --command extension build`. +# +# https://docs.blender.org/manual/en/latest/advanced/extensions/getting_started.html + +id = "compas" +version = "2.15.0" +name = "COMPAS" +tagline = "Computational framework for research and collaboration in AEC" +maintainer = "COMPAS Association" +type = "add-on" + +website = "https://compas.dev" + +tags = ["Development", "Modeling", "Mesh"] + +blender_version_min = "4.2.0" + +# The COMPAS extension is distributed outside the Blender Extensions Platform, +# which requires add-ons to be GPL-3.0-or-later. Switching to the platform only +# requires changing this to "SPDX:GPL-3.0-or-later"; the bundled `compas` wheel stays MIT. +license = [ + "SPDX:MIT", +] +copyright = [ + "2017-2026 COMPAS Association", +] diff --git a/extensions/blender/build.py b/extensions/blender/build.py new file mode 100644 index 000000000000..9b333f552a7b --- /dev/null +++ b/extensions/blender/build.py @@ -0,0 +1,225 @@ +"""Build the COMPAS Blender extension. + +Blender extensions must be self-contained: all third-party Python packages are +bundled as wheels inside the extension ``.zip``. Blender LTS releases ship different +Python versions, so one set of packages is built per Python version, each one +limited to the Blender versions that use that Python version, and split per platform. + +Usage:: + + python extensions/blender/build.py + python extensions/blender/build.py --blender /path/to/blender --python 3.13 + +The resulting ``.zip`` files are written to ``dist/blender`` and can be installed +in Blender via *Edit > Preferences > Get Extensions > Install from Disk...*. + +Requires ``pip`` in the current environment and a Blender executable (4.2 or newer) +to run ``blender --command extension build``. +""" + +import argparse +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parent.parent +ADDON_DIR = HERE / "addon" +REQUIREMENTS = ROOT / "requirements.txt" +DEFAULT_OUTPUT = ROOT / "dist" / "blender" + +# Packages that Blender already ships with its own Python and therefore must not be bundled. +PROVIDED_BY_BLENDER = {"numpy"} + +LINUX_X64 = ["manylinux_2_28_x86_64", "manylinux_2_17_x86_64", "manylinux2014_x86_64"] + +# One build per Python version used by Blender. +# ``blender_max`` is exclusive (the first version that is *not* supported). +# Platform tags are passed to ``pip download --platform``; pip expands macOS tags to +# older macOS versions automatically, but manylinux tags have to be listed explicitly. +TARGETS = { + "3.11": { + "blender_min": "4.2.0", + "blender_max": "5.1.0", + "platforms": { + "windows-x64": ["win_amd64"], + "macos-arm64": ["macosx_12_0_arm64"], + "macos-x64": ["macosx_11_0_x86_64"], + "linux-x64": LINUX_X64, + }, + }, + "3.13": { + "blender_min": "5.1.0", + "blender_max": None, + "platforms": { + "windows-x64": ["win_amd64"], + "windows-arm64": ["win_arm64"], + "macos-arm64": ["macosx_12_0_arm64"], + "linux-x64": LINUX_X64, + }, + }, +} + +ALL_PLATFORMS = sorted({platform for target in TARGETS.values() for platform in target["platforms"]}) + + +def log(message): + print(message, flush=True) + + +def run(cmd, **kwargs): + log(" $ " + " ".join(str(c) for c in cmd)) + return subprocess.run([str(c) for c in cmd], check=True, **kwargs) + + +def find_blender(explicit=None): + candidates = [explicit, os.environ.get("BLENDER"), shutil.which("blender")] + if sys.platform == "darwin": + candidates.append("/Applications/Blender.app/Contents/MacOS/Blender") + for candidate in candidates: + if candidate and Path(candidate).is_file(): + return candidate + raise SystemExit("Blender executable not found. Pass --blender or set the BLENDER environment variable.") + + +def compas_version(): + content = (ROOT / "src" / "compas" / "__init__.py").read_text(encoding="utf-8") + return re.search(r'^__version__ = "([^"]+)"', content, re.M).group(1) + + +def distribution_name(wheel): + return wheel.name.split("-")[0].lower().replace("_", "-") + + +def build_compas_wheel(dest, version, from_pypi): + dest.mkdir(parents=True, exist_ok=True) + if from_pypi: + run([sys.executable, "-m", "pip", "download", "compas=={}".format(version), "--no-deps", "--only-binary=:all:", "--dest", dest, "--quiet"]) + else: + run([sys.executable, "-m", "pip", "wheel", ROOT, "--no-deps", "--wheel-dir", dest, "--quiet"]) + wheels = list(dest.glob("compas-*.whl")) + if len(wheels) != 1: + raise SystemExit("Expected exactly one compas wheel in {}, found: {}".format(dest, wheels)) + return wheels[0] + + +def download_dependencies(dest, python_version, platform_tags): + cmd = [sys.executable, "-m", "pip", "download", "-r", REQUIREMENTS, "--only-binary=:all:"] + cmd += ["--python-version", python_version, "--implementation", "cp", "--dest", dest, "--quiet"] + for tag in platform_tags: + cmd += ["--platform", tag] + try: + run(cmd) + except subprocess.CalledProcessError: + return False + return True + + +def write_manifest(source, target, version, blender_min, blender_max, platforms, wheels): + content = source.read_text(encoding="utf-8") + content = re.sub(r'^version = ".*"$', 'version = "{}"'.format(version), content, flags=re.M) + content = re.sub(r'^blender_version_min = ".*"$', 'blender_version_min = "{}"'.format(blender_min), content, flags=re.M) + + lines = ["", "# Generated by extensions/blender/build.py"] + if blender_max: + lines.append('blender_version_max = "{}"'.format(blender_max)) + lines.append("platforms = [{}]".format(", ".join('"{}"'.format(p) for p in platforms))) + lines.append("wheels = [") + lines += [' "./wheels/{}",'.format(wheel.name) for wheel in wheels] + lines.append("]") + target.write_text(content.rstrip() + "\n" + "\n".join(lines) + "\n", encoding="utf-8") + + +def build_target(blender, python_version, target, compas_wheel, version, output_dir, work_dir, only_platforms=None): + tag = "py" + python_version.replace(".", "") + blender_min_short = ".".join(target["blender_min"].split(".")[:2]) + log("\nBuilding for Python {} (Blender {}{})".format(python_version, target["blender_min"], " to < " + target["blender_max"] if target["blender_max"] else "+")) + + staging = work_dir / tag / "compas" + if staging.exists(): + shutil.rmtree(staging) + wheels_dir = staging / "wheels" + wheels_dir.mkdir(parents=True) + + shutil.copy2(ADDON_DIR / "__init__.py", staging / "__init__.py") + shutil.copy2(compas_wheel, wheels_dir / compas_wheel.name) + + platforms = [] + for platform, platform_tags in target["platforms"].items(): + if only_platforms and platform not in only_platforms: + continue + log("- {}".format(platform)) + if download_dependencies(wheels_dir, python_version, platform_tags): + platforms.append(platform) + else: + log(" WARNING: dependencies not available for {} on Python {}, skipping this platform.".format(platform, python_version)) + + if not platforms: + raise SystemExit("No platform could be built for Python {}.".format(python_version)) + + for wheel in list(wheels_dir.glob("*.whl")): + if distribution_name(wheel) in PROVIDED_BY_BLENDER: + wheel.unlink() + + wheels = sorted(wheels_dir.glob("*.whl")) + write_manifest(ADDON_DIR / "blender_manifest.toml", staging / "blender_manifest.toml", version, target["blender_min"], target["blender_max"], platforms, wheels) + + build_output = work_dir / tag / "out" + if build_output.exists(): + shutil.rmtree(build_output) + build_output.mkdir(parents=True) + run([blender, "--factory-startup", "--command", "extension", "build", "--split-platforms", "--source-dir", staging, "--output-dir", build_output]) + + results = [] + for package in sorted(build_output.glob("*.zip")): + # compas-2.15.0-macos_arm64.zip -> compas-2.15.0-blender4.2-macos_arm64.zip + name = package.name.replace("compas-{}-".format(version), "compas-{}-blender{}-".format(version, blender_min_short)) + target_path = output_dir / name + shutil.move(str(package), str(target_path)) + results.append(target_path) + return results + + +def main(): + parser = argparse.ArgumentParser(description="Build the COMPAS Blender extension.") + parser.add_argument("--blender", help="Path to the Blender executable (defaults to $BLENDER, then PATH).") + parser.add_argument("--python", choices=sorted(TARGETS), action="append", help="Only build for these Blender Python versions (default: all).") + parser.add_argument("--platform", choices=ALL_PLATFORMS, action="append", help="Only build for these platforms (default: all).") + parser.add_argument("--output-dir", default=str(DEFAULT_OUTPUT), help="Output folder for the extension packages.") + parser.add_argument("--from-pypi", action="store_true", help="Bundle the released compas wheel from PyPI instead of building it from this checkout.") + args = parser.parse_args() + + blender = find_blender(args.blender) + version = compas_version() + output_dir = Path(args.output_dir).resolve() + work_dir = output_dir / "build" + output_dir.mkdir(parents=True, exist_ok=True) + for old in output_dir.glob("compas-*.zip"): + old.unlink() + + log("Building COMPAS {} wheel".format(version)) + compas_wheel = build_compas_wheel(work_dir / "compas_wheel", version, args.from_pypi) + + packages = [] + for python_version in args.python or sorted(TARGETS): + target = TARGETS[python_version] + if args.platform and not set(args.platform) & set(target["platforms"]): + log("\nSkipping Python {}: none of {} supported.".format(python_version, ", ".join(args.platform))) + continue + packages += build_target(blender, python_version, target, compas_wheel, version, output_dir, work_dir, args.platform) + + if not packages: + raise SystemExit("Nothing was built.") + + shutil.rmtree(work_dir, ignore_errors=True) + + log("\nExtension packages:") + for package in packages: + log(" {} ({:.1f} MB)".format(package, package.stat().st_size / 1e6)) + + +if __name__ == "__main__": + main() diff --git a/pyproject.toml b/pyproject.toml index ae06d4dc998b..9af4a026238b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -100,6 +100,11 @@ filename = "src/compas_blender/__init__.py" search = "{current_version}" replace = "{new_version}" +[[tool.bumpversion.files]] +filename = "extensions/blender/addon/blender_manifest.toml" +search = 'version = "{current_version}"' +replace = 'version = "{new_version}"' + [[tool.bumpversion.files]] filename = "src/compas_ghpython/__init__.py" search = "{current_version}" diff --git a/src/compas_blender/__init__.py b/src/compas_blender/__init__.py index 935d6abe545b..b82018cb1b96 100644 --- a/src/compas_blender/__init__.py +++ b/src/compas_blender/__init__.py @@ -1,7 +1,4 @@ # type: ignore -import io -import os -import compas try: import bpy @@ -14,24 +11,13 @@ __version__ = "2.15.0" -INSTALLABLE_PACKAGES = ["compas", "compas_blender"] -SUPPORTED_VERSIONS = ["3.3", "3.6", "4.2"] -DEFAULT_VERSION = "4.2" - -INSTALLATION_ARGUMENTS = None - - __all__ = [ - "INSTALLABLE_PACKAGES", - "SUPPORTED_VERSIONS", - "DEFAULT_VERSION", "clear", "redraw", ] __all_plugins__ = [ "compas_blender.geometry.booleans", - "compas_blender.install", "compas_blender.scene", ] @@ -70,183 +56,6 @@ def clear(guids=None): def redraw(): """Trigger a redraw.""" - bpy.ops.wm.redraw_timer(type="DRAW_WIN_SWAP", iterations=1) - - -def _check_blender_version(version): - supported_versions = SUPPORTED_VERSIONS - - if not version: - return DEFAULT_VERSION - - if version not in supported_versions: - raise Exception("Unsupported Blender version: {}".format(version)) - - return version - - -def _get_package_path(package): - return os.path.abspath(os.path.dirname(package.__file__)) - - -# ============================================================================= -# ============================================================================= -# ============================================================================= -# Bootstrapper -# ============================================================================= -# ============================================================================= -# ============================================================================= - - -def _get_bootstrapper_path(install_path): - return os.path.join(install_path, "compas_bootstrapper.py") - - -def _get_bootstrapper_data(compas_bootstrapper): - data = {} - - if not os.path.exists(compas_bootstrapper): - return data - - content = io.open(compas_bootstrapper, encoding="utf8").read() - exec(content, data) - - return data - - -def _try_remove_bootstrapper(path): - """Try to remove bootstrapper. - - Returns - ------- - bool: True if the operation did not cause errors, False otherwise. - """ - - bootstrapper = _get_bootstrapper_path(path) - - if os.path.exists(bootstrapper): - try: - os.remove(bootstrapper) - return True - except: # noqa: E722 - return False - return True - - -# ============================================================================= -# ============================================================================= -# ============================================================================= -# Blender executable -# ============================================================================= -# ============================================================================= -# ============================================================================= - - -def _get_default_blender_executable_path(version): - version = _check_blender_version(version) - - if compas.OSX: - path = _get_default_blender_executable_path_mac(version) - elif compas.WINDOWS: - path = _get_default_blender_executable_path_windows(version) - elif compas.LINUX: - path = _get_default_blender_executable_path_linux(version) - else: - raise Exception("Unsupported platform.") - - if not os.path.exists(path): - raise Exception("The default installation folder for Blender doesn't exist.") - - return path - - -def _get_default_blender_executable_path_mac(version): - return "/Applications/Blender.app/Contents/MacOS/Blender" - - -def _get_default_blender_executable_path_windows(version): - return "C:\\Program Files\\Blender Foundation\\Blender {}".format(version) - - -def _get_default_blender_executable_path_linux(version): - raise NotImplementedError - - -# ============================================================================= -# ============================================================================= -# ============================================================================= -# Blender Python -# ============================================================================= -# ============================================================================= -# ============================================================================= - - -def _get_default_blender_python_path(version): - version = _check_blender_version(version) - - if compas.OSX: - path = _get_default_blender_python_path_mac(version) - elif compas.WINDOWS: - path = _get_default_blender_python_path_windows(version) - elif compas.LINUX: - path = _get_default_blender_python_path_linux(version) - else: - raise Exception("Unsupported platform.") - - if not os.path.exists(path): - raise Exception("The default installation folder for Blender {} doesn't exist.".format(version)) - - return path - - -def _get_default_blender_python_path_mac(version): - if version == "4.2": - return "/Applications/Blender.app/Contents/Resources/{}/python/bin/python3.11".format(version) - return "/Applications/Blender.app/Contents/Resources/{}/python/bin/python3.10".format(version) - - -def _get_default_blender_python_path_windows(version): - return "C:\\Program Files\\Blender Foundation\\Blender {}\\{}\\python\\bin\\python.exe".format(version, version) - - -def _get_default_blender_python_path_linux(version): - raise NotImplementedError - - -# ============================================================================= -# ============================================================================= -# ============================================================================= -# Blender Python site-packages -# ============================================================================= -# ============================================================================= -# ============================================================================= - - -def _get_default_blender_sitepackages_path(version): - version = _check_blender_version(version) - - if compas.OSX: - path = _get_default_blender_sitepackages_path_mac(version) - elif compas.WINDOWS: - path = _get_default_blender_sitepackages_path_windows(version) - elif compas.LINUX: - path = _get_default_blender_sitepackages_path_linux(version) - else: - raise Exception("Unsupported platform.") - - if not os.path.exists(path): - raise Exception("The default installation folder for Blender {} doesn't exist.".format(version)) - - return path - - -def _get_default_blender_sitepackages_path_mac(version): - return "/Applications/Blender.app/Contents/Resources/{}/python/lib/python3.10/site-packages".format(version) - - -def _get_default_blender_sitepackages_path_windows(version): - return "C:\\Program Files\\Blender Foundation\\Blender {}\\{}\\python\\lib\\site-packages".format(version, version) - - -def _get_default_blender_sitepackages_path_linux(version): - raise NotImplementedError + # There is no window to redraw when Blender runs in background mode. + if bpy.ops.wm.redraw_timer.poll(): + bpy.ops.wm.redraw_timer(type="DRAW_WIN_SWAP", iterations=1) diff --git a/src/compas_blender/install.py b/src/compas_blender/install.py deleted file mode 100644 index 359abc6206c8..000000000000 --- a/src/compas_blender/install.py +++ /dev/null @@ -1,338 +0,0 @@ -import importlib -import itertools -import os -import sys - -import compas._os -import compas.plugins -import compas_blender - -INSTALLED_VERSION = None - - -def install(version=None, packages=None, clean=False): - """Install COMPAS for Blender. - - Parameters - ---------- - version : str, optional - The version number of Blender. - Default is ``'4.0'``. - packages : list of str, optional - List of packages to install or None to use default package list. - Default is the result of ``installable_blender_packages``, - which collects all installable packages in the current environment. - clean : bool, optional - If True, this will clean up the site packages folder and remove - also existing symlinks that are not importable in the current environment. - - Examples - -------- - .. code-block:: python - - import compas_blender.install - - compas_blender.install.install() - - .. code-block:: bash - - python -m compas_blender.install - - """ - version = compas_blender._check_blender_version(version) - - # We install COMPAS packages in the site packages folder - sitepackages_path = compas_blender._get_default_blender_sitepackages_path(version) - - # Filter the provided list of packages - # If no packages are provided - # this first collects all installable packages from the environment. - packages = _filter_installable_packages(version, packages) - - results = [] - symlinks_to_install = [] - symlinks_to_uninstall = [] - exit_code = 0 - - # check all installable packages - # add the packages that can't be imported from the current env to the list of symlinks to uninstall - # and remove the package name from the list of installable packages - # make a copy of the list to avoid problems with removing items - # note: perhaps this should already happen in the filter function... - for name in packages[:]: - try: - importlib.import_module(name) - except ImportError: - path = os.path.join(sitepackages_path, name) - symlinks_to_uninstall.append(dict(name=name, link=path)) - packages.remove(name) - - # Also remove all broken symlinks from the scripts folder - # because ... they're broken! - # If it is an actual folder or a file, leave it alone - # because probably someone put it there on purpose. - for name in os.listdir(sitepackages_path): - path = os.path.join(sitepackages_path, name) - if os.path.islink(path): - if not os.path.exists(path): - symlinks_to_uninstall.append(dict(name=name, link=path)) - try: - importlib.import_module(name) - except ImportError: - pass - else: - if name not in packages: - packages.append(name) - - # If the scripts folder is supposed to be cleaned - # also remove all existing symlinks that cannot be imported - # and reinstall symlinks that can be imported - if clean: - for name in os.listdir(sitepackages_path): - path = os.path.join(sitepackages_path, name) - if os.path.islink(path): - if os.path.exists(path): - try: - importlib.import_module(name) - except ImportError: - path = os.path.join(sitepackages_path, name) - symlinks_to_uninstall.append(dict(name=name, link=path)) - else: - if name not in packages: - packages.append(name) - - # add all of the packages in the list of installable packages - # to the list of symlinks to uninstall - # and to the list of symlinks to install - for package in packages: - symlink_path = os.path.join(sitepackages_path, package) - symlinks_to_uninstall.append(dict(name=package, link=symlink_path)) - - package_path = compas_blender._get_package_path(importlib.import_module(package)) - symlinks_to_install.append(dict(name=package, source_path=package_path, link=symlink_path)) - - # ------------------------- - # Uninstall first - # ------------------------- - - symlinks = [link["link"] for link in symlinks_to_uninstall] - uninstall_results = compas._os.remove_symlinks(symlinks) - - # Let the user know if some symlinks could not be removed. - for uninstall_data, success in zip(symlinks_to_uninstall, uninstall_results): - if not success: - results.append( - ( - uninstall_data["name"], - "ERROR: Cannot remove symlink, try to run as administrator.", - ) - ) - - # ------------------------- - # Ready to start installing - # ------------------------- - - # create new symlinks and register the results - symlinks = [(link["source_path"], link["link"]) for link in symlinks_to_install] - install_results = compas._os.create_symlinks(symlinks) - - # set the exit code based on the installation results - if not all(install_results): - exit_code = -1 - - # make a list of installed packages - # based on the installation results - # and update the general results list - installed_packages = [] - for install_data, success in zip(symlinks_to_install, install_results): - if success: - installed_packages.append(install_data["name"]) - result = "OK" - else: - result = "ERROR: Cannot create symlink, try to run as administrator." - results.append((install_data["name"], result)) - - # finalize the general results list with info about the bootstrapper - if exit_code == -1: - results.append( - ( - "compas_bootstrapper", - "WARNING: One or more packages failed, will not install bootstrapper, try uninstalling first", - ) - ) - else: - try: - _update_bootstrapper(sitepackages_path, packages) - results.append(("compas_bootstrapper", "OK")) - except: # noqa: E722 - results.append( - ( - "compas_bootstrapper", - "ERROR: Could not create compas_bootstrapper to auto-determine Python environment", - ) - ) - - # output the outcome of the installation process - # perhaps we should more info here - print("\nInstalling COMPAS packages to Blender {0} site-packages folder:".format(version)) - print("{}\n".format(sitepackages_path)) - - for package, status in results: - print(" {} {}".format(package.ljust(20), status)) - if status != "OK": - exit_code = -1 - - if exit_code == 0 and len(installed_packages): - print("\nRunning post-installation steps...\n") - if not _run_post_execution_steps(after_blender_install(installed_packages)): - exit_code = -1 - - print("\nInstall completed.") - if exit_code != 0: - sys.exit(exit_code) - - global INSTALLED_VERSION - INSTALLED_VERSION = version - - -def _run_post_execution_steps(steps_generator): - all_steps_succeeded = True - post_execution_errors = [] - - for result in steps_generator: - if isinstance(result, Exception): - post_execution_errors.append(result) - continue - - for item in result: - try: - package, message, success = item - status = "OK" if success else "ERROR" - if not success: - all_steps_succeeded = False - print(" {} {}: {}".format(package.ljust(20), status, message)) - except ValueError: - post_execution_errors.append(ValueError("Step ran without errors but result is wrongly formatted: {}".format(str(item)))) - - if post_execution_errors: - print("\nOne or more errors occurred:\n") - for error in post_execution_errors: - print(" - {}".format(repr(error))) - - all_steps_succeeded = False - - return all_steps_succeeded - - -@compas.plugins.plugin(category="install", pluggable_name="installable_blender_packages", tryfirst=True) -def default_installable_blender_packages(): - # While this list could obviously be hard-coded, I think - # eating our own dogfood and using plugins to define this, just like - # any other extension/plugin would be is a better way to ensure consistent behavior. - return compas_blender.INSTALLABLE_PACKAGES - - -@compas.plugins.pluggable(category="install", selector="collect_all") -def installable_blender_packages(): - """Provide a list of packages to make available inside Blender. - - Extensions providing Blender features - can implement this pluggable interface to automatically - have their packages made available inside Blender when - COMPAS is installed into it. - - Examples - -------- - >>> import compas.plugins - >>> @compas.plugins.plugin(category="install") - ... def installable_blender_packages(): - ... return ["compas_fab"] - - Returns - ------- - :obj:`list` of :obj:`str` - List of package names to make available inside Blender. - """ - pass - - -@compas.plugins.pluggable(category="install", selector="collect_all") -def after_blender_install(installed_packages): - """Allows extensions to execute actions after install to Blender is done. - - Extensions providing Blender features - can implement this pluggable interface to perform - additional steps after an installation to Blender has - been completed. - - Parameters - ---------- - installed_packages : :obj:`list` of :obj:`str` - List of packages that have been installed successfully. - - Examples - -------- - >>> import compas.plugins - >>> @compas.plugins.plugin(category="install") - ... def after_blender_install(installed_packages): - ... # Do something after package is installed to Blender, e.g. install the COMPAS UI, etc - ... return [("compas_blender", "COMPAS Blender UI installed", True)] - - Returns - ------- - :obj:`list` of 3-tuple (str, str, bool) - List containing a 3-tuple with component name, message and True/False success flag. - - """ - pass - - -def _update_bootstrapper(install_path, packages): - # Take either the CONDA environment directory or the current Python executable's directory - python_directory = os.environ.get("CONDA_PREFIX", None) or os.path.dirname(sys.executable) - environment_name = os.environ.get("CONDA_DEFAULT_ENV", "") - conda_exe = os.environ.get("CONDA_EXE", "") - - compas_bootstrapper = compas_blender._get_bootstrapper_path(install_path) - - bootstrapper_data = compas_blender._get_bootstrapper_data(compas_bootstrapper) - installed_packages = bootstrapper_data.get("INSTALLED_PACKAGES", []) - installed_packages = list(set(installed_packages + list(packages))) - - with open(compas_bootstrapper, "w") as f: - f.write('ENVIRONMENT_NAME = r"{}"\n'.format(environment_name)) - f.write('PYTHON_DIRECTORY = r"{}"\n'.format(python_directory)) - f.write('CONDA_EXE = r"{}"\n'.format(conda_exe)) - f.write("INSTALLED_PACKAGES = {}".format(repr(installed_packages))) - - -def _filter_installable_packages(version, packages): - if not packages: - # Flatten list of results (resulting from collect_all pluggable) - packages = sorted(set(itertools.chain.from_iterable(installable_blender_packages()))) # type: ignore - return packages - - -# ============================================================================== -# Main -# ============================================================================== - -if __name__ == "__main__": - import argparse - - parser = argparse.ArgumentParser() - - parser.add_argument( - "-v", - "--version", - choices=compas_blender.SUPPORTED_VERSIONS, - default=compas_blender.DEFAULT_VERSION, - help="The version of Blender to install the packages in.", - ) - parser.add_argument("-p", "--packages", nargs="+", help="The packages to install.") - parser.add_argument("-c", "--clean", default=False, action="store_true", help="Clean up the installation directory") - - args = parser.parse_args() - compas_blender.INSTALLATION_ARGUMENTS = args - - install(version=args.version, packages=args.packages, clean=args.clean) diff --git a/src/compas_blender/print_python_path.py b/src/compas_blender/print_python_path.py deleted file mode 100644 index eee9622e0093..000000000000 --- a/src/compas_blender/print_python_path.py +++ /dev/null @@ -1,18 +0,0 @@ -import compas_blender - -if __name__ == "__main__": - import argparse - - parser = argparse.ArgumentParser() - - parser.add_argument( - "-v", - "--version", - choices=compas_blender.SUPPORTED_VERSIONS, - default=compas_blender.DEFAULT_VERSION, - help="The version of Blender.", - ) - - args = parser.parse_args() - - print(compas_blender._get_default_blender_python_path(args.version)) diff --git a/tasks.py b/tasks.py index 7286febdb086..bdc5385cc3d2 100644 --- a/tasks.py +++ b/tasks.py @@ -8,6 +8,24 @@ from compas_invocations2 import tests from compas_invocations2 import grasshopper from invoke.collection import Collection +from invoke.tasks import task + + +@task( + help={ + "blender": "Path to the Blender executable (defaults to $BLENDER, then PATH).", + "from_pypi": "Bundle the released compas wheel from PyPI instead of building it from this checkout.", + } +) +def build_blender_extension(ctx, blender=None, from_pypi=False): + """Build the COMPAS Blender extension packages into dist/blender.""" + cmd = "python extensions/blender/build.py" + if blender: + cmd += ' --blender "{}"'.format(blender) + if from_pypi: + cmd += " --from-pypi" + ctx.run(cmd, pty=False) + ns = Collection( docs.help, @@ -24,6 +42,7 @@ build.release, build.build_ghuser_components, build.build_cpython_ghuser_components, + build_blender_extension, grasshopper.yakerize, grasshopper.publish_yak, grasshopper.update_gh_header,