Last active
May 11, 2016 19:15
-
-
Save yamahigashi/9805198 to your computer and use it in GitHub Desktop.
指定パス以下の スクリプトを元にシェルフを動的作成 for maya
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""coding: utf-8""" | |
# | |
# 指定パス以下の スクリプトを元にシェルフを動的作成 for maya | |
# | |
# ├─dyn_shelf_root | |
# │ ├─hoge_tab | |
# │ │ └─ スクリプト1.py | |
# │ │ └─ スクリプト2.py | |
# │ └─mage_tab | |
# └─ スクリプト3.py | |
# └─ スクリプト4.mel | |
# | |
# のような構成で使用する | |
# | |
# スクリプト1.py の内容は | |
#def main(): | |
# print 'called!' | |
# | |
#if __name__ == 'pymel.mayautils': | |
# main() | |
# | |
# などとする | |
############################################################################## | |
import os | |
import maya.cmds as cmds | |
############################################################################## | |
py_source = lambda s:''' | |
import pymel.mayautils as u | |
u.source(r"{file_path}") | |
'''.format(file_path=s) | |
mel_source = lambda s:''' | |
source "{file_path}"; | |
'''.format(file_path=s.replace("\\", "/")) | |
ROOT_PATH = r"C:\Users\hoge\maya\dyn_shelf" # 書き換えてね!! | |
############################################################################## | |
def list_dirs(start_path): | |
''' 指定ルートパスからpythonファイルを探索 | |
フォルダ名をキーに pyファイルのタプルを値に した辞書を返す''' | |
command_files = {} # {tabname: file_path} | |
for root, dirs, files in os.walk(start_path): | |
level = root.replace(start_path, '').count(os.sep) | |
# トップディレクトリはタブになる | |
if level is 0: | |
continue | |
dir_name = os.path.basename(root) | |
for f in files: | |
if f.endswith(".py") or f.endswith(".mel"): | |
if dir_name in command_files: | |
command_files[dir_name].append(root + os.sep + f) | |
else: | |
command_files[dir_name] = [root + os.sep + f] | |
return command_files | |
def create_window(commands): | |
''' list_dirs の結果から maya ウィンドを作成する ''' | |
def draw_tab(tab_name, files): | |
cmds.shelfLayout(tab_name) | |
for f in files: | |
if not f: | |
print "ERROR: empty string" | |
continue | |
if f.endswith(".py"): | |
st = "python" | |
command_text = py_source(f) | |
elif f.endswith(".mel"): | |
st = "mel" | |
command_text = mel_source(f) | |
cmds.shelfButton( | |
width=48, | |
imageOverlayLabel=os.path.basename(f), | |
label=os.path.basename(f), | |
sourceType=st, | |
image1="commandButton.png", | |
command=command_text) | |
cmds.setParent("..") | |
window = cmds.window(title = "hogemage") | |
cmds.tabLayout() | |
for tab in commands.keys(): | |
draw_tab(tab, commands[tab]) | |
cmds.setParent("..") | |
cmds.showWindow( window ) | |
commands = list_dirs(ROOT_PATH) | |
create_window(commands) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment