我正在尝试用Python在Maya中创建一个文件夹来保存文件。然而,我得到了一个错误,我不确定如何解决。(在脚本编写方面还不是很有经验)
这是创建目录字符串的代码:(Maya正确地打印出目录)
# 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中的按钮控制:
##########################
# 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)实际的函数:
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):
# 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我希望这是足够的信息。我尽可能保持函数的整洁,所以很明显,问题来自于检查新目录的路径是否已经存在。如果你需要更多的信息,我很乐意提供。
发布于 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。
def save(self, *args):
directory = DIRECTORY
# creates a directory
self.createDir(directory)
print("saving things")如果您有一些要选择目录或手动编写目录小部件,那么您还必须在函数中使用它
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")https://stackoverflow.com/questions/65561800
复制相似问题