我是编程的初学者,我想知道如何通过在maya程序中使用python在textField中键入名称来创建文件夹
import maya.cmds as cmds
cmds.window()
cmds.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'right', 0), columnWidth=[(1, 100), (2, 250)] )
cmds.text( label='Name' )
tb = cmds.textField('textBox')
cmds.button( label='Button 1', command='MakeFolder()' )
cmds.showWindow( )
def MakeFolder():
cmds.sysFile("E:/test/folder/%s" , makeDir=True)
答案 0 :(得分:1)
我使用os模块和这个功能:
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
所以你可以查询:
path = cmds.textField(tb ,q=True, tx=True)
make_dir(path)
---编辑---
你应该写这个,以便在按下按钮时正确绑定命令以调用(必须传递函数而不是字符串):
# 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命令中,你必须使用lambda或partial(它有点高级)。这是一个链接,其中包含一些解释:
more about ui and passing arguments, another example
---编辑---
这是一个有效的代码:
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元素名称并避免嵌套var,通过命令传递参数。