好的,所以我有一个菜单主题为使用python编写的maya中设置的mainWindow。我还有3个包含Mel和Python文件的文件夹。
folder_names = C:/scriptsFolder/c.mel + d.py
C:/scriptsFolder/Mel/a.mel
C:/scriptsFolder/Python/b.py
我之前已经通过@theodox得到了关于这个问题的帮助,但是对于这个问题,他帮助我创建了一个字典文件,但当他将funtools-partial
整合到菜单中时,他向我展示了而不是从字典中抓取这些文件创建了他的菜单栏,将相应物理文件夹的名称添加到菜单栏,并列出菜单下的文件。对于某些人来说,这是一个非常好的主意,但不是我需要的。我需要在已经创建的MenuItem下列出的字典文件的输出。
上一篇文章在这里:Multiple Paths Traversed and Displayed Filed type in Maya Menu with Python
感谢您提前的帮助和@theodox如果您是lookig感谢您的帮助以及:)
import os
import maya.cmds as cmds
from functools import partial
# Location of Folders
folder_names = ['C:/Users/OCuinn/Dropbox/Maya Scripts/Python Code'
'C:/Users/OCuinn/Dropbox/Maya Scripts/Mel Code'
'C:/Users/OCuinn/Dropbox/Maya Scripts/']
# Delete the Menu if it already exists
if cmds.menu('PadraigsTools', exists = True):
cmds.deleteUI('PadraigsTools')
#Creating the GUI
PadraigsTools=cmds.menu('PadraigsTools', p='MayaWindow', label='PadraigsTools')
# The Refresh Function / Refresh the scripts
cmds.menuItem(p=PadraigsTools, l="Refresh", c='PadraigsTools')
cmds.menuItem(p=PadraigsTools, d=1)
# Browse through Directories
cmds.menuItem(p=PadraigsTools, l="Change")
cmds.menuItem(p=PadraigsTools, d=1)
Python = cmds.menuItem(subMenu=1, label='Python', c=test)
cmds.setParent(Python, menu=True )
cmds.menuItem( label='pythonFileList.py')
Mel = cmds.menuItem(subMenu=1,label='Mel',p='PadraigsTools',c=test())
cmds.setParent( Mel, menu=True)
cmds.menuItem (label='melFileList.mel')
Plugins = cmds.menuItem(subMenu=1,label='Plugins',p='PadraigsTools', c=test)
cmds.setParent( Plugins, menu=True)
cmds.menuItem (label='Plugin.mll')
Other = cmds.menuItem(subMenu=1,label='Other',p='PadraigsTools', c=test)
cmds.setParent( Other, menu=True)
cmds.menuItem (label='Other')
# Searching for Mel and Python Files
def find_files(root, extensions = ('mel', 'py')):
def clean_path(*p):
return "/".join(p).replace('\\', '/')
for root, _, files in os.walk(root):
used = [f for f in files if f.split(".")[-1] in extensions]
for u in used:
yield clean_path(root, u)
# Creating a dictionary with given files
def relativize(abs, roots):
low_roots = map (str.lower, roots) # all lower for comparison
for root, low_root in zip(roots,low_roots):
if abs.lower().startswith(low_root):
return root, abs[len(root):]
return "", abs
relative_paths = find_files('C:/Users/OCuinn/Dropbox/Maya Scripts/')
root_dict = {}
for item in relative_paths :
folders, files = relativize(item, ('C:/Users/OCuinn/Dropbox/Maya Scripts/Python Code','C:/Users/OCuinn/Dropbox/Maya Scripts/Mel Code', 'C:/Users/OCuinn/Dropbox/Maya Scripts'))
if not folders in root_dict:
root_dict[folders] = []
root_dict[folders].append(files)
# call this on every button selection
def test(filepath, ignore):
# maya will send "test(name, False)"; we just ignore the 'False'
print "Here's where I would reload", filepath
for name in folder_names:
PadraigsTools = name
if PadraigsTools:
PadraigsTools = PadraigsTools.split("/")[-2] # we used trailing slashes
else:
PadraigsTools = "root"
file_names = root_dict[name]
file_names.sort()
for fn in file_names:
mi = cmds.menuItem(label = fn, command = partial(test, fn))
cmds.setParent(Python)
好的,这样就告诉我Python不可用,但它通过路径MayaWindow|PadraigsTools|menuItem1345
明确指示您,或者我标记它MayaWindow|PadraigsTools|Python
-Padraig
答案 0 :(得分:0)
我设法找到了一种自己完成这一切的方法。如果有更多的pythonic方式来接近这个,我很乐意看到建议。
import os
import maya.cmds as cmds
from functools import partial
# Location of Folders
folder_names = ('C:/Users/OCuinn/Dropbox/Maya Scripts/Python Code',
'C:/Users/OCuinn/Dropbox/Maya Scripts/Mel Code',
'C:/Users/OCuinn/Dropbox/Maya Scripts')
# Delete the Menu if it already exists
if cmds.menu('PadraigsTools', exists = True):
cmds.deleteUI('PadraigsTools')
#Creating the GUI
PadraigsTools=cmds.menu('PadraigsTools', p='MayaWindow', label='PadraigsTools')
# The Refresh Function / Refresh the scripts
cmds.menuItem(p=PadraigsTools, l="Refresh", c='PadraigsTools')
cmds.menuItem(p=PadraigsTools, d=1)
# Browse through Directories
cmds.menuItem(p=PadraigsTools, l="Change")
cmds.menuItem(p=PadraigsTools, d=1)
Python = cmds.menuItem("Python",subMenu=1, label='Python')
cmds.setParent(Python, menu=True )
cmds.menuItem(label ='Python Files')
Mel = cmds.menuItem("Mel", subMenu=1, label='Mel', p='PadraigsTools')
cmds.setParent( Mel, menu=True)
cmds.menuItem (label='melFileList.mel')
Plugins = cmds.menuItem("Plugins", subMenu=1, label='Plugins', p='PadraigsTools')
cmds.setParent( Plugins, menu=True)
cmds.menuItem (label='Plugin.mll')
Other = cmds.menuItem("Other", subMenu=1, label='Other', p='PadraigsTools')
cmds.setParent( Other, menu=True)
cmds.menuItem (label='Other', p=Other)
# Searching for Mel and Python Files
def find_files(root, extensions = ('mel', 'py')):
def clean_path(*p):
return "/".join(p).replace('\\', '')
for root, _, files in os.walk(root):
used = [f for f in files if f.split(".")[-1] in extensions]
for u in used:
yield clean_path(root, u)
# Creating a dictionary with given files
def relativize(abs, roots):
low_roots = map (str.lower, roots) # all lower for comparison
for root, low_root in zip(roots,low_roots):
if abs.lower().startswith(low_root):
return root, abs[len(root):]
return "", abs
relative_paths = find_files('C:/Users/OCuinn/Dropbox/Maya Scripts/')
root_dict = {}
for item in relative_paths :
folders, files = relativize(item, ('C:/Users/OCuinn/Dropbox/Maya Scripts/Python Code','C:/Users/OCuinn/Dropbox/Maya Scripts/Mel Code', 'C:/Users/OCuinn/Dropbox/Maya Scripts'))
if not folders in root_dict:
root_dict[folders] = []
root_dict[folders].append(files)
# call this on every button selection
def test(filepath, ignore):
# maya will send "test(name, False)"; we just ignore the 'False'
print "Here's where I would reload", filepath
for name in folder_names:
PadraigsTools = name
if PadraigsTools:
PadraigsTools = PadraigsTools.split("/")[-2] # we used trailing slashes
else:
PadraigsTools = "root"
cmds.menu(label = PadraigsTools)
file_names = root_dict[name]
file_names.sort()
for fn in file_names:
if fn.endswith(".py"):
cmds.menuItem(label = fn, p="Python", command = partial(test, fn))
else:
if fn.endswith(".mel"):
cmds.menuItem(label = fn, p="Mel", command = partial(test, fn))