terça-feira, 20 de março de 2018

Python CacheManager for houdini

Python scripts to copy files to another folder if its greater then maximus capacity .
Source this functions using  Houdini by Python Source Editor  then write down  this line in your Pre-Frame Script using python as main language

    hou.session.cacheCopyManager(hou.node('/out/geometry1'), 890409 , "/home/chrysl666/PROJECTS/TEMP")

this function needs a path to the ROP node , a maximus Size in bytes  and the destination folder


script code :



# python scripts to copy files to another folder if its greater then maximus capacity
# source this functions using  Houdini by Python Source Editor
# then put this line in your Pre-Frame Script
#
#     hou.session.cacheCopyManager(hou.node('/out/geometry1'), 890409 , "/home/chrysl666/PROJECTS/TEMP")
#
#     this function needs a path to the ROP node , a maximus Size in kbits 
#     and the destination folder


#get total size of a folder in Kbytes
def get_size(startDirPath):
    import os
   
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(startDirPath):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size

#copy files from a folder to another one
def copyCurrentCache(dirPath , dest ):
    import os
    import shutil

    src_files = os.listdir(dirPath)

    for file_name in src_files:
        full_file_name = os.path.join(dirPath, file_name)
        if (os.path.isfile(full_file_name)):
            shutil.copy(full_file_name, dest)
           

#remove old files from a folder
def removeOldCache(dirPath):
    import os
   
    filelist = [ f for f in os.listdir(dirPath) if f.endswith(".sc") ]
    for f in filelist:
        os.remove(os.path.join(dirPath, f))

# main function for managing cache folder size
def cacheCopyManager(ropNode , maxsize , dest):
    import os
    import hou
   
    #get cacheFolderPath
    outFileParm = ropNode.evalParm("sopoutput")
    dirPath = os.path.split(outFileParm)
   
    #get cacheFolder size in Kb
    dirSize = hou.session.get_size(dirPath[0])
   
    #check if it greather then maxi size
    if dirSize >= maxsize :
        print dirSize
        #if its greather then copy to another folder and delete oldCache
        hou.session.copyCurrentCache(dirPath[0] , dest)
        hou.session.removeOldCache(dirPath[0])