如何在Maya中使用Python设置目录?目前使用PyQt4,欢迎任何建议
我需要在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 个回答
1
这个问题可以更清楚一些。如果你想强制一个对话框在特定的文件夹中打开,应该从一个QFileDialog对象调用QFileDialog.setDirectory
;如果你已经创建了一个QFileDialog对象,可以直接调用它的setDirectory()方法,这样就能正常工作了。
不过,你也可以用下面的方式做到这一点:
import maya.cmds as cmds
cmds.fileDialog2(dir='path/to/dir', dialogStyle=2, fileMode =4)
关于fileDialog2命令的详细信息,可以查看这里