0

I am beginner in programming, I want to know how can I create a folder by typing the name from textField using python in maya program

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)
Marcel
  • 1,443
  • 2
  • 14
  • 24

1 Answers1

1

I'm using os module and this function :

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

so you can query :

path = cmds.textField(tb ,q=True, tx=True)
make_dir(path)

--- EDIT ---

you should write this in order to properly bind the command to call when button is pressed (must pass the function not a string):

# 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)

if you want to directly pass some arguments like 'path' in the button command, you have to use lambda or partial (it is a bit more advanced). Here is a link with some explanations about that :

more about ui and passing arguments, another example

--- EDIT ---

Here a working code :

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( )

Keep in mind that this code avoid : passing ui elements name and avoiding nesting var, passing arguments throught command.

DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • what is `_folder`? – Netwave Dec 11 '17 at 10:40
  • corrected, it just a bad copy paste of my working code (i had to simplify it for this example) – DrWeeny Dec 11 '17 at 10:42
  • I really appreciate ur answer DrWeeny but I tried it and it didn't solve the problem, what I need is to type a name in the textField (as a user) and click a button, so it creates a new folder with that name in a specific directory. So i need more clarification " And here is my code " import maya.cmds as cmds import os def make_dir(path): path="E:/Andrew/andrew" if not os.path.exists(path): os.makedirs(path) return path def MakeFolder(): path = cmds.textField(tb ,q=True, tx=True) make_dir(path) cmds.button( label='Button 1', command=MakeFolder) – Andrew Ashraf Dec 12 '17 at 11:45
  • okay, I've just edited the answer with a working example but keep in mind this code is very rough – DrWeeny Dec 12 '17 at 14:46