From dc2ad56d9967bf186c1e4d5b7a7bc191cb7d402a Mon Sep 17 00:00:00 2001 From: Maic Siemering Date: Mon, 18 Nov 2024 16:37:41 +0100 Subject: [PATCH] make pymunk imports optional, which allows to exclude it during packaging with cx_freeze --- arcade/__init__.py | 9 ++++++--- arcade/hitbox/__init__.py | 12 +++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/arcade/__init__.py b/arcade/__init__.py index a4c812d3e..7a13f3c33 100644 --- a/arcade/__init__.py +++ b/arcade/__init__.py @@ -199,9 +199,12 @@ def configure_logging(level: int | None = None): from .tilemap import read_tmx from .tilemap import TileMap -from .pymunk_physics_engine import PymunkPhysicsEngine -from .pymunk_physics_engine import PymunkPhysicsObject -from .pymunk_physics_engine import PymunkException +try: + from .pymunk_physics_engine import PymunkPhysicsEngine + from .pymunk_physics_engine import PymunkPhysicsObject + from .pymunk_physics_engine import PymunkException +except ImportError: + print("Pymunk is not installed. Pymunk physics engine will not be available.") from .version import VERSION diff --git a/arcade/hitbox/__init__.py b/arcade/hitbox/__init__.py index ab5d304dc..974d505e0 100644 --- a/arcade/hitbox/__init__.py +++ b/arcade/hitbox/__init__.py @@ -6,18 +6,23 @@ from .base import HitBox, HitBoxAlgorithm, RotatableHitBox from .bounding_box import BoundingHitBoxAlgorithm -from .pymunk import PymunkHitBoxAlgorithm from .simple import SimpleHitBoxAlgorithm #: The simple hit box algorithm. algo_simple = SimpleHitBoxAlgorithm() -#: The detailed hit box algorithm. -algo_detailed = PymunkHitBoxAlgorithm() #: The bounding box hit box algorithm. algo_bounding_box = BoundingHitBoxAlgorithm() #: The default hit box algorithm. algo_default = algo_simple +#: The detailed hit box algorithm, if pymunk available. +try: + from .pymunk import PymunkHitBoxAlgorithm + algo_detailed = PymunkHitBoxAlgorithm() +except ImportError: + algo_detailed = None + print("Warning: pymunk is not installed. PymunkHitBoxAlgorithm will not be available.") + # Temporary functions for backwards compatibility def calculate_hit_box_points_simple(image: Image, *args) -> Point2List: @@ -46,6 +51,7 @@ def calculate_hit_box_points_detailed( How detailed to make the hit box. There's a trade-off in number of points vs. accuracy. """ + assert algo_detailed, "PymunkHitBoxAlgorithm is not available." return algo_detailed.calculate(image, detail=hit_box_detail)