我正在使用操作系统模块和此函数:import os
def make_dir(path):
"""
input a path to check if it exists, if not, it creates all the path
:return: path string
"""
if not os.path.exists(path):
os.makedirs(path)
return path
因此您可以查询:
^{pr2}$
-编辑-
您应该编写以下内容,以便在按下按钮时正确地绑定命令以调用(必须传递函数而不是字符串):# create a function to query your ui text :
def MakeFolder():
path = cmds.textField(tb ,q=True, tx=True)
make_dir(path)
# Use the function in command
cmds.button( label='Button 1', command=MakeFolder)
如果您想在button命令中直接传递一些参数,比如'path',那么必须使用lambda或partial(它更高级一些)。下面是一个链接,其中有一些解释:
-编辑-
这里有一个工作代码:import maya.cmds as cmds
import os
def make_dir(path):
"""
input a path to check if it exists, if not, it creates all the path
:return: path string
"""
if not os.path.exists(path):
os.makedirs(path)
return path
def MakeFolder(*args):
# always put *args to function inside ui command flag because maya pass by default one argument True
userInput = cmds.textField('textBox', q=1, tx=1)
# you should here verify that this path is valid
path = make_dir(userInput)
print('{0} has been created'.format(path))
cmds.window()
cmds.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'right', 0), columnWidth=[(1, 100), (2, 250)] )
cmds.text( label='Name' )
tb = cmds.textField('textBox', tx='E:/Andrew/')
cmds.button( label='Button 1', command=MakeFolder )
cmds.showWindow( )
请记住,这段代码避免:传递ui元素名称和避免嵌套变量,通过命令传递参数。在