Maya ma文件支持ASCII格式,可以直接用记事本打开,可以很快分析出文件参考的哪些文件,在替换资产、更新文件上可以使用
from __future__ import print_function
import argparse
import os
import re
class MayaRefParser(object):
def __init__(self, ma_file=None):
self.header = ''
self.refHeader = ''
self.otherHeader = ''
self.contents = ''
self.contentsLineNum = 0
self.ma_file = ma_file
self.layoutIsLoaded = False
self._need_update_files = []
if ma_file and os.path.isfile(ma_file):
self.parse(ma_file)
def parse(self, ma_file):
'''
Store header refHeader otherHeader and contentsLineNum after parsing
'''
with open(ma_file) as inFile:
# print("open file {}".format(ma_file))
inHeader = True
inRefHeader = False
inOtherHeader = False
inContents = False
for line in inFile:
# if line.startswith('fileInfo "UUID"'):
# print(line)
if inHeader and line.startswith('file -r'):
inHeader = False
inRefHeader = True
elif inRefHeader and not line.startswith('file -r') and not line.startswith('\t'):
inRefHeader = False
inOtherHeader = True
elif inOtherHeader and line.startswith('createNode'):
inOtherHeader = False
inContents = True
elif line.startswith('fileInfo "UUID"'):
inHeader = False
inContents = True
if inHeader:
self.header += line
elif inRefHeader:
self.refHeader += line
elif inOtherHeader:
self.otherHeader += line
elif inContents:
break
self.contentsLineNum += 1
# print("close file {}".format(ma_file))
def _par_ref_header(self, header):
parser = argparse.ArgumentParser()
parser.add_argument('-rfn')
parser.add_argument('-rdi')
parser.add_argument('-ns')
parser.add_argument('-dr')
parser.add_argument('-typ')
parser.add_argument('-op')
# group = parser.add_mutually_exclusive_group()
parser.add_argument('-r', action='store_true')
parser.add_argument('file')
parser.add_argument('-rpr')
parser.add_argument('-shd', action='append')
# args = parser.parse_args(header)
_space_re = re.compile(r'["](.*?)["]', re.S) #最小匹配
_spaces = re.findall(_space_re, header)
if _spaces:
for _space in _spaces:
if " " in _space:
header = header.replace(_space, _space.replace(" ", ""))
args = parser.parse_args(header.split()[1:])
# args.file = args.file.replace("'","")
_old_file, _new_file = self.get_no_version(args.file)#要在外面重构,结合自己的环境用来替换文件
return _old_file, _new_file