-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdependencies.py
More file actions
93 lines (71 loc) · 2.92 KB
/
dependencies.py
File metadata and controls
93 lines (71 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Dependency Management for BlenderPhotonics
* Authors: (c) 2021-2022 Qianqian Fang <q.fang at neu.edu>
* License: GNU General Public License V3 or later (GPLv3)
* Website: http://mcx.space/bp
This module handles the import and installation of external dependencies
for BlenderPhotonics, providing graceful fallbacks when packages are missing.
"""
import sys
import importlib
import bpy
# Global flags to track which dependencies are available
DEPENDENCIES = {
"jdata": False,
"numpy": False,
"iso2mesh": False,
"pmcx": False,
"pmmc": False,
}
# Error messages for missing dependencies
MISSING_MESSAGES = {
"jdata": "JData package is required for JSON/JMesh file operations",
"numpy": "NumPy package is required for numerical operations",
"iso2mesh": "iso2mesh package is required for mesh generation operations",
"pmcx": "pmcx package is required for Monte Carlo eXtreme simulations",
"pmmc": "pmmc package is required for Mesh-based Monte Carlo simulations",
}
def try_import(module_name):
"""Try to import a module and return True if successful, False otherwise."""
try:
importlib.import_module(module_name)
return True
except ImportError:
return False
def check_dependencies():
"""Check which dependencies are available and update the global flags."""
for dep in DEPENDENCIES:
DEPENDENCIES[dep] = try_import(dep)
return DEPENDENCIES
def get_missing_dependencies():
"""Return a list of missing dependencies."""
check_dependencies()
return [dep for dep, available in DEPENDENCIES.items() if not available]
def require_dependency(module_name, operation_name="this operation"):
"""Check if a dependency is available, show error if not."""
if not DEPENDENCIES.get(module_name, False):
message = f"{MISSING_MESSAGES.get(module_name, f'{module_name} is required')} for {operation_name}. Please install it using the buttons in the BlenderPhotonics panel."
show_error_message(message)
return False
return True
def show_error_message(message, title="Missing Dependency"):
"""Show an error message to the user."""
def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title=title, icon="ERROR")
def safe_import(module_name, fallback=None):
"""Safely import a module, returning fallback if import fails."""
try:
# Suppress specific warnings for pmmc binary extension issues
if module_name == "pmmc":
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message=".*pmmc binary extension.*")
return importlib.import_module(module_name)
else:
return importlib.import_module(module_name)
except ImportError:
if fallback is not None:
return fallback
return None
# Initialize dependency check
check_dependencies()