how to rename files in osx from c4d



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/07/2012 at 13:22, xxxxxxxx wrote:

    I'm trying to rename a rendered list of image files that correspond to a list of objects in a c4d scene using os.rename.  My test (DRYRUN=True) works just right to rename the files, but when I try to actually rename the files (DRYRUN=True) I get this error: OSError: [Errno 2] No such file or directory.
    The error references this line: os.rename(PreDirList,nameT).

    I'm not sure if os.rename works with OSX (I'm on a Mac).  Suggestions anyone?  Here is the code:

    import c4d, math, os
    from c4d import gui, utils
      
    def main() :
      
    # Get the root xref's into a list
        c4d.CallCommand(12112) #Select all root objects
        Selected = doc.GetActiveObjects(0)
        theCount = len(Selected)
        xrefSel = []
        for ListOb in Selected:
            nameT = ListOb.GetName() #return the name of an object
            if nameT[0:4] == "xref":
                #print nameT
                xrefSel.append(ListOb)
        #print xrefSel
      
    # Create a list of files from the current directory who's last 4 characters
    # as lowercase are either '.jpg' or '.png'
        path = "/Volumes/Argo3D/Render Index/Universal/Accessory_catalog_Scene/"
        #path=c4d.storage.LoadDialog(c4d.FILESELECTTYPE_ANYTHING, "Please choose a folder", c4d.FILESELECT_DIRECTORY)
        dirList=os.listdir(path)
        dirList = dirList [1:]
        #print path
        files = [f for f in dirList if f[-4:] in ('.jpg','.png') ] #Filer out the image files into a new list
        #print files
      
    # Rename images to the same name as the xrefs in order    
        DRYRUN=False  #!!!!!!!!!!!!!Change this to False when ready to change names!!!!!!!!!!!!!!!!
        x = 1
        for ListObj in xrefSel:
            nameT = ListObj.GetName() #return the name of an object
            nameT = nameT[9:-4] + "_" + "%04d" % (x,) + ".jpg"
            #print nameT
            PreDirList = dirList[x-1]
            if DRYRUN:
                print "Would rename " + PreDirList + " to " + nameT
            else:
                print "Renaming " + PreDirList + " to " + nameT
                os.rename(PreDirList,nameT)
            x += 1
      
    if __name__=='__main__':
        main()
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/07/2012 at 13:27, xxxxxxxx wrote:

    If os.rename would not work on OSX, there would be a statement in the Python documentation. Unfortunately I don't have a MAC nor I currently can get to my Pc to test the code.

    -Nik



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/07/2012 at 13:41, xxxxxxxx wrote:

    Thanks for your reply Nik.  This is what the documentation has to say: Availability: Unix, Windows.

    BUT, after reading this link: http://www.gossamer-threads.com/lists/python/python/913433
    it seamed like os.rename would work in OSX.  I'm curious if my code will work on a PC (when you have your PC in front of you) instead of a Mac.

    How do you rename files from within c4d?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/07/2012 at 13:59, xxxxxxxx wrote:

    > How do you rename files from within c4d?
    Well, using os.rename.

    Why don't you just try it out? Open your Python interpreter and rename a file using os.rename.

    -Nik

    PS: I couldn't test your code anyway, because I don't have the same directory- and file-structure on my PC. ;-)
    PS2: Mac is a Unix based system.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/07/2012 at 14:21, xxxxxxxx wrote:

    I did try renaming using os.rename in my os (OSX) and got this message: _<__<_t_>_OSError: [Errno 2] No such file or directory.  Interestingly I tried this code:

    os.rename(os.path.join(path, PreDirList),nameT)
    

    and received this error: OSError: [Errno 18] Cross-device link.
    I'm new to OSX, so I'm slow at understanding it's methods.
    Thanks for your time and input Nik.  I'm wondering if os.renamer can handle a "/" or a " " in the the path in OSX.  
    Anyone with a Mac able to rename a file within C4D?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/07/2012 at 17:24, xxxxxxxx wrote:

    I finally got it to work...sort of.  When I have my image files at the root of my local drive it works, but not otherwise.  I wonder why?  Here's the code:

    import c4d, math, os
    from c4d import gui, utils
      
    def main() :
      
    # Get the root xref's into a list
        c4d.CallCommand(12112) #Select all root objects
        Selected = doc.GetActiveObjects(0)
        xrefSel = []
        for ListOb in Selected:
            nameT = ListOb.GetName() #return the name of an object
            if nameT[0:4] == "xref":
                xrefSel.append(ListOb)
      
    # Create a list of files from the current directory who's last 4 characters
    # are either '.jpg' or '.png'
        path = "/"
        dirList=os.listdir(path)
        dirList = dirList [1:]
        files = [f for f in dirList if f[-4:] in ('.jpg','.png') ] #Filer out the image files into a new list
      
    # Rename images to the same name as the xrefs in order    
        DRYRUN=False  #!!!!!!!!!!!!!Change this to False when ready to change names!!!!!!!!!!!!!!!!
        x = 1
        for ListObj in xrefSel:
            nameT = ListObj.GetName() #return the name of an object
            nameT = nameT[9:-4] + "_" + "%04d" % (x,) + ".jpg"
            PreDirList = dirList[x-1]
            if DRYRUN:
                print "Would rename " + path + files[x-1] + " to " + nameT
            else:
                print "Renaming " + files[x-1] + " to " + nameT
                os.rename(files[x-1],nameT)
            x += 1
      
    if __name__=='__main__':
        main()
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/07/2012 at 22:34, xxxxxxxx wrote:

    1. os.listdir returns only filenames, relative to the path you passed. It only works for root because python in cinema 4d thinks that CWD is root (print os.getcwd()) and your filenames are not absolute.
    2. use the functions from os.path to concatenate path-names
    3. checking an object for it's name is unsafe, rather check for it's type-id

    I don't think it will have a good learning effect when just giving you the corrected code. :)
    -Nik



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 06/07/2012 at 09:43, xxxxxxxx wrote:

    Thanks for the tips Nik.  I should have enough to go on now to get the path to work for me.  It is frustrating to get Python to do what I want, but the learning is fun.  I'll be back here soon enough with my next eventual question after I have given it a day of trial and error.


Log in to reply