Skip to content

Instantly share code, notes, and snippets.

@yamadaaaaaaa
Last active January 21, 2022 05:02
Show Gist options
  • Select an option

  • Save yamadaaaaaaa/c200af51317ce8fbc7d58ced590477e0 to your computer and use it in GitHub Desktop.

Select an option

Save yamadaaaaaaa/c200af51317ce8fbc7d58ced590477e0 to your computer and use it in GitHub Desktop.
ymd_scriptLib_maya.py
# -*- coding: utf-8 -*-
import os
import sys
from maya import cmds
from maya import mel
from functools import partial
sys.dont_write_bytecode = True # not create pyc
import importlib
import subprocess
# change path #####################
PATH_SCRIPTLIB = r'D:\scriptLib'
# change name #####################
NAME_PATH_SCRIPTLIB = 'ymd_scriptLib'
# change file tipe ################
FILETYPE_SCRIPTLIB = ['py','pyc','mel','pdf','htm','html','txt']
###################################
def call_script(item_name, *args ):
item_name = item_name.replace(':_:',os.sep)
if not os.path.exists(item_name):return
_name, _ext = os.path.splitext(os.path.basename(item_name))
_ext = _ext.replace('.','')
if _ext == 'py' or _ext == 'pyc':
is_sys = os.path.dirname(item_name) in sys.path
if not is_sys:
sys.path.append(os.path.dirname(item_name))
if _name in sys.modules:
reload(sys.modules[_name])
else:
importlib.import_module(_name)
if not is_sys:
sys.path.remove(os.path.dirname(item_name))
elif _ext == 'mel':
item_name = item_name.replace(os.sep, '/')
mel.eval('source \"' + item_name + '\";')
else:
subprocess.call('start "" "{}"'.format(item_name), shell=True)
def set_menuItem(_menu,_current, list_dir, list_file):
dir_name = _current.split(os.sep).pop()
parent_item = _menu
if _current != PATH_SCRIPTLIB:
dir_current = _current.replace(PATH_SCRIPTLIB,'')
dir_current = dir_current.split(os.sep)
dir_current = list(filter(None, dir_current))
for i in range(len(dir_current)):
item_name = ':_:'.join(dir_current[:i+1])
dir_name = dir_current[:i+1].pop()
if i != 0:
parent_item_name = ':_:'.join(dir_current[:i])
parent_item = cmds.menuItem( parent_item_name, e=1 )
if not cmds.menuItem( item_name, q=1, ex=1 ):
parent_item = cmds.menuItem( item_name, l=dir_name, to=1, sm=1, p=parent_item )
for _file in list_file:
_name, _ext = os.path.splitext(_file)
_ext = _ext.replace('.','')
if not _ext in FILETYPE_SCRIPTLIB :continue
if '__init__.py' in _file:continue
item_name = os.path.join(_current,_file).replace(os.sep,':_:')
cmds.menuItem( item_name, l=_file, c=partial(call_script, item_name), p=parent_item )
def set_menu( _menu, *args ):
dic_scripts = {}
_menuItems = cmds.menu( _menu, q=1, ni=1)
if _menuItems:
cmds.menu( _menu, dai=1, e=1 )
cmds.refresh()
for _current, list_dir, list_file in os.walk(PATH_SCRIPTLIB):
if len(list_file) == 0:continue
set_menuItem(_menu, _current, list_dir, list_file)
def main():
_menu = cmds.menu( NAME_PATH_SCRIPTLIB, l=NAME_PATH_SCRIPTLIB, to=1)
cmds.menu( _menu, e=1, pmc=partial(set_menu, _menu) )
if __name__ == '__main__':
main()
#cmds.deleteUI(NAME_PATH_SCRIPTLIB)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment