gpt4 book ai didi

python - 在python中的mayagui中选择参数

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:13 26 4
gpt4 key购买 nike

我必须在 Maya python 中创建一个 GUI,其中我必须使用 Sql 查询来获取项目名称、操作系统、项目 ID、路径。例如,在我的代码中,如果选择的项目名称是“Abc”,则应根据所选项目名称选择操作系统、项目 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 模块和核心模块(核心功能部分或数据层)分开。
  • 如果您有任何数据库关联,请将数据库模块分开,理想情况下该模块应该读取、写入或更新数据库。
  • 如果可能,您还可以保留一个交互层或模块,用于将核心和 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 内部进行测试。但我希望这会给你一个起点。同样,如果不熟悉类,您可以通过传递参数以程序方式执行相同的操作。我强烈建议将 PySide 或 PyQt 与 PyMel 一起使用,以获得强大而高效的 Ui 工具。

关于python - 在python中的mayagui中选择参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com