#execfile('/stuhome/PythonMel_2/Python2_finalProj/CheeFinal3.py')
#execfile('D:/pyScripts/lighting/gakiLightManager.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 listVol(): 
    volList =  mc.ls(type='volumeAxisField')
    return volList
  
def getPList():
    particleShapes = mc.ls(type='particle')
    particleTrans = [mc.listRelatives(x, parent=True, type='transform')[0] for x in particleShapes]
    return particleTrans
  
  
  
#create simple functions to carry out the basic Task in mind#
#------------------------------------------------------------#
  
    
def pPosListDict(particleName, 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.particle( particleName, query=True, count =True)
        
        print pCount
  
  
        for pId in range(pCount):
        
            #----only[x,y,z]----#
            eachPartPos = mc.particle(particleName, query=True, id = pId , attribute='position')
        
            #trying something, if "anything" happens do something else.
            #This is to catagorize of each particle Id positions.
            try:
                pPosDict[pId].append(eachPartPos)
            #keyError can be other stuff#
            except KeyError:
                pPosDict[pId] = [eachPartPos]
        
    return pPosDict
    
def createCurve(singParPosList):
    #checking length of list
    faultlist = 4
    if len(singParPosList) >= faultlist:
        mc.curve(d=3, p=singParPosList)
    else:
        #none#
        print "basket"
        
def gWire(pLastFrm):
    
    pList = getPList()
    startFrm = 1
    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)
  
    
##___deleteALL__________########################################################33
def deleteVolEmit(*args):
    a = mc.ls('wGrp','objEmit')
  
    b= getPList()
    mc.delete(a,b)
  
##___createEMITTER_____###############################################################
  
def creEmit(obj):
    gemit = mc.emitter([obj], dx=1, dy=0, dz=0, sp=0.33,n='myEmitter')
  
def creSphere():
    gSphere = mc.polySphere(n='myObjEmit1')
  
def creCube():
    gcube = mc.polyCube(n='myObjEmit1')
    
  
##__Set Max COunt___#######
def setPartAttr(pmaxCount):
    pCount = mc.intFieldGrp(pmaxCount, query=True, value1=True)
    particleShapes = mc.ls(type='particle')
    for eachPshape in particleShapes:
        cName = eachPshape + '.maxCount'
        mc.setAttr(cName, pCount)
    
  
        
###Delete_Curves####
def deleteCurves(*args):
    lsCurve = listCurve()
    mc.delete(lsCurve)
    
####Scale Volume Fields#####
def scaleVolF(*args):
    lsVol = listVol() 
    mc.select(lsVol)
  
#--- Working on Multiple Curves ----################################################
  
  
##____VolumeAxis duplicate along curve___###
def dupAlongCurve(volumeFName, startFrm, endFrm):
    mc.cycleCheck(e=0)    
    for eachFrame in range (startFrm,(endFrm+1)):
        mc.currentTime(eachFrame)
    
        mc.duplicate(volumeFName)
  
def volToCurve(startFrm,endFrm):
    
    lspath = listCurve()
    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=frmBegin, etu=frmEnd, fa='x', ua='y', worldUpVector=(0,1,0), bank=False, c=eachpath )
        lsVol = listVol()
    
    for volumeX in lsVol:
        createDupFields = dupAlongCurve(volumeX,1,frmEnd)
    
    listAll = 
    mc.group(name='wGrp')
            
  
def emitToCurve(*args):
    mc.particle(n='myparticle')
    getCurve = listCurve()
    #print getCurve
  
    n=0
    for eachCurve in getCurve:
        creEmit()
        eachEmit = mc.ls(type='pointEmitter')
        addCname = eachCurve +".cv[0]"
        curveP1 = mc.getAttr(addCname)
        #print eachEmit
    
        #print curveP1    
        
        mc.move(curveP1[0][0],curveP1[0][1],curveP1[0][2],eachEmit[n],absolute=True)
        n=n+1    
  
def emitCubeToCurve(*args):
    mc.particle(n='myparticle')
    getCurve = listCurve()
    #print getCurve
  
    n=0
    for eachCurve in getCurve:
        creCube()
    
        getCube = listObj()
        print getCube
        specifyCube = getCube[n]
        print specifyCube
        creEmit(specifyCube)
        n=n+1
        
    snaptoCurve()    
  
def emitSphToCurve(*args):
    mc.particle(n='myparticle')
    getCurve = listCurve()
    #print getCurve
  
    n=0
    for eachCurve in getCurve:
        creSphere()
    
        getSphere = listObj()
        
        specifySphere = getSphere[n]
        print specifySphere
        creEmit(specifySphere)
        n=n+1
        
    snaptoCurve()    
  
  
  
def snaptoCurve():
    eachObj = listObj()
    getCurve = listCurve()
    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    
        
  
    
#REFERENCE#######        
#a = mc.polyCube()
#mc.emitter(a)
#mc.particle()
#mc.connectDynamic('particle1',em='emitter1')
#....REF....#
  
##__ConnectEmitters__########
def connectEmit(*args):    
    fConnect()
    getPart = getPList()
    getObj = mc.ls('myObjEmit')
    listEmit = mc.ls(type='pointEmitter')
    n=0
    for eachEmit in listEmit:
        mc.connectDynamic(getPart, em=eachEmit)
    
        
    
    #listEmit = mc.ls(type='pointEmitter')
    #mc.group(name='myEmitter')
    
####dynamicRelations#################    
def fConnect():
  
    getVol = listVol()
    getPart = getPList()
    
    for eachPart in getPart:
        print eachPart
        for eachVol in getVol:
            mc.connectDynamic(eachPart,f=eachVol)
  
#extrude########
def extrude(*args):
    mc.circle( nr=(0, 1, 0), c=(0, 0, 0),r=0.2,n='myCircle' )
  
    getCurves = listCurve()
    for eachCurve in getCurves:
        mc.extrude( 'myCircle',eachCurve, extrudeType = 2, ucp = 1, fpt = True, upn = True, rsp=True)
    
##Select particles####
def selectParticles(*args):
    getPart = getPList()
    mc.select(getPart)
        
  
    
#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= 'Please Create Your Desired Curve')
#stFrm = mc.intField(value=1)
etFrm = mc.intFieldGrp(label='number of copies',value1=50)
pLastFrm = mc.intFieldGrp(label ='Particle Cache Frames', value1=100)
  
mc.button( label= 'delete all', actOnPress=True, command=deleteVolEmit)
mc.button( label= 'Generate Fields', actOnPress=True, command=lambda x: volToCurve( 1, etFrm))
#mc.button( label= 'Add emitter to Curve', actOnPress=True, command=emitToCurve)
  
mc.button( label= 'Make Emitter Cubes', actOnPress=True, command=emitCubeToCurve)
mc.button( label= 'Make Emitter Spheres', actOnPress=True, command=emitSphToCurve)
  
mc.button( label= 'ConnectFields To Particles', actOnPress=True, command=connectEmit)
  
MaxCount = mc.intFieldGrp(label ='Each Particle Max Count', value1=50)
  
mc.button( label= 'Set MaxCount', actOnPress=True, command=lambda x: setPartAttr(MaxCount))
mc.button( label= 'selectall Particles', actOnPress=True, command=selectParticles)
mc.button( label= 'selectall VolumeFields', actOnPress=True, command=scaleVolF)
mc.button( label= 'Create Curves From Particles', actOnPress=True, command=lambda x:gWire(pLastFrm))
mc.button( label= 'Extrude Surface', actOnPress=True, command=extrude)
mc.button( label= 'Delete All Curves', actOnPress=True, command=deleteCurves)
mc.showWindow( windowG )