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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# ##### 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 #####
# <pep8 compliant>
import os
import traceback
from string import capwords
from collections import defaultdict
import bpy
from .utils.rig import METARIG_DIR, get_resource
from . import feature_set_list
class ArmatureSubMenu(bpy.types.Menu):
# bl_idname = 'ARMATURE_MT_armature_class'
def draw(self, context):
layout = self.layout
layout.label(text=self.bl_label)
for op, name in self.operators:
text = capwords(name.replace("_", " ")) + " (Meta-Rig)"
layout.operator(op, icon='OUTLINER_OB_ARMATURE', text=text)
def get_metarigs(metarigs, base_dir, base_path, *, path=[], nested=False):
""" Searches for metarig modules, and returns a list of the
imported modules.
"""
dir_path = os.path.join(base_dir, *path)
try:
files = os.listdir(dir_path)
except FileNotFoundError:
files = []
files.sort()
for f in files:
is_dir = os.path.isdir(os.path.join(dir_path, 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(path, f))
continue
if is_dir: # Check directories
get_metarigs(metarigs[f], base_dir, base_path, path=[*path, f], nested=True) # "" adds a final slash
elif f.endswith(".py"):
# Check straight-up python files
f = f[:-3]
module = get_resource('.'.join([*base_path, *path, f]))
if nested:
metarigs[f] = module
else:
metarigs[METARIG_DIR][f] = module
def make_metarig_add_execute(m):
""" Create an execute method for a metarig creation operator.
"""
def execute(self, context):
# Add armature object
bpy.ops.object.armature_add()
obj = context.active_object
obj.name = "metarig"
obj.data.name = "metarig"
# Remove default bone
bpy.ops.object.mode_set(mode='EDIT')
bones = context.active_object.data.edit_bones
bones.remove(bones[0])
# Create metarig
m.create(obj)
bpy.ops.object.mode_set(mode='OBJECT')
return {'FINISHED'}
return execute
def make_metarig_menu_func(bl_idname, text):
""" For some reason lambda's don't work for adding multiple menu
items, so we use this instead to generate the functions.
"""
def metarig_menu(self, context):
self.layout.operator(bl_idname, icon='OUTLINER_OB_ARMATURE', text=text)
return metarig_menu
def make_submenu_func(bl_idname, text):
def metarig_menu(self, context):
self.layout.menu(bl_idname, icon='OUTLINER_OB_ARMATURE', text=text)
return metarig_menu
# Get the metarig modules
def get_internal_metarigs():
BASE_RIGIFY_DIR = os.path.dirname(__file__)
BASE_RIGIFY_PATH = __name__.split('.')[:-1]
get_metarigs(metarigs, os.path.join(BASE_RIGIFY_DIR, METARIG_DIR), [*BASE_RIGIFY_PATH, METARIG_DIR])
def infinite_defaultdict():
return defaultdict(infinite_defaultdict)
metarigs = infinite_defaultdict()
metarig_ops = {}
armature_submenus = []
menu_funcs = []
def create_metarig_ops(dic=metarigs):
"""Create metarig add Operators"""
for metarig_category in dic:
if metarig_category == "external":
create_metarig_ops(dic[metarig_category])
continue
if not metarig_category in metarig_ops:
metarig_ops[metarig_category] = []
for m in dic[metarig_category].values():
name = m.__name__.rsplit('.', 1)[1]
# Dynamically construct an Operator
T = type("Add_" + name + "_Metarig", (bpy.types.Operator,), {})
T.bl_idname = "object.armature_" + name + "_metarig_add"
T.bl_label = "Add " + name.replace("_", " ").capitalize() + " (metarig)"
T.bl_options = {'REGISTER', 'UNDO'}
T.execute = make_metarig_add_execute(m)
metarig_ops[metarig_category].append((T, name))
def create_menu_funcs():
global menu_funcs
for mop, name in metarig_ops[METARIG_DIR]:
text = capwords(name.replace("_", " ")) + " (Meta-Rig)"
menu_funcs += [make_metarig_menu_func(mop.bl_idname, text)]
def create_armature_submenus(dic=metarigs):
global menu_funcs
metarig_categories = list(dic.keys())
metarig_categories.sort()
for metarig_category in metarig_categories:
# Create menu functions
if metarig_category == "external":
create_armature_submenus(dic=metarigs["external"])
continue
if metarig_category == METARIG_DIR:
continue
armature_submenus.append(type('Class_' + metarig_category + '_submenu', (ArmatureSubMenu,), {}))
armature_submenus[-1].bl_label = metarig_category + ' (submenu)'
armature_submenus[-1].bl_idname = 'ARMATURE_MT_%s_class' % metarig_category
armature_submenus[-1].operators = []
menu_funcs += [make_submenu_func(armature_submenus[-1].bl_idname, metarig_category)]
for mop, name in metarig_ops[metarig_category]:
arm_sub = next((e for e in armature_submenus if e.bl_label == metarig_category + ' (submenu)'), '')
arm_sub.operators.append((mop.bl_idname, name,))
def init_metarig_menu():
get_internal_metarigs()
create_metarig_ops()
create_menu_funcs()
create_armature_submenus()
### Registering ###
def register():
from bpy.utils import register_class
for cl in metarig_ops:
for mop, name in metarig_ops[cl]:
register_class(mop)
for arm_sub in armature_submenus:
register_class(arm_sub)
for mf in menu_funcs:
bpy.types.VIEW3D_MT_armature_add.append(mf)
def unregister():
from bpy.utils import unregister_class
for cl in metarig_ops:
for mop, name in metarig_ops[cl]:
unregister_class(mop)
for arm_sub in armature_submenus:
unregister_class(arm_sub)
for mf in menu_funcs:
bpy.types.VIEW3D_MT_armature_add.remove(mf)
def get_external_metarigs(set_list):
unregister()
# Clear and fill metarigs public variables
metarigs.clear()
get_internal_metarigs()
for feature_set in set_list:
try:
base_dir, base_path = feature_set_list.get_dir_path(feature_set, METARIG_DIR)
get_metarigs(metarigs['external'], base_dir, base_path)
except Exception:
print("Rigify Error: Could not load feature set '%s' metarigs: exception occurred.\n" % (feature_set))
traceback.print_exc()
print("")
continue
metarig_ops.clear()
armature_submenus.clear()
menu_funcs.clear()
create_metarig_ops()
create_menu_funcs()
create_armature_submenus()
register()
|