-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts_to_menu.py
189 lines (149 loc) · 5.59 KB
/
scripts_to_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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
scripts_to_menu.py
Description of scripts_to_menu.py.
"""
import sys
import maya.cmds as cmds
import os
import re
import logging
# MENU_PATH = r'/Users/johannes/dropbox/dev/repos/maya_scripts'
MENU_PATH = os.path.dirname(os.path.realpath(__file__))
SCRIPTS_PATH = os.path.join(MENU_PATH, 'scripts')
ICONS_PATH = os.path.join(SCRIPTS_PATH, '.icons')
# Add repo to PATH
sys.path.append(os.path.abspath(os.path.join(MENU_PATH, '..')))
logging.info(os.path.abspath(os.path.join(MENU_PATH, '..')))
#
# from importlib import reload # Python 3.4+
#
# rn = reload(rn)
def reload(*args, **kwargs):
create_menu()
def get_icon(path):
"""Get the icon for the given file or directory"""
# If folder, use the image inside with the same name
if os.path.isdir(path):
icon = os.path.join(ICONS_PATH, os.path.basename(path) + '.png')
else:
# If file just use image with same name in same folder
icon = os.path.join(ICONS_PATH, os.path.splitext(os.path.basename(path))[0] + '.png')
# If image doesn't exist, use nothing
if not os.path.isfile(icon):
icon = ''
return icon
def get_docstring(file):
logging.debug(file)
with open(file, "r") as f:
content = f.read() # read file
pattern = re.compile(r'"""([\s\S]*?)"""') # create docstring pattern
found = re.findall(pattern, content)
if found:
return found[0].strip().replace('\n', '- ')
def create_command(path, function='main'):
path = os.path.relpath(path, MENU_PATH)
parent = os.path.basename(MENU_PATH)
split_path = path.split(os.sep)
split_path.insert(0, parent)
abs_import = '.'.join(split_path).replace('.py', '')
command = 'import {mods}\n'.format(mods=abs_import)
if sys.version_info.major >= 3:
command = command + 'from importlib import reload\n'
command = command + 'reload({mods})\n' \
'{mods}.{function}()'.format(mods=abs_import, function=function)
return command
def create_submenu(parent, structure):
"""Create a submenu"""
for k in sorted(structure):
parsed_name = parse_filename(k)
# Get annotation from docstring
annotation = parsed_name
if os.path.isfile(k):
annotation = get_docstring(k)
v = structure.get(k)
logging.debug(k + ': ' + str(v))
# print k + ': ' + str(v)
icon = get_icon(k)
if v:
menu_item = cmds.menuItem(subMenu=True,
parent=parent,
label=parsed_name,
fi=icon,
i=icon,
tearOff=True)
# Create title for sub menu
cmds.menuItem(parent=menu_item,
label=parsed_name,
fi=icon,
i=icon,
enable=False)
cmds.menuItem(divider=True)
create_submenu(menu_item, v)
else:
if k.endswith('.py'):
# print k
with open(k, 'r') as p:
pscript = p.readlines()
menu_item = cmds.menuItem(parent=parent,
label=parsed_name,
i=icon,
annotation=annotation,
# command=''.join(pscript),
command=create_command(k)
)
return
def delete_menu(menu_id):
"""Delete a menu
Args:
menu_id (str):
"""
if cmds.menu(menu_id, exists=True):
cmds.deleteUI(menu_id)
def create_menu():
structure = path_to_dict(SCRIPTS_PATH)
# pprint(structure)
menu_id = 'custom_scripts_menu'
# enhancify_maya_path = os.path.dirname(os.path.realpath(__file__))
# maya_menu_path = os.path.dirname(os.path.realpath(__file__))
# Delete menu if it already exists
delete_menu(menu_id)
# Create menu
menu_obj = cmds.menu(menu_id,
allowOptionBoxes=True,
parent='MayaWindow',
label='Script Kiddie',
tearOff=True)
cmds.menuItem(parent=menu_id,
label='Reload',
i=os.path.join(ICONS_PATH, 'reload.png'),
enable=True,
c=reload)
cmds.menuItem(divider=True)
create_submenu(menu_id, structure)
def parse_filename(filename):
"""Generates a readable name from snake case filename"""
basename = os.path.basename(filename)
return os.path.splitext(basename)[0].replace("_", " ").title()
def path_to_dict(path):
d = {}
paths = [os.path.join(path, x) for x in sorted(os.listdir(path)) if x != '__init__.py']
paths = [x for x in paths if os.path.isdir(x) or x.endswith('.py')]
# Remove dotfiles
paths = [x for x in paths if not os.path.basename(x).startswith('.')]
for f in sorted(paths):
if os.path.isdir(os.path.join(path, f)):
d[f] = path_to_dict(os.path.join(path, f))
else:
d[f] = {}
return d
def main():
p = r'C:\Users\JohannesAndersson\OneDrive - Frank Valiant AB\Desktop\scripts\maya_scripts\scripts\modeling\create_locator_from_vertex.py'
print(create_command(p))
if __name__ == '__main__':
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG)
# # pprint(path_to_dict(SCRIPTS_PATH))
# create_menu()
main()