Hello, I’m trying to develop a little pipeline in Maya with Python.
The scope of this tool is very simple. Here the thing:
- A modeler after has made his model in Maya runs the tool, a prompt window appears and into it he needs just to type the name of file.
- At this point the file will be saved in two different folder on a server, just call them, update and backup, what fantasy, isn’t? :D:
They are two copies of the same file, but in the update folder will be only one instance of the scene file without its History, while in the backup folder will be the file with that indexed name and its whole History.
My code seems to work properly, but there’s a problem with the indexes when I save a scene with different name or save the egual scene with same name at some point after.
Well, we suppose to have in the update folder a file named “Anto” and in the back folder we already have “Anto_1” and “Anto_2”, if I save another scene, said “Bobo”, it will be saved not with “Bobo_1” but, instead, with “Bobo_3” or if I do another saving of “Anto” after that I will have “Anto_4” an so on, I need to correct this behaviour.
Here my code, I’m doing my endeavours straight on my computer for trying to avoid other technical issues of different order.
import maya.cmds as cmds
import dircache
update_dir = "C:\Users\ASUS\Desktop\ProvaPython\\"
backup_dir = "C:\Users\ASUS\Desktop\ProvaPython1\\"
file_list = dircache.listdir(backup_dir)
unique_project_list = []
list_lenght = len(file_list) + 1
selected_item = 0
fname = ''
counter = 0
status = 'No'
#copy function definition
def copy_file(fname):
cmds.file(rename = (update_dir + fname))
cmds.delete(ch = True)
cmds.file(save = True, type = 'mayaAscii')
cmds.file(rename = (backup_dir + fname + "_" + str(list_lenght)))
cmds.file(save = True, type = 'mayaAscii')
cmds.confirmDialog(title='Saved', message='Save success', defaultButton='Close', cancelButton='Close')
return
print("
")
while counter < len(file_list):
if file_list[counter][0 : file_list[counter].index( '_' ) ] not in unique_project_list:
unique_project_list.append (file_list[counter][0 : file_list[counter].index( '_' ) ])
counter = counter + 1
while counter < len(unique_project_list):
print(str(counter) + " = " + unique_project_list[counter])
counter = counter + 1
selected_item = cmds.promptDialog(title='Choose an option', message='Enter displayed number:' + ' ' + str(len(unique_project_list)) + ' ' + 'for a new scene', button=['OK', 'Cancel'], defaultButton='OK', cancelButton='Cancel', dismissString='Cancel')
fname = raw_input('type a name file for backup
')
if fname not in unique_project_list:
copy_file(fname)
elif fname in unique_project_list:
status = cmds.confirmDialog(title='Confirm', message='WARNING!!:' + fname + ' ' + 'already exist.' + ' ' + 'Do you want to overwrite it?', button=['Yes','No'], defaultButton='Yes', cancelButton='No')
if status == 'Yes':
fname = raw_input('type a name file for backup
') #cmds.promptDialog(title='Save File', message='Type a name file for backup:', button=['Save', 'Cancel'], defaultButton='Save', cancelButton='Cancel', dismissString='Cancel')
copy_file(fname)
else:
cmds.confirmDialog(title='Close', button=['Close'], defaultButton='Close', cancelButton='Close')
Now I was thinking to make a test into the function copy_file() I have defined, before to run those commands that save a file in the backup folder. Here the code of my idea:
import maya.cmds as cmds
import dircache
backup_dir = "C:\Users\ASUS\Desktop\ProvaPython1\\"
file_list = dircache.listdir(backup_dir)
fname = 'Anto'
unique_project_list = []
sub_backup_list = []
print file_list[0][0 : file_list[0].index( '_' ) ]
counter
while counter < len(file_list):
if file_list[counter][0 : file_list[counter].index( '_' ) ] == fname:
sub_backup_list.append (file_list[counter])
print file_list[counter]
counter = counter + 1
print ("
")
print file_list
print sub_backup_list
print len(sub_backup_list)
So, from “file_list” I can set apart another list which I called “sub_backup_list” where it will be only the file instances with the current name stored in the “fname” variable and counting them by len(sub_backup_list), I want to use this information for generating an index, just the sublist’s lenght, and using it in the command inside copy_file(), as cmds.file(rename = (backup_dir + fname + “_” + str(list_lenght))) ,but str(list_lenght) returns me an integer value one unit back than len(sub_backup_list). How can I fix it?
Thanks a lot for any help or suggestions and I’m sorry again for grammar mistakes or improper language eventualy, as I already said the English is just a second language for me.
Gabriele.