python 在 mayagui 中选择参数

Selecting parameters in mayagui in python

我必须在 Maya 中创建一个 GUI python,其中我必须使用 Sql 查询来获取 项目名称,os,项目编号,路径。例如,在我的代码中,如果选择的项目名称是 "Abc",则应根据所选项目名称选择 os、项目 ID 和路径。怎么做??

我得到了项目名称,但我无法获得其他参数

import os,mysql.connector as mc , platform 
cmds.window(title='Test Window')
name = []
id = []  
Os = []
path = []
wpath = []
# Query for getting various projects from db 
cursor = db.cursor()
#selecting projectname
ans = cursor.fetchall()
cursor.close()
def projid(name):
  #selecting project id
   pid = cursor.fetchone()
   print "out"
   print pid



 def Getprojid(z):
   global pname
    pname =  cmds.optionMenu(z , query=True,value = True)


for ans1 in ans:
   name .append(ans1[0])

cmds.columnLayout()
polygonSelectMenu = cmds.optionMenu(w = 250, h = 30, label = "Project 
Selection:")
for proj in name:
   cmds.menuItem(label = proj)



cmds.button(label='click me Select project ', 
command='printTxtField(polygonSelectMenu)')

cmds.showWindow()

由于您是 Python Maya Gui 的新手,因此在编写涉及 ui 交互的工具时,您必须注意一些事情。

  • 将您的 UI 模块和核心模块(核心功能部分或数据层)分开。
  • 如果您有任何数据库关联,请将 DB 模块分开,理想情况下应该读取、写入或更新数据库。
  • 如果可能,您还可以保留一个交互层或模块来集成核心和 ui 模块以及数据库层。

可以有更细化或更有条理的设计,但就开始而言,这将帮助您完成工作。 以下是您的要求ui评论中的示例。

这里我假设您的项目及其相关详细信息来自数据库或任何其他资源。

#=========================================================================#
# db code:
#=========================================================================#

# This module can have your own implementation. I am just writing some example code.

def get_projects_data(*args, **kwargs):
    """ This should return all the records of the project table from your db.
    For filters you can pass them through kwargs.
    """
    return [{'id': 1021, 'name': 'Abc', 'os': 'some os', 'path': 'some path'},
            {'id': 1022, 'name': 'Def', 'os': 'some other os', 'path': 'some other path'}]


#=========================================================================#
# Ui Code:
#=========================================================================#

import maya.cmds as cmds

class ProjectUI(object):
    _name = 'ProjectManager'
    _title = 'Project Manager'


    def __init__(self, project_data=None):
        self.project_data = project_data
        self.win = None

        # Create the UI
        self.create_ui()

        # Try to populate the projects
        self.populate_projects()

        # Force update the details for first time
        self.update_project_details()

    def create_ui(self):
        """This will create required UI components
        """
        # First check if the ui exists, then close it
        self.close()

        # Now thw create the window
        self.win = cmds.window(self.name, title=self.title)
        # Creat a column layout with adjustable True and row spacing to 5
        cmds.columnLayout(adj=True, rs=5)

        # Create project option menu and add a change commang to trigger while
        # you chnage the projects from the option menu.
        self.project_om = cmds.optionMenu(
            label='Projects', ChangeCommand=self.update_project_details)

        # Create required text fields
        self.project_id_tf = cmds.textFieldGrp(label='Id', editable=False)     
        self.project_path_tf = cmds.textFieldGrp(label='Path', editable=False)
        self.project_os_tf = cmds.textFieldGrp(label='OS', editable=False)


    def populate_projects(self):
        """This should populate all the available projects in poject option menu.
        """
        # First check if we have any project data in hand. If not then we should 
        # exit right away.
        if not self.project_data:
            print('No project data found for populatin projects')
            retturn

        for data in project_data:
            prject = data.get('name')
            menuItem(label=project, parent=self.project_om)

    def update_project_details(self, project=''):
        """This should update all other project details in UI and must me
        triggered while changing projects from poject option menu.
        """
        if not self.project_data:
            retturn

        if not project:
            project = cmds.optionMenu(self.project_om, q=True, value=True)

        project_details = None
        for data in self.project_data:
            if project == data.get('name'):
                project_details = data
                break

        if not project_details:
            print('No project details found for %s' % project)
            return

        proj_id = project_details.get('id')
        proj_path = project_details.get('path')
        proj_os = project_details.get('os')

        cmds.textFieldGrp(self.project_id_tf, e=True, text='%s' % proj_id)
        cmds.textFieldGrp(self.project_path_tf, e=True, text=proj_path)
        cmds.textFieldGrp(self.project_os_tf, e=True, text=proj_os)

    def show(self):
        """Just show the UI if its created ever.
        """
        if self.win:
            cmds.showWindow(self.win)

    def close(self):
        """For deleting the UI if exists
        """
        if cmds.window(self._name, query=True, exists=True):
            cmds.deleteUi(self.name, window=True)

#=========================================================================#
# Integration Code:
#=========================================================================# 

def main():
    # First fetch the data
    data = get_projects_data()

    if not data:
        print('No project data fetched.')
        return

    win = ProjectUI(project_data=data)
    win.show()

    # Return the win just if you want an pointer to same
    return win

# Now just call the main method, whenever required
main()

上面的代码片段只是一个例子。这未在 maya 内部测试。但我希望这会给你一个起点。同样,如果不熟悉 类,您可以通过传递 args 以程序方式执行相同的操作。我会强烈建议 PySide 或 PyQt 以及 PyMel 以获得强大而高效的 Ui 工具。