Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import json
- import re
- from maya import cmds as mc
- class mFile(object):
- def __init__(self, path, file_name, file_format, indexed, index_padding=4):
- self._path = path
- self._file_name = file_name
- self._file_format = file_format
- self._indexed = indexed
- self._index_padding = index_padding
- @property
- def full_path(self):
- if not self._indexed:
- return os.path.join(self._path,
- ".".join([self._file_name, self._file_format]))
- else:
- file_index = self._get_file_index()
- file_name = self._file_name + "." \
- + repr(file_index).zfill(self._index_padding) \
- + "." + self._file_format
- filePath = os.path.join(self._path, file_name)
- return filePath
- def _exists(self):
- if os.path.exists(self.full_path):
- return 1
- else:
- return 0
- def _list_files(self):
- if not os.path.exists(self._path):
- raise ValueError("Path %s doesn't exist!" % self._path)
- list_files = []
- if not self._indexed:
- if os.path.exists(self.full_path):
- list_files.append(self.full_path)
- else:
- raise ValueError("File %s doesn't exist!" % self.full_path)
- else:
- list_dir = os.listdir(self._path)
- list_files = []
- for dfile in list_dir:
- file_path = os.path.join(self._path, dfile)
- if os.path.isfile(file_path):
- if file_path.endswith("." + self._file_format):
- list_files.append(file_path)
- if list_files:
- list_files.sort(reverse=0, key=lambda x: int(re.findall(
- r'\d+', os.path.splitext(os.path.split(x)[1])[0])[-1]))
- return list_files
- def _get_file_index(self):
- file_index = 0
- file_list = self._list_files()
- if file_list:
- file_list.sort()
- file_index = int(file_list[-1].split(".")[1])+1
- return file_index
- def _get_last_version(self, path):
- pass
- def load(self):
- return self._list_files()[-1]
- def save(self):
- pass
- class mMayaFile(mFile):
- def __init__(self, path):
- super(mMayaFile, self).__init__(path)
- def load(self):
- if self._exists():
- mc.file(self.full_path, f=1, mergeNamespacesOnClash=1, rpr="", i=1)
- class mDictFile(mFile):
- def __init__(self, path, file_name, file_format, indexed, index_padding=4,
- dictionary={}):
- super(mDictFile, self).__init__(path,
- file_name,
- file_format,
- indexed,
- index_padding)
- self._dict = dictionary
- class mJsonFile(mDictFile):
- def __init__(self, path, file_name, file_format, indexed, index_padding=4,
- dictionary={}):
- super(mJsonFile, self).__init__(path,
- file_name,
- file_format,
- indexed,
- index_padding,
- dictionary)
- def save(self, overwrite=0):
- if not overwrite and self._exists():
- raise ValueError("File already exists!")
- else:
- with open(self.full_path, "wb") as data_file:
- json.dump(self._dict, data_file,
- sort_keys=True, indent=2)
- data_file.close()
- def load(self):
- guide_file = open(self._list_files()[-1], "rb")
- self._dict = json.load(guide_file)
- guide_file.close()
- return self._dict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement