Maya Python - 将字典文件附加到已创建的子菜单

Maya Python - attaching dictionary files to already created submenus

好的,所以我有一个主窗口的父菜单栏,它是用 python 编写的 maya 设置的。我还有 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 下列出的字典文件的输出。

前一个post在这里:

提前感谢您的帮助,如果您正在寻找@theodox,也感谢您的帮助:)

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

-帕德莱格

我设法找到了一种自己做这一切的方法。如果有更多的 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))