1 # SPDX-License-Identifier: GPL-2.0-or-later
8 from typing
import Optional
, Iterable
10 from .utils
.rig
import RIG_DIR
12 from . import feature_set_list
15 def get_rigs(base_dir
: str, base_path
: list[str], *,
16 path
: Iterable
[str] = (),
17 feature_set
=feature_set_list
.DEFAULT_NAME
):
18 """ Recursively searches for rig types, and returns a list.
21 base_dir: root directory
22 base_path: base dir where rigs are stored
23 path: rig path inside the base dir
24 feature_set: feature set that is being loaded
30 dir_path
= os
.path
.join(base_dir
, *path
)
33 files
= os
.listdir(dir_path
)
34 except FileNotFoundError
:
40 is_dir
= os
.path
.isdir(os
.path
.join(dir_path
, f
)) # Whether the file is a directory
43 if f
[0] in [".", "_"]:
45 if f
.count(".") >= 2 or (is_dir
and "." in f
):
46 print("Warning: %r, filename contains a '.', skipping" % os
.path
.join(*base_path
, *path
, f
))
51 sub_rigs
, sub_impls
= get_rigs(base_dir
, base_path
, path
=[*path
, f
], feature_set
=feature_set
)
52 rig_table
.update(sub_rigs
)
53 impl_rigs
.update(sub_impls
)
54 elif f
.endswith(".py"):
55 # Check straight-up python files
56 sub_path
= [*path
, f
[:-3]]
57 key
= '.'.join(sub_path
)
58 # Don't reload rig modules - it breaks isinstance
59 rig_module
= importlib
.import_module('.'.join(base_path
+ sub_path
))
60 if hasattr(rig_module
, "Rig"):
61 rig_table
[key
] = {"module": rig_module
,
62 "feature_set": feature_set
}
63 if hasattr(rig_module
, 'IMPLEMENTATION') and rig_module
.IMPLEMENTATION
:
64 impl_rigs
[key
] = rig_module
66 return rig_table
, impl_rigs
71 implementation_rigs
= {}
74 def get_rig_class(name
: str) -> Optional
[typing
.Type
]:
76 return rigs
[name
]["module"].Rig
77 except (KeyError, AttributeError):
81 def get_internal_rigs():
82 global rigs
, implementation_rigs
84 base_rigify_dir
= os
.path
.dirname(__file__
)
85 base_rigify_path
= __name__
.split('.')[:-1]
87 rigs
, implementation_rigs
= get_rigs(os
.path
.join(base_rigify_dir
, RIG_DIR
),
88 [*base_rigify_path
, RIG_DIR
])
91 def get_external_rigs(set_list
):
92 # Clear and fill rigify rigs and implementation rigs public variables
93 for rig
in list(rigs
.keys()):
94 if rigs
[rig
]["feature_set"] != feature_set_list
.DEFAULT_NAME
:
96 if rig
in implementation_rigs
:
97 implementation_rigs
.pop(rig
)
100 for feature_set
in set_list
:
101 # noinspection PyBroadException
103 base_dir
, base_path
= feature_set_list
.get_dir_path(feature_set
, RIG_DIR
)
105 external_rigs
, external_impl_rigs
= get_rigs(base_dir
, base_path
, feature_set
=feature_set
)
107 print(f
"Rigify Error: Could not load feature set '{feature_set}' rigs: exception occurred.\n")
108 traceback
.print_exc()
112 rigs
.update(external_rigs
)
113 implementation_rigs
.update(external_impl_rigs
)