#StemCache
#==========
#Base class for particle data gathering
  
import math
  
#============================================
class StemCache( ):
    def __init__( self ):
        self.data = []
        self.dataStr = ''
        self.dataPath = ''
  
    def reset(self, pnum):
        self.data = []
        self.dataStr = ''
        print "I am resetting"
        print "self.data is %s" % self.data
        print "self.dataStr is '%s'" % self.dataStr
        self.data = ([1] * pnum)
        newLength = len(self.data)
        print "the new length of self.data is %s" % newLength
  
    def addIndiv(self, particleNum, pos):
        if self.data[particleNum] == 1:
            self.data[particleNum] = []
        self.data[particleNum].append(pos[0])
        self.data[particleNum].append(pos[1])
        self.data[particleNum].append(pos[2])
    
    def getIndiv(self, particleNum):
        return self.data[particleNum]
        
    def length(self):
        return len(self.data)
  
    def setDataPath(self, fullpath):
        self.dataPath = fullpath
        print("set path = %s" % self.dataPath)
    
    def writeToFile(self):
        print("write path = %s" % self.dataPath)
        fileid = open(self.dataPath, 'w')
        fileid.write(self.dataStr)
        fileid.close()
        return self.dataPath
  
  
  
#StemCurveWriter
#=================
#gathers, parses, and writes data to MEL file to generate
#curves and extrusions based on particle data
  
  
import StemCache as SC
import maya.cmds as mc
import ProjectUtilities as PU
  
#============================================
class StemCurveWriter(SC.StemCache):
  
    def updateIndivCache(self, tnode):
        pnum = mc.particle(tnode, q = True, count = True)
        for n in range(pnum):
            pname = tnode + ".pt[%s]" % n
            pos = mc.getParticleAttr(pname,at = 'position')
            self.addIndiv(n, pos)
  
    def writeStemCurves(self):
        numPart = len(self.data)
        path = "/stuhome/maya/projects/vsfx705/data/particleDistance.txt"
        input = open(path, 'r')
        distances = input.readlines()
        input.close()
        melcmd = ''
        melcmd += 'circle -c 0 0 0 -nr 0 1 0 -sw 360 -r 0.1 -ch 1;\n'
        for n in range(numPart):
            particle = self.getIndiv(n)
            melcmd += 'curve -d 1 '
            numFrames = len(particle)/3
            for i in range(numFrames):
                melcmd += '-p %1.3f %1.3f %1.3f ' % (particle[i*3],particle[(i*3)+1],particle[(i*3)+2])
            melcmd += ';\n'
            melcmd += 'setAttr "makeNurbCircle1.radius" %s; \n' % distances[n]
            melcmd += 'extrude -ch false -po 0 -et 2 -ucp 1 -fpt 1 -upn 1 -rsp 1 "nurbsCircle1" "curve%s";\n\n' % (n+1)
        self.dataStr = melcmd
        return melcmd
        
#============================================
  
pCache = StemCurveWriter()
projUtils = PU.ProjectUtilities()
  
def particlesToStemCurves(tnode, startAt, endFrame):
        
    pCache.setDataPath(projUtils.getDataDir() + "/" + projUtils.getSceneName() + ".mel");
    pnum = mc.particle(tnode, q = True, count = True)
    
    for currFrame in range(endFrame):
        currFrame += 1;
        mc.currentTime(currFrame);
        print("frame %s" % currFrame)
        
        if currFrame == 1:
            pCache.reset(pnum)
        if currFrame >= startAt and currFrame <= endFrame:
            pCache.updateIndivCache(tnode)
        if currFrame == endFrame:
            return pCache.writeStemCurves()
            
def writeToFile():
    return pCache.writeToFile()