一半君的总结纸

听话只听一半君

turn reference on without opening maya file (.mb .ma) ? mayaRefParser

我知道应该用argparse,但是上一个人是用的optparse,所以就能不改就不改了。这里是改进版的mayaRefParser

class MayaRefParser( object ):
    def __init__( self , maFile=None ):
        self.header = ''
        self.refHeader = ''
        self.otherHeader = ''
        self.contents = ''
        self.contentsLineNum = 0
        self.maFile = maFile
        if maFile and os.path.isfile(maFile):
            self.parse(maFile)
            self.parseRefHeader()

    def parse( self , maFile ):
        '''
        Store header refHeader otherHeader and contentsLineNum after parsing
        '''
        with open(maFile) as inFile:

            inHeader = True
            inRefHeader = False
            inOtherHeader = False
            inContents = False
            for line in inFile:

                if inHeader and line.startswith('file -r'):
                    inHeader = False
                    inRefHeader = True
                elif inRefHeader and not line.startswith('file -r'):
                    inRefHeader = False
                    inOtherHeader = True
                elif inOtherHeader and line.startswith('createNode'):
                    inOtherHeader = False
                    inContents = True
                if inHeader:
                    self.header+=line
                elif inRefHeader:
                    self.refHeader+=line
                elif inOtherHeader:
                    self.otherHeader+=line
                elif inContents:
                    break

                self.contentsLineNum+=1

    def parseRefHeader(self):
        '''
        Parse refHeader and get referenced asset namespace / reference node / loaded status
        '''
        import argparse
        import OrderedDict
        parser = argparse.ArgumentParser()
        parser.add_argument('-rfn')
        parser.add_argument('-rdi')
        parser.add_argument('-ns')
        parser.add_argument('-dr')
        parser.add_argument('-r',action='store_true')
        parser.add_argument('file')

        assetInfo=OrderedDict.OrderedDict()
        isLoaded=OrderedDict.OrderedDict()

        for line in self.refHeader.split('\n')[:-1]:

            args = parser.parse_args(line.split()[1:])

            if args.rdi:
                nspace = args.ns.strip('"')                     # Namespace

                aname = args.file.split("/")[-1].split(".")[0]  # Asset Name
                refNode = args.rfn.strip('"')                   # Reference Node
                refIsLoaded = False if args.dr else True        # Is reference loaded

                # Gather asset info
                assetInfo[nspace] = {'asset':aname,'refNode':refNode,'refIsLoaded':refIsLoaded}

                if refIsLoaded:
                    isLoaded[nspace] = {'asset':aname,'refNode':refNode,'refIsLoaded':refIsLoaded}

        self.assetInfo = assetInfo

    def loadRefAndWriteNewMayaFile(self,loadPackages=None,newFile=None):

        with open(newFile,'w') as new_file:

            new_file.write(self.header)

            for line in self.refHeader.split('\n')[:-1]:
                newLine = line

                ns = re.search('(?<=-ns \").+(?=\" -)',line).group()
                if ns in loadPackages:
                    newLine = re.sub('(?<=\" )-dr 1 (?=-rfn)','',line)
                new_file.write(newLine+'\n')

            new_file.write(self.otherHeader)

            with open(self.maFile,'r') as old_file:
                for _ in xrange(self.contentsLineNum):
                    old_file.next()
                for old_line in old_file:
                    new_file.write(old_line)

我又觉得多此一举了,应该用maya自己的selective pre-load里使用的方法把,上面这么大一段显然只能用在.ma上
先用file -f -o -buildLoadSettings xxx.ma , ref信息就存在了 implicitLoadSettings里, 然后selLoadSettings -deferReference 1 “1”就能让id为1的ref不要load(0是要load), 但是这id的顺序不是一定按照.ma文件里出现的顺序的,所以使用了下面的弱智方法得到id.这方法可以读.mb可以读.ma 但是没法得到 namespace..

global proc string[] getRefOrder(string $file){

    string $allRef[]={};
    file -f -o -buildLoadSettings $file;
    int $allRefCount = `selLoadSettings -q  -ns`-1;
    print $allRefCount;
    for($i=1;$i<=$allRefCount;$i++){
        print ("get id: "+("\""+$i+"\"")+"\n");
        print ("selLoadSettings -q  -rfn  "+("\""+$i+"\"")+"\n");
        //string $rfn = `selLoadSettings -q  -rfn ("\""+$i+"\"")`;
        string $rfns[] = eval("selLoadSettings -q  -rfn "+("\""+$i+"\""));
        string $rfn =$rfns[0];
        $allRef[$i-1]=$rfn;
        print($i);
    }
    return $allRef;
}

global proc loadRef(string $asset,int $load,string $allRefOrder[]){
    // This can be called only once, or the implicitLoadSettings would be overwritten

    string $id="";
    string $rfn=$asset+"RN";
    for($i=1;$i<=size($allRefOrder);$i++){
        if ($allRefOrder[$i-1]==$rfn){
            $id = (string)$i;
            break;
        }

    }
    if ($id!=""){
        selLoadSettings -e -deferReference (!$load) $id;
        print ("======================================\n");
        print ("set load ref : " + $asset + " to " + $load + "\n");
    }
    else
        print ("!!!!!!!!!!!!!!!!!!!!!!!!!! not found \n");

}
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: