簡體   English   中英

如何使用python在Maya中設置目錄? 目前正在使用PyQt4,但歡迎提出任何建議

[英]How do you set the directory in Maya with python? Currently using PyQt4 but open to any suggestions

我需要在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/

和別的。

提前致謝!!

這個問題可能更清楚。 如果你試圖迫使一個對話框打開特定文件夾, QFileDialog.setDirectory是應該從QFileDialog對象調用; 如果已經創建了一個,則可以調用其setDirectory()方法,它應該可以工作。

但是,您可以使用

import maya.cmds as cmds
cmds.fileDialog2(dir='path/to/dir', dialogStyle=2, fileMode =4)

有關fileDialog2命令,請參見此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM