Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. import os
  2. import json
  3. import re
  4. from maya import cmds as mc
  5.  
  6.  
  7. class mFile(object):
  8.     def __init__(self, path, file_name, file_format, indexed, index_padding=4):
  9.         self._path = path
  10.         self._file_name = file_name
  11.         self._file_format = file_format
  12.         self._indexed = indexed
  13.         self._index_padding = index_padding
  14.  
  15.     @property
  16.     def full_path(self):
  17.         if not self._indexed:
  18.             return os.path.join(self._path,
  19.                                 ".".join([self._file_name, self._file_format]))
  20.         else:
  21.             file_index = self._get_file_index()
  22.             file_name = self._file_name + "." \
  23.                 + repr(file_index).zfill(self._index_padding) \
  24.                 + "." + self._file_format
  25.             filePath = os.path.join(self._path, file_name)
  26.             return filePath
  27.  
  28.     def _exists(self):
  29.         if os.path.exists(self.full_path):
  30.             return 1
  31.         else:
  32.             return 0
  33.  
  34.     def _list_files(self):
  35.         if not os.path.exists(self._path):
  36.             raise ValueError("Path %s doesn't exist!" % self._path)
  37.         list_files = []
  38.         if not self._indexed:
  39.             if os.path.exists(self.full_path):
  40.                 list_files.append(self.full_path)
  41.             else:
  42.                 raise ValueError("File %s doesn't exist!" % self.full_path)
  43.         else:
  44.             list_dir = os.listdir(self._path)
  45.             list_files = []
  46.             for dfile in list_dir:
  47.                 file_path = os.path.join(self._path, dfile)
  48.                 if os.path.isfile(file_path):
  49.                     if file_path.endswith("." + self._file_format):
  50.                         list_files.append(file_path)
  51.  
  52.             if list_files:
  53.                 list_files.sort(reverse=0, key=lambda x: int(re.findall(
  54.                     r'\d+', os.path.splitext(os.path.split(x)[1])[0])[-1]))
  55.         return list_files
  56.  
  57.     def _get_file_index(self):
  58.         file_index = 0
  59.         file_list = self._list_files()
  60.         if file_list:
  61.             file_list.sort()
  62.             file_index = int(file_list[-1].split(".")[1])+1
  63.         return file_index
  64.  
  65.     def _get_last_version(self, path):
  66.         pass
  67.  
  68.     def load(self):
  69.         return self._list_files()[-1]
  70.  
  71.     def save(self):
  72.         pass
  73.  
  74.  
  75. class mMayaFile(mFile):
  76.     def __init__(self, path):
  77.         super(mMayaFile, self).__init__(path)
  78.  
  79.     def load(self):
  80.         if self._exists():
  81.             mc.file(self.full_path, f=1, mergeNamespacesOnClash=1, rpr="", i=1)
  82.  
  83.  
  84. class mDictFile(mFile):
  85.     def __init__(self, path, file_name, file_format, indexed, index_padding=4,
  86.                  dictionary={}):
  87.         super(mDictFile, self).__init__(path,
  88.                                         file_name,
  89.                                         file_format,
  90.                                         indexed,
  91.                                         index_padding)
  92.         self._dict = dictionary
  93.  
  94.  
  95. class mJsonFile(mDictFile):
  96.     def __init__(self, path, file_name, file_format, indexed, index_padding=4,
  97.                  dictionary={}):
  98.         super(mJsonFile, self).__init__(path,
  99.                                         file_name,
  100.                                         file_format,
  101.                                         indexed,
  102.                                         index_padding,
  103.                                         dictionary)
  104.  
  105.     def save(self, overwrite=0):
  106.         if not overwrite and self._exists():
  107.             raise ValueError("File already exists!")
  108.  
  109.         else:
  110.             with open(self.full_path, "wb") as data_file:
  111.                 json.dump(self._dict, data_file,
  112.                           sort_keys=True, indent=2)
  113.             data_file.close()
  114.  
  115.     def load(self):
  116.         guide_file = open(self._list_files()[-1], "rb")
  117.         self._dict = json.load(guide_file)
  118.         guide_file.close()
  119.         return self._dict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement