cwisbg

Maya MR lightbaker v .4

Mar 28th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.57 KB | None | 0 0
  1. # Maya MR lightbaker
  2. # Version 0.4
  3. from pymel.core import *
  4.  
  5. def preBake():
  6.     lightMapCheck = checkMap()
  7.     if lightMapCheck:
  8.         lightMapName = lightMapCheck[0]
  9.     else:
  10.         lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
  11.     #lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
  12.     fileTexture = ls(lightMapName)
  13.     if fileTexture:
  14.         delete(fileTexture)
  15.         #print "Deleting file node for lightmap"
  16.  def makeUVSet():
  17.     for ass in assMatList:
  18.         uvSetCheck = polyUVSet(ass, auv=1, query=1)
  19.         if uvSetCheck:
  20.             if not "lightmapUV" in uvSetCheck:
  21.                 newUv = polyUVSet(ass, cr=1, uvs = "lightmapUV")
  22.                 #print "creating lightmapUV for: ",ass
  23.             else:
  24.                 pass              
  25. def makeUVlayout():
  26.     # make new uv projection, and layout
  27.     for ass in assMatList:
  28.         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)
  29.     newUVlayout = polyMultiLayoutUV(assMatList, lm=1, sc=2, rbf=2, fr=1,l=2)
  30.     for ass in assMatList:
  31.         polyUVSet(ass, cuv = 1, uvSet = "map1")      
  32.    
  33.    
  34. def checkMe():
  35.     boxValue = useExistingLightmapCheck.getValue()
  36.     print "Checked", boxValue  
  37.     if boxValue:
  38.         boxValue = 0
  39.     else:
  40.         boxValue = 1
  41.     textFieldGrp(lightMapNameTxt,e=1,en=boxValue)
  42.    
  43. def getSl(*args):
  44.     list = []
  45.     #global assMatList
  46.     #assMatList = []
  47.     for s in sl:
  48.         list.append(str(s))
  49.         assMatList.append(s)
  50.     try:
  51.         scrollBar.removeAll(val=1)
  52.         scrollBar.append(list)
  53.     except:
  54.         pass
  55.     return  assMatList
  56.  
  57. def makeBakeSet():# makes bake set
  58.     import maya.mel as mel
  59.     #assMatList = selected()
  60.     select(cl=1)
  61.     select(assMatList)
  62.     mel.eval( 'createAndAssignBakeSet textureBakeSet "";' )
  63.     bakeSet = listConnections(assMatList[0].getShape(),t="textureBakeSet")[0]
  64.     return bakeSet  
  65.    
  66. def getMat(s):# get material from selected
  67.     #------------------------------------------------------------- This needs work ----------------------
  68.     matSG = s.getShape().listConnections(t="shadingEngine")
  69.     mat = ls(listConnections(matSG),materials=1)
  70.     """
  71.    if len(mat) > 1:
  72.        return mat[0], mat[1]
  73.    else:
  74.        return mat
  75.    """
  76.     return mat
  77.        
  78.            
  79. def setBakeSetAttr(bakeSet):# Needs Uniqu naming for map
  80.  
  81.     lightMapCheck = checkMap()
  82.     if lightMapCheck:
  83.         lightMapName = lightMapCheck[0]
  84.     else:
  85.         lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
  86.        
  87.     #lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
  88.     print lightMapName
  89.     bakeType = optionMenu(typeMenu,v=1,q=1)
  90.     mapSize = optionMenu(mapSizeMenu,v=1,q=1)
  91.     if bakeType == "Light And Color":
  92.         bakeType = 0
  93.     if bakeType == "AO":
  94.         bakeType = 3  
  95.     if bakeType == "Gi Only":
  96.         bakeType = 2
  97.    
  98.     quality = optionMenu(qualityMenu,v=1,q=1)    
  99.     if quality == "High":
  100.         fgQuality = 5
  101.         aoQuality = 124
  102.     if quality == "Medium":
  103.         fgQuality = 3
  104.         aoQuality = 64          
  105.     if quality == "Low":
  106.         fgQuality = 2
  107.         aoQuality = 24
  108.     if quality == "Preview":
  109.         fgQuality = 1
  110.         aoQuality = 16        
  111.     setAttr(bakeSet.prefix,lightMapName, type="string")
  112.     setAttr(bakeSet.uvSetName,"lightmapUV", type="string")
  113.     setAttr(bakeSet.xResolution, int(mapSize))
  114.     setAttr(bakeSet.yResolution, int(mapSize))
  115.     setAttr(bakeSet.finalGatherQuality, fgQuality)
  116.     setAttr(bakeSet.colorMode, bakeType)
  117.     setAttr(bakeSet.occlusionRays, aoQuality)  
  118.  
  119.     setAttr(bakeSet.bakeToOneMap, 1)
  120.     setAttr(bakeSet.fillTextureSeams, 3)
  121.     setAttr(bakeSet.overrideUvSet, 1)
  122.  
  123.  
  124. def getLightMapInfo():
  125.     sgList = []
  126.     bakeList = ""
  127.     bakeList2 = []
  128.     for ass in assMatList: # get sg connected to mesh
  129.         matSG = ass.getShape().listConnections(t="shadingEngine")
  130.         if not matSG in sgList:
  131.             bakeList2.append(matSG[0])
  132.             bakeList2.append(ass)
  133.             #bakeList += str(matSG[0])+" |"+str(ass)+"|"+str(ass.getShape()+" ")
  134.            
  135.     return bakeList2
  136.    
  137. def afterBake():
  138.  
  139.     lightMapCheck = checkMap()
  140.     if lightMapCheck:
  141.         lightMapName = lightMapCheck[0]
  142.     else:
  143.         lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
  144.     #lightMapName = textFieldGrp(lightMapNameTxt,tx=1,q=1)
  145.     nwFile = "cache\mr\lightMap\{0}.tif".format(lightMapName)
  146.     #---------------------
  147.     fileTexture = [shadingNode("file", asTexture=1, n=lightMapName)]    
  148.     setAttr(fileTexture[0].fileTextureName, nwFile)  
  149.     #---------------------
  150.     for ass in assMatList:
  151.         mat = getMat(ass)
  152.         testCon = listConnections(mat[0].color)
  153.         if len(testCon) == 0:
  154.             connectAttr(fileTexture[0]+".outColor", mat[0].color,f=1)# needs to connect to something else
  155.         uvLink(uvs = ass.uvSet[1]+"uvSetName", t= fileTexture[0])
  156.     select(cl=1)        
  157.     select(assMatList)
  158.    
  159.     lightMapCheck = checkMap()
  160.     if lightMapCheck:
  161.         checkBox(useExistingLightmapCheck,e=1, en=1)
  162.     else:
  163.         checkBox(useExistingLightmapCheck,e=1, en=0)
  164.        
  165.        
  166. def checkMap():
  167.     assMatList = selected()
  168.     lightMapCheck = []
  169.     if assMatList:
  170.         for ass in assMatList:
  171.             mat = getMat(ass)
  172.             testCon = listConnections(mat[0].color)
  173.             if not testCon in lightMapCheck:
  174.                 lightMapCheck.append(testCon)
  175.         return lightMapCheck[0]
  176. def refreshMap():  
  177.     lightMapCheck = checkMap()
  178.     if lightMapCheck:
  179.         print "map is there"
  180.        
  181.         checkBox(useExistingLightmapCheck,e=1, en=1)
  182.     else:
  183.         print "not"
  184.        
  185.         checkBox(useExistingLightmapCheck,e=1, en=0)                
  186. def bakeObjs():
  187.     print ""
  188.     print "   Starting Bake   "
  189.     global assMatList
  190.     assMatList = selected()
  191.     print assMatList
  192.     print "1"
  193.     preBake()
  194.     print "2"
  195.     makeUVSet()
  196.     print "3"
  197.     makeUVlayout()
  198.     print "4"
  199.     bakeSet = makeBakeSet()
  200.     print "5"
  201.     setBakeSetAttr(bakeSet)
  202.     print "6"
  203.     bakeList = getLightMapInfo()
  204.     print "7"
  205.     #convertLightmapSetup(c = "persp", sh=1, ksg=1)
  206.     #  convertLightmap(akeList,bo = "lightBake")
  207.     convertLightmap(bakeList, bo = bakeSet,sh=1)
  208.     afterBake()
  209.     print ""
  210.     print "  Done"
  211.  
  212.  
  213.  
  214.  
  215. #________________________________________________________________  
  216. def test():
  217.     for ass in assMatList:
  218.         polyUVSet(ass, cuv = 1, uvSet = "map1")
  219. def test2():
  220.     for ass in assMatList:
  221.         polyUVSet(ass, cuv = 1, uvSet = "lightmapUV")
  222.        
  223. def test3():
  224.     #afterBake()
  225.     #bakeList = getLightMapInfo()
  226.     #print bakeList
  227.     lightMapCheck = checkMap()
  228.     print lightMapCheck
  229.  
  230.  
  231.     #print optionMenu(qualityMenu,v=1,q=1)      
  232. #________________________________________________________________          
  233.                    
  234. """
  235. global assMatList
  236. assMatList = selected()
  237.  
  238. if not assMatList:
  239.    assMatList = []  
  240. """
  241. assMatList = selected()
  242. matAs = []
  243. lightMapName = []
  244. win = window(title= "Baker", s=0)
  245. mainLayout = rowColumnLayout(nc=1)
  246.  
  247. typeMenu = optionMenu(l = "Bake Type")
  248. typeBtnL = menuItem(label='Light And Color' )
  249. typeBtnAO = menuItem(label='AO' )
  250. typeBtnGi = menuItem(label='Gi Only')
  251.  
  252. lightMapCheck = checkMap()
  253. if lightMapCheck:
  254.     lightMapName = lightMapCheck[0]
  255.     lightMapCheck = 1
  256. else:
  257.     lightMapCheck = 0
  258.     lightMapName = "lightmap_AO"
  259.    
  260. refreshButton = button(l="Check for Map", command=Callback(refreshMap))    
  261. useExistingLightmapCheck = checkBox(l = "Use Existing Lightmap", cc = Callback(checkMe),en=lightMapCheck)
  262.  
  263.  
  264.  
  265. lightMapNameTxt = textFieldGrp(label = "Map Name",w=10,cal=[1,"left"],tx=lightMapName,cw=[1,50])
  266. #lightMapNameTxt = textField(tx="lightBake_AO")
  267.  
  268.  
  269. qualityMenu = optionMenu(l = "Quality")
  270. hqBtn = menuItem( label='High' )
  271. mqBtn = menuItem( label='Medium' )
  272. lqBtn = menuItem( label='Low' )
  273. pqBtn = menuItem( label='Preview' )
  274.  
  275.  
  276. mapSizeMenu = optionMenu(l = "Map Size")
  277. mapHqBtn = menuItem( label='4096' )
  278. mapMqBtn = menuItem( label='2048' )
  279. mapLqBtn = menuItem( label='1024' )
  280. mapSqBtn = menuItem( label='512' )
  281.  
  282.  
  283.  
  284. optionMenu(typeMenu,e=1, v='AO' )
  285. optionMenu(qualityMenu,e=1, v='Low' )
  286. optionMenu(mapSizeMenu,e=1, v='512' )
  287.  
  288. bakeObjsBtn = button(l="Bake", command=Callback(bakeObjs))
  289. #test = button(l="test", command=Callback(test))
  290. #test2 = button(l="test2", command=Callback(test2))
  291. #test3 = button(l="test3", command=Callback(test3))
  292. win.show()
Advertisement
Add Comment
Please, Sign In to add comment