Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Alembic Cacher Maya 2015 v 1.6
- #
- # Updates 8-10-2015
- # Added function to check for objects for issues
- #
- from pymel.core import *
- import maya.cmds as cmds
- def getSG(s):
- s = ls(s)[0]
- print s
- objSG = s.getShape().listConnections(s, t = "shadingEngine")
- #objSG = listConnections(s)
- objMat = objSG[0].surfaceShader.listConnections()
- if not objMat:
- objMat = objSG[0].miMaterialShader.listConnections()
- return objSG, objMat
- def exportMat(matList,outputDirectory):
- select(cl=1)
- select(matList)
- saveFile = outputDirectory+"/cached_materials.mb"
- cmds.file(str(saveFile),type="mayaBinary",es=1)
- def exportData(data,outputDirectory):
- saveFile = open(outputDirectory+"/rebuildData.txt", "w+")
- writer = ""
- for d in data:
- writer += str(d[0])+", "+str(d[1][0])+", "+str(d[2][0])+", "+str(d[3][0])+","+"\r\n"
- saveFile.write(writer)
- saveFile.close()
- def dupCheck(sl):
- print "DupCheck"
- returnList = []
- dupCheck = []
- dupList = []
- for s in sl:
- n = s.split("|")[-1:][0]
- if n not in dupCheck:
- dupCheck.append(n)
- returnList.append(n)
- else:
- dupList.append(s)
- i = 0
- """
- for d in dupList:
- n = d.split("|")[-1:][0]
- xx = rename(d,str(n)+"_dup"+str(i))
- returnList.append(d)
- i +=1
- return returnList
- """
- return dupList
- def badCheck(sl):
- returnList = []
- noSgList = []
- dupCheck = []
- dupList = []
- isBad = []
- isClean = 1
- type = ""
- for s in sl:
- objSG = s.getShape().listConnections(s, t = "shadingEngine")
- if not objSG:
- #noSgList.append(s)
- isClean = 0
- add = s," Shading group"
- returnList.append(add)
- n = s.split("|")[-1:][0]
- if n not in dupCheck:
- dupCheck.append(n)
- else:
- add = s," Duplicate nameing"
- returnList.append(add)
- #dupList.append(s)
- isClean = 0
- i = 0
- add = dupList+noSgList
- returnList.append(add)
- return returnList, isClean
- def exportMaster():
- sl = selected()
- isBad, isClean = badCheck(sl)
- if isClean == 0:
- print "\n"+" Clean up your shit,"+"\n"+" Offending items:"+"\n"
- selectMe = []
- for i in isBad:
- if i:
- selectMe.append(i[0])
- print i[0],i[-1]
- select(cl=1)
- select(selectMe,add=1)
- else:
- w = workspace(act=1, q=1)
- wC = w+"/cache/alembic/"
- outputDirectory = fileDialog2(dir = wC, ds=1, fm = 3 )[0].replace("\\","/")
- exportList = ""
- #sl = dupCheck(sl)
- data = []
- matList = []
- for s in sl:
- papCheck = listRelatives(s,p=1)
- if papCheck:
- exportList += " -root "+str(s)
- else:
- exportList += " -root |"+str(s)
- matSG, mat = getSG(s)
- matList.append(mat)
- ps = displaySmoothness(s,po=1,q=1)
- d = [s,mat,matSG,ps]
- data.append(d)
- startFrame = fRangeS.getText()
- endFrame = fRangeE.getText()
- exportData(data,outputDirectory)
- exportMat(matList,outputDirectory)
- exportName = outputDirectory+"/exportedStuff.abc"
- print "exporting to: ", exportName
- 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
- print "Eport Command: ",command
- cmds.AbcExport(j= command)
- print "Done!"
- def readData(filePath):
- readFile = open(filePath,"r").readlines()
- formatted = []
- for item in readFile:
- item = item.split(",")
- add = [item[0],item[1],item[2][:-2],item[3]]
- formatted.append(add)
- return formatted
- def assMat(data):
- matList = []
- matCheck = []
- for d in data:
- if d[3] == " 3":
- displaySmoothness(d[0],du=3,dv=3,pw=16,ps=4,po=3)
- if d[1] not in matCheck:
- newMatSG = sets(renderable=True, noSurfaceShader=True, empty=True, name=d[1]+"SG")
- matCheck.append(d[1])
- mat = ls(str(d[1]))
- print mat
- matList.append(mat)
- mat[0].message >> newMatSG.surfaceShader
- mat[0].message >> newMatSG.miMaterialShader
- mat[0].message >> newMatSG.miShadowShader
- mat[0].message >> newMatSG.miPhotonShader
- i = 0
- for d in data:
- newMatSG = ls(d[1]+"SG")[0]
- dd = d[0].split("|")[-1:][0]
- dd = dd.split(":")[-1:]
- sets(newMatSG,e=1,forceElement = dd)
- i += 1
- def groupAll(g,gc):
- sl = ls(typ="transform")
- for s in sl:
- if s != "cached_geo":
- if s not in gc:
- parent(s,g)
- def grpr(name):
- g = ls(name)
- if not g:
- g = group(n=name,em=1)
- else:
- g = group(n=name+"_dup",em=1)
- return g
- def importStuff(filePath):
- cachePath = "{0}exportedStuff.abc".format(str(filePath[:-15]))
- cmds.AbcImport(cachePath, sts=1,m="import")
- matPath = "{0}cached_materials.mb".format(str(filePath[:-15]))
- cmds.file(matPath, i=1,typ = "mayaBinary",iv=1, mnc=0, rpr = "cached_materials", op = "v=0;")
- def importMaster():
- w = workspace(act=1, q=1)
- wC = w+"/cache/alembic/"
- fFilter = "*.txt"
- filePath = fileDialog2(dir = wC, ds=1, fm = 1,ff= fFilter )[0].replace("\\","/")
- fileData = readData(filePath)
- print "01 Importing Data"
- gc = ls(typ="transform")
- importStuff(filePath)
- print "02 Assigning materials"
- assMat(fileData)
- g = grpr("cached_geo")
- groupAll(g,gc)
- select(cl=1)
- print "Done!"
- print "here0"
- # Gui
- win = window(title= "Alembic Exporter", s=0, w=200)
- print "here1"
- mainLayout = rowColumnLayout(nc=1)
- txt = text(" Frame Range ",al="left",p=mainLayout)
- scaleLayout = rowColumnLayout(nc=1,p=mainLayout)
- scaleTxtLayout = rowColumnLayout(nr=1,p=mainLayout)
- print "here2"
- startFrame = int(playbackOptions(min=1,q=1))
- endFrame = int(playbackOptions(max=1,q=1))
- fRangeS = textField(tx=str(startFrame),p=scaleTxtLayout,w=50)
- fRangeE = textField(tx=str(endFrame),p=scaleTxtLayout,w=50)
- print "here3"
- exportBtn = button(l="Export", command=Callback(exportMaster), w=200,bgc = [.1,8,.1],p=mainLayout)
- importBtn = button(l="Import", command=Callback(importMaster), w=200,bgc = [.2,.7,.8],p=mainLayout)
- print "here4"
- win.show()
Advertisement
Add Comment
Please, Sign In to add comment