-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmodule_finder.py
More file actions
70 lines (57 loc) · 3.08 KB
/
bmodule_finder.py
File metadata and controls
70 lines (57 loc) · 3.08 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
# This tiny lib is for importing any addon module from other addons,
# since with the introduction of extension repositories, the import
# path is not the same as in the past.
import bpy
import os
def find_module(module_name: str):
"""Find BlenderKit module in extension repositories and return its import path."""
# print(f"DEBUG: Searching for module: {module_name}")
try:
import bl_ext
# print(f"DEBUG: Successfully imported bl_ext")
# Look through all modules in bl_ext
bl_ext_modules = dir(bl_ext)
# print(f"DEBUG: Found {len(bl_ext_modules)} modules in bl_ext: {bl_ext_modules}")
for ext_module_name in bl_ext_modules:
if ext_module_name.startswith("__"): # Skip internal modules
continue
# print(f"DEBUG: Checking extension module: {ext_module_name}")
try:
# Get the module
module = getattr(bl_ext, ext_module_name)
# print(f"DEBUG: Got module object: {module}")
# List all attributes in the module
module_attrs = dir(module)
# print(f"DEBUG: Module {ext_module_name} has attributes: {[attr for attr in module_attrs if not attr.startswith('__')]}")
# Check if this module contains our target module
if hasattr(module, module_name):
# print(f"DEBUG: Found {module_name} in bl_ext.{ext_module_name}")
return f"bl_ext.{ext_module_name}.{module_name}"
# Also check if any submodule might contain it
for attr in module_attrs:
if not attr.startswith("__"):
try:
submodule = getattr(module, attr)
if hasattr(submodule, module_name):
# print(f"DEBUG: Found {module_name} in bl_ext.{ext_module_name}.{attr}")
return f"bl_ext.{ext_module_name}.{attr}.{module_name}"
except Exception:
# some attributes might not be modules or accessible
pass
except Exception as e:
pass # Silently ignore errors checking modules, they might not be fully loaded
# print(f"DEBUG: Error checking module {ext_module_name}: {e}")
except ImportError:
# print(f"DEBUG: bl_ext not found, checking addon paths")
pass # bl_ext not found, continue with addon paths
# Also check default addon paths as fallback
addon_paths = bpy.utils.script_paths(subdir="addons")
# print(f"DEBUG: Addon paths: {addon_paths}")
for path in addon_paths:
module_path = os.path.join(path, module_name)
# print(f"DEBUG: Checking path: {module_path}")
if os.path.exists(module_path):
# print(f"DEBUG: Found {module_name} at {module_path}")
return module_name
# print(f"DEBUG: Module {module_name} not found anywhere")
return None