cwisbg

Alembic Cacher v1

May 22nd, 2015
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.80 KB | None | 0 0
  1. #       Alembic Cacher Maya 2015 v 1.6
  2. #      
  3. #       Updates 8-10-2015
  4. #   Added function to check for objects for issues
  5. #
  6. from pymel.core import *
  7. import maya.cmds as cmds
  8. def getSG(s):
  9.     s = ls(s)[0]
  10.     print s
  11.     objSG = s.getShape().listConnections(s, t = "shadingEngine")
  12.     #objSG = listConnections(s)
  13.     objMat = objSG[0].surfaceShader.listConnections()
  14.     if not objMat:
  15.         objMat = objSG[0].miMaterialShader.listConnections()
  16.     return objSG, objMat
  17. def exportMat(matList,outputDirectory):
  18.     select(cl=1)
  19.     select(matList)
  20.     saveFile = outputDirectory+"/cached_materials.mb"
  21.     cmds.file(str(saveFile),type="mayaBinary",es=1)
  22. def exportData(data,outputDirectory):
  23.     saveFile = open(outputDirectory+"/rebuildData.txt", "w+")
  24.     writer = ""
  25.     for d in data:
  26.         writer += str(d[0])+", "+str(d[1][0])+", "+str(d[2][0])+", "+str(d[3][0])+","+"\r\n"
  27.     saveFile.write(writer)
  28.     saveFile.close()
  29.    
  30.    
  31. def dupCheck(sl):
  32.     print "DupCheck"
  33.     returnList = []
  34.     dupCheck = []
  35.     dupList = []
  36.     for s in sl:
  37.  
  38.         n = s.split("|")[-1:][0]
  39.         if n not in dupCheck:
  40.             dupCheck.append(n)
  41.             returnList.append(n)
  42.         else:
  43.             dupList.append(s)
  44.     i = 0
  45.     """
  46.    for d in dupList:
  47.        n = d.split("|")[-1:][0]
  48.        xx = rename(d,str(n)+"_dup"+str(i))
  49.        returnList.append(d)
  50.        i +=1
  51.    return returnList
  52.    """
  53.     return dupList
  54.  
  55.  
  56.  
  57. def badCheck(sl):
  58.     returnList = []
  59.     noSgList = []
  60.     dupCheck = []
  61.     dupList = []
  62.     isBad = []
  63.     isClean = 1
  64.     type = ""
  65.     for s in sl:
  66.         objSG = s.getShape().listConnections(s, t = "shadingEngine")
  67.         if not objSG:
  68.             #noSgList.append(s)
  69.             isClean = 0
  70.             add = s," Shading group"
  71.             returnList.append(add)
  72.            
  73.         n = s.split("|")[-1:][0]
  74.         if n not in dupCheck:
  75.             dupCheck.append(n)
  76.         else:
  77.             add = s," Duplicate nameing"
  78.             returnList.append(add)        
  79.             #dupList.append(s)
  80.             isClean = 0
  81.        
  82.     i = 0
  83.     add = dupList+noSgList
  84.     returnList.append(add)
  85.     return returnList, isClean
  86.    
  87.                
  88. def exportMaster():
  89.     sl = selected()
  90.     isBad, isClean = badCheck(sl)
  91.     if isClean == 0:
  92.         print "\n"+"   Clean up your shit,"+"\n"+"   Offending items:"+"\n"
  93.         selectMe = []
  94.         for i in isBad:
  95.             if i:
  96.                 selectMe.append(i[0])
  97.                 print i[0],i[-1]
  98.         select(cl=1)
  99.         select(selectMe,add=1)
  100.     else:
  101.         w = workspace(act=1, q=1)
  102.         wC = w+"/cache/alembic/"
  103.         outputDirectory = fileDialog2(dir = wC, ds=1, fm = 3 )[0].replace("\\","/")
  104.         exportList = ""
  105.        
  106.         #sl = dupCheck(sl)
  107.         data = []
  108.         matList = []  
  109.         for s in sl:
  110.             papCheck = listRelatives(s,p=1)
  111.             if papCheck:
  112.                 exportList += " -root "+str(s)
  113.             else:
  114.                 exportList += " -root |"+str(s)
  115.             matSG, mat = getSG(s)
  116.             matList.append(mat)
  117.             ps = displaySmoothness(s,po=1,q=1)
  118.             d = [s,mat,matSG,ps]
  119.             data.append(d)  
  120.         startFrame = fRangeS.getText()
  121.         endFrame = fRangeE.getText()
  122.         exportData(data,outputDirectory)
  123.         exportMat(matList,outputDirectory)
  124.         exportName = outputDirectory+"/exportedStuff.abc"
  125.         print "exporting to: ", exportName
  126.         command = "-frameRange" + " " + str(int(startFrame)) + " " + str(int(endFrame)) + " "+ "-uvWrite -writeColorSets -writeFaceSets -wholeFrameGeo -worldSpace -writeVisibility -eulerFilter -dataFormat hdf " +exportList+ " -file " + exportName  #only a string as command to pass all args
  127.         print "Eport Command: ",command
  128.         cmds.AbcExport(j= command)
  129.         print "Done!"    
  130. def readData(filePath):
  131.     readFile = open(filePath,"r").readlines()
  132.     formatted = []
  133.     for item in readFile:
  134.         item = item.split(",")
  135.         add = [item[0],item[1],item[2][:-2],item[3]]
  136.         formatted.append(add)
  137.     return formatted            
  138. def assMat(data):
  139.     matList = []
  140.     matCheck = []
  141.     for d in data:
  142.         if d[3] == " 3":
  143.             displaySmoothness(d[0],du=3,dv=3,pw=16,ps=4,po=3)
  144.         if d[1] not in matCheck:
  145.             newMatSG = sets(renderable=True, noSurfaceShader=True, empty=True, name=d[1]+"SG")
  146.             matCheck.append(d[1])
  147.             mat = ls(str(d[1]))
  148.             print mat
  149.             matList.append(mat)
  150.             mat[0].message >> newMatSG.surfaceShader
  151.             mat[0].message >> newMatSG.miMaterialShader
  152.             mat[0].message >> newMatSG.miShadowShader
  153.             mat[0].message >> newMatSG.miPhotonShader
  154.     i = 0        
  155.     for d in data:
  156.         newMatSG = ls(d[1]+"SG")[0]
  157.         dd = d[0].split("|")[-1:][0]
  158.         dd = dd.split(":")[-1:]
  159.         sets(newMatSG,e=1,forceElement = dd)  
  160.         i += 1  
  161.                
  162. def groupAll(g,gc):
  163.     sl = ls(typ="transform")
  164.     for s in sl:
  165.         if s != "cached_geo":
  166.             if s not in gc:
  167.                 parent(s,g)
  168.  
  169. def grpr(name):
  170.     g = ls(name)
  171.     if not g:
  172.         g = group(n=name,em=1)
  173.     else:
  174.         g = group(n=name+"_dup",em=1)
  175.     return g              
  176.                
  177. def importStuff(filePath):
  178.     cachePath = "{0}exportedStuff.abc".format(str(filePath[:-15]))
  179.     cmds.AbcImport(cachePath, sts=1,m="import")
  180.     matPath = "{0}cached_materials.mb".format(str(filePath[:-15]))
  181.     cmds.file(matPath, i=1,typ = "mayaBinary",iv=1, mnc=0, rpr = "cached_materials", op = "v=0;")
  182. def importMaster():
  183.     w = workspace(act=1, q=1)
  184.     wC = w+"/cache/alembic/"
  185.     fFilter = "*.txt"
  186.     filePath = fileDialog2(dir = wC, ds=1, fm = 1,ff= fFilter )[0].replace("\\","/")
  187.     fileData = readData(filePath)
  188.     print "01  Importing Data"
  189.     gc = ls(typ="transform")
  190.     importStuff(filePath)
  191.     print "02  Assigning materials"
  192.     assMat(fileData)
  193.     g = grpr("cached_geo")
  194.     groupAll(g,gc)
  195.     select(cl=1)
  196.     print "Done!"
  197.    
  198. print "here0"
  199. # Gui
  200. win = window(title= "Alembic Exporter", s=0, w=200)
  201. print "here1"
  202. mainLayout = rowColumnLayout(nc=1)
  203. txt = text("   Frame Range  ",al="left",p=mainLayout)
  204. scaleLayout = rowColumnLayout(nc=1,p=mainLayout)
  205. scaleTxtLayout = rowColumnLayout(nr=1,p=mainLayout)
  206. print "here2"
  207. startFrame = int(playbackOptions(min=1,q=1))
  208. endFrame = int(playbackOptions(max=1,q=1))
  209. fRangeS = textField(tx=str(startFrame),p=scaleTxtLayout,w=50)
  210. fRangeE = textField(tx=str(endFrame),p=scaleTxtLayout,w=50)
  211. print "here3"
  212. exportBtn = button(l="Export", command=Callback(exportMaster), w=200,bgc = [.1,8,.1],p=mainLayout)
  213. importBtn = button(l="Import", command=Callback(importMaster), w=200,bgc = [.2,.7,.8],p=mainLayout)
  214. print "here4"
  215. win.show()
Advertisement
Add Comment
Please, Sign In to add comment