首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError,在Maya中使用Python创建目录

TypeError,在Maya中使用Python创建目录
EN

Stack Overflow用户
提问于 2021-01-04 19:17:54
回答 1查看 89关注 0票数 0

我正在尝试用Python在Maya中创建一个文件夹来保存文件。然而,我得到了一个错误,我不确定如何解决。(在脚本编写方面还不是很有经验)

这是创建目录字符串的代码:(Maya正确地打印出目录)

代码语言:javascript
复制
# Creates a directory to save the .json file to
USERAPPDIR = cmds.internalVar(userAppDir = True)
DIRECTORY = os.path.join(USERAPPDIR, "gradingManager")
print('Maya application directory: ', DIRECTORY)

创建目录的功能由Maya中的按钮控制:

代码语言:javascript
复制
    ##########################
    # Safe data in json file #
    ##########################
    cmds.text(label = "")
    cmds.text(label = " Save all results to a .json file for record keeping.",
              font = "boldLabelFont")
    cmds.button(label = "Save to .json file", command = self.save, width = 600)

实际的函数:

代码语言:javascript
复制
def save(self, directory=DIRECTORY, *args):
    ######################################################################
    ## This method saves the information gathered above in a .json file ##
    ######################################################################

    # creates a directory
    self.createDir(directory)

    print("saving things")

def createDir(self, directory=DIRECTORY):
    ###################################################################
    ## This function creates a directory for the save functionality. ##
    ###################################################################

    if not os.path.exists(directory):
        os.mkdir(directory)

它引用的错误函数(&F):

代码语言:javascript
复制
# Error: TypeError: file C:\Program Files\Autodesk\Maya2020\bin\python27.zip\genericpath.py line 26: coercing to Unicode: need string or buffer, bool found #

# Does a path exist?
# This is false for dangling symbolic links on systems that support them.
def exists(path):
    """Test whether a path exists.  Returns False for broken symbolic links"""
    try:
        os.stat(path)
    except os.error:
        return False
    return True

我希望这是足够的信息。我尽可能保持函数的整洁,所以很明显,问题来自于检查新目录的路径是否已经存在。如果你需要更多的信息,我很乐意提供。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-04 21:55:12

当你按下按钮时,你会期望它运行save(some_path)save(),其默认值为DIRECTORY,但它不是这样工作的。

按钮使用Maya作者预定义的某些值执行函数。我不知道在Maya中发送按钮到save()的值是什么,但在其他GUI中,它通常会发送事件信息。点击了什么小部件,使用了什么鼠标按钮,鼠标位置是什么,等等。

所以看起来按钮执行save(True),或者甚至是save(True, other values),然后在你的def save(self, dictionary, ...)中分配True到字典,然后它运行createDir(True) and exists(True)`,你就会得到错误信息。

你应该在函数中直接使用DIRECTORY

代码语言:javascript
复制
def save(self, *args):

    directory = DIRECTORY

    # creates a directory
    self.createDir(directory)

    print("saving things")

如果您有一些要选择目录或手动编写目录小部件,那么您还必须在函数中使用它

代码语言:javascript
复制
def save(self, *args):

    directory = some_widget_to_select_folder.get_selected_folder()
   
    if not directory:
        directory = DIRECTORY

    # creates a directory
    self.createDir(directory)

    print("saving things")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65561800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档