Incremental saving

43 views
Skip to first unread message

likage

unread,
Feb 17, 2015, 6:48:34 AM2/17/15
to python_in...@googlegroups.com
This is somewhat related to my batch process that I have posted but it is slightly different. I am trying to reuse some of the incremental saves script that I once used to have and have it implemented into use within the treeWidget and pushButton

Initially, I has something like this:
def importAndSave(self):
    root
= self.ui.treeWidget_migrateAnm.invisibleRootItem()
    count
= root.childCount()
               
   
for i in range(count):
        item
= root.child(i)
        ns
= item.text(1)
           
       
# For each item in list, it will be saved into each file
       
# Have to force create a new scene
        cmds
.file(f=True, new=True)
        cmds
.file(rename= str(ns))
           
       
# Import Rig commands

       
# ....


       cmds
.file(f=True, type='mayaAscii', save=True )

However the file saved directory is not what I had wanted, as I wanted it to be somewhere temporary and have it removed once the publishing fiasco is over...
I also noticed that in the event if I had multiple of the same items, since I am using the ns for the naming, I thought of adding in some sort of incremental values and tried changing the code to:

def importAndSave(self):
    root
= self.ui.treeWidget_migrateAnm.invisibleRootItem()
    count
= root.childCount()
           
   
for i in range(count):
        item
= root.child(i)
        ns
= item.text(1)
       
       
# Import Rig Commands
       
# ....
       
       
self.tmpSave()
       
def tmpSave(self):
    path
= '/user_data/.tmp/anm_tmpFiles'
    newName
= name + "_0001" + ".ma"
    newSaveDir
= os.path.abspath(os.path.join(path))
    newSavePath
= os.path.join(newSaveDir, newName)


   
if os.path.exists(newSaveDir):
        savedFiles
= os.listdir(newSaveDir)
       
print savedFiles
        savedFiles
.sort()
        incFiles
= []
       
for i in savedFiles:
           
if fileName in i:
                incFiles
.append(i)
        lastFile
= incFiles[len(incFiles)-1]
        name
= lastFile.partition(".")[0]
        newFileName
= name[:-4]+(str(int(name[-4:])+1).zfill(4))+".ma"
        incSaveFilePath
= os.path.join(newSaveDir, newFileName)
        system
.saveAs(incSaveFilePath)

And this time round, I got the following traceback errors:
# Traceback (most recent call last):
#   File "/user_data/dev_test/anm/versions/anmTool_v02a.py", line 211, in test
#     self.tmpSave()
#   File "/user_data/dev_test/anm/versions/anmTool_v02a.py", line 232, in tmpSave
#     newSavePath = os.path.join(newSaveDir, newName)
#   File "/apps/Linux64/aw/maya2014/lib/python2.7/posixpath.py", line 66, in join
#     if b.startswith('/'):
# AttributeError: 'QString' object has no attribute 'startswith'


Or perhaps is there a better way to approach this?

David Moulder

unread,
Feb 17, 2015, 11:53:45 AM2/17/15
to python_inside_maya
It's hard to tell from the above code.  But I think newName = name + "_0001" + ".ma" is the source of your problem.  The var "name " is a QString object and not a python str.  The concatenation is returning another QString   So I think you should cast it to a string 1st,  str(name).

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/0116e3c5-a06d-46cc-8413-a93b82026179%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

likage

unread,
Feb 18, 2015, 12:02:15 AM2/18/15
to python_in...@googlegroups.com
Hi there,

you are right. it seems to have bypass the error, never know about QString as I thought it will still work since it is still string..

However I have a problem in the "lastFile" line, especially so if there isn't a file to begin with.

For example, if there is already a "AvenueA_0001.ma" in the directory, it will automatically creates a "AvenueA_0002.ma" file, so on and forth. However say if I have a new file with the naming of "AvenueB", instead of creating "AvenueB_0001.ma", it will gives me the following error:

# Traceback (most recent call last):
#   File "/user_data/dev_test/anmg/versions/anmgTool_v02a.py", line 211, in test
#     self.tmpSave()
#   File "/user_data/dev_test/anmg/versions/anmgTool_v02a.py", line 246, in tmpSave
#     lastFile = incFiles[len(incFiles)-1]
# IndexError: list index out of range

Justin Israel

unread,
Feb 18, 2015, 12:08:16 AM2/18/15
to python_in...@googlegroups.com
Thats because you aren't checking your results after you list the directory contents, and then you are trying to index into an empty list:

       savedFiles = os.listdir(newSaveDir)
        
print savedFiles
        savedFiles
.sort()
        incFiles 
= []
        
for i in savedFiles:
            
if fileName in i:
                incFiles
.append(i)
        lastFile 
= incFiles[len(incFiles)-1]

You have to handle the case where it is an empty directory.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

likage

unread,
Feb 18, 2015, 1:09:51 AM2/18/15
to python_in...@googlegroups.com
Ah okay, damn I just found another problem.
Just when I thought it is increment-ing, it can only be incremented up to 0002 only, despite there is already a xxx_0002.ma within the directory.
The last file show is always 0001 instead of 0002. I was thinking if this is a hassle, I may as well just have the file save the with "row index" as it is listed within its QTreeWidget?

Will that be better or not practical?


On Wednesday, February 18, 2015 at 1:08:16 PM UTC+8, Justin Israel wrote:
Thats because you aren't checking your results after you list the directory contents, and then you are trying to index into an empty list:

       savedFiles = os.listdir(newSaveDir)
        
print savedFiles
        savedFiles
.sort()
        incFiles 
= []
        
for i in savedFiles:
            
if fileName in i:
                incFiles
.append(i)
        lastFile 
= incFiles[len(incFiles)-1]

You have to handle the case where it is an empty directory.


On Wed Feb 18 2015 at 6:02:20 PM likage <dissid...@gmail.com> wrote:
Hi there,

you are right. it seems to have bypass the error, never know about QString as I thought it will still work since it is still string..

However I have a problem in the "lastFile" line, especially so if there isn't a file to begin with.

For example, if there is already a "AvenueA_0001.ma" in the directory, it will automatically creates a "AvenueA_0002.ma" file, so on and forth. However say if I have a new file with the naming of "AvenueB", instead of creating "AvenueB_0001.ma", it will gives me the following error:

# Traceback (most recent call last):
#   File "/user_data/dev_test/anmg/versions/anmgTool_v02a.py", line 211, in test
#     self.tmpSave()
#   File "/user_data/dev_test/anmg/versions/anmgTool_v02a.py", line 246, in tmpSave
#     lastFile = incFiles[len(incFiles)-1]
# IndexError: list index out of range

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Feb 18, 2015, 4:28:23 AM2/18/15
to python_in...@googlegroups.com

I don't really know what to suggest right now, because your earlier pasted example seemed like pseudo code, since it contained a number of inconsistencies. Really only seeing fully working code is going to allow help with debugging. Obviously there is a right way to implement file name incrementing.


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e9637a6a-56e1-4182-928a-a268f6ad75f2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages