This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
metarig_menu.py
125 lines (99 loc) · 3.59 KB
/
metarig_menu.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
122
123
124
125
# ##### 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
from string import capwords
import bpy
from . import utils
def get_metarig_list(path):
""" Searches for metarig modules, and returns a list of the
imported modules.
"""
metarigs = []
MODULE_DIR = os.path.dirname(__file__)
METARIG_DIR_ABS = os.path.join(MODULE_DIR, utils.METARIG_DIR)
SEARCH_DIR_ABS = os.path.join(METARIG_DIR_ABS, path)
files = os.listdir(SEARCH_DIR_ABS)
files.sort()
for f in files:
# Is it a directory?
if os.path.isdir(os.path.join(SEARCH_DIR_ABS, f)):
continue
elif not f.endswith(".py"):
continue
elif f == "__init__.py":
continue
else:
module_name = f[:-3]
try:
metarigs += [utils.get_metarig_module(module_name)]
except (ImportError):
pass
return metarigs
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"
# 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
# Get the metarig modules
metarigs = get_metarig_list("")
# Create metarig add Operators
metarig_ops = []
for m in metarigs:
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.append((T, name))
# Create menu functions
menu_funcs = []
for mop, name in metarig_ops:
text = capwords(name.replace("_", " ")) + " (Meta-Rig)"
menu_funcs += [make_metarig_menu_func(mop.bl_idname, text)]
def register():
for mop, name in metarig_ops:
bpy.utils.register_class(mop)
for mf in menu_funcs:
bpy.types.INFO_MT_armature_add.append(mf)
def unregister():
for mop, name in metarig_ops:
bpy.utils.unregister_class(mop)
for mf in menu_funcs:
bpy.types.INFO_MT_armature_add.remove(mf)