Sep 12 2012

Exporting .pc2 files straight out of maya..

So recently I’ve been working on maya to max pipeline…
i ran into some issues with the built in cacheFile mechanism in maya so i decided that seance .pc2 is such a simple binary format that i could just write my own .pc2 exporter!

here’s what i came up with 🙂

ps. this heavily borrowed from the blender importer written by “Matt Ebb” http://mattebb.com

import maya.cmds as cmds
import struct

def frange(x, y, jump,):
    ar =[]
    while x < y:
        ar.append(x)
        x += jump
    return ar

    
def writePC2File(filename, shape, world=True, zUp=False, startF=0, endF=100, samplesPerFrame=1):
    sampleRate = 1.0/samplesPerFrame
    numPoints = cmds.polyEvaluate(shape,v=True)#len(me.verts)
    totalFrames = endF - startF
    timeRange = frange(startF, startF+totalFrames+sampleRate, sampleRate)
    numSamples = len(timeRange)
    
    headerFormat='<12ciiffi'
    headerStr = struct.pack(headerFormat, 'P','O','I','N','T','C','A','C','H','E','2','\0', 1, numPoints, startF, sampleRate, numSamples)
    
    file = open(filename, "wb")
    file.write(headerStr)
    #---loop through time..
    cmds.refresh(su=True)
    sampleFrames = [] #--generate sample frames...
    
    print "startF=%s endF=%s sampleRate=%s shape=%s" % (startF, startF+numSamples, sampleRate,shape)
    #print "range is ",range(startF, startF+numSamples, sampleRate)

    for i in timeRange:
        #print "time is ",i
        cmds.currentTime(i)
        for p in range(numPoints):
            vPos = cmds.pointPosition( shape+'.vtx['+str(p)+']',w=True)
            vFormat = "fff" #-- add a "less than" character at the begining of the string.. i removed it becuse my wordpress code syntax was doing goofy stuff"
            thisVertex = struct.pack(vFormat, vPos[0], vPos[1], vPos[2])
            file.write(thisVertex)

    
    cmds.refresh(su=False)
    file.flush()
    file.close()
    print filename," was sucessfully written out"
    return True
    
#example    
out = r'C:\testMaya\test.pc2'
shp = cmds.listRelatives(shapes=True)
writePC2File(out,shp[0],startF=1000,endF=1200,samplesPerFrame=2)