#execfile('/stuhome/PythonMel_2/Python2_finalProj/CheeFinal4_1.py')
#execfile('F:/Work/PythonMel_2/Python2_finalProj/CheeFinal4_1.py')
#execfile('H:/PythonMel_2/Python2_finalProj/CheeFinal4_1.py')
import maya.cmds as mc
  
##__LISTINGS___########
  
def selectCurve():
    curveSelect = mc.select('curve1', add=True)
    return curveSelect
  
def listCurve():
    curveList = mc.ls(type='curveShape')
    curveTrans = [mc.listRelatives(x, parent=True, type='transform')[0] for x in curveList]
    return curveTrans
    print curveTrans
  
def listObj():
    objList = mc.ls(type='mesh')
    objTrans = [mc.listRelatives(x, parent=True, type='transform')[0] for x in objList]
    return objTrans
    print objTrans
  
def getPList():
    nParticleShapes = mc.ls(type='nParticle')
    nParticleTrans = [mc.listRelatives(x, parent=True, type='transform')[0] for x in nParticleShapes]
    return nParticleTrans
  
#-----------------------------------------------------------#
#Capturing Positions of each Id into Dictionary, 
#and Creating Curves
#------------------------------------------------------------#
  
    
def pPosListDict(nParticleName, startFrm, endFrm):
    #this is a dictionary#
    #in dictionary there are keys and value#
        #eg: {key1: value1, key2: value2}
    pPosDict = {}
    #-------
    for eachFrm in range(startFrm, (endFrm + 1)):
        mc.currentTime(eachFrm)
        #pCount = mc.nParticle( nParticleName, query=True, count =True)
        getPshape = mc.ls (type = 'nParticle')
        checkpid = mc.getAttr( getPshape[0] + '.particleId')
        
         
        for pId in checkpid:
        
            #----only[x,y,z]----#
            eachPartPos = mc.nParticle(nParticleName, query=True, id = pId , attribute='position')
        
            #trying something, if "anything" happens do something else.
            #This is to catagorize of each nParticle Id positions.
            try:
                pPosDict[pId].append(eachPartPos)
            #keyError can be other stuff#
            except KeyError:
                pPosDict[pId] = [eachPartPos]
        
    return pPosDict
    
####___command to create just one wire___####
  
def createCurve(singParPosList):
    
    #checking length of list
    faultlist = 4
    if len(singParPosList) >= faultlist:
        gCurve = mc.curve(d=3, p=singParPosList)
        
  
####___Command to read each and everynParticle and create a wire from each___######         
  
def gWire(pLastFrm):
    #listCurve = []
    pList = getPList()
    startFrm = 2
    endFrm = mc.intFieldGrp(pLastFrm, query=True, value1=True)    
    
    for pName in pList:
        pPosDict = pPosListDict(pName, startFrm, endFrm)
        for each in pPosDict.values():
             gCurve = createCurve(each)
             #listCurve.append(gCurve[0])
    #return listCurve
    
####___Emitters To Curve___###############################################################
  
def    emitCubeToCurve(*args):
    getCurve = []
    gnParticle = mc.nParticle(n='mynParticle')
    getCurve = listCurve()
    #getCurve.append(mc.ls(selection = True))
    for eachCurve in getCurve:
        gCube = mc.polyCube(n='myObjEmit1')
        gEmit = mc.emitter(gCube[0], dx=1, dy=0, dz=0, sp=0.33,n='myEmitter')
        mc.connectDynamic(gnParticle[0], em=gEmit[0])
    #return getCurve    
    
    snaptoCurve()    
    fConnect()
  
####___Snap Emitters To Curve___#########################3
  
def snaptoCurve():
    eachObj = listObj()
    getCurve = listCurve()
    #getCurve = emitCubeToCurve()
    a= 0    
    for eachCurve in getCurve:    
        addCname = eachCurve +".cv[0]"
        curveP1 = mc.getAttr(addCname)
        mc.move(curveP1[0][0],curveP1[0][1],curveP1[0][2],eachObj[a],absolute=True)
    
        a=a+1    
        
    
####__Set Max COunt___#######
  
def setPartAttr(pmaxCount):
    pCount = mc.intFieldGrp(pmaxCount, query=True, value1=True)
    nParticleShapes = mc.ls(type='nParticle')
    for eachPshape in nParticleShapes:
        cName = eachPshape + '.maxCount'
        mc.setAttr(cName, pCount)
        
