Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Maya MR lightbaker
- # Version 0.4
- from pymel.core import *
- def preBake():
- lightMapCheck = checkMap()
- if lightMapCheck:
- lightMapName = lightMapCheck[0]
- else:
- lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
- #lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
- fileTexture = ls(lightMapName)
- if fileTexture:
- delete(fileTexture)
- #print "Deleting file node for lightmap"
- def makeUVSet():
- for ass in assMatList:
- uvSetCheck = polyUVSet(ass, auv=1, query=1)
- if uvSetCheck:
- if not "lightmapUV" in uvSetCheck:
- newUv = polyUVSet(ass, cr=1, uvs = "lightmapUV")
- #print "creating lightmapUV for: ",ass
- else:
- pass
- def makeUVlayout():
- # make new uv projection, and layout
- for ass in assMatList:
- newProjection = polyAutoProjection(ass,p=6,uvs="lightmapUV",scaleMode=1,ps =.2,lm=1,pb=0,ibd=1,cm=0,sc=1,o=1,ws=0)
- newUVlayout = polyMultiLayoutUV(assMatList, lm=1, sc=2, rbf=2, fr=1,l=2)
- for ass in assMatList:
- polyUVSet(ass, cuv = 1, uvSet = "map1")
- def checkMe():
- boxValue = useExistingLightmapCheck.getValue()
- print "Checked", boxValue
- if boxValue:
- boxValue = 0
- else:
- boxValue = 1
- textFieldGrp(lightMapNameTxt,e=1,en=boxValue)
- def getSl(*args):
- list = []
- #global assMatList
- #assMatList = []
- for s in sl:
- list.append(str(s))
- assMatList.append(s)
- try:
- scrollBar.removeAll(val=1)
- scrollBar.append(list)
- except:
- pass
- return assMatList
- def makeBakeSet():# makes bake set
- import maya.mel as mel
- #assMatList = selected()
- select(cl=1)
- select(assMatList)
- mel.eval( 'createAndAssignBakeSet textureBakeSet "";' )
- bakeSet = listConnections(assMatList[0].getShape(),t="textureBakeSet")[0]
- return bakeSet
- def getMat(s):# get material from selected
- #------------------------------------------------------------- This needs work ----------------------
- matSG = s.getShape().listConnections(t="shadingEngine")
- mat = ls(listConnections(matSG),materials=1)
- """
- if len(mat) > 1:
- return mat[0], mat[1]
- else:
- return mat
- """
- return mat
- def setBakeSetAttr(bakeSet):# Needs Uniqu naming for map
- lightMapCheck = checkMap()
- if lightMapCheck:
- lightMapName = lightMapCheck[0]
- else:
- lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
- #lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
- print lightMapName
- bakeType = optionMenu(typeMenu,v=1,q=1)
- mapSize = optionMenu(mapSizeMenu,v=1,q=1)
- if bakeType == "Light And Color":
- bakeType = 0
- if bakeType == "AO":
- bakeType = 3
- if bakeType == "Gi Only":
- bakeType = 2
- quality = optionMenu(qualityMenu,v=1,q=1)
- if quality == "High":
- fgQuality = 5
- aoQuality = 124
- if quality == "Medium":
- fgQuality = 3
- aoQuality = 64
- if quality == "Low":
- fgQuality = 2
- aoQuality = 24
- if quality == "Preview":
- fgQuality = 1
- aoQuality = 16
- setAttr(bakeSet.prefix,lightMapName, type="string")
- setAttr(bakeSet.uvSetName,"lightmapUV", type="string")
- setAttr(bakeSet.xResolution, int(mapSize))
- setAttr(bakeSet.yResolution, int(mapSize))
- setAttr(bakeSet.finalGatherQuality, fgQuality)
- setAttr(bakeSet.colorMode, bakeType)
- setAttr(bakeSet.occlusionRays, aoQuality)
- setAttr(bakeSet.bakeToOneMap, 1)
- setAttr(bakeSet.fillTextureSeams, 3)
- setAttr(bakeSet.overrideUvSet, 1)
- def getLightMapInfo():
- sgList = []
- bakeList = ""
- bakeList2 = []
- for ass in assMatList: # get sg connected to mesh
- matSG = ass.getShape().listConnections(t="shadingEngine")
- if not matSG in sgList:
- bakeList2.append(matSG[0])
- bakeList2.append(ass)
- #bakeList += str(matSG[0])+" |"+str(ass)+"|"+str(ass.getShape()+" ")
- return bakeList2
- def afterBake():
- lightMapCheck = checkMap()
- if lightMapCheck:
- lightMapName = lightMapCheck[0]
- else:
- lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
- #lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
- nwFile = "cache\mr\lightMap\{0}.tif".format(lightMapName)
- #---------------------
- fileTexture = [shadingNode("file", asTexture=1, n=lightMapName)]
- setAttr(fileTexture[0].fileTextureName, nwFile)
- #---------------------
- for ass in assMatList:
- mat = getMat(ass)
- testCon = listConnections(mat[0].color)
- if len(testCon) == 0:
- connectAttr(fileTexture[0]+".outColor", mat[0].color,f=1)# needs to connect to something else
- uvLink(uvs = ass.uvSet[1]+"uvSetName", t= fileTexture[0])
- select(cl=1)
- select(assMatList)
- lightMapCheck = checkMap()
- if lightMapCheck:
- checkBox(useExistingLightmapCheck,e=1, en=1)
- else:
- checkBox(useExistingLightmapCheck,e=1, en=0)
- def checkMap():
- assMatList = selected()
- lightMapCheck = []
- if assMatList:
- for ass in assMatList:
- mat = getMat(ass)
- testCon = listConnections(mat[0].color)
- if not testCon in lightMapCheck:
- lightMapCheck.append(testCon)
- return lightMapCheck[0]
- def refreshMap():
- lightMapCheck = checkMap()
- if lightMapCheck:
- print "map is there"
- checkBox(useExistingLightmapCheck,e=1, en=1)
- else:
- print "not"
- checkBox(useExistingLightmapCheck,e=1, en=0)
- def bakeObjs():
- print ""
- print " Starting Bake "
- global assMatList
- assMatList = selected()
- print assMatList
- print "1"
- preBake()
- print "2"
- makeUVSet()
- print "3"
- makeUVlayout()
- print "4"
- bakeSet = makeBakeSet()
- print "5"
- setBakeSetAttr(bakeSet)
- print "6"
- bakeList = getLightMapInfo()
- print "7"
- #convertLightmapSetup(c = "persp", sh=1, ksg=1)
- # convertLightmap(akeList,bo = "lightBake")
- convertLightmap(bakeList, bo = bakeSet,sh=1)
- afterBake()
- print ""
- print " Done"
- #________________________________________________________________
- def test():
- for ass in assMatList:
- polyUVSet(ass, cuv = 1, uvSet = "map1")
- def test2():
- for ass in assMatList:
- polyUVSet(ass, cuv = 1, uvSet = "lightmapUV")
- def test3():
- #afterBake()
- #bakeList = getLightMapInfo()
- #print bakeList
- lightMapCheck = checkMap()
- print lightMapCheck
- #print optionMenu(qualityMenu,v=1,q=1)
- #________________________________________________________________
- """
- global assMatList
- assMatList = selected()
- if not assMatList:
- assMatList = []
- """
- assMatList = selected()
- matAs = []
- lightMapName = []
- win = window(title= "Baker", s=0)
- mainLayout = rowColumnLayout(nc=1)
- typeMenu = optionMenu(l = "Bake Type")
- typeBtnL = menuItem(label='Light And Color' )
- typeBtnAO = menuItem(label='AO' )
- typeBtnGi = menuItem(label='Gi Only')
- lightMapCheck = checkMap()
- if lightMapCheck:
- lightMapName = lightMapCheck[0]
- lightMapCheck = 1
- else:
- lightMapCheck = 0
- lightMapName = "lightmap_AO"
- refreshButton = button(l="Check for Map", command=Callback(refreshMap))
- useExistingLightmapCheck = checkBox(l = "Use Existing Lightmap", cc = Callback(checkMe),en=lightMapCheck)
- lightMapNameTxt = textFieldGrp(label = "Map Name",w=10,cal=[1,"left"],tx=lightMapName,cw=[1,50])
- #lightMapNameTxt = textField(tx="lightBake_AO")
- qualityMenu = optionMenu(l = "Quality")
- hqBtn = menuItem( label='High' )
- mqBtn = menuItem( label='Medium' )
- lqBtn = menuItem( label='Low' )
- pqBtn = menuItem( label='Preview' )
- mapSizeMenu = optionMenu(l = "Map Size")
- mapHqBtn = menuItem( label='4096' )
- mapMqBtn = menuItem( label='2048' )
- mapLqBtn = menuItem( label='1024' )
- mapSqBtn = menuItem( label='512' )
- optionMenu(typeMenu,e=1, v='AO' )
- optionMenu(qualityMenu,e=1, v='Low' )
- optionMenu(mapSizeMenu,e=1, v='512' )
- bakeObjsBtn = button(l="Bake", command=Callback(bakeObjs))
- #test = button(l="test", command=Callback(test))
- #test2 = button(l="test2", command=Callback(test2))
- #test3 = button(l="test3", command=Callback(test3))
- win.show()
Advertisement
Add Comment
Please, Sign In to add comment