forked from cessen/rigify
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrig_lists.py
121 lines (102 loc) · 4.32 KB
/
rig_lists.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#====================== BEGIN GPL LICENSE BLOCK ======================
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#======================= END GPL LICENSE BLOCK ========================
import os
import bpy
from . import utils
def get_rig_list(path, mode='relative'):
""" Recursively searches for rig types, and returns a list.
:param path
:type path:str
:param mode: path is absolute or relative?
:type mode: str
"""
if mode == 'relative':
base_path = ''
MODULE_DIR = os.path.dirname(__file__)
RIG_DIR_ABS = os.path.join(MODULE_DIR, utils.RIG_DIR)
SEARCH_DIR_ABS = os.path.join(RIG_DIR_ABS, path)
elif mode == 'absolute':
base_path = path
SEARCH_DIR_ABS = path
else:
return
rigs_dict = dict()
rigs = []
impl_rigs = []
files = os.listdir(SEARCH_DIR_ABS)
files.sort()
for f in files:
is_dir = os.path.isdir(os.path.join(SEARCH_DIR_ABS, f)) # Whether the file is a directory
# Stop cases
if f[0] in [".", "_"]:
continue
if f.count(".") >= 2 or (is_dir and "." in f):
print("Warning: %r, filename contains a '.', skipping" % os.path.join(SEARCH_DIR_ABS, f))
continue
if is_dir:
# Check directories
if mode == 'relative':
module_name = os.path.join(path, f).replace(os.sep, ".")
rig = utils.get_rig_type(module_name, base_path=base_path)
else:
module_name = "__init__"
rig = utils.get_rig_type(module_name, base_path=base_path + f + os.sep)
# Check if it's a rig itself
if hasattr(rig, "Rig"):
rigs += [f]
else:
# Check for sub-rigs
sub_dict = get_rig_list(os.path.join(path, f, ""), mode=mode) # "" adds a final slash
rigs.extend(["%s.%s" % (f, l) for l in sub_dict['rig_list']])
impl_rigs.extend(["%s.%s" % (f, l) for l in sub_dict['implementation_rigs']])
elif f.endswith(".py"):
# Check straight-up python files
t = f[:-3]
if mode == 'relative':
module_name = os.path.join(path, t).replace(os.sep, ".")
rig = utils.get_rig_type(module_name, base_path=base_path)
else:
external_folder = bpy.context.user_preferences.addons['rigify'].preferences.custom_rigs_folder
module_name = os.path.join(path, t).replace(external_folder, '').replace(os.sep, ".")
rig = utils.get_rig_type(module_name, base_path=external_folder)
if hasattr(rig, "Rig"):
rigs += [t]
if hasattr(rig, 'IMPLEMENTATION') and rig.IMPLEMENTATION:
impl_rigs += [t]
rigs.sort()
rigs_dict['rig_list'] = rigs
rigs_dict['implementation_rigs'] = impl_rigs
return rigs_dict
def get_collection_list(rig_list):
collection_list = []
for r in rig_list:
a = r.split(".")
if len(a) >= 2 and a[0] not in collection_list:
collection_list += [a[0]]
return collection_list
# Public variables
rigs_dict = get_rig_list("")
rig_list = rigs_dict['rig_list']
implementation_rigs = rigs_dict['implementation_rigs']
collection_list = get_collection_list(rig_list)
col_enum_list = [("All", "All", ""), ("None", "None", "")] + [(c, c, "") for c in collection_list]
def get_external_rigs():
external_folder = bpy.context.user_preferences.addons['rigify'].preferences.custom_rigs_folder
if external_folder:
external_rigs_dict = get_rig_list(external_folder, mode='absolute')
rigs_dict['external'] = external_rigs_dict