在python的mayagui中选择参数

在python的mayagui中选择参数,python,maya,Python,Maya,我必须在maya python中创建一个GUI,在其中我必须使用Sql查询来获取 projectname、os、projectid、路径。例如,在我的代码中,如果选择的Projectname为“Abc”,则应根据所选Projectname选择操作系统、projectid和路径。怎么做 我已获取项目名称,但无法获取其他参数 import os,mysql.connector as mc , platform cmds.window(title='Test Window') name = [] i

我必须在maya python中创建一个GUI,在其中我必须使用Sql查询来获取 projectname、os、projectid、路径。例如,在我的代码中,如果选择的Projectname为“Abc”,则应根据所选Projectname选择操作系统、projectid和路径。怎么做

我已获取项目名称,但无法获取其他参数

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模块与db层集成在一起
可以有更细粒度或有组织的设计,但对于开始来说,这将帮助您完成任务。 以下是您的需求示例

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

#=========================================================================#
# 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工具。

因为您是Python Maya Gui新手,所以在编写涉及Ui交互的工具时,您必须注意一些事情

  • 将UI模块和核心模块(核心功能部分或数据层)分开
  • 如果您有任何数据库关联,请将DB模块分开,理想情况下,它应该读取、写入或更新数据库
  • 如果可能,您还可以保留一个交互层或模块,用于将核心和ui模块与db层集成在一起
可以有更细粒度或有组织的设计,但对于开始来说,这将帮助您完成任务。 以下是您的需求示例

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

#=========================================================================#
# 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工具。

我是python maya gui新手我是python maya gui新手