Multiple Paths Traversed and Displayed Filed type in Maya Menu with Python -



Multiple Paths Traversed and Displayed Filed type in Maya Menu with Python -

i'm new here bare in mind , hope questions asked lot help me out. trying alter brent tylers dropbox script able list python under python, mel under mel , on(eventually plugins , other files not now)

ok directory so:

1. sf=c:/users/scripts/ a.py + b.mel pf=c:/users/scripts/python/c.py mf=c:/users/scripts/mel/d.mel

(these folders scripts placed in)

code :

absolutefiles = [] relativefiles = [] folders = [] allfiles = [] currentfile = '' root, dirs, files in os.walk(sf): x in files: right = root.replace('\\', '/') currentfile = (correct + '/' + x) allfiles.append(currentfile) if currentfile.endswith('.mel'): relativefiles.append(currentfile.replace((mf + '/'), "")) if currentfile.endswith('.py'): relativefiles.append(currentfile.replace((pf + '/'), "")) relativefiles.sort() relativefile in relativefiles: split = relativefile.split('/') filename = split[-1].split('.') i=0 while i<(len(split)): ### create folders ### if i==0 , len(split) != 1: if cmds.menu(split[i] ,ex=1) == 0: cmds.menuitem(split[i], p=padraigstools, bld=1, sm=1, to=1, l=split[i]) if > 0 , < (len(split)-1): if cmds.menu(split[i] ,ex=1) == 0: cmds.menuitem(split[i], p=split[i-1], bld=1, sm=1, to=1, l=split[i]) ### create .mel files ### if filename[-1] == 'mel': if i==len(split)-1 , len(split) > 1: scriptname = split[-1].split('.') temp1 = 'source ' + '"' + sf + '/' + relativefile + '"; ' + scriptname[0] command = '''mel.eval(''' + "'" + temp1 + '''')''' cmds.menuitem(split[i], p=split[i-1], c=command, l=split[i]) if i==len(split)-1 , len(split) == 1: scriptname = split[-1].split('.') temp1 = 'source ' + '"' + sf + '/' + relativefile + '"; ' + scriptname[0] command = '''mel.eval(''' + "'" + temp1 + '''')''' cmds.menuitem(split[i], p=mel, c=command, l=split[i]) ### create .py files ### if filename[-1] == 'py': if i==len(split)-1 , len(split) > 1: command = 'import ' + filename[0] + '\n' + filename[0] + '.' + filename[0]+ '()' cmds.menuitem(split[i], p=split[i-1], c=command, l=split[i]) if i==len(split)-1 , len(split) == 1: command = 'import ' + filename[0] + '\n' + filename[0] + '.' + filename[0]+ '()' cmds.menuitem(split[i], p=python, c=command, l=split[i]) i+=1

so far can print out individually (sf, pf, mf) corresponding directory cant list out @ 1 time , files under sf not show @ all. regarding folders created ends odd. duplicate folder submenu , if utilize sf give me c:/.

after days , hours of research trying mend script have found no reply including

from itertools import chain paths = (mf, sf, pf) path, dirs, files in chain.from_iterable(os.walk(path) path in paths):

::question:: there way can set sanely new folders show contents on refresh submenu , files show , allow me execute them corresponding submenu.

i appreciate help possible including downwards votes haha. , bare in mind don't want hand me reply on golden spoon because wont know corrected or needs :)

thanks -- padraig

there's couple of things can simplify things bit.

first, it's thought create data-driven possible don't have re-write if needs change. more or less do, collects results dictionary key root paths supplied , values lists of relative paths:

def find_files(root, extensions = ('mel', 'py')): def clean_path(*p): homecoming "/".join(p).replace('\\', '/') root, _, files in os.walk(root): used = [f f in files if f.split(".")[-1] in extensions] u in used: yield clean_path(root, u) def relativize(abs, roots): low_roots = map (str.lower, roots) # lower comparing root, low_root in zip(roots,low_roots): if abs.lower().startswith(low_root): homecoming root, abs[len(root):] homecoming ("", abs) relative_paths = find_files('c:/users/scripts') root_dict = {} item in relative_paths : folder, file = relativize(item, ('c:/users/scripts/python/', 'c:/users/scripts/mel/', 'c:/users/scripts/')) if not folder in root_dict: root_dict[folder] = [] root_dict[folder].append(file)

so have dictionary bunch of root folders , lists of relative paths (files not in relative path supplied keyed empty string , show absolute paths). can create menus in generic way because in same format. if need entire list, can this:

results = [] each_root in root_dict: relpath in root_dict[each_root]: results.append(each_root + relpath)

for creating actual menus, want utilize single function , bind filename each menu item create it. tricky topic (more detail here). easy way utilize functools.partial object, bundles command , bunch of arguments object looks function: can create partial , attach command of menu items phone call same function individual arguments. here's simple illustration using variables above , menubar; can see how adapt other kinds of menus pretty easily:

from functools import partial # phone call on every button selection def test(filepath, ignore): # maya send "test(name, false)"; ignore 'false' print "here's reload", filepath illustration = cmds.window(title = 'example') menubar = cmds.menubarlayout() name in folder_names: menuname = name if menuname: menuname = menuname.split("/")[-2] # used trailing slashes else: menuname = "root" cmds.menu(label = menuname) file_names = root_dict[name] file_names.sort() fn in file_names: mi = cmds.menuitem(label = fn, command = partial(test, fn)) cmds.setparent(menubar)

python menu directory maya

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -