New to Python for Maya.... create a txt file with clientname and clienttag

Hello, I am new to Python and can’t see my mistake in the code. I hope someone can help me out.

The Idea:
I want to create a Interface to manage clients by creating a “clientname.txt” file and store the name itself and a 3 digits clienttag on the next txt line in it (thats only one part of the whole concept).

I created a GUI with the QT Designer and saved it as *.ui. This part of the GUI consists of two textfields and a “create” button.

I want to access to the GUI and use it to create a new client.

Here is the code snippet for the file creation:

def createTxtFile():
    print "do something!"
    createNewClientTextField = cmds.textField("createProject_CreateNewClientTextBox", q=True, text=True )
    pathTxtFile = file(pathToSaveClientFile + "%s.txt" %(selectionName),"w" )
selectionName= cmds.textField("createProject_CreateNewClientTextBox", edit=True, text="text")
cmds.button("createProject_CreateNewClientButton", edit=True, command="createTxtFile()")

After executing the code I got a file called “createProject_CreateNewClientButton” :frowning:

All I need is a hint to get the filename…

Thank you in advance!

The file name is coming from

selectionName= cmds.textField("createProject_CreateNewClientTextBox", edit=True, text="text")

You’re editing the field, not querying it, so you’re getting the name of the field. The underscore is a widget path name ‘createProject|CreateNewClientButton’ being converted to a safe file name.

You’ve got a couple of other issues to clean up after that, though: it looks like you are reading the text field into the variable createNewClientTextField but then using an undefined variable (or maybe one that’s in the outer scope) called pathToSaveClientFile to actually name your file.

Also, ‘file’ is not the right function if you want to write a text file in python: you want ‘open’:

with open (path_to_filename, 'wt') as disk_file:

Lastly, because you are using the string name of the function ‘createTxtFile()’ it’s going to execute in the global scope - so you may be getting pathToSaveClientFile and pathToSaveClientFile from somewhere you don’t expect.

You might want to check this for some hints on how to make sure you know the scope of your functions:

Thank you Theodox,

“pathToSaveClientFile” consists a folder path.

I changed the code but I get a None.txt.

def createFile():
    print "do something!"
    createTxtFile= open(pathToSaveClientFile + "%s.txt" % querydText(), "w")

def querydText():
    cmds.textField("TextBox", query=True, text=True)

cmds.button("Button1", edit=True, command="createFile()")

The logic behind this mess is to type a clientname in the textfield and hit the create button “createProjectCreateNewClientButton”. I want to query the input of the textfield “createProjectCreateNewClientTextBox” and use it to set the name of the .txt file and save the file at a specific location (pathToSaveClientFile). I think it’s the querydText function…

You’re getting a None back from querydText(), which will happen if there’s nothing in the text field. You may also have lost track of your text box - without more code it’s hard to tell if you’re actually querying the box you want. Its most common to use a variable to store the address of a control you need to check later, since you can never be sure that Maya has named it what you asked for when creating it.

Here’s a brutally minimalist example:

def choosefile():
     w = cmds.window()
     c = cmds.columnLayout()
     tb = cmds.textField('textBox')

     # because this is defined inside the outer function, it knows what 'tb'  means....
     def save_file(*ignore):
          filetext = cmds.textField(tb, q=True, text=True)
          if not filetext:
              cmds.error("no file name sujpplied ")
          with open(filetext + ".txt", 'wt') as output:
               output.write('hello world')
          print ("%s.txt written" % filetext)
     # this has to come late so that the save_file function is defined
     # note that it's not a string - it executes in the scope of this function
     # so it will remember what 'tb' and 'w' are....
     b = cmds.button(label =  'save',  c= save_file)


Theodox, thank you again for your help.
I attached the ui file (zip file). Here is the complete code:

## Define maya imports
import maya.cmds as cmds

import os
import sys
from PySide import *

## Define path to ui file
pathToGuiFile = "G:\pipeline\pipeline.ui"
pathToProjects = "G:\maya\projects\\"
pathToSaveClientFile = "G:\pipeline\clients\\"

## Load our window and put it into a variable.
class myWin(object):
    def __init__(self):
        self.Window = None
    def pipeCreateUI():
        global pipelineWindow
            if cmds.window(pipelineWindow, exists=True):
                print "window already open!!"
            pipelineWindow = cmds.loadUI(uiFile = pathToGuiFile)
            pipelineWindow = cmds.loadUI(uiFile = pathToGuiFile)
        cmds.window(pipelineWindow, exists = True, topLeftCorner = [400, 600])

def createFile():
    print "do something!"
    createTxtFile= open(pathToSaveClientFile + "%s.txt" % querydText(), "w")

def querydText():
    cmds.textField("TextBox", query=True, text=True)
cmds.button("Button1", edit=True, command="createFile()")

I will check your example. I hope I can adapt your solution to my code. But for now I need to sleep :slight_smile:

Thank you

Theodox thank you so much!!!
I modified the code to work with the UI. Now it is working :slight_smile:

If anyone wanna see it…here is the updated Code:

## Function to create a new Client with clientname and clienttag as .txt file
def saveClientFile(*ignore):
    createNewClient = cmds.textField("createProject_createNewClient", query=True, text=True)
    createClientTag = cmds.textField("createProject_clienttag", query=True, text=True)
    if not createNewClient:
        cmds.error("Please type a Clientname")
    if not createClientTag:
        cmds.error("Please type a Clienttag")
    with open(pathToSaveClientFile + createClientTag + "_" + createNewClient + ".txt", 'wt') as output:
        output.write(createClientTag + "
    print ("Client: %s_" % createClientTag + "%s created" % createNewClient)

createClientButton = cmds.button("createProject_createButton", edit=True, command= saveClientFile)

Two points to do with clientcreation:

  1. I will replace the error msg with an popup window plus info message
  2. An info message should popup if the client exists

thanks again Theodox