如何使用PySide2和Python在Maya中设置导出文件的路径

huangapple go评论83阅读模式
英文:

How to set path for exporting files from Maya using PySide2 ad Python

问题

以下是您要的代码的中文翻译:

我正在学习Python同时尝试使用Python和PySide为Maya开发辅助工具 (相当雄心勃勃但没有挑战就没有进步对吧)

基本上我正在编写一个工具可以帮助我将动画导出为fbx文件并保存到我在PC上指定的任何位置

我已经完成了导出过程的编写在这里应该没有问题我卡在了UI部分

[![输入图像描述](https://i.stack.imgur.com/1RCVY.jpg)](https://i.stack.imgur.com/1RCVY.jpg)

目前我的UI看起来是这样的我想能够设置导出文件的位置路径

[![输入图像描述](https://i.stack.imgur.com/0wgrq.jpg)](https://i.stack.imgur.com/0wgrq.jpg)

当我选择路径并点击"选择文件夹"我希望所选文件夹的路径显示在UI的文本行中"记住"这样当我点击"导出FBX动画""导出骨骼"它将使用该路径并将文件保存在那里

但我不知道如何做到这一点有人可以帮助我这个无知的人如何实现这一点吗

我会感激任何帮助谢谢 :)

以下是我的当前代码

```python
import CreatureAnimBakeProcess
reload(CreatureAnimBakeProcess)
from maya import cmds
import os
from PySide2 import QtWidgets, QtCore, QtGui

class CreatureAnimBakeUI(QtWidgets.QDialog):

    def __init__(self):
        super(CreatureAnimBakeUI, self).__init__()

        self.setWindowTitle('生物导出器')
        self.library = CreatureAnimBakeProcess.CreatureExport()
        self.buildUI()

    def buildUI(self):
        print('构建UI')
        layout = QtWidgets.QVBoxLayout(self)
        setPathWidget = QtWidgets.QWidget()
        setPathLayout = QtWidgets.QVBoxLayout(setPathWidget)

        layout.addWidget(setPathWidget)
        self.setPathField = QtWidgets.QLineEdit()
        setPathLayout.addWidget(self.setPathField)
        setBtn = QtWidgets.QPushButton('设置导出文件夹')
        setBtn.clicked.connect(self.setPath)
        setPathLayout.addWidget(setBtn)

        #============================================

        btnWidget = QtWidgets.QWidget()
        btnLayout = QtWidgets.QVBoxLayout(btnWidget)
        layout.addWidget(btnWidget)

        ExportFBXBtn = QtWidgets.QPushButton('导出FBX动画')
        ExportFBXBtn.clicked.connect(self.exportFbxAnim)
        btnLayout.addWidget(ExportFBXBtn)

        ExportRIGBtn = QtWidgets.QPushButton('导出骨骼')
        ExportRIGBtn.clicked.connect(self.exportRIG)
        btnLayout.addWidget(ExportRIGBtn)

    def getDirectory(self):
        directory = os.path.join(cmds.internalVar(userAppDir=True), 'Animation')
        if not os.exists(directory):
            os.mkdir(directory)
        return

    def setPath(self):
        directory = self.getDirectory()
        pathName = QtWidgets.QFileDialog.getExistingDirectory(self, directory, "生物导出器")
        return

    def exportFbxAnim(self):
        pass

    def exportRIG(self):
        pass

def showUI():
    ui = CreatureAnimBakeUI()
    ui.show()
    return ui

希望这个翻译对您有帮助。如果有其他问题,请随时提出。

英文:

I'm learning Python while trying to develop helper tools for Maya using Python and PySide. (Quite ambitious but there is no progress without challenges, right)

Basically, I'm writing a tool that would help me export animation as fbx files to a folder I set in any given location on my PC.

I've finished to write the export process. There should be no issue here, I'm stuck with the UI part of it.

如何使用PySide2和Python在Maya中设置导出文件的路径

Currently this is how my UI is looking. I want to be able to set the path to the place where I want the script to export the files.
如何使用PySide2和Python在Maya中设置导出文件的路径

When I select the path and press "Select Folder", I want the path of the selected folder to display in the text line of the UI. And "Remember" it so that when I press Export FBX animation or Export rig it would use that path and would save files there.

But I have no clue how to do that. Can anyone help the clueless me how to make this happen?

I would appreciate any help. Thank you )

Here is my current code:

`

import CreatureAnimBakeProcess
reload(CreatureAnimBakeProcess)
from maya import cmds
import os
from PySide2 import QtWidgets, QtCore, QtGui
class CreatureAnimBakeUI(QtWidgets.QDialog):
def __init__(self):
super(CreatureAnimBakeUI, self).__init__()
self.setWindowTitle('Creature Exporter')
self.library = CreatureAnimBakeProcess.CreatureExport()
self.buildUI()
def buildUI(self):
print 'building ui'
layout = QtWidgets.QVBoxLayout(self)
setPathWidget = QtWidgets.QWidget()
setPathLayout = QtWidgets.QVBoxLayout(setPathWidget)
layout.addWidget(setPathWidget)
self.setPathField = QtWidgets.QLineEdit()
setPathLayout.addWidget(self.setPathField)
setBtn = QtWidgets.QPushButton('Set Export Folder')
setBtn.clicked.connect(self.setPath)
setPathLayout.addWidget(setBtn)
#============================================
btnWidget = QtWidgets.QWidget()
btnLayout = QtWidgets.QVBoxLayout(btnWidget)
layout.addWidget(btnWidget)
ExportFBXBtn = QtWidgets.QPushButton('Export FBX Animation')
ExportFBXBtn.clicked.connect(self.exportFbxAnim)
btnLayout.addWidget(ExportFBXBtn)
ExportRIGBtn = QtWidgets.QPushButton('Export RIG')
ExportRIGBtn.clicked.connect(self.exportRIG)
btnLayout.addWidget(ExportRIGBtn)
return
def getDirectory(self):
directory = os.path.join(cmds.internalVar(userAppDir=True), 'Animation')
if not os.path.exists(directory):
os.mkdir(directory)
return
def setPath(self):
directory = self.getDirectory()
pathName = QtWidgets.QFileDialog.getExistingDirectory(self, directory, "Creature Exporter")
return
def exportFbxAnim(self):
pass
def exportRIG(self):
pass
def showUI():
ui = CreatureAnimBakeUI()
ui.show()
return ui

`

答案1

得分: 1

你可以使用 self.setPathField.setText(pathName) 来显示该数值,你也可以将它分配给 self.exportPath = pathName 以便重复使用。

我只展示了我更改的代码片段,你的其余代码不需要更改。

    def __init__(self):
        super(CreatureAnimBakeUI, self).__init__()

        # 将其分配为None
        self.exportPath = None  # 在这里更改

        self.setWindowTitle("Creature Exporter")
        self.buildUI()
    def setPath(self):
        directory = self.getDirectory()
        pathName = QtWidgets.QFileDialog.getExistingDirectory(
            self, directory, "Creature Exporter"
        )
        if pathName:  # 在这里更改
            # "记住该值"
            self.exportPath = pathName
            # 在UI的文本行中显示它
            self.setPathField.setText(pathName)
        return

self.exportPath 最初将为 None,一旦你选择一个文件夹,它将保存该值。因此,这个 if 检查是否该值未设置,并强制用户设置一个值,如果已经设置,将使用该值。

    def exportFbxAnim(self):  # 在这里更改
        if self.exportPath is None:
            # 如果self.exportPath为None,则调用self.setPath
            self.setPath()

        # self.exportPath应该包含你期望的值

    def exportRIG(self):  # 在这里更改
        if self.exportPath is None:
            # 如果self.exportPath为None,则调用self.setPath
            self.setPath()

        # self.exportPath应该包含你期望的值
英文:

You can use self.setPathField.setText(pathName) to show the value, you can also assign it to self.exportPath = pathName so you can re use it.

I am only showing code snippets where I changed, the remainder of your code needs no changes.

    def __init__(self):
super(CreatureAnimBakeUI, self).__init__()
# have it assigned to None
self.exportPath = None  # changed here
self.setWindowTitle("Creature Exporter")
self.buildUI()
    def setPath(self):
directory = self.getDirectory()
pathName = QtWidgets.QFileDialog.getExistingDirectory(
self, directory, "Creature Exporter"
)
if pathName:  # changed here
# "remember the value"
self.exportPath = pathName
# show it in the text line of the UI
self.setPathField.setText(pathName)
return

self.exportPath will be None initially, once you select a folder it will have that value saved. So this if checks if the value is not set and forces the user to set a value, if it is already set that value will be used.

    def exportFbxAnim(self):  # changed here
if self.exportPath is None:
# call self.setPath if self.exportPath is None
self.setPath()
# self.exportPath should have the value you expect
def exportRIG(self):  # changed here
if self.exportPath is None:
# call self.setPath if self.exportPath is None
self.setPath()
# self.exportPath should have the value you expect

huangapple
  • 本文由 发表于 2023年2月16日 17:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定