##____VolumeAxis duplicate along curve___###
  
def volToCurve(startFrm,endFrm):
    
    lsVol = [] 
    lspath = listCurve()
    #lspath = mc.ls(selection = True)
    #frmBegin = startFrm
    frmEnd = mc.intFieldGrp(endFrm, query=True, value1=True)
  
    for eachpath in lspath:
        volAxis = mc.volumeAxis(pos=(0, 0, 0), afc=0, afx=0, arx=0, alx=0, drs=0.5)
        gPath = mc.pathAnimation( volAxis, stu=startFrm, etu=frmEnd, fa='x', ua='y', worldUpVector=(0,1,0), bank=False, c=eachpath )
        lsVol.append(volAxis)
  
  
    for volumeX in lsVol:
    
        lsSecVol = []    
        
        for eachFrame in range (startFrm,(frmEnd+1)):
            mc.currentTime(eachFrame)
            duplicated = mc.duplicate(volumeX)
            lsSecVol[len(lsSecVol):] = [duplicated[0]]
    
        mc.group( lsSecVol, name='VolumAxisGrp')
  
####dynamicRelations#################    
def fConnect():
  
    getVol = mc.ls(type='volumeAxisField')
    getPart = getPList()
    
    for eachPart in getPart:
        print eachPart
        for eachVol in getVol:
            mc.connectDynamic(eachPart,f=eachVol)
  
  
  
####Selecting All VolumeFields######
  
def SelVolF(*args):
    lsVol = mc.ls(type='volumeAxisField')
    mc.select(lsVol)
  
##Select nParticles####
def selectnParticles(*args):
    getPart = getPList()
    mc.select(getPart)    
    
##Select nParticles####
def selectEmits(*args):
    getEmits = mc.ls( 'myEmitter*') 
    mc.select(getEmits)        
    
  
  
#####extrude########
def extrude(*args):
    control = mc.circle( nr=(0, 1, 0), c=(0, 0, 0),r=0.2,n='myCircle' )
    extrusion = []
    getCurves = listCurve()
    #getCurve = gWire()
    for eachCurve in getCurves:
        extrudeCurve = mc.extrude( 'myCircle',eachCurve, extrudeType = 2, ucp = 1, fpt = True, upn = True, rsp=True)
        extrusion.append(extrudeCurve[0])
        
    mc.group (extrusion, control, name = 'wireGrp')    
  
    
###----UI Window
###----avoid duplicate window
  
windowG = 'WireGenerationTool'
 
if (mc.window(windowG, query=True, ex=True)):
    mc.deleteUI(windowG)
  
windowG = mc.window('WireGenerationTool', title='WireGenerationTool', iconName='WGT', widthHeight=(400,400), rtf=True )
mc.columnLayout( adjustableColumn=True )
  
mc.text( label= 'STEP 1 : Please DRAW THE CURVE FIRST ')
mc.text( label= '======================================')
etFrm = mc.intFieldGrp(label='VolumeAxisFields copies',value1=50)
  
pLastFrm = mc.intFieldGrp(label ='nParticle Cache Frames', value1=20)
  
mc.button( label= 'Generate Fields', actOnPress=True, command=lambda x: volToCurve( 1, etFrm))
  
mc.button( label= 'Make Emitter Cubes', actOnPress=True, command=emitCubeToCurve)
  
MaxCount = mc.intFieldGrp(label ='Each nParticle Max Count', value1=50)
  
mc.button( label= 'Set MaxCount', actOnPress=True, command=lambda x: setPartAttr(MaxCount))
mc.text( label = '=====================================')
  
mc.button( label= 'Set General Attr in Emitters', actOnPress=True, command=selectEmits)
mc.button( label= 'Set General Attr in nParticles', actOnPress=True, command=selectnParticles)
mc.button( label= 'Set General Attr in VolumeFields', actOnPress=True, command=SelVolF)
  
mc.text( label = '=====================================')
mc.button( label= 'Create Curves From nParticles', actOnPress=True, command=lambda x:gWire(pLastFrm))
mc.button( label= 'Extrude Surface', actOnPress=True, command=extrude)
  
mc.showWindow( windowG )