如何使用 python 在 Maya 中设置目录?目前使用 PyQt4,但愿意接受任何建议
Posted
技术标签:
【中文标题】如何使用 python 在 Maya 中设置目录?目前使用 PyQt4,但愿意接受任何建议【英文标题】:How do you set the directory in Maya with python? Currently using PyQt4 but open to any suggestions 【发布时间】:2014-07-03 19:47:26 【问题描述】:我需要在 Maya 中设置目录,这样当我导入一个新文件时,它会转到我从中导入最后一个文件的那个目录。它适用于多个脚本。我导入文件,运行脚本,然后运行新脚本。我不能使用字符串或重置我的项目文件夹。这是我无法开始工作的脚本中唯一的一部分。这是我的进口清单:
import maya.cmds as cmds
import sip
import maya.OpenMayaUI as apiUI
from PyQt4 import QtGui, QtCore
import os
import os.path
import maya.mel as mel
这是给我带来麻烦的部分(为简单起见,我放了一条实际路径)。一般来说,我也是脚本新手。我知道我缺少信息,但我不知道是什么。我已经厌倦了很多不同的事情。
#Set file path
directory = QtGui.QFileDialog.setDirectory('C:\Users')
我每次都会收到这个错误:
# TypeError: # arguments did not match any overloaded call:
# QFileDialog.setDirectory(QString): first argument of unbound method must have type 'QFileDialog'
QFileDialog.setDirectory(QDir): first argument of unbound method must have type 'QFileDialog'
我什至尝试过简单地使用工作区。它可以正确打印,没有错误,但是当我打开文件时,它会将我带回到上次保存的位置。
#Give file path
cmds.workspace(dir='C:\Users')
#Find file path
foo = cmds.workspace(q=True, dir=True)
print foo
这是脚本。它是我和另一个人之间的复合体。它的功能是导入网格和自动投影UV。然后由艺术家对其进行优化。然后导出保存一个单独的版本。下一部分是导入优化网格(需要导入,因为这些阶段需要能够独立工作)。我希望这现在更有意义。
import maya.cmds as cmds
import sip
import maya.OpenMayaUI as apiUI
from PyQt4 import QtGui, QtCore
import os
import os.path
import maya.mel as mel
class LPUnion(QtGui.QMainWindow):
def __init__(self,
parent=getMayaMainWindow(),
uniqueHandle='ImportExport'):
QtGui.QMainWindow.__init__(self, parent)
self.setWindowTitle('Low Poly')
self.setObjectName(uniqueHandle)
self.init_ui()
def init_ui(self):
"""
Create the class vars and UI elements
"""
print('setting window')
central_widget = QtGui.QWidget()
central_layout = QtGui.QGridLayout()
central_widget.setLayout(central_layout)
self.setCentralWidget(central_widget)
self.setGeometry(300,300, 250, 150)
lbt=QtGui.QLabel("Import low poly for automatic UV mapping")
self.aBtn = QtGui.QPushButton("Import Low Poly")
self.aBtn.clicked.connect(self.import_low_obj)
lbh=QtGui.QLabel("Resize UVs before exporting optimized")
self.oBtn = QtGui.QPushButton("Export Optimized")
self.oBtn.clicked.connect(self.export_opt_obj)
lbn=QtGui.QLabel("Import union for chunking")
self.uBtn = QtGui.QPushButton("Import for Chunking")
self.uBtn.clicked.connect(self.import_uni_obj)
lbk=QtGui.QLabel("Export chunking")
self.yBtn = QtGui.QPushButton("Export Chunks")
self.yBtn.clicked.connect(self.export_chks)
central_layout.addWidget(lbt)
central_layout.addWidget(self.aBtn)
central_layout.addWidget(lbh)
central_layout.addWidget(self.oBtn)
central_layout.addWidget(lbn)
central_layout.addWidget(self.uBtn)
central_layout.addWidget(lbk)
central_layout.addWidget(self.yBtn)
self.show()
#import for optimization
def import_low_obj(self):
"""
import a selected file
"""
lpfiles = QtGui.QFileDialog.getOpenFileName(self, 'Import for Optimized')
if lpfiles != None:
n = str(lpfiles)
print "name is 0".format(n)
#split up name to import other items
self.root = n.replace(n.split('/')[-1:][0], "")
print "root is 0".format(self.root)
#create new scene
cmds.file(new=True, force=True)
# import n
self.objs = []
for fn in [n]:
cmds.file(fn, i=True)
for o in cmds.ls(type="mesh"):
if not o in self.objs:
self.objs.append(o)
#####set directory
fileName = os.path.dirname(n)
#####directory = QtGui.QFileDialog.setDirectory('path/to/dir')####
print self.objs
cmds.select(cl=True)
#triangulate mesh
for o in self.objs:
cmds.polyTriangulate(o)
cmds.select(cl=True)
#automatic mapping of UVs
cmds.select('mesh', r=True)
cmds.hilite('mesh', r=True)
cmds.selectMode(component=True)
cmds.select('mesh.f[0:10000]', r=True)
cmds.polyAutoProjection('mesh.f[0:10000]', lm = 1, cm=False, l = 2, sc = 1, o = 1, ps = 0.2, ws=False)
QtGui.QMessageBox.information(None,
"Uploaded",
"Optimize Uvs before exporting.".format(self.intr))
#export optimized
def export_opt_obj(self):
"""
export the new mesh
Q:\KITCHEN\mesh\Modo\ECO\113
"""
#export two copies of obj
cmds.select(cl=True)
cmds.select('mesh')
cmds.file(self.root+"mesh-lowpoly-optimize", force=True, es=True, op="groups=1;ptgroups=0;materials=0;smoothing=0;normals=0", typ="OBJexport")
cmds.select(cl=True)
cmds.select('mesh')
cmds.file(self.root+"mesh-lowpoly", force=True, es=True, op="groups=1;ptgroups=0;materials=0;smoothing=0;normals=0", typ="OBJexport")
QtGui.QMessageBox.information(None,
"Saved",
"Optimized mesh files have been saved.".format(self.intr))
#import for chunking
def import_uni_obj(self):
"""
import a selected file
"""
unifiles = QtGui.QFileDialog.getOpenFileName(self, 'Import for Chunking')
if unifiles != None:
n = str(unifiles)
print "name is 0".format(n)
#split up name
self.root = n.replace(n.split('/')[-1:][0], "")
print "root is 0".format(self.root)
#create new scene
cmds.file(new=True, force=True)
# import n
self.objs = [] #add mesh to this list, LP mesh
for fn in [n]:
cmds.file(fn, i=True)
for o in cmds.ls(type="mesh"):
if not o in self.objs:
self.objs.append(o)
print self.objs
cmds.select(cl=True)
#triangulate mesh
cmds.polyTriangulate('mesh')
#create locator
cmds.spaceLocator(p = [0, 0, 0])
cmds.setAttr("locator1.tx", lock=True, channelBox=True)
cmds.setAttr("locator1.ty", lock=True, channelBox=True)
QtGui.QMessageBox.information(None,
"Great!",
"From side view move locator to cut position .".format(self.intr))
#export chunking
def export_chks(self):
import maya.cmds as cmds
###make chunks
cmds.duplicate('mesh')
cmds.duplicate('mesh')
cmds.duplicate('mesh')
#delete the right side
leftChunks = ['mesh', 'mesh1']
for n in leftChunks:
cmds.select(n)
cmds.polyCut(cutPlaneCenterX = 0 , cutPlaneRotate = [0, 90, 0], deleteFaces = True)
#delete the left side
rightChunks = ['mesh2', 'mesh3']
for p in rightChunks:
cmds.select(p)
cmds.polyCut(cutPlaneCenterX = 0 , cutPlaneRotate = [0, -90, 0], deleteFaces = True)
#get position of locator
loPosition = cmds.pointPosition('locator1', w=True)
#delete front
backChunks = ['mesh', 'mesh2']
for f in backChunks:
cmds.select(f)
cmds.polyCut(cutPlaneCenter = loPosition, cutPlaneRotate = [180, 0, 0], deleteFaces = True)
cmds.rename('mesh', 'back_Left')
cmds.rename('mesh2', 'back_Right')
#delete back
frontChunks = ['mesh1', 'mesh3']
for t in frontChunks:
cmds.select(t)
cmds.polyCut(cutPlaneCenter = loPosition, cutPlaneRotate = [0, 0, 0], deleteFaces = True)
cmds.rename('mesh1', 'front_Left')
cmds.rename('mesh3', 'front_Right')
#trianglate chunks
triMesh = ['back_Left', 'back_Right', 'front_Left', 'front_Right']
for s in triMesh:
cmds.select(s)
cmds.polyTriangulate(s)
"""
export the new mesh
"""
#save out chunks
cmds.select(cl=True)
cmds.select('back_Left')
cmds.file(self.root+"mesh-back-left",force=True, es=True, op="groups=1;ptgroups=0;materials=0;smoothing=0;normals=0", typ="OBJexport")
cmds.select(cl=True)
cmds.select('back_Right')
cmds.file(self.root+"mesh-back-right",force=True, es=True, op="groups=1;ptgroups=0;materials=0;smoothing=0;normals=0", typ="OBJexport")
cmds.select(cl=True)
cmds.select('front_Left')
cmds.file(self.root+"mesh-front-left",force=True, es=True, op="groups=1;ptgroups=0;materials=0;smoothing=0;normals=0", typ="OBJexport")
cmds.select(cl=True)
cmds.select('front_Right')
cmds.file(self.root+"mesh-front-right",force=True, es=True, op="groups=1;ptgroups=0;materials=0;smoothing=0;normals=0", typ="OBJexport")
LPUnion()
这是我的参考资料:
http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html
http://zetcode.com/gui/pyqt4/dialogs/
还有其他人。
提前致谢!!
【问题讨论】:
【参考方案1】:这个问题可能更清楚。如果您试图强制在特定文件夹中打开对话框,则应从 QFileDialog 对象调用QFileDialog.setDirectory
;如果你已经创建了一个,你可以调用它的 setDirectory() 方法,它应该可以工作。
但是你可以做同样的事情
import maya.cmds as cmds
cmds.fileDialog2(dir='path/to/dir', dialogStyle=2, fileMode =4)
fileDialog2命令见here
【讨论】:
对不起,我的问题没有更清楚。我回去并包括我的脚本。我不相信我可以让 fileDialog2 命令工作。如果我错了,请纠正我:)以上是关于如何使用 python 在 Maya 中设置目录?目前使用 PyQt4,但愿意接受任何建议的主要内容,如果未能解决你的问题,请参考以下文章
如何在绝对导入中设置没有点符号的 Pypi 包 - python